Add the continue button flow for TDE (#1248)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 403cfc94f0
commit 0a63d85457
9 changed files with 691 additions and 7 deletions
@@ -5,10 +5,12 @@ import com.bitwarden.core.AuthRequestMethod
import com.bitwarden.core.AuthRequestResponse
import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.RegisterKeyResponse
import com.bitwarden.core.RegisterTdeKeyResponse
import com.bitwarden.core.UpdatePasswordResponse
import com.bitwarden.crypto.HashPurpose
import com.bitwarden.crypto.Kdf
import com.bitwarden.crypto.RsaKeyPair
import com.bitwarden.crypto.TrustDeviceResponse
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountTokensJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.EnvironmentUrlDataJson
@@ -55,6 +57,7 @@ import com.x8bit.bitwarden.data.auth.repository.model.BreachCountResult
import com.x8bit.bitwarden.data.auth.repository.model.DeleteAccountResult
import com.x8bit.bitwarden.data.auth.repository.model.KnownDeviceResult
import com.x8bit.bitwarden.data.auth.repository.model.LoginResult
import com.x8bit.bitwarden.data.auth.repository.model.NewSsoUserResult
import com.x8bit.bitwarden.data.auth.repository.model.OrganizationDomainSsoDetailsResult
import com.x8bit.bitwarden.data.auth.repository.model.PasswordHintResult
import com.x8bit.bitwarden.data.auth.repository.model.PasswordStrengthResult
@@ -733,6 +736,430 @@ class AuthRepositoryTest {
}
}
@Test
fun `createNewSsoUser when no active user returns failure`() = runTest {
fakeAuthDiskSource.userState = null
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
}
@Test
fun `createNewSsoUser when remembered org identifier returns failure`() = runTest {
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = null
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
}
@Test
fun `createNewSsoUser when getOrganizationAutoEnrollStatus fails returns failure`() = runTest {
val orgIdentifier = "rememberedOrgIdentifier"
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns Throwable().asFailure()
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
}
}
@Test
fun `createNewSsoUser when getOrganizationKeys fails returns failure`() = runTest {
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery { organizationService.getOrganizationKeys(orgId) } returns Throwable().asFailure()
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
}
}
@Test
fun `createNewSsoUser when makeRegisterTdeKeysAndUnlockVault fails returns failure`() =
runTest {
val shouldTrustDevice = false
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgPublicKey = "organizationPublicKey"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
val orgKeysResponse = OrganizationKeysResponseJson(
privateKey = "privateKey",
publicKey = orgPublicKey,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
fakeAuthDiskSource.shouldTrustDevice = shouldTrustDevice
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery {
organizationService.getOrganizationKeys(orgId)
} returns orgKeysResponse.asSuccess()
coEvery {
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
} returns Throwable().asFailure()
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
}
}
@Test
fun `createNewSsoUser when createAccountKeys fails returns failure`() = runTest {
val shouldTrustDevice = false
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgPublicKey = "organizationPublicKey"
val userPrivateKey = "userPrivateKey"
val userPublicKey = "userPublicKey"
val userAdminReset = "userAdminReset"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
val orgKeysResponse = OrganizationKeysResponseJson(
privateKey = "privateKey",
publicKey = orgPublicKey,
)
val registerTdeKeyResponse = RegisterTdeKeyResponse(
privateKey = userPrivateKey,
publicKey = userPublicKey,
adminReset = userAdminReset,
deviceKey = null,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
fakeAuthDiskSource.shouldTrustDevice = shouldTrustDevice
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery {
organizationService.getOrganizationKeys(orgId)
} returns orgKeysResponse.asSuccess()
coEvery {
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
} returns registerTdeKeyResponse.asSuccess()
coEvery {
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
} returns Throwable().asFailure()
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
}
}
@Test
fun `createNewSsoUser when organizationResetPasswordEnroll fails returns failure`() = runTest {
val shouldTrustDevice = false
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgPublicKey = "organizationPublicKey"
val userPrivateKey = "userPrivateKey"
val userPublicKey = "userPublicKey"
val userAdminReset = "userAdminReset"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
val orgKeysResponse = OrganizationKeysResponseJson(
privateKey = "privateKey",
publicKey = orgPublicKey,
)
val registerTdeKeyResponse = RegisterTdeKeyResponse(
privateKey = userPrivateKey,
publicKey = userPublicKey,
adminReset = userAdminReset,
deviceKey = null,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
fakeAuthDiskSource.shouldTrustDevice = shouldTrustDevice
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery {
organizationService.getOrganizationKeys(orgId)
} returns orgKeysResponse.asSuccess()
coEvery {
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
} returns registerTdeKeyResponse.asSuccess()
coEvery {
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
} returns Unit.asSuccess()
coEvery {
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
} returns Throwable().asFailure()
val result = repository.createNewSsoUser()
assertEquals(NewSsoUserResult.Failure, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
}
}
@Suppress("MaxLineLength")
@Test
fun `createNewSsoUser when shouldTrustDevice false should not trust the device returns Success`() =
runTest {
val shouldTrustDevice = false
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgPublicKey = "organizationPublicKey"
val userPrivateKey = "userPrivateKey"
val userPublicKey = "userPublicKey"
val userAdminReset = "userAdminReset"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
val orgKeysResponse = OrganizationKeysResponseJson(
privateKey = "privateKey",
publicKey = orgPublicKey,
)
val registerTdeKeyResponse = RegisterTdeKeyResponse(
privateKey = userPrivateKey,
publicKey = userPublicKey,
adminReset = userAdminReset,
deviceKey = null,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
fakeAuthDiskSource.shouldTrustDevice = shouldTrustDevice
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery {
organizationService.getOrganizationKeys(orgId)
} returns orgKeysResponse.asSuccess()
coEvery {
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
} returns registerTdeKeyResponse.asSuccess()
coEvery {
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
} returns Unit.asSuccess()
coEvery {
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
} returns Unit.asSuccess()
coEvery { vaultRepository.syncVaultState(userId = USER_ID_1) } just runs
val result = repository.createNewSsoUser()
fakeAuthDiskSource.assertPrivateKey(userId = USER_ID_1, privateKey = userPrivateKey)
assertEquals(NewSsoUserResult.Success, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
vaultRepository.syncVaultState(userId = USER_ID_1)
}
}
@Test
fun `createNewSsoUser when shouldTrustDevice true should trust the device returns Success`() =
runTest {
val shouldTrustDevice = true
val orgIdentifier = "rememberedOrgIdentifier"
val orgId = "organizationId"
val orgPublicKey = "organizationPublicKey"
val userPrivateKey = "userPrivateKey"
val userPublicKey = "userPublicKey"
val userAdminReset = "userAdminReset"
val orgAutoEnrollStatusResponse = OrganizationAutoEnrollStatusResponseJson(
organizationId = orgId,
isResetPasswordEnabled = false,
)
val orgKeysResponse = OrganizationKeysResponseJson(
privateKey = "privateKey",
publicKey = orgPublicKey,
)
val trustDeviceResponse = mockk<TrustDeviceResponse>()
val registerTdeKeyResponse = RegisterTdeKeyResponse(
privateKey = userPrivateKey,
publicKey = userPublicKey,
adminReset = userAdminReset,
deviceKey = trustDeviceResponse,
)
fakeAuthDiskSource.userState = SINGLE_USER_STATE_1
fakeAuthDiskSource.rememberedOrgIdentifier = orgIdentifier
fakeAuthDiskSource.shouldTrustDevice = shouldTrustDevice
coEvery {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
} returns orgAutoEnrollStatusResponse.asSuccess()
coEvery {
organizationService.getOrganizationKeys(orgId)
} returns orgKeysResponse.asSuccess()
coEvery {
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
} returns registerTdeKeyResponse.asSuccess()
coEvery {
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
} returns Unit.asSuccess()
coEvery {
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
} returns Unit.asSuccess()
coEvery {
trustedDeviceManager.trustThisDevice(
userId = USER_ID_1,
trustDeviceResponse = trustDeviceResponse,
)
} returns Unit.asSuccess()
coEvery { vaultRepository.syncVaultState(userId = USER_ID_1) } just runs
val result = repository.createNewSsoUser()
fakeAuthDiskSource.assertPrivateKey(userId = USER_ID_1, privateKey = userPrivateKey)
assertEquals(NewSsoUserResult.Success, result)
coVerify(exactly = 1) {
organizationService.getOrganizationAutoEnrollStatus(orgIdentifier)
organizationService.getOrganizationKeys(orgId)
authSdkSource.makeRegisterTdeKeysAndUnlockVault(
userId = USER_ID_1,
orgPublicKey = orgPublicKey,
rememberDevice = shouldTrustDevice,
)
accountsService.createAccountKeys(
publicKey = userPublicKey,
encryptedPrivateKey = userPrivateKey,
)
organizationService.organizationResetPasswordEnroll(
organizationId = orgId,
userId = USER_ID_1,
passwordHash = null,
resetPasswordKey = userAdminReset,
)
trustedDeviceManager.trustThisDevice(
userId = USER_ID_1,
trustDeviceResponse = trustDeviceResponse,
)
vaultRepository.syncVaultState(userId = USER_ID_1)
}
}
@Test
fun `completeTdeLogin without active user fails`() = runTest {
val requestPrivateKey = "requestPrivateKey"
@@ -1285,6 +1285,46 @@ class VaultLockManagerTest {
assertTrue(vaultLockManager.isVaultUnlocked(userId = USER_ID))
}
@Suppress("MaxLineLength")
@Test
fun `syncVaultState with getUserEncryptionKey failure should update the users vault state to locked`() =
runTest {
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = USER_ID)
} returns Throwable().asFailure()
// Begin in a locked state
assertFalse(vaultLockManager.isVaultUnlocked(userId = USER_ID))
vaultLockManager.syncVaultState(userId = USER_ID)
// Confirm the vault is still locked
assertFalse(vaultLockManager.isVaultUnlocked(userId = USER_ID))
coVerify(exactly = 1) {
vaultSdkSource.getUserEncryptionKey(userId = USER_ID)
}
}
@Suppress("MaxLineLength")
@Test
fun `syncVaultState with getUserEncryptionKey success should update the users vault state to unlocked`() =
runTest {
coEvery {
vaultSdkSource.getUserEncryptionKey(userId = USER_ID)
} returns "UserEncryptionKey".asSuccess()
// Begin in a locked state
assertFalse(vaultLockManager.isVaultUnlocked(userId = USER_ID))
vaultLockManager.syncVaultState(userId = USER_ID)
// Confirm the vault is unlocked
assertTrue(vaultLockManager.isVaultUnlocked(userId = USER_ID))
coVerify(exactly = 1) {
vaultSdkSource.getUserEncryptionKey(userId = USER_ID)
}
}
/**
* Resets the verification call count for the given [mock] while leaving all other mocked
* behavior in place.
@@ -2,13 +2,17 @@ package com.x8bit.bitwarden.ui.auth.feature.trusteddevice
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.NewSsoUserResult
import com.x8bit.bitwarden.data.auth.repository.model.UserState
import com.x8bit.bitwarden.data.platform.repository.EnvironmentRepository
import com.x8bit.bitwarden.data.platform.repository.model.Environment
import com.x8bit.bitwarden.data.platform.repository.util.FakeEnvironmentRepository
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
@@ -77,14 +81,68 @@ class TrustedDeviceViewModelTest : BaseViewModelTest() {
}
@Test
fun `on ContinueClick emits ShowToast`() = runTest {
val viewModel = createViewModel()
fun `on ContinueClick with createNewSsoUser failure should display the error dialog state`() =
runTest {
every { authRepository.shouldTrustDevice = true } just runs
coEvery { authRepository.createNewSsoUser() } returns NewSsoUserResult.Failure
val viewModel = createViewModel()
viewModel.eventFlow.test {
viewModel.trySendAction(TrustedDeviceAction.ContinueClick)
assertEquals(TrustedDeviceEvent.ShowToast("Not yet implemented".asText()), awaitItem())
viewModel.stateFlow.test {
assertEquals(DEFAULT_STATE, awaitItem())
viewModel.trySendAction(TrustedDeviceAction.ContinueClick)
assertEquals(
DEFAULT_STATE.copy(
dialogState = TrustedDeviceState.DialogState.Loading(
message = R.string.loading.asText(),
),
),
awaitItem(),
)
assertEquals(
DEFAULT_STATE.copy(
dialogState = TrustedDeviceState.DialogState.Error(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
),
awaitItem(),
)
}
verify(exactly = 1) {
authRepository.shouldTrustDevice = true
}
coVerify(exactly = 1) {
authRepository.createNewSsoUser()
}
}
@Test
fun `on ContinueClick with createNewSsoUser success should display the loading dialog state`() =
runTest {
every { authRepository.shouldTrustDevice = true } just runs
coEvery { authRepository.createNewSsoUser() } returns NewSsoUserResult.Success
val viewModel = createViewModel()
viewModel.stateFlow.test {
assertEquals(DEFAULT_STATE, awaitItem())
viewModel.trySendAction(TrustedDeviceAction.ContinueClick)
assertEquals(
DEFAULT_STATE.copy(
dialogState = TrustedDeviceState.DialogState.Loading(
message = R.string.loading.asText(),
),
),
awaitItem(),
)
assertEquals(DEFAULT_STATE, awaitItem())
}
verify(exactly = 1) {
authRepository.shouldTrustDevice = true
}
coVerify(exactly = 1) {
authRepository.createNewSsoUser()
}
}
}
@Test
fun `on ApproveWithAdminClick emits NavigateToApproveWithAdmin`() = runTest {