diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt index afbd42923..d551d1d11 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSource.kt @@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView import com.bitwarden.core.CipherView import com.bitwarden.core.Collection import com.bitwarden.core.CollectionView +import com.bitwarden.core.DerivePinKeyResponse import com.bitwarden.core.Folder import com.bitwarden.core.FolderView import com.bitwarden.core.InitOrgCryptoRequest @@ -28,6 +29,18 @@ interface VaultSdkSource { */ 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 + /** * Gets the user's encryption key, which can be used to later unlock their vault via a call to * [initializeCrypto] with [InitUserCryptoMethod.DecryptedKey]. diff --git a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt index 2afefd95e..220349752 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceImpl.kt @@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView import com.bitwarden.core.CipherView import com.bitwarden.core.Collection import com.bitwarden.core.CollectionView +import com.bitwarden.core.DerivePinKeyResponse import com.bitwarden.core.Folder import com.bitwarden.core.FolderView import com.bitwarden.core.InitOrgCryptoRequest @@ -31,6 +32,16 @@ class VaultSdkSourceImpl( sdkClientManager.destroyClient(userId = userId) } + override suspend fun derivePinKey( + userId: String, + pin: String, + ): Result = + runCatching { + getClient(userId = userId) + .crypto() + .derivePinKey(pin = pin) + } + override suspend fun getUserEncryptionKey( userId: String, ): Result = diff --git a/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt index c9fd0896d..044b5f157 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/vault/datasource/sdk/VaultSdkSourceTest.kt @@ -5,6 +5,7 @@ import com.bitwarden.core.CipherListView import com.bitwarden.core.CipherView import com.bitwarden.core.Collection import com.bitwarden.core.CollectionView +import com.bitwarden.core.DerivePinKeyResponse import com.bitwarden.core.Folder import com.bitwarden.core.FolderView import com.bitwarden.core.InitOrgCryptoRequest @@ -60,6 +61,28 @@ class VaultSdkSourceTest { 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() + 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 fun `getUserEncryptionKey should call SDK and return a Result with correct data`() = runBlocking {