Allow device key to be persisted to memory (#1209)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent 039253ac96
commit 0561879d6b
5 changed files with 58 additions and 12 deletions
@@ -38,7 +38,7 @@ class FakeAuthDiskSource : AuthDiskSource {
mutableMapOf<String, List<SyncResponseJson.Profile.Organization>?>()
private val storedOrganizationKeys = mutableMapOf<String, Map<String, String>?>()
private val storedAccountTokens = mutableMapOf<String, AccountTokensJson?>()
private val storedDeviceKey = mutableMapOf<String, String?>()
private val storedDeviceKey = mutableMapOf<String, Pair<String?, Boolean>>()
private val storedPendingAuthRequests = mutableMapOf<String, PendingAuthRequestJson?>()
private val storedBiometricKeys = mutableMapOf<String, String?>()
private val storedMasterPasswordHashes = mutableMapOf<String, String?>()
@@ -167,10 +167,14 @@ class FakeAuthDiskSource : AuthDiskSource {
getMutableOrganizationsFlow(userId = userId).tryEmit(organizations)
}
override fun getDeviceKey(userId: String): String? = storedDeviceKey[userId]
override fun getDeviceKey(userId: String): String? = storedDeviceKey[userId]?.first
override fun storeDeviceKey(userId: String, deviceKey: String?) {
storedDeviceKey[userId] = deviceKey
override fun storeDeviceKey(
userId: String,
deviceKey: String?,
inMemoryOnly: Boolean,
) {
storedDeviceKey[userId] = deviceKey to inMemoryOnly
}
override fun getPendingAuthRequest(userId: String): PendingAuthRequestJson? =
@@ -298,8 +302,8 @@ class FakeAuthDiskSource : AuthDiskSource {
/**
* Assert that the [deviceKey] was stored successfully using the [userId].
*/
fun assertDeviceKey(userId: String, deviceKey: String?) {
assertEquals(deviceKey, storedDeviceKey[userId])
fun assertDeviceKey(userId: String, deviceKey: String?, inMemoryOnly: Boolean = false) {
assertEquals(deviceKey to inMemoryOnly, storedDeviceKey[userId])
}
/**
@@ -32,13 +32,30 @@ class TrustedDeviceManagerTests {
fun `trustThisDeviceIfNecessary when shouldTrustDevice false should return success with false`() =
runTest {
val userId = "userId"
val deviceKey = "deviceKey"
val trustedDeviceResponse = TrustDeviceResponse(
deviceKey = deviceKey,
protectedUserKey = "protectedUserKey",
protectedDevicePrivateKey = "protectedDevicePrivateKey",
protectedDevicePublicKey = "protectedDevicePublicKey",
)
fakeAuthDiskSource.shouldTrustDevice = false
coEvery {
vaultSdkSource.getTrustDevice(userId = userId)
} returns trustedDeviceResponse.asSuccess()
val result = manager.trustThisDeviceIfNecessary(userId = userId)
assertEquals(false.asSuccess(), result)
coVerify(exactly = 0) {
fakeAuthDiskSource.assertDeviceKey(
userId = userId,
deviceKey = deviceKey,
inMemoryOnly = true,
)
coVerify(exactly = 1) {
vaultSdkSource.getTrustDevice(userId = userId)
}
coVerify(exactly = 0) {
devicesService.trustDevice(
appId = any(),
encryptedUserKey = any(),