Move the sdk trustDevice API to the VaultSdkSource (#1184)

This commit is contained in:
David Perez 2024-03-28 14:26:41 -05:00 committed by Álison Fernandes
parent de6f31775b
commit ed8dfa841e
6 changed files with 48 additions and 40 deletions

View file

@ -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<TrustDeviceResponse>
/**
* Gets the data needed to create a new auth request.
*/

View file

@ -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<TrustDeviceResponse> = runCatching {
clientAuth.trustDevice()
}
override suspend fun getNewAuthRequest(
email: String,
): Result<AuthRequestResponse> = runCatching {

View file

@ -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<TrustDeviceResponse>
/**
* 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].

View file

@ -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<TrustDeviceResponse> = runCatching {
getClient(userId = userId)
.auth()
.trustDevice()
}
override suspend fun derivePinKey(
userId: String,
pin: String,

View file

@ -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<TrustDeviceResponse>()
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"

View file

@ -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<TrustDeviceResponse>()
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"