Skip to content

Commit

Permalink
Merge pull request #461 from ForgeRock/SDKS-3541
Browse files Browse the repository at this point in the history
SDKS-3541 Align Device Self service naming with iOS SDK
  • Loading branch information
spetrov authored Nov 5, 2024
2 parents f1b5c1f + 19963a8 commit 1ad6a27
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OathDevice>
suspend fun oathDevices(): List<OathDevice>

/**
* Retrieves a list of Push devices.
* @return A list of [PushDevice].
*/
suspend fun pushDevice(): List<PushDevice>
suspend fun pushDevices(): List<PushDevice>

/**
* Retrieves a list of Binding devices.
* @return A list of [BindingDevice].
*/
suspend fun bindingDevice(): List<BindingDevice>
suspend fun bindingDevices(): List<BindingDevice>

/**
* Retrieves a list of WebAuthn devices.
* @return A list of [WebAuthnDevice].
*/
suspend fun webAuthnDevice(): List<WebAuthnDevice>
suspend fun webAuthnDevices(): List<WebAuthnDevice>

/**
* Retrieves a list of Profile devices.
* @return A list of [ProfileDevice].
*/
suspend fun profileDevice(): List<ProfileDevice>
suspend fun profileDevices(): List<ProfileDevice>

/**
* Updates the given device.
Expand All @@ -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)
Expand All @@ -96,7 +96,7 @@ class UserRepository(
* Retrieves a list of Oath devices.
* @return A list of [OathDevice].
*/
override suspend fun oathDevice(): List<OathDevice> = withContext(Dispatchers.IO) {
override suspend fun oathDevices(): List<OathDevice> = withContext(Dispatchers.IO) {
httpClient.newCall(request("devices/2fa/oath")).execute().use { response ->
get(response)
}
Expand All @@ -106,7 +106,7 @@ class UserRepository(
* Retrieves a list of Push devices.
* @return A list of [PushDevice].
*/
override suspend fun pushDevice(): List<PushDevice> = withContext(Dispatchers.IO) {
override suspend fun pushDevices(): List<PushDevice> = withContext(Dispatchers.IO) {
httpClient.newCall(request("devices/2fa/push")).execute().use { response ->
get(response)
}
Expand All @@ -116,7 +116,7 @@ class UserRepository(
* Retrieves a list of Binding devices.
* @return A list of [BindingDevice].
*/
override suspend fun bindingDevice(): List<BindingDevice> = withContext(Dispatchers.IO) {
override suspend fun bindingDevices(): List<BindingDevice> = withContext(Dispatchers.IO) {
httpClient.newCall(request("devices/2fa/binding")).execute().use { response ->
get(response)
}
Expand All @@ -126,7 +126,7 @@ class UserRepository(
* Retrieves a list of WebAuthn devices.
* @return A list of [WebAuthnDevice].
*/
override suspend fun webAuthnDevice(): List<WebAuthnDevice> = withContext(Dispatchers.IO) {
override suspend fun webAuthnDevices(): List<WebAuthnDevice> = withContext(Dispatchers.IO) {
httpClient.newCall(request("devices/2fa/webauthn")).execute().use { response ->
get(response)
}
Expand All @@ -136,7 +136,7 @@ class UserRepository(
* Retrieves a list of Profile devices.
* @return A list of [ProfileDevice].
*/
override suspend fun profileDevice(): List<ProfileDevice> = withContext(Dispatchers.IO) {
override suspend fun profileDevices(): List<ProfileDevice> = withContext(Dispatchers.IO) {
httpClient.newCall(request("devices/profile")).execute().use { response ->
get(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -37,7 +37,7 @@ class UserRepositoryMockTest : BaseTest() {
.url(url)
.realm("alpha")
.cookieName("5421aeddf91aa20")
userRepository = UserRepository(serverConfig.build()) {
deviceClient = DeviceClient(serverConfig.build()) {
SSOToken("ssoTokenValue")
}
}
Expand All @@ -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")
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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) }
Expand Down

0 comments on commit 1ad6a27

Please sign in to comment.