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 6f3444534..86b6659f7 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 @@ -5,18 +5,12 @@ import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf -import com.bitwarden.crypto.TrustDeviceResponse import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength /** * Source of authentication information and functionality from the Bitwarden SDK. */ interface AuthSdkSource { - /** - * Gets the data to authenticate with trusted device encryption. - */ - suspend fun getTrustDevice(): Result - /** * Gets the data needed to create a new auth request. */ 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 d418ee2c2..a8e7b243b 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 @@ -6,7 +6,6 @@ import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf -import com.bitwarden.crypto.TrustDeviceResponse import com.bitwarden.sdk.ClientAuth import com.bitwarden.sdk.ClientPlatform import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength @@ -36,10 +35,6 @@ class AuthSdkSourceImpl( } } - override suspend fun getTrustDevice(): Result = runCatching { - clientAuth.trustDevice() - } - override suspend fun getNewAuthRequest( email: String, ): Result = runCatching { diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt index f3055a753..39bdb79e0 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt @@ -22,6 +22,7 @@ import com.bitwarden.core.Send import com.bitwarden.core.SendView import com.bitwarden.core.TotpResponse import com.bitwarden.core.UpdatePasswordResponse +import com.bitwarden.crypto.TrustDeviceResponse import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult /** @@ -36,6 +37,14 @@ interface VaultSdkSource { */ fun clearCrypto(userId: String) + /** + * Gets the data to authenticate with trusted device encryption. + * + * This should only be called after a successful call to [initializeCrypto] for the associated + * user. + */ + suspend fun getTrustDevice(userId: String): Result + /** * Derives a "pin key" from the given [pin] for the given [userId]. This can be used to later * unlock their vault via a call to [initializeCrypto] with [InitUserCryptoMethod.Pin]. diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt index 1d208f9af..f73e1f6b8 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt @@ -21,6 +21,7 @@ import com.bitwarden.core.Send import com.bitwarden.core.SendView import com.bitwarden.core.TotpResponse import com.bitwarden.core.UpdatePasswordResponse +import com.bitwarden.crypto.TrustDeviceResponse import com.bitwarden.sdk.BitwardenException import com.bitwarden.sdk.Client import com.bitwarden.sdk.ClientVault @@ -39,6 +40,14 @@ class VaultSdkSourceImpl( sdkClientManager.destroyClient(userId = userId) } + override suspend fun getTrustDevice( + userId: String, + ): Result = runCatching { + getClient(userId = userId) + .auth() + .trustDevice() + } + override suspend fun derivePinKey( userId: String, pin: 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 aaa57eb9b..14e1d0481 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 @@ -6,12 +6,10 @@ import com.bitwarden.core.MasterPasswordPolicyOptions import com.bitwarden.core.RegisterKeyResponse import com.bitwarden.crypto.HashPurpose import com.bitwarden.crypto.Kdf -import com.bitwarden.crypto.TrustDeviceResponse import com.bitwarden.sdk.ClientAuth import com.bitwarden.sdk.ClientPlatform import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength import com.x8bit.bitwarden.data.platform.base.FakeDispatcherManager -import com.x8bit.bitwarden.data.platform.util.asFailure import com.x8bit.bitwarden.data.platform.util.asSuccess import com.x8bit.bitwarden.data.vault.datasource.sdk.BitwardenFeatureFlagManager import io.mockk.coEvery @@ -50,33 +48,6 @@ class AuthSdkSourceTest { } } - @Test - fun `getTrustDevice with trustDevice success should return success with correct data`() = - runBlocking { - val expectedResult = mockk() - coEvery { clientAuth.trustDevice() } returns expectedResult - - val result = authSkdSource.getTrustDevice() - - assertEquals(expectedResult.asSuccess(), result) - coVerify(exactly = 1) { - clientAuth.trustDevice() - } - } - - @Test - fun `getTrustDevice with trustDevice exception should return a failure`() = runBlocking { - val error = Throwable("Fail") - coEvery { clientAuth.trustDevice() } throws error - - val result = authSkdSource.getTrustDevice() - - assertEquals(error.asFailure(), result) - coVerify(exactly = 1) { - clientAuth.trustDevice() - } - } - @Test fun `getNewAuthRequest should call SDK and return a Result with correct data`() = runBlocking { val email = "test@gmail.com" diff --git a/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt index 9224952da..d37e49c05 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt @@ -21,6 +21,7 @@ import com.bitwarden.core.Send import com.bitwarden.core.SendView import com.bitwarden.core.TotpResponse import com.bitwarden.core.UpdatePasswordResponse +import com.bitwarden.crypto.TrustDeviceResponse import com.bitwarden.sdk.BitwardenException import com.bitwarden.sdk.Client import com.bitwarden.sdk.ClientAuth @@ -83,6 +84,35 @@ class VaultSdkSourceTest { verify { sdkClientManager.destroyClient(userId = userId) } } + @Test + fun `getTrustDevice with trustDevice success should return success with correct data`() = + runBlocking { + val userId = "userId" + val expectedResult = mockk() + coEvery { clientAuth.trustDevice() } returns expectedResult + + val result = vaultSdkSource.getTrustDevice(userId = userId) + + assertEquals(expectedResult.asSuccess(), result) + coVerify(exactly = 1) { + clientAuth.trustDevice() + } + } + + @Test + fun `getTrustDevice with trustDevice exception should return a failure`() = runBlocking { + val userId = "userId" + val error = Throwable("Fail") + coEvery { clientAuth.trustDevice() } throws error + + val result = vaultSdkSource.getTrustDevice(userId = userId) + + assertEquals(error.asFailure(), result) + coVerify(exactly = 1) { + clientAuth.trustDevice() + } + } + @Test fun `derivePinKey should call SDK and return a Result with the correct data`() = runBlocking { val userId = "userId"