From 31d06391ed89e415dabc30c53f015a36114771d6 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 13 May 2026 11:39:46 +0200 Subject: [PATCH] Pin code: remove the key if there is no pin code --- .../lockscreen/impl/pin/DefaultPinCodeManager.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/pin/DefaultPinCodeManager.kt b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/pin/DefaultPinCodeManager.kt index 2bda5759a8..d699357933 100644 --- a/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/pin/DefaultPinCodeManager.kt +++ b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/pin/DefaultPinCodeManager.kt @@ -16,6 +16,10 @@ import io.element.android.libraries.cryptography.api.EncryptionDecryptionService import io.element.android.libraries.cryptography.api.EncryptionResult import io.element.android.libraries.cryptography.api.SecretKeyRepository import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import java.util.concurrent.CopyOnWriteArrayList internal const val SECRET_KEY_ALIAS = "elementx.SECRET_KEY_ALIAS_PIN_CODE" @@ -29,6 +33,8 @@ class DefaultPinCodeManager( ) : PinCodeManager { private val callbacks = CopyOnWriteArrayList() + private val migrationMutex = Mutex() + override fun addCallback(callback: PinCodeManager.Callback) { callbacks.add(callback) } @@ -39,6 +45,15 @@ class DefaultPinCodeManager( override fun hasPinCode(): Flow { return secretKeyRepository.hasKey(SECRET_KEY_ALIAS) + .onStart { + migrationMutex.withLock { + val hasKey = secretKeyRepository.hasKey(SECRET_KEY_ALIAS).first() + if (hasKey && lockScreenStore.getEncryptedCode() == null) { + // Remove the key if there is no pin code + secretKeyRepository.deleteKey(SECRET_KEY_ALIAS) + } + } + } } override suspend fun getPinCodeSize(): Int? {