[PM-23608] Add SDK method for generating TOTP for CipherListView (#5519)

This commit is contained in:
Patrick Honkonen 2025-07-14 16:02:20 -04:00 committed by GitHub
parent 8f783a43e4
commit 811f0f2757
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 0 deletions

View File

@ -35,6 +35,7 @@ import com.x8bit.bitwarden.data.vault.datasource.sdk.model.DeriveKeyConnectorRes
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.InitializeCryptoResult
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.RegisterFido2CredentialRequest
import java.io.File
import java.time.Instant
/**
* Source of vault information and functionality from the Bitwarden SDK.
@ -405,6 +406,15 @@ interface VaultSdkSource {
time: DateTime,
): Result<TotpResponse>
/**
* Generate a verification code for the given [cipherListView] and [time].
*/
suspend fun generateTotpForCipherListView(
userId: String,
cipherListView: CipherListView,
time: Instant?,
): Result<TotpResponse>
/**
* Re-encrypts the [cipherView] with the organizations encryption key.
*/

View File

@ -48,6 +48,7 @@ import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import java.io.File
import java.time.Instant
/**
* Primary implementation of [VaultSdkSource] that serves as a convenience wrapper around a
@ -451,6 +452,19 @@ class VaultSdkSourceImpl(
)
}
override suspend fun generateTotpForCipherListView(
userId: String,
cipherListView: CipherListView,
time: Instant?,
): Result<TotpResponse> = runCatchingWithLogs {
getClient(userId = userId)
.vault()
.generateTotpCipherView(
view = cipherListView,
time = time,
)
}
override suspend fun moveToOrganization(
userId: String,
organizationId: String,

View File

@ -1048,6 +1048,36 @@ class VaultSdkSourceTest {
coVerify { sdkClientManager.getOrCreateClient(userId = userId) }
}
@Test
fun `generateTotpForCipherListView should call SDK and return a Result with correct data`() =
runTest {
val userId = "userId"
val totpResponse = TotpResponse("TestCode", 30u)
coEvery {
clientVault.generateTotpCipherView(
view = any(),
time = any(),
)
} returns totpResponse
val result = vaultSdkSource.generateTotpForCipherListView(
userId = userId,
cipherListView = mockk(),
time = mockk(),
)
assertEquals(totpResponse.asSuccess(), result)
coVerify {
clientVault.generateTotpCipherView(
view = any(),
time = any(),
)
}
coVerify { sdkClientManager.getOrCreateClient(userId = userId) }
}
@Test
fun `moveToOrganization should call SDK and a Result with correct data`() = runTest {
val userId = "userId"