mirror of
https://github.com/bitwarden/android.git
synced 2025-12-10 00:06:22 -06:00
Implement SDK Repository example
This commit is contained in:
parent
4f5c28e248
commit
3c0ba2a268
@ -2,7 +2,13 @@ package com.x8bit.bitwarden.data.platform.manager
|
||||
|
||||
import android.os.Build
|
||||
import com.bitwarden.core.util.isBuildVersionAtLeast
|
||||
import com.bitwarden.sdk.CipherRepository
|
||||
import com.bitwarden.sdk.Client
|
||||
import com.bitwarden.vault.Cipher
|
||||
import com.x8bit.bitwarden.data.vault.datasource.disk.VaultDiskSource
|
||||
import com.x8bit.bitwarden.data.vault.repository.util.toEncryptedNetworkCipherResponse
|
||||
import com.x8bit.bitwarden.data.vault.repository.util.toEncryptedSdkCipher
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
|
||||
/**
|
||||
* Primary implementation of [SdkClientManager].
|
||||
@ -10,9 +16,14 @@ import com.bitwarden.sdk.Client
|
||||
class SdkClientManagerImpl(
|
||||
private val featureFlagManager: FeatureFlagManager,
|
||||
nativeLibraryManager: NativeLibraryManager,
|
||||
private val clientProvider: suspend () -> Client = {
|
||||
private val vaultDiskSource: VaultDiskSource,
|
||||
private val clientProvider: suspend (String?) -> Client = { userId ->
|
||||
Client(settings = null).apply {
|
||||
platform().loadFlags(featureFlagManager.sdkFeatureFlags)
|
||||
if (userId != null) {
|
||||
platform().state()
|
||||
.registerCipherRepository(CipherRepositoryImpl(userId, vaultDiskSource));
|
||||
}
|
||||
}
|
||||
},
|
||||
) : SdkClientManager {
|
||||
@ -29,7 +40,7 @@ class SdkClientManagerImpl(
|
||||
|
||||
override suspend fun getOrCreateClient(
|
||||
userId: String?,
|
||||
): Client = userIdToClientMap.getOrPut(key = userId) { clientProvider() }
|
||||
): Client = userIdToClientMap.getOrPut(key = userId) { clientProvider(userId) }
|
||||
|
||||
override fun destroyClient(
|
||||
userId: String?,
|
||||
@ -39,3 +50,29 @@ class SdkClientManagerImpl(
|
||||
?.close()
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This should probably be moved somewhere else?
|
||||
class CipherRepositoryImpl(private val userId: String, private val vaultDiskSource: VaultDiskSource): CipherRepository {
|
||||
override suspend fun get(id: String): Cipher? {
|
||||
return vaultDiskSource.getCiphers(userId).firstOrNull()
|
||||
.orEmpty().firstOrNull { it.id == id }?.toEncryptedSdkCipher()
|
||||
}
|
||||
|
||||
override suspend fun has(id: String): Boolean {
|
||||
return this.get(id) != null
|
||||
}
|
||||
|
||||
override suspend fun list(): List<Cipher> {
|
||||
return vaultDiskSource.getCiphers(userId).firstOrNull()
|
||||
.orEmpty().map { it.toEncryptedSdkCipher() }
|
||||
}
|
||||
|
||||
override suspend fun set(id: String, value: Cipher) {
|
||||
assert(value.id == id)
|
||||
vaultDiskSource.saveCipher(userId, value.toEncryptedNetworkCipherResponse(userId))
|
||||
}
|
||||
|
||||
override suspend fun remove(id: String) {
|
||||
vaultDiskSource.deleteCipher(userId, id)
|
||||
}
|
||||
}
|
||||
|
||||
@ -233,9 +233,11 @@ object PlatformManagerModule {
|
||||
fun provideSdkClientManager(
|
||||
featureFlagManager: FeatureFlagManager,
|
||||
nativeLibraryManager: NativeLibraryManager,
|
||||
vaultDiskSource: VaultDiskSource,
|
||||
): SdkClientManager = SdkClientManagerImpl(
|
||||
featureFlagManager = featureFlagManager,
|
||||
nativeLibraryManager = nativeLibraryManager,
|
||||
vaultDiskSource = vaultDiskSource,
|
||||
)
|
||||
|
||||
@Provides
|
||||
|
||||
@ -82,5 +82,6 @@ class SdkClientManagerTest {
|
||||
clientProvider = { mockk(relaxed = true) },
|
||||
nativeLibraryManager = mockNativeLibraryManager,
|
||||
featureFlagManager = mockk(),
|
||||
vaultDiskSource = mockk(),
|
||||
)
|
||||
}
|
||||
|
||||
@ -25,7 +25,7 @@ androidxRoom = "2.7.2"
|
||||
androidxSecurityCrypto = "1.1.0-alpha06"
|
||||
androidxSplash = "1.1.0-rc01"
|
||||
androidxWork = "2.10.2"
|
||||
bitwardenSdk = "1.0.0-20250623.141835-223"
|
||||
bitwardenSdk = "1.0.0-20250701.082214-229"
|
||||
crashlytics = "3.0.4"
|
||||
detekt = "1.23.8"
|
||||
firebaseBom = "33.15.0"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user