Update the Bitwarden SDK (#276)

This commit is contained in:
David Perez 2023-11-27 09:56:53 -06:00 committed by Álison Fernandes
parent d36601fa3a
commit baafab6e67
6 changed files with 93 additions and 78 deletions

View file

@ -5,7 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Folder import com.bitwarden.core.Folder
import com.bitwarden.core.FolderView import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Send import com.bitwarden.core.Send
import com.bitwarden.core.SendView import com.bitwarden.core.SendView
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult
@ -19,7 +19,7 @@ interface VaultSdkSource {
* Attempts to initialize cryptography functionality for the Bitwarden SDK * Attempts to initialize cryptography functionality for the Bitwarden SDK
* with a given [InitCryptoRequest]. * with a given [InitCryptoRequest].
*/ */
suspend fun initializeCrypto(request: InitCryptoRequest): Result<InitializeCryptoResult> suspend fun initializeCrypto(request: InitUserCryptoRequest): Result<InitializeCryptoResult>
/** /**
* Decrypts a [Cipher] returning a [CipherView] wrapped in a [Result]. * Decrypts a [Cipher] returning a [CipherView] wrapped in a [Result].

View file

@ -5,7 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Folder import com.bitwarden.core.Folder
import com.bitwarden.core.FolderView import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Send import com.bitwarden.core.Send
import com.bitwarden.core.SendView import com.bitwarden.core.SendView
import com.bitwarden.sdk.BitwardenException import com.bitwarden.sdk.BitwardenException
@ -22,11 +22,11 @@ class VaultSdkSourceImpl(
private val clientCrypto: ClientCrypto, private val clientCrypto: ClientCrypto,
) : VaultSdkSource { ) : VaultSdkSource {
override suspend fun initializeCrypto( override suspend fun initializeCrypto(
request: InitCryptoRequest, request: InitUserCryptoRequest,
): Result<InitializeCryptoResult> = ): Result<InitializeCryptoResult> =
runCatching { runCatching {
try { try {
clientCrypto.initializeCrypto(req = request) clientCrypto.initializeUserCrypto(req = request)
InitializeCryptoResult.Success InitializeCryptoResult.Success
} catch (exception: BitwardenException) { } catch (exception: BitwardenException) {
// The only truly expected error from the SDK is an incorrect password. // The only truly expected error from the SDK is an incorrect password.

View file

@ -2,7 +2,8 @@ package com.x8bit.bitwarden.data.vault.repository
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.FolderView import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Kdf import com.bitwarden.core.Kdf
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
@ -195,13 +196,14 @@ class VaultRepositoryImpl constructor(
emit( emit(
vaultSdkSource vaultSdkSource
.initializeCrypto( .initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
.fold( .fold(

View file

@ -5,7 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Folder import com.bitwarden.core.Folder
import com.bitwarden.core.FolderView import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Send import com.bitwarden.core.Send
import com.bitwarden.core.SendView import com.bitwarden.core.SendView
import com.bitwarden.sdk.BitwardenException import com.bitwarden.sdk.BitwardenException
@ -20,7 +20,6 @@ import io.mockk.mockk
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import kotlin.IllegalStateException
class VaultSdkSourceTest { class VaultSdkSourceTest {
private val clientVault = mockk<ClientVault>() private val clientVault = mockk<ClientVault>()
@ -33,9 +32,9 @@ class VaultSdkSourceTest {
@Test @Test
fun `initializeCrypto with sdk success should return InitializeCryptoResult Success`() = fun `initializeCrypto with sdk success should return InitializeCryptoResult Success`() =
runBlocking { runBlocking {
val mockInitCryptoRequest = mockk<InitCryptoRequest>() val mockInitCryptoRequest = mockk<InitUserCryptoRequest>()
coEvery { coEvery {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} returns Unit } returns Unit
@ -47,18 +46,18 @@ class VaultSdkSourceTest {
result, result,
) )
coVerify { coVerify {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} }
} }
@Test @Test
fun `initializeCrypto with sdk failure should return failure`() = runBlocking { fun `initializeUserCrypto with sdk failure should return failure`() = runBlocking {
val mockInitCryptoRequest = mockk<InitCryptoRequest>() val mockInitCryptoRequest = mockk<InitUserCryptoRequest>()
val expectedException = IllegalStateException("mock") val expectedException = IllegalStateException("mock")
coEvery { coEvery {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} throws expectedException } throws expectedException
@ -70,19 +69,19 @@ class VaultSdkSourceTest {
result, result,
) )
coVerify { coVerify {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} }
} }
@Test @Test
fun `initializeCrypto with BitwardenException failure should return AuthenticationError`() = fun `initializeUserCrypto with BitwardenException failure should return AuthenticationError`() =
runBlocking { runBlocking {
val mockInitCryptoRequest = mockk<InitCryptoRequest>() val mockInitCryptoRequest = mockk<InitUserCryptoRequest>()
val expectedException = BitwardenException.E(message = "") val expectedException = BitwardenException.E(message = "")
coEvery { coEvery {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} throws expectedException } throws expectedException
@ -94,7 +93,7 @@ class VaultSdkSourceTest {
result, result,
) )
coVerify { coVerify {
clientCrypto.initializeCrypto( clientCrypto.initializeUserCrypto(
req = mockInitCryptoRequest, req = mockInitCryptoRequest,
) )
} }
@ -208,7 +207,7 @@ class VaultSdkSourceTest {
send = mockSend, send = mockSend,
) )
assertEquals( assertEquals(
expectedResult.asSuccess(), result, expectedResult.asSuccess(), result,
) )
coVerify { coVerify {
clientVault.sends().decrypt( clientVault.sends().decrypt(

View file

@ -3,7 +3,8 @@ package com.x8bit.bitwarden.data.vault.repository
import app.cash.turbine.test import app.cash.turbine.test
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.FolderView import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Kdf import com.bitwarden.core.Kdf
import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson import com.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
@ -489,13 +490,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()), kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email", email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1", privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(), method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
), ),
) )
} returns Result.success(InitializeCryptoResult.Success) } returns Result.success(InitializeCryptoResult.Success)
@ -549,13 +551,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()), kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email", email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1", privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(), method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
), ),
) )
} just awaits } just awaits
@ -594,13 +597,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()), kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email", email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1", privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(), method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
), ),
) )
} just awaits } just awaits
@ -641,13 +645,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()), kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email", email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1", privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(), method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
), ),
) )
} returns Result.failure(IllegalStateException()) } returns Result.failure(IllegalStateException())
@ -696,13 +701,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()), kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email", email = "email",
password = "",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1", privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(), method = InitUserCryptoMethod.Password(
password = "",
userKey = "mockKey-1",
),
), ),
) )
} returns Result.success(InitializeCryptoResult.AuthenticationError) } returns Result.success(InitializeCryptoResult.AuthenticationError)
@ -828,13 +834,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>() val organizationalKeys = emptyMap<String, String>()
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} returns InitializeCryptoResult.Success.asSuccess() } returns InitializeCryptoResult.Success.asSuccess()
@ -864,13 +871,14 @@ class VaultRepositoryTest {
) )
coVerify(exactly = 1) { coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} }
@ -889,13 +897,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>() val organizationalKeys = emptyMap<String, String>()
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} returns InitializeCryptoResult.AuthenticationError.asSuccess() } returns InitializeCryptoResult.AuthenticationError.asSuccess()
@ -925,13 +934,14 @@ class VaultRepositoryTest {
) )
coVerify(exactly = 1) { coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} }
@ -948,13 +958,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>() val organizationalKeys = emptyMap<String, String>()
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} returns Throwable("Fail").asFailure() } returns Throwable("Fail").asFailure()
@ -984,13 +995,14 @@ class VaultRepositoryTest {
) )
coVerify(exactly = 1) { coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} }
@ -1007,13 +1019,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>() val organizationalKeys = emptyMap<String, String>()
coEvery { coEvery {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} just awaits } just awaits
@ -1037,13 +1050,14 @@ class VaultRepositoryTest {
coVerify(exactly = 0) { syncService.sync() } coVerify(exactly = 0) { syncService.sync() }
coVerify(exactly = 1) { coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto( vaultSdkSource.initializeCrypto(
request = InitCryptoRequest( request = InitUserCryptoRequest(
kdfParams = kdf, kdfParams = kdf,
email = email, email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey, privateKey = privateKey,
organizationKeys = organizationalKeys, method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
), ),
) )
} }

View file

@ -23,7 +23,7 @@ androidxRoom = "2.6.0"
androidxSplash = "1.1.0-alpha02" androidxSplash = "1.1.0-alpha02"
# Once the app and SDK reach a critical point of completeness we should begin fixing the version # Once the app and SDK reach a critical point of completeness we should begin fixing the version
# here (BIT-311). # here (BIT-311).
bitwardenSdk = "0.3.1-20231116.203646-8" bitwardenSdk = "0.3.1-20231127.121400-20"
detekt = "1.23.1" detekt = "1.23.1"
firebaseBom = "32.5.0" firebaseBom = "32.5.0"
glide = "4.15.1" glide = "4.15.1"