From 19963a8c7e089ea0c3d333b688d7f08b1575992a Mon Sep 17 00:00:00 2001 From: Andy Witrisna Date: Mon, 4 Nov 2024 13:52:13 -0800 Subject: [PATCH] SDKS-3541 Align Device Self service naming with iOS SDK --- .../{UserRepository.kt => DeviceClient.kt} | 28 +++++++-------- ...oryMockTest.kt => DeviceClientMockTest.kt} | 34 +++++++++---------- .../app/selfservice/SelfServiceViewModel.kt | 15 ++++---- 3 files changed, 38 insertions(+), 39 deletions(-) rename forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/{UserRepository.kt => DeviceClient.kt} (92%) rename forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/{UserRepositoryMockTest.kt => DeviceClientMockTest.kt} (93%) diff --git a/forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/UserRepository.kt b/forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/DeviceClient.kt similarity index 92% rename from forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/UserRepository.kt rename to forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/DeviceClient.kt index f719c7e1..2e6fa634 100644 --- a/forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/UserRepository.kt +++ b/forgerock-auth/src/main/java/org/forgerock/android/auth/selfservice/DeviceClient.kt @@ -35,36 +35,36 @@ private const val APPLICATION_JSON = "application/json" /** * Interface defining the repository for user devices. */ -interface UserRepo { +interface DeviceRepository { /** * Retrieves a list of Oath devices. * @return A list of [OathDevice]. */ - suspend fun oathDevice(): List + suspend fun oathDevices(): List /** * Retrieves a list of Push devices. * @return A list of [PushDevice]. */ - suspend fun pushDevice(): List + suspend fun pushDevices(): List /** * Retrieves a list of Binding devices. * @return A list of [BindingDevice]. */ - suspend fun bindingDevice(): List + suspend fun bindingDevices(): List /** * Retrieves a list of WebAuthn devices. * @return A list of [WebAuthnDevice]. */ - suspend fun webAuthnDevice(): List + suspend fun webAuthnDevices(): List /** * Retrieves a list of Profile devices. * @return A list of [ProfileDevice]. */ - suspend fun profileDevice(): List + suspend fun profileDevices(): List /** * Updates the given device. @@ -80,14 +80,14 @@ interface UserRepo { } /** - * Implementation of [UserRepo] for managing user devices. + * Implementation of [DeviceRepository] for managing user devices. * @property server The server configuration. * @property ssoTokenBlock A suspend function to retrieve the SSO token. */ -class UserRepository( +class DeviceClient( private val server: ServerConfig = Config.getInstance().serverConfig, private val ssoTokenBlock: suspend () -> SSOToken = { ssoToken() } -) : UserRepo { +) : DeviceRepository { private val httpClient: OkHttpClient = OkHttpClientProvider.getInstance() .lookup(server) @@ -96,7 +96,7 @@ class UserRepository( * Retrieves a list of Oath devices. * @return A list of [OathDevice]. */ - override suspend fun oathDevice(): List = withContext(Dispatchers.IO) { + override suspend fun oathDevices(): List = withContext(Dispatchers.IO) { httpClient.newCall(request("devices/2fa/oath")).execute().use { response -> get(response) } @@ -106,7 +106,7 @@ class UserRepository( * Retrieves a list of Push devices. * @return A list of [PushDevice]. */ - override suspend fun pushDevice(): List = withContext(Dispatchers.IO) { + override suspend fun pushDevices(): List = withContext(Dispatchers.IO) { httpClient.newCall(request("devices/2fa/push")).execute().use { response -> get(response) } @@ -116,7 +116,7 @@ class UserRepository( * Retrieves a list of Binding devices. * @return A list of [BindingDevice]. */ - override suspend fun bindingDevice(): List = withContext(Dispatchers.IO) { + override suspend fun bindingDevices(): List = withContext(Dispatchers.IO) { httpClient.newCall(request("devices/2fa/binding")).execute().use { response -> get(response) } @@ -126,7 +126,7 @@ class UserRepository( * Retrieves a list of WebAuthn devices. * @return A list of [WebAuthnDevice]. */ - override suspend fun webAuthnDevice(): List = withContext(Dispatchers.IO) { + override suspend fun webAuthnDevices(): List = withContext(Dispatchers.IO) { httpClient.newCall(request("devices/2fa/webauthn")).execute().use { response -> get(response) } @@ -136,7 +136,7 @@ class UserRepository( * Retrieves a list of Profile devices. * @return A list of [ProfileDevice]. */ - override suspend fun profileDevice(): List = withContext(Dispatchers.IO) { + override suspend fun profileDevices(): List = withContext(Dispatchers.IO) { httpClient.newCall(request("devices/profile")).execute().use { response -> get(response) } diff --git a/forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/UserRepositoryMockTest.kt b/forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/DeviceClientMockTest.kt similarity index 93% rename from forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/UserRepositoryMockTest.kt rename to forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/DeviceClientMockTest.kt index c34fa850..906046a7 100644 --- a/forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/UserRepositoryMockTest.kt +++ b/forgerock-auth/src/test/java/org/forgerock/android/auth/selfservice/DeviceClientMockTest.kt @@ -23,10 +23,10 @@ import org.junit.runner.RunWith import java.net.HttpURLConnection @RunWith(AndroidJUnit4::class) -class UserRepositoryMockTest : BaseTest() { +class DeviceClientMockTest : BaseTest() { var context: Context = ApplicationProvider.getApplicationContext() - private lateinit var userRepository: UserRepository + private lateinit var deviceClient: DeviceClient @Before fun setUp() { @@ -37,7 +37,7 @@ class UserRepositoryMockTest : BaseTest() { .url(url) .realm("alpha") .cookieName("5421aeddf91aa20") - userRepository = UserRepository(serverConfig.build()) { + deviceClient = DeviceClient(serverConfig.build()) { SSOToken("ssoTokenValue") } } @@ -46,7 +46,7 @@ class UserRepositoryMockTest : BaseTest() { fun oathDevice_returnsListOfOathDevices() = runTest { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successOath.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.oathDevice() + val devices = deviceClient.oathDevices() assert(devices.isNotEmpty()) val sessionInfoReq = server.takeRequest() assertThat(sessionInfoReq.method).isEqualTo("POST") @@ -71,7 +71,7 @@ class UserRepositoryMockTest : BaseTest() { fun pushDevice_returnsListOfPushDevices() = runTest { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successPush.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.pushDevice() + val devices = deviceClient.pushDevices() assert(devices.isNotEmpty()) val sessionInfoReq = server.takeRequest() @@ -93,7 +93,7 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successDeviceBinding.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.bindingDevice() + val devices = deviceClient.bindingDevices() assert(devices.isNotEmpty()) assertThat(devices.size).isEqualTo(4) val sessionInfoReq = server.takeRequest() @@ -116,7 +116,7 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successWebAuthn.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.webAuthnDevice() + val devices = deviceClient.webAuthnDevices() assert(devices.isNotEmpty()) val sessionInfoReq = server.takeRequest() val oathReq = server.takeRequest() @@ -138,7 +138,7 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successDeviceProfile.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.profileDevice() + val devices = deviceClient.profileDevices() assert(devices.isNotEmpty()) val sessionInfoReq = server.takeRequest() @@ -163,10 +163,10 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successUpdateDeviceBinding.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.bindingDevice() + val devices = deviceClient.bindingDevices() assert(devices.isNotEmpty()) assertThat(devices.size).isEqualTo(4) - userRepository.update(devices[0]) + deviceClient.update(devices[0]) val sessionInfoReq = server.takeRequest() val bindingReq = server.takeRequest() val sessionInfoReq2 = server.takeRequest() @@ -184,9 +184,9 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/successUpdateDeviceBinding.json", HttpURLConnection.HTTP_OK) - val devices = userRepository.bindingDevice() + val devices = deviceClient.bindingDevices() assert(devices.isNotEmpty()) - userRepository.delete(devices[0]) + deviceClient.delete(devices[0]) val sessionInfoReq = server.takeRequest() val bindingReq = server.takeRequest() val sessionInfoReq2 = server.takeRequest() @@ -205,9 +205,9 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/accessDenied.json", HttpURLConnection.HTTP_UNAUTHORIZED) - val devices = userRepository.bindingDevice() + val devices = deviceClient.bindingDevices() assert(devices.isNotEmpty()) - userRepository.delete(devices[0]) + deviceClient.delete(devices[0]) val sessionInfoReq = server.takeRequest() val bindingReq = server.takeRequest() val sessionInfoReq2 = server.takeRequest() @@ -227,9 +227,9 @@ class UserRepositoryMockTest : BaseTest() { enqueue("/selfservice/sessionInfo.json", HttpURLConnection.HTTP_OK) enqueue("/selfservice/forbidden.json", HttpURLConnection.HTTP_UNAUTHORIZED) - val devices = userRepository.bindingDevice() + val devices = deviceClient.bindingDevices() assert(devices.isNotEmpty()) - userRepository.delete(devices[0]) + deviceClient.delete(devices[0]) val sessionInfoReq = server.takeRequest() val bindingReq = server.takeRequest() val sessionInfoReq2 = server.takeRequest() @@ -247,7 +247,7 @@ class UserRepositoryMockTest : BaseTest() { fun sessionExpired() = runTest { enqueue("/selfservice/accessDenied.json", HttpURLConnection.HTTP_UNAUTHORIZED) try { - userRepository.bindingDevice() + deviceClient.bindingDevices() } catch (e: ApiException) { assertThat(e.statusCode).isEqualTo(HttpURLConnection.HTTP_UNAUTHORIZED) throw e diff --git a/samples/app/src/main/java/com/example/app/selfservice/SelfServiceViewModel.kt b/samples/app/src/main/java/com/example/app/selfservice/SelfServiceViewModel.kt index 3cae4438..0941a835 100644 --- a/samples/app/src/main/java/com/example/app/selfservice/SelfServiceViewModel.kt +++ b/samples/app/src/main/java/com/example/app/selfservice/SelfServiceViewModel.kt @@ -9,17 +9,16 @@ package com.example.app.selfservice import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.example.app.token.TokenState import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.yield import org.forgerock.android.auth.selfservice.Device -import org.forgerock.android.auth.selfservice.UserRepository +import org.forgerock.android.auth.selfservice.DeviceClient class SelfServiceViewModel : ViewModel() { - private val repo = UserRepository() + private val repo = DeviceClient() private var selectedType = "Oath" var state = MutableStateFlow(SelfServiceState()) @@ -57,11 +56,11 @@ class SelfServiceViewModel : ViewModel() { viewModelScope.launch { try { selectedDevices = when (type) { - "Oath" -> repo.oathDevice() - "Push" -> repo.pushDevice() - "WebAuthn" -> repo.webAuthnDevice() - "Binding" -> repo.bindingDevice() - "Profile" -> repo.profileDevice() + "Oath" -> repo.oathDevices() + "Push" -> repo.pushDevices() + "WebAuthn" -> repo.webAuthnDevices() + "Binding" -> repo.bindingDevices() + "Profile" -> repo.profileDevices() else -> emptyList() } state.update { it.copy(devices = selectedDevices, throwable = null) }