mirror of
https://github.com/bitwarden/android.git
synced 2024-11-29 06:18:52 +03:00
Move the sdk trustDevice API to the VaultSdkSource (#1184)
This commit is contained in:
parent
de6f31775b
commit
ed8dfa841e
6 changed files with 48 additions and 40 deletions
|
@ -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.
|
||||
*/
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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].
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue