Skip to content

Commit

Permalink
SDKS-3408 Self device management implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
witrisna committed Oct 9, 2024
1 parent 4ba6a5e commit a44d0bc
Show file tree
Hide file tree
Showing 27 changed files with 1,540 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.X.X]
#### Added
- Self device management [SDKS-3408]

## [4.6.0]
#### Added
- Allow developers to customize SDK storage [SDKS-3378]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static Config getInstance() {
return mInstance;
}

ServerConfig getServerConfig() {
public ServerConfig getServerConfig() {
return ServerConfig.builder()
.context(context)
.identifier(identifier)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 - 2023 ForgeRock. All rights reserved.
* Copyright (c) 2020 - 2024 ForgeRock. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
Expand Down Expand Up @@ -30,6 +30,7 @@ public class ServerConfig extends NetworkConfig {

public static final String API_VERSION_2_1 = "resource=2.1, protocol=1.0";
public static final String API_VERSION_3_1 = "resource=3.1, protocol=1.0";
public static final String API_VERSION_1_0 = "resource=1.0";
public static final String ACCEPT_API_VERSION = "Accept-API-Version";

private String url;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024 ForgeRock. All rights reserved.
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

package org.forgerock.android.auth.selfservice

import android.net.Uri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import okhttp3.Request
import okhttp3.internal.EMPTY_REQUEST
import org.forgerock.android.auth.FRSession
import org.forgerock.android.auth.OkHttpClientProvider
import org.forgerock.android.auth.SSOToken
import org.forgerock.android.auth.ServerConfig
import org.forgerock.android.auth.exception.ApiException
import org.forgerock.android.auth.json
import java.net.URL

/**
* Retrieves the current SSO token.
*
* @return The current [SSOToken] or an empty token if no session is available.
*/
internal fun ssoToken(): SSOToken {
return FRSession.getCurrentSession()?.sessionToken
?: SSOToken("")

Check warning on line 31 in forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt

View check run for this annotation

Codecov / codecov/patch

forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt#L31

Added line #L31 was not covered by tests
}

/**
* Extension function for [ServerConfig] to build the AM URL.
*
* @return A [Uri.Builder] for the AM URL.
* @throws IllegalArgumentException if the URL is not set.
*/
fun ServerConfig.am(): Uri.Builder {
url?.let {
return Uri.parse(url).buildUpon()
} ?: throw IllegalArgumentException("URL is not set")

Check warning on line 43 in forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt

View check run for this annotation

Codecov / codecov/patch

forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt#L43

Added line #L43 was not covered by tests
}

/**
* Retrieves the session information from the server.
*
* @param server The [ServerConfig] containing server details.
* @param ssoTokenBlock A suspend function to retrieve the SSO token.
* @return The [Session] information.
* @throws ApiException if the session information retrieval fails.
*/
internal suspend fun session(server: ServerConfig, ssoTokenBlock: suspend () -> SSOToken): Session {
val httpClient = OkHttpClientProvider.lookup(server)
val uri = server.am().apply {
appendPath("json")
appendPath("realms")
appendPath(server.realm)
appendPath("sessions")
appendQueryParameter("_action", "getSessionInfo")
}
val request: Request = Request.Builder()
.url(URL(uri.toString()))
.post(EMPTY_REQUEST)
.header("Content-Type", "application/json")
.header(server.cookieName, ssoTokenBlock().value)
.header(ServerConfig.ACCEPT_API_VERSION, ServerConfig.API_VERSION_2_1)
.build()
return withContext(Dispatchers.IO) {
httpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw ApiException(response.code,
response.message,
response.body?.string() ?: "Failed to retrieve user")
}
val resp = response.body?.string()
resp?.let {
json.decodeFromString(it)
} ?: throw ApiException(response.code, response.message, "Failed to retrieve session info")

Check warning on line 80 in forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt

View check run for this annotation

Codecov / codecov/patch

forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt#L80

Added line #L80 was not covered by tests
}
}
}

/**
* Data class representing a user session.
*
* @property username The username of the session.
* @property universalId The universal ID of the session.
* @property realm The realm of the session.
* @property latestAccessTime The latest access time of the session.
* @property maxIdleExpirationTime The maximum idle expiration time of the session.
* @property maxSessionExpirationTime The maximum session expiration time.
*/
@Serializable
data class Session(

Check warning on line 96 in forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt

View check run for this annotation

Codecov / codecov/patch

forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt#L96

Added line #L96 was not covered by tests
val username: String,
val universalId: String,
val realm: String,
val latestAccessTime: String,
val maxIdleExpirationTime: String,

Check warning on line 101 in forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt

View check run for this annotation

Codecov / codecov/patch

forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/Session.kt#L98-L101

Added lines #L98 - L101 were not covered by tests
val maxSessionExpirationTime: String)
Loading

0 comments on commit a44d0bc

Please sign in to comment.