Add VaultSdkSource.getUserEncryptionKey (#545)

This commit is contained in:
Brian Yencho 2024-01-08 22:07:58 -06:00 committed by Álison Fernandes
parent 2d4427a7cf
commit 6686d98e34
3 changed files with 38 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import com.bitwarden.core.CollectionView
import com.bitwarden.core.Folder
import com.bitwarden.core.FolderView
import com.bitwarden.core.InitOrgCryptoRequest
import com.bitwarden.core.InitUserCryptoMethod
import com.bitwarden.core.InitUserCryptoRequest
import com.bitwarden.core.PasswordHistory
import com.bitwarden.core.PasswordHistoryView
@ -27,6 +28,15 @@ interface VaultSdkSource {
*/
fun clearCrypto(userId: String)
/**
* Gets the user's encryption key, which can be used to later unlock their vault via a call to
* [initializeCrypto] with [InitUserCryptoMethod.DecryptedKey].
*
* This should only be called after a successful call to [initializeCrypto] for the associated
* user.
*/
suspend fun getUserEncryptionKey(userId: String): Result<String>
/**
* Attempts to initialize cryptography functionality for an individual user with the given
* [userId] for the Bitwarden SDK with a given [InitUserCryptoRequest].

View file

@ -31,6 +31,15 @@ class VaultSdkSourceImpl(
sdkClientManager.destroyClient(userId = userId)
}
override suspend fun getUserEncryptionKey(
userId: String,
): Result<String> =
runCatching {
getClient(userId = userId)
.crypto()
.getUserEncryptionKey()
}
override suspend fun initializeCrypto(
userId: String,
request: InitUserCryptoRequest,

View file

@ -60,6 +60,25 @@ class VaultSdkSourceTest {
verify { sdkClientManager.destroyClient(userId = userId) }
}
@Test
fun `getUserEncryptionKey should call SDK and return a Result with correct data`() =
runBlocking {
val userId = "userId"
val expectedResult = "userEncryptionKey"
coEvery {
clientCrypto.getUserEncryptionKey()
} returns expectedResult
val result = vaultSdkSource.getUserEncryptionKey(userId = userId)
assertEquals(
expectedResult.asSuccess(),
result,
)
coVerify {
clientCrypto.getUserEncryptionKey()
}
verify { sdkClientManager.getOrCreateClient(userId = userId) }
}
@Test
fun `initializeUserCrypto with sdk success should return InitializeCryptoResult Success`() =
runBlocking {