diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSource.kt index 86b6659f79..fd593e1caf 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSource.kt @@ -3,6 +3,7 @@ package com.x8bit.bitwarden.data.auth.datasource.sdk import com.bitwarden.core.AuthRequestResponse import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse +import com.bitwarden.core.RegisterTdeKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength @@ -45,6 +46,16 @@ interface AuthSdkSource { kdf: Kdf, ): Result + /** + * Creates a set of encryption key information for registration of a trusted device and unlocks + * the vault for the user. + */ + suspend fun makeRegisterTdeKeysAndUnlockVault( + userId: String, + orgPublicKey: String, + rememberDevice: Boolean, + ): Result + /** * Checks the password strength for the given [email] and [password] combination, along with * some [additionalInputs]. diff --git a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceImpl.kt index e2557519e7..4d6db7a20d 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceImpl.kt @@ -4,6 +4,7 @@ import com.bitwarden.core.AuthRequestResponse import com.bitwarden.core.FingerprintRequest import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse +import com.bitwarden.core.RegisterTdeKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf import com.bitwarden.sdk.Client @@ -75,6 +76,19 @@ class AuthSdkSourceImpl( ) } + override suspend fun makeRegisterTdeKeysAndUnlockVault( + userId: String, + orgPublicKey: String, + rememberDevice: Boolean, + ): Result = runCatching { + getClient(userId = userId) + .auth() + .makeRegisterTdeKeys( + orgPublicKey = orgPublicKey, + rememberDevice = rememberDevice, + ) + } + override suspend fun passwordStrength( email: String, password: String, diff --git a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceTest.kt index dc2caca7b8..5f4597b6ec 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/auth/datasource/sdk/AuthSdkSourceTest.kt @@ -4,6 +4,7 @@ import com.bitwarden.core.AuthRequestResponse import com.bitwarden.core.FingerprintRequest import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse +import com.bitwarden.core.RegisterTdeKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf import com.bitwarden.sdk.Client @@ -157,6 +158,36 @@ class AuthSdkSourceTest { } } + @Suppress("MaxLineLength") + @Test + fun `makeRegisterTdeKeysAndUnlockVault should call SDK and return a Result with the correct data`() = + runBlocking { + val userId = "userId" + val orgPublicKey = "orgPublicKey" + val rememberDevice = true + val expectedResult = mockk() + coEvery { sdkClientManager.getOrCreateClient(userId = userId) } returns client + coEvery { + clientAuth.makeRegisterTdeKeys( + orgPublicKey = orgPublicKey, + rememberDevice = rememberDevice, + ) + } returns expectedResult + + val result = authSkdSource.makeRegisterTdeKeysAndUnlockVault( + userId = userId, + orgPublicKey = orgPublicKey, + rememberDevice = rememberDevice, + ) + assertEquals(expectedResult.asSuccess(), result) + coVerify(exactly = 1) { + clientAuth.makeRegisterTdeKeys( + orgPublicKey = orgPublicKey, + rememberDevice = rememberDevice, + ) + } + } + // TODO: This test is disabled due to issue here with mocking UByte (BIT-877). // See: https://github.com/mockk/mockk/issues/544 @Disabled