Improve detection of configure PIN code.
This commit is contained in:
parent
7cbecc6b94
commit
95c182595c
13 changed files with 77 additions and 44 deletions
|
|
@ -36,13 +36,13 @@ interface BiometricAuthenticator {
|
||||||
}
|
}
|
||||||
|
|
||||||
val isActive: Boolean
|
val isActive: Boolean
|
||||||
fun setup()
|
suspend fun setup()
|
||||||
suspend fun authenticate(): AuthenticationResult
|
suspend fun authenticate(): AuthenticationResult
|
||||||
}
|
}
|
||||||
|
|
||||||
class NoopBiometricAuthentication : BiometricAuthenticator {
|
class NoopBiometricAuthentication : BiometricAuthenticator {
|
||||||
override val isActive: Boolean = false
|
override val isActive: Boolean = false
|
||||||
override fun setup() = Unit
|
override suspend fun setup() = Unit
|
||||||
override suspend fun authenticate() = BiometricAuthenticator.AuthenticationResult.Failure()
|
override suspend fun authenticate() = BiometricAuthenticator.AuthenticationResult.Failure()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -58,7 +58,7 @@ class DefaultBiometricAuthentication(
|
||||||
|
|
||||||
private var cryptoObject: CryptoObject? = null
|
private var cryptoObject: CryptoObject? = null
|
||||||
|
|
||||||
override fun setup() {
|
override suspend fun setup() {
|
||||||
try {
|
try {
|
||||||
val secretKey = ensureKey()
|
val secretKey = ensureKey()
|
||||||
val cipher = encryptionDecryptionService.createEncryptionCipher(secretKey)
|
val cipher = encryptionDecryptionService.createEncryptionCipher(secretKey)
|
||||||
|
|
@ -86,7 +86,7 @@ class DefaultBiometricAuthentication(
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(KeyPermanentlyInvalidatedException::class)
|
@Throws(KeyPermanentlyInvalidatedException::class)
|
||||||
private fun ensureKey() = secretKeyRepository.getOrCreateKey(keyAlias, true).also {
|
private suspend fun ensureKey() = secretKeyRepository.getOrCreateKey(keyAlias, true).also {
|
||||||
encryptionDecryptionService.createEncryptionCipher(it)
|
encryptionDecryptionService.createEncryptionCipher(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import java.util.concurrent.CopyOnWriteArrayList
|
import java.util.concurrent.CopyOnWriteArrayList
|
||||||
|
|
||||||
private const val SECRET_KEY_ALIAS = "elementx.SECRET_KEY_ALIAS_PIN_CODE"
|
internal const val SECRET_KEY_ALIAS = "elementx.SECRET_KEY_ALIAS_PIN_CODE"
|
||||||
|
|
||||||
@ContributesBinding(AppScope::class)
|
@ContributesBinding(AppScope::class)
|
||||||
@SingleIn(AppScope::class)
|
@SingleIn(AppScope::class)
|
||||||
|
|
@ -38,7 +38,7 @@ class DefaultPinCodeManager(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasPinCode(): Flow<Boolean> {
|
override fun hasPinCode(): Flow<Boolean> {
|
||||||
return lockScreenStore.hasPinCode()
|
return secretKeyRepository.hasKey(SECRET_KEY_ALIAS)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun getPinCodeSize(): Int {
|
override suspend fun getPinCodeSize(): Int {
|
||||||
|
|
@ -79,6 +79,7 @@ class DefaultPinCodeManager(
|
||||||
override suspend fun deletePinCode() {
|
override suspend fun deletePinCode() {
|
||||||
lockScreenStore.deleteEncryptedPinCode()
|
lockScreenStore.deleteEncryptedPinCode()
|
||||||
lockScreenStore.resetCounter()
|
lockScreenStore.resetCounter()
|
||||||
|
secretKeyRepository.deleteKey(SECRET_KEY_ALIAS)
|
||||||
callbacks.forEach { it.onPinCodeRemoved() }
|
callbacks.forEach { it.onPinCodeRemoved() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,6 @@
|
||||||
|
|
||||||
package io.element.android.features.lockscreen.impl.storage
|
package io.element.android.features.lockscreen.impl.storage
|
||||||
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Should be implemented by any class that provides access to the encrypted PIN code.
|
* Should be implemented by any class that provides access to the encrypted PIN code.
|
||||||
* All methods are suspending in case there are async IO operations involved.
|
* All methods are suspending in case there are async IO operations involved.
|
||||||
|
|
@ -29,9 +27,4 @@ interface EncryptedPinCodeStorage {
|
||||||
* Deletes the PIN code from some persistable storage.
|
* Deletes the PIN code from some persistable storage.
|
||||||
*/
|
*/
|
||||||
suspend fun deleteEncryptedPinCode()
|
suspend fun deleteEncryptedPinCode()
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns whether the PIN code is stored or not.
|
|
||||||
*/
|
|
||||||
fun hasPinCode(): Flow<Boolean>
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -70,12 +70,6 @@ class PreferencesLockScreenStore(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasPinCode(): Flow<Boolean> {
|
|
||||||
return dataStore.data.map { preferences ->
|
|
||||||
preferences[pinCodeKey] != null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
||||||
return dataStore.data.map { preferences ->
|
return dataStore.data.map { preferences ->
|
||||||
preferences[biometricUnlockKey] ?: false
|
preferences[biometricUnlockKey] ?: false
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,12 @@ import io.element.android.features.lockscreen.impl.biometric.BiometricAuthentica
|
||||||
import io.element.android.features.lockscreen.impl.biometric.FakeBiometricAuthenticatorManager
|
import io.element.android.features.lockscreen.impl.biometric.FakeBiometricAuthenticatorManager
|
||||||
import io.element.android.features.lockscreen.impl.fixtures.aLockScreenConfig
|
import io.element.android.features.lockscreen.impl.fixtures.aLockScreenConfig
|
||||||
import io.element.android.features.lockscreen.impl.pin.PinCodeManager
|
import io.element.android.features.lockscreen.impl.pin.PinCodeManager
|
||||||
|
import io.element.android.features.lockscreen.impl.pin.SECRET_KEY_ALIAS
|
||||||
import io.element.android.features.lockscreen.impl.pin.createDefaultPinCodeManager
|
import io.element.android.features.lockscreen.impl.pin.createDefaultPinCodeManager
|
||||||
import io.element.android.features.lockscreen.impl.pin.storage.InMemoryLockScreenStore
|
import io.element.android.features.lockscreen.impl.pin.storage.InMemoryLockScreenStore
|
||||||
import io.element.android.features.lockscreen.impl.storage.LockScreenStore
|
import io.element.android.features.lockscreen.impl.storage.LockScreenStore
|
||||||
|
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
||||||
|
import io.element.android.libraries.cryptography.test.SimpleSecretKeyRepository
|
||||||
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
|
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
|
||||||
import io.element.android.libraries.sessionstorage.test.observer.FakeSessionObserver
|
import io.element.android.libraries.sessionstorage.test.observer.FakeSessionObserver
|
||||||
import io.element.android.services.appnavstate.api.AppForegroundStateService
|
import io.element.android.services.appnavstate.api.AppForegroundStateService
|
||||||
|
|
@ -38,18 +41,18 @@ class DefaultLockScreenServiceTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `when the pin is mandatory, isSetupRequired emits true`() = runTest {
|
fun `when the pin is mandatory, isSetupRequired emits true`() = runTest {
|
||||||
val lockScreenStore = InMemoryLockScreenStore()
|
val secretKeyRepository = SimpleSecretKeyRepository()
|
||||||
val sut = createDefaultLockScreenService(
|
val sut = createDefaultLockScreenService(
|
||||||
lockScreenConfig = aLockScreenConfig(isPinMandatory = true),
|
lockScreenConfig = aLockScreenConfig(isPinMandatory = true),
|
||||||
lockScreenStore = lockScreenStore,
|
secretKeyRepository = secretKeyRepository,
|
||||||
)
|
)
|
||||||
sut.isSetupRequired().test {
|
sut.isSetupRequired().test {
|
||||||
assertThat(awaitItem()).isTrue()
|
assertThat(awaitItem()).isTrue()
|
||||||
// When the user configures the pin code, the setup is not required anymore
|
// When the user configures the pin code, the setup is not required anymore
|
||||||
lockScreenStore.saveEncryptedPinCode("encryptedCode")
|
secretKeyRepository.getOrCreateKey(SECRET_KEY_ALIAS, true)
|
||||||
assertThat(awaitItem()).isFalse()
|
assertThat(awaitItem()).isFalse()
|
||||||
// Users deletes the pin code
|
// Users deletes the pin code
|
||||||
lockScreenStore.deleteEncryptedPinCode()
|
secretKeyRepository.deleteKey("elementx.SECRET_KEY_ALIAS_PIN_CODE")
|
||||||
assertThat(awaitItem()).isTrue()
|
assertThat(awaitItem()).isTrue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -57,16 +60,16 @@ class DefaultLockScreenServiceTest {
|
||||||
@Test
|
@Test
|
||||||
fun `when the last session is deleted, the pin code is removed`() = runTest {
|
fun `when the last session is deleted, the pin code is removed`() = runTest {
|
||||||
val sessionObserver = FakeSessionObserver()
|
val sessionObserver = FakeSessionObserver()
|
||||||
val lockScreenStore = InMemoryLockScreenStore()
|
val secretKeyRepository = SimpleSecretKeyRepository()
|
||||||
val sut = createDefaultLockScreenService(
|
val sut = createDefaultLockScreenService(
|
||||||
lockScreenConfig = aLockScreenConfig(isPinMandatory = true),
|
lockScreenConfig = aLockScreenConfig(isPinMandatory = true),
|
||||||
lockScreenStore = lockScreenStore,
|
secretKeyRepository = secretKeyRepository,
|
||||||
sessionObserver = sessionObserver,
|
sessionObserver = sessionObserver,
|
||||||
)
|
)
|
||||||
sut.isPinSetup().test {
|
sut.isPinSetup().test {
|
||||||
assertThat(awaitItem()).isFalse()
|
assertThat(awaitItem()).isFalse()
|
||||||
// When the user configure the pin code, the setup is not required anymore
|
// When the user configure the pin code, the setup is not required anymore
|
||||||
lockScreenStore.saveEncryptedPinCode("encryptedCode")
|
secretKeyRepository.getOrCreateKey(SECRET_KEY_ALIAS, true)
|
||||||
assertThat(awaitItem()).isTrue()
|
assertThat(awaitItem()).isTrue()
|
||||||
sessionObserver.onSessionDeleted("userId", wasLastSession = false)
|
sessionObserver.onSessionDeleted("userId", wasLastSession = false)
|
||||||
expectNoEvents()
|
expectNoEvents()
|
||||||
|
|
@ -79,8 +82,10 @@ class DefaultLockScreenServiceTest {
|
||||||
private fun TestScope.createDefaultLockScreenService(
|
private fun TestScope.createDefaultLockScreenService(
|
||||||
lockScreenConfig: LockScreenConfig = aLockScreenConfig(),
|
lockScreenConfig: LockScreenConfig = aLockScreenConfig(),
|
||||||
lockScreenStore: LockScreenStore = InMemoryLockScreenStore(),
|
lockScreenStore: LockScreenStore = InMemoryLockScreenStore(),
|
||||||
|
secretKeyRepository: SecretKeyRepository = SimpleSecretKeyRepository(),
|
||||||
pinCodeManager: PinCodeManager = createDefaultPinCodeManager(
|
pinCodeManager: PinCodeManager = createDefaultPinCodeManager(
|
||||||
lockScreenStore = lockScreenStore,
|
lockScreenStore = lockScreenStore,
|
||||||
|
secretKeyRepository = secretKeyRepository,
|
||||||
),
|
),
|
||||||
sessionObserver: SessionObserver = FakeSessionObserver(),
|
sessionObserver: SessionObserver = FakeSessionObserver(),
|
||||||
appForegroundStateService: AppForegroundStateService = FakeAppForegroundStateService(),
|
appForegroundStateService: AppForegroundStateService = FakeAppForegroundStateService(),
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,6 @@ class FakeBiometricAuthenticator(
|
||||||
override val isActive: Boolean = false,
|
override val isActive: Boolean = false,
|
||||||
private val authenticateLambda: suspend () -> BiometricAuthenticator.AuthenticationResult = { BiometricAuthenticator.AuthenticationResult.Success },
|
private val authenticateLambda: suspend () -> BiometricAuthenticator.AuthenticationResult = { BiometricAuthenticator.AuthenticationResult.Success },
|
||||||
) : BiometricAuthenticator {
|
) : BiometricAuthenticator {
|
||||||
override fun setup() = Unit
|
override suspend fun setup() = Unit
|
||||||
override suspend fun authenticate() = authenticateLambda()
|
override suspend fun authenticate() = authenticateLambda()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
private const val DEFAULT_REMAINING_ATTEMPTS = 3
|
private const val DEFAULT_REMAINING_ATTEMPTS = 3
|
||||||
|
|
||||||
class InMemoryLockScreenStore : LockScreenStore {
|
class InMemoryLockScreenStore : LockScreenStore {
|
||||||
private val hasPinCode = MutableStateFlow(false)
|
|
||||||
private var pinCode: String? = null
|
private var pinCode: String? = null
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
hasPinCode.value = value != null
|
|
||||||
}
|
|
||||||
private var remainingAttempts: Int = DEFAULT_REMAINING_ATTEMPTS
|
private var remainingAttempts: Int = DEFAULT_REMAINING_ATTEMPTS
|
||||||
private var isBiometricUnlockAllowed = MutableStateFlow(false)
|
private var isBiometricUnlockAllowed = MutableStateFlow(false)
|
||||||
|
|
||||||
|
|
@ -48,10 +43,6 @@ class InMemoryLockScreenStore : LockScreenStore {
|
||||||
pinCode = null
|
pinCode = null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hasPinCode(): Flow<Boolean> {
|
|
||||||
return hasPinCode
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
|
||||||
return isBiometricUnlockAllowed
|
return isBiometricUnlockAllowed
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,3 +13,7 @@ plugins {
|
||||||
android {
|
android {
|
||||||
namespace = "io.element.android.libraries.cryptography.api"
|
namespace = "io.element.android.libraries.cryptography.api"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(libs.coroutines.core)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
package io.element.android.libraries.cryptography.api
|
package io.element.android.libraries.cryptography.api
|
||||||
|
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
import javax.crypto.SecretKey
|
import javax.crypto.SecretKey
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -15,16 +16,18 @@ import javax.crypto.SecretKey
|
||||||
* Implementation should be able to store the generated key securely.
|
* Implementation should be able to store the generated key securely.
|
||||||
*/
|
*/
|
||||||
interface SecretKeyRepository {
|
interface SecretKeyRepository {
|
||||||
|
fun hasKey(alias: String): Flow<Boolean>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get or create a secret key for a given alias.
|
* Get or create a secret key for a given alias.
|
||||||
* @param alias the alias to use
|
* @param alias the alias to use
|
||||||
* @param requiresUserAuthentication true if the key should be protected by user authentication
|
* @param requiresUserAuthentication true if the key should be protected by user authentication
|
||||||
*/
|
*/
|
||||||
fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey
|
suspend fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete the secret key for a given alias.
|
* Delete the secret key for a given alias.
|
||||||
* @param alias the alias to use
|
* @param alias the alias to use
|
||||||
*/
|
*/
|
||||||
fun deleteKey(alias: String)
|
suspend fun deleteKey(alias: String)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ setupDependencyInjection()
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(projects.libraries.di)
|
implementation(projects.libraries.di)
|
||||||
|
implementation(libs.coroutines.core)
|
||||||
api(projects.libraries.cryptography.api)
|
api(projects.libraries.cryptography.api)
|
||||||
|
|
||||||
testCommonDependencies(libs)
|
testCommonDependencies(libs)
|
||||||
|
|
|
||||||
|
|
@ -13,11 +13,16 @@ import android.security.keystore.KeyGenParameterSpec
|
||||||
import android.security.keystore.KeyProperties
|
import android.security.keystore.KeyProperties
|
||||||
import dev.zacsweers.metro.AppScope
|
import dev.zacsweers.metro.AppScope
|
||||||
import dev.zacsweers.metro.ContributesBinding
|
import dev.zacsweers.metro.ContributesBinding
|
||||||
|
import dev.zacsweers.metro.SingleIn
|
||||||
import io.element.android.libraries.cryptography.api.AESEncryptionSpecs
|
import io.element.android.libraries.cryptography.api.AESEncryptionSpecs
|
||||||
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
import java.security.KeyStore
|
import java.security.KeyStore
|
||||||
import java.security.KeyStoreException
|
import java.security.KeyStoreException
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.crypto.KeyGenerator
|
import javax.crypto.KeyGenerator
|
||||||
import javax.crypto.SecretKey
|
import javax.crypto.SecretKey
|
||||||
|
|
||||||
|
|
@ -25,13 +30,22 @@ import javax.crypto.SecretKey
|
||||||
* Default implementation of [SecretKeyRepository] that uses the Android Keystore to store the keys.
|
* Default implementation of [SecretKeyRepository] that uses the Android Keystore to store the keys.
|
||||||
* The generated key uses AES algorithm, with a key size of 128 bits, and the GCM block mode.
|
* The generated key uses AES algorithm, with a key size of 128 bits, and the GCM block mode.
|
||||||
*/
|
*/
|
||||||
|
@SingleIn(AppScope::class)
|
||||||
@ContributesBinding(AppScope::class)
|
@ContributesBinding(AppScope::class)
|
||||||
class KeyStoreSecretKeyRepository(
|
class KeyStoreSecretKeyRepository(
|
||||||
private val keyStore: KeyStore,
|
private val keyStore: KeyStore,
|
||||||
) : SecretKeyRepository {
|
) : SecretKeyRepository {
|
||||||
|
private val hasKeyMap = ConcurrentHashMap<String, MutableStateFlow<Boolean>>()
|
||||||
|
|
||||||
|
override fun hasKey(alias: String): Flow<Boolean> {
|
||||||
|
return hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(runCatching { keyStore.containsAlias(alias) }.getOrDefault(false))
|
||||||
|
}.asStateFlow()
|
||||||
|
}
|
||||||
|
|
||||||
// False positive lint issue
|
// False positive lint issue
|
||||||
@SuppressLint("WrongConstant")
|
@SuppressLint("WrongConstant")
|
||||||
override fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey {
|
override suspend fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey {
|
||||||
val secretKeyEntry = (keyStore.getEntry(alias, null) as? KeyStore.SecretKeyEntry)
|
val secretKeyEntry = (keyStore.getEntry(alias, null) as? KeyStore.SecretKeyEntry)
|
||||||
?.secretKey
|
?.secretKey
|
||||||
return if (secretKeyEntry == null) {
|
return if (secretKeyEntry == null) {
|
||||||
|
|
@ -46,15 +60,22 @@ class KeyStoreSecretKeyRepository(
|
||||||
.setUserAuthenticationRequired(requiresUserAuthentication)
|
.setUserAuthenticationRequired(requiresUserAuthentication)
|
||||||
.build()
|
.build()
|
||||||
generator.init(keyGenSpec)
|
generator.init(keyGenSpec)
|
||||||
generator.generateKey()
|
generator.generateKey().also {
|
||||||
|
hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(true)
|
||||||
|
}.emit(true)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
secretKeyEntry
|
secretKeyEntry
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deleteKey(alias: String) {
|
override suspend fun deleteKey(alias: String) {
|
||||||
try {
|
try {
|
||||||
keyStore.deleteEntry(alias)
|
keyStore.deleteEntry(alias)
|
||||||
|
hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(false)
|
||||||
|
}.emit(false)
|
||||||
} catch (e: KeyStoreException) {
|
} catch (e: KeyStoreException) {
|
||||||
Timber.e(e)
|
Timber.e(e)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,4 +16,5 @@ android {
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
api(projects.libraries.cryptography.api)
|
api(projects.libraries.cryptography.api)
|
||||||
|
implementation(libs.coroutines.core)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,20 +10,39 @@ package io.element.android.libraries.cryptography.test
|
||||||
|
|
||||||
import io.element.android.libraries.cryptography.api.AESEncryptionSpecs
|
import io.element.android.libraries.cryptography.api.AESEncryptionSpecs
|
||||||
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
import io.element.android.libraries.cryptography.api.SecretKeyRepository
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import javax.crypto.KeyGenerator
|
import javax.crypto.KeyGenerator
|
||||||
import javax.crypto.SecretKey
|
import javax.crypto.SecretKey
|
||||||
|
|
||||||
class SimpleSecretKeyRepository : SecretKeyRepository {
|
class SimpleSecretKeyRepository : SecretKeyRepository {
|
||||||
private var secretKeyForAlias = HashMap<String, SecretKey>()
|
private var secretKeyForAlias = HashMap<String, SecretKey>()
|
||||||
|
|
||||||
override fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey {
|
private val hasKeyMap = ConcurrentHashMap<String, MutableStateFlow<Boolean>>()
|
||||||
|
|
||||||
|
override fun hasKey(alias: String): Flow<Boolean> {
|
||||||
|
return hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(false)
|
||||||
|
}.asStateFlow()
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getOrCreateKey(alias: String, requiresUserAuthentication: Boolean): SecretKey {
|
||||||
return secretKeyForAlias.getOrPut(alias) {
|
return secretKeyForAlias.getOrPut(alias) {
|
||||||
generateKey()
|
generateKey().also {
|
||||||
|
hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(true)
|
||||||
|
}.emit(true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun deleteKey(alias: String) {
|
override suspend fun deleteKey(alias: String) {
|
||||||
secretKeyForAlias.remove(alias)
|
secretKeyForAlias.remove(alias)
|
||||||
|
hasKeyMap.getOrPut(alias) {
|
||||||
|
MutableStateFlow(false)
|
||||||
|
}.emit(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun generateKey(): SecretKey {
|
private fun generateKey(): SecretKey {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue