Add VaultSdkSource.derivePinKey (#609)

This commit is contained in:
Brian Yencho 2024-01-14 14:32:43 -06:00 committed by Álison Fernandes
parent 32c1c2155e
commit c59ae03f97
3 changed files with 47 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Collection import com.bitwarden.core.Collection
import com.bitwarden.core.CollectionView import com.bitwarden.core.CollectionView
import com.bitwarden.core.DerivePinKeyResponse
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
@ -28,6 +29,18 @@ interface VaultSdkSource {
*/ */
fun clearCrypto(userId: String) fun clearCrypto(userId: String)
/**
* Derives a "pin key" from the given [pin] for the given [userId]. This can be used to later
* unlock their vault via a call to [initializeCrypto] with [InitUserCryptoMethod.Pin].
*
* This should only be called after a successful call to [initializeCrypto] for the associated
* user.
*/
suspend fun derivePinKey(
userId: String,
pin: String,
): Result<DerivePinKeyResponse>
/** /**
* Gets the user's encryption key, which can be used to later unlock their vault via a call to * Gets the user's encryption key, which can be used to later unlock their vault via a call to
* [initializeCrypto] with [InitUserCryptoMethod.DecryptedKey]. * [initializeCrypto] with [InitUserCryptoMethod.DecryptedKey].

View file

@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Collection import com.bitwarden.core.Collection
import com.bitwarden.core.CollectionView import com.bitwarden.core.CollectionView
import com.bitwarden.core.DerivePinKeyResponse
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
@ -31,6 +32,16 @@ class VaultSdkSourceImpl(
sdkClientManager.destroyClient(userId = userId) sdkClientManager.destroyClient(userId = userId)
} }
override suspend fun derivePinKey(
userId: String,
pin: String,
): Result<DerivePinKeyResponse> =
runCatching {
getClient(userId = userId)
.crypto()
.derivePinKey(pin = pin)
}
override suspend fun getUserEncryptionKey( override suspend fun getUserEncryptionKey(
userId: String, userId: String,
): Result<String> = ): Result<String> =

View file

@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView
import com.bitwarden.core.CipherView import com.bitwarden.core.CipherView
import com.bitwarden.core.Collection import com.bitwarden.core.Collection
import com.bitwarden.core.CollectionView import com.bitwarden.core.CollectionView
import com.bitwarden.core.DerivePinKeyResponse
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
@ -60,6 +61,28 @@ class VaultSdkSourceTest {
verify { sdkClientManager.destroyClient(userId = userId) } verify { sdkClientManager.destroyClient(userId = userId) }
} }
@Test
fun `derivePinKey should call SDK and return a Result with the correct data`() = runBlocking {
val userId = "userId"
val pin = "pin"
val expectedResult = mockk<DerivePinKeyResponse>()
coEvery {
clientCrypto.derivePinKey(pin = pin)
} returns expectedResult
val result = vaultSdkSource.derivePinKey(
userId = userId,
pin = pin,
)
assertEquals(
expectedResult.asSuccess(),
result,
)
coVerify {
clientCrypto.derivePinKey(pin)
}
verify { sdkClientManager.getOrCreateClient(userId = userId) }
}
@Test @Test
fun `getUserEncryptionKey should call SDK and return a Result with correct data`() = fun `getUserEncryptionKey should call SDK and return a Result with correct data`() =
runBlocking { runBlocking {