mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 15:15:34 +03:00
Add VaultSdkSource.getUserEncryptionKey (#545)
This commit is contained in:
parent
2d4427a7cf
commit
6686d98e34
3 changed files with 38 additions and 0 deletions
|
@ -8,6 +8,7 @@ import com.bitwarden.core.CollectionView
|
||||||
import com.bitwarden.core.Folder
|
import com.bitwarden.core.Folder
|
||||||
import com.bitwarden.core.FolderView
|
import com.bitwarden.core.FolderView
|
||||||
import com.bitwarden.core.InitOrgCryptoRequest
|
import com.bitwarden.core.InitOrgCryptoRequest
|
||||||
|
import com.bitwarden.core.InitUserCryptoMethod
|
||||||
import com.bitwarden.core.InitUserCryptoRequest
|
import com.bitwarden.core.InitUserCryptoRequest
|
||||||
import com.bitwarden.core.PasswordHistory
|
import com.bitwarden.core.PasswordHistory
|
||||||
import com.bitwarden.core.PasswordHistoryView
|
import com.bitwarden.core.PasswordHistoryView
|
||||||
|
@ -27,6 +28,15 @@ interface VaultSdkSource {
|
||||||
*/
|
*/
|
||||||
fun clearCrypto(userId: String)
|
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
|
* Attempts to initialize cryptography functionality for an individual user with the given
|
||||||
* [userId] for the Bitwarden SDK with a given [InitUserCryptoRequest].
|
* [userId] for the Bitwarden SDK with a given [InitUserCryptoRequest].
|
||||||
|
|
|
@ -31,6 +31,15 @@ class VaultSdkSourceImpl(
|
||||||
sdkClientManager.destroyClient(userId = userId)
|
sdkClientManager.destroyClient(userId = userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override suspend fun getUserEncryptionKey(
|
||||||
|
userId: String,
|
||||||
|
): Result<String> =
|
||||||
|
runCatching {
|
||||||
|
getClient(userId = userId)
|
||||||
|
.crypto()
|
||||||
|
.getUserEncryptionKey()
|
||||||
|
}
|
||||||
|
|
||||||
override suspend fun initializeCrypto(
|
override suspend fun initializeCrypto(
|
||||||
userId: String,
|
userId: String,
|
||||||
request: InitUserCryptoRequest,
|
request: InitUserCryptoRequest,
|
||||||
|
|
|
@ -60,6 +60,25 @@ class VaultSdkSourceTest {
|
||||||
verify { sdkClientManager.destroyClient(userId = userId) }
|
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
|
@Test
|
||||||
fun `initializeUserCrypto with sdk success should return InitializeCryptoResult Success`() =
|
fun `initializeUserCrypto with sdk success should return InitializeCryptoResult Success`() =
|
||||||
runBlocking {
|
runBlocking {
|
||||||
|
|
Loading…
Reference in a new issue