Improve detection of configure PIN code.

This commit is contained in:
Benoit Marty 2026-05-05 18:24:25 +02:00 committed by Benoit Marty
parent 2f45ca8835
commit 61374bca4e
13 changed files with 77 additions and 44 deletions

View file

@ -36,13 +36,13 @@ interface BiometricAuthenticator {
}
val isActive: Boolean
fun setup()
suspend fun setup()
suspend fun authenticate(): AuthenticationResult
}
class NoopBiometricAuthentication : BiometricAuthenticator {
override val isActive: Boolean = false
override fun setup() = Unit
override suspend fun setup() = Unit
override suspend fun authenticate() = BiometricAuthenticator.AuthenticationResult.Failure()
}
@ -58,7 +58,7 @@ class DefaultBiometricAuthentication(
private var cryptoObject: CryptoObject? = null
override fun setup() {
override suspend fun setup() {
try {
val secretKey = ensureKey()
val cipher = encryptionDecryptionService.createEncryptionCipher(secretKey)
@ -86,7 +86,7 @@ class DefaultBiometricAuthentication(
}
@Throws(KeyPermanentlyInvalidatedException::class)
private fun ensureKey() = secretKeyRepository.getOrCreateKey(keyAlias, true).also {
private suspend fun ensureKey() = secretKeyRepository.getOrCreateKey(keyAlias, true).also {
encryptionDecryptionService.createEncryptionCipher(it)
}
}

View file

@ -18,7 +18,7 @@ import io.element.android.libraries.cryptography.api.SecretKeyRepository
import kotlinx.coroutines.flow.Flow
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)
@SingleIn(AppScope::class)
@ -38,7 +38,7 @@ class DefaultPinCodeManager(
}
override fun hasPinCode(): Flow<Boolean> {
return lockScreenStore.hasPinCode()
return secretKeyRepository.hasKey(SECRET_KEY_ALIAS)
}
override suspend fun getPinCodeSize(): Int {
@ -79,6 +79,7 @@ class DefaultPinCodeManager(
override suspend fun deletePinCode() {
lockScreenStore.deleteEncryptedPinCode()
lockScreenStore.resetCounter()
secretKeyRepository.deleteKey(SECRET_KEY_ALIAS)
callbacks.forEach { it.onPinCodeRemoved() }
}

View file

@ -8,8 +8,6 @@
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.
* 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.
*/
suspend fun deleteEncryptedPinCode()
/**
* Returns whether the PIN code is stored or not.
*/
fun hasPinCode(): Flow<Boolean>
}

View file

@ -70,12 +70,6 @@ class PreferencesLockScreenStore(
}
}
override fun hasPinCode(): Flow<Boolean> {
return dataStore.data.map { preferences ->
preferences[pinCodeKey] != null
}
}
override fun isBiometricUnlockAllowed(): Flow<Boolean> {
return dataStore.data.map { preferences ->
preferences[biometricUnlockKey] ?: false