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.Folder
import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Send
import com.bitwarden.core.SendView
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
* 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].

View file

@ -5,7 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView
import com.bitwarden.core.Folder
import com.bitwarden.core.FolderView
import com.bitwarden.core.InitCryptoRequest
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Send
import com.bitwarden.core.SendView
import com.bitwarden.sdk.BitwardenException
@ -22,11 +22,11 @@ class VaultSdkSourceImpl(
private val clientCrypto: ClientCrypto,
) : VaultSdkSource {
override suspend fun initializeCrypto(
request: InitCryptoRequest,
request: InitUserCryptoRequest,
): Result<InitializeCryptoResult> =
runCatching {
try {
clientCrypto.initializeCrypto(req = request)
clientCrypto.initializeUserCrypto(req = request)
InitializeCryptoResult.Success
} catch (exception: BitwardenException) {
// 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.FolderView
import com.bitwarden.core.InitCryptoRequest
import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.Kdf
import com.x8bit.bitwarden.data.auth.datasource.disk.AuthDiskSource
import com.x8bit.bitwarden.data.auth.repository.util.toSdkParams
@ -195,13 +196,14 @@ class VaultRepositoryImpl constructor(
emit(
vaultSdkSource
.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
.fold(

View file

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

View file

@ -3,7 +3,8 @@ package com.x8bit.bitwarden.data.vault.repository
import app.cash.turbine.test
import com.bitwarden.core.CipherView
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.x8bit.bitwarden.data.auth.datasource.disk.model.AccountJson
import com.x8bit.bitwarden.data.auth.datasource.disk.model.UserStateJson
@ -489,13 +490,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(),
method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
),
)
} returns Result.success(InitializeCryptoResult.Success)
@ -549,13 +551,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(),
method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
),
)
} just awaits
@ -594,13 +597,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(),
method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
),
)
} just awaits
@ -641,13 +645,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email",
password = "mockPassword-1",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(),
method = InitUserCryptoMethod.Password(
password = "mockPassword-1",
userKey = "mockKey-1",
),
),
)
} returns Result.failure(IllegalStateException())
@ -696,13 +701,14 @@ class VaultRepositoryTest {
fakeAuthDiskSource.userState = MOCK_USER_STATE
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = Kdf.Pbkdf2(iterations = DEFAULT_PBKDF2_ITERATIONS.toUInt()),
email = "email",
password = "",
userKey = "mockKey-1",
privateKey = "mockPrivateKey-1",
organizationKeys = mapOf(),
method = InitUserCryptoMethod.Password(
password = "",
userKey = "mockKey-1",
),
),
)
} returns Result.success(InitializeCryptoResult.AuthenticationError)
@ -828,13 +834,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>()
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
} returns InitializeCryptoResult.Success.asSuccess()
@ -864,13 +871,14 @@ class VaultRepositoryTest {
)
coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
}
@ -889,13 +897,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>()
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
} returns InitializeCryptoResult.AuthenticationError.asSuccess()
@ -925,13 +934,14 @@ class VaultRepositoryTest {
)
coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
}
@ -948,13 +958,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>()
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
} returns Throwable("Fail").asFailure()
@ -984,13 +995,14 @@ class VaultRepositoryTest {
)
coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
}
@ -1007,13 +1019,14 @@ class VaultRepositoryTest {
val organizationalKeys = emptyMap<String, String>()
coEvery {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
privateKey = privateKey,
organizationKeys = organizationalKeys,
method = InitUserCryptoMethod.Password(
password = masterPassword,
userKey = userKey,
),
),
)
} just awaits
@ -1037,13 +1050,14 @@ class VaultRepositoryTest {
coVerify(exactly = 0) { syncService.sync() }
coVerify(exactly = 1) {
vaultSdkSource.initializeCrypto(
request = InitCryptoRequest(
request = InitUserCryptoRequest(
kdfParams = kdf,
email = email,
password = masterPassword,
userKey = userKey,
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"
# Once the app and SDK reach a critical point of completeness we should begin fixing the version
# here (BIT-311).
bitwardenSdk = "0.3.1-20231116.203646-8"
bitwardenSdk = "0.3.1-20231127.121400-20"
detekt = "1.23.1"
firebaseBom = "32.5.0"
glide = "4.15.1"