Add getTrustDevice API to AuthSdkSource (#1147)

This commit is contained in:
David Perez 2024-03-14 17:26:23 -05:00 committed by Álison Fernandes
parent 058c4f0dfc
commit 2244e21e29
3 changed files with 40 additions and 0 deletions

View file

@ -5,12 +5,18 @@ 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,6 +6,7 @@ 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
@ -21,6 +22,10 @@ class AuthSdkSourceImpl(
private val clientPlatform: ClientPlatform,
) : AuthSdkSource {
override suspend fun getTrustDevice(): Result<TrustDeviceResponse> = runCatching {
clientAuth.trustDevice()
}
override suspend fun getNewAuthRequest(
email: String,
): Result<AuthRequestResponse> = runCatching {

View file

@ -6,9 +6,11 @@ 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.util.asFailure
import com.x8bit.bitwarden.data.platform.util.asSuccess
import io.mockk.coEvery
import io.mockk.coVerify
@ -27,6 +29,33 @@ class AuthSdkSourceTest {
clientPlatform = clientPlatform,
)
@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"