From 3216713c45300b87a89c86f71275e38282e83ad1 Mon Sep 17 00:00:00 2001 From: Mauro <34335419+Velin92@users.noreply.github.com> Date: Mon, 19 Oct 2026 12:53:50 +0200 Subject: [PATCH] Replace the deprecated biometric domain state in AppLockService (#6243) --- .../Sources/Services/AppLock/AppLockService.swift | 15 ++++++++++++--- .../Sources/Services/AppLock/LAContextMock.swift | 12 ++++++------ .../Sources/UITests/UITestsAppCoordinator.swift | 4 ++-- .../Sources/AppLock/AppLockServiceTests.swift | 10 +++++----- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/ElementX/Sources/Services/AppLock/AppLockService.swift b/ElementX/Sources/Services/AppLock/AppLockService.swift index 8b5c91b61..cce2d022e 100644 --- a/ElementX/Sources/Services/AppLock/AppLockService.swift +++ b/ElementX/Sources/Services/AppLock/AppLockService.swift @@ -45,7 +45,7 @@ class AppLockService: AppLockServiceProtocol { var biometryType: LABiometryType { updateBiometrics() - guard context.evaluatedPolicyDomainState != nil else { return .none } + guard context.biometricStateHash != nil else { return .none } return context.biometryType } @@ -56,7 +56,7 @@ class AppLockService: AppLockServiceProtocol { var biometricUnlockTrusted: Bool { guard let state = keychainController.pinCodeBiometricState() else { return false } updateBiometrics() - return state == context.evaluatedPolicyDomainState + return state == context.biometricStateHash } var numberOfPINAttempts: AnyPublisher { @@ -94,7 +94,7 @@ class AppLockService: AppLockServiceProtocol { func enableBiometricUnlock() -> Result { guard isEnabled else { return .failure(.pinNotSet) } - guard let state = context.evaluatedPolicyDomainState else { return .failure(.biometricUnlockNotSupported) } + guard let state = context.biometricStateHash else { return .failure(.biometricUnlockNotSupported) } do { try keychainController.setPINCodeBiometricState(state) @@ -229,3 +229,12 @@ class AppLockService: AppLockServiceProtocol { appSettings.appLockNumberOfPINAttempts = 0 } } + +nonisolated extension LAContext { + /// A hash of the enrolled biometric data that changes whenever biometrics are added or removed. + /// Unlike `evaluatedPolicyDomainState`, this doesn't need `canEvaluatePolicy` to be called first, + /// so the `updateBiometrics()` calls made before reading it are now redundant. + @objc var biometricStateHash: Data? { + domainState.biometry.stateHash + } +} diff --git a/ElementX/Sources/Services/AppLock/LAContextMock.swift b/ElementX/Sources/Services/AppLock/LAContextMock.swift index c2ee20049..85d549044 100644 --- a/ElementX/Sources/Services/AppLock/LAContextMock.swift +++ b/ElementX/Sources/Services/AppLock/LAContextMock.swift @@ -10,7 +10,7 @@ import LocalAuthentication /// A customised context that allows injecting a few mock values but otherwise behaves as expected. /// It works as the actual context does and won't update the return values of `biometryType` and -/// `evaluatedPolicyDomainStateValue` until either `canEvaluatePolicy` or +/// `biometricStateHash` until either `canEvaluatePolicy` or /// `evaluatePolicy` have been called. nonisolated class LAContextMock: LAContext { var biometryTypeValue: LABiometryType! @@ -19,10 +19,10 @@ nonisolated class LAContextMock: LAContext { internalBiometryTypeValue } - var evaluatedPolicyDomainStateValue: Data? - private var internalEvaluatedPolicyDomainStateValue: Data? - override var evaluatedPolicyDomainState: Data? { - internalEvaluatedPolicyDomainStateValue + var biometricStateHashValue: Data? + private var internalBiometricStateHashValue: Data? + override var biometricStateHash: Data? { + internalBiometricStateHashValue } var canEvaluatePolicyReturnValue: Bool? @@ -50,6 +50,6 @@ nonisolated class LAContextMock: LAContext { private func updateInternalValues() { internalBiometryTypeValue = biometryTypeValue - internalEvaluatedPolicyDomainStateValue = evaluatedPolicyDomainStateValue + internalBiometricStateHashValue = biometricStateHashValue } } diff --git a/ElementX/Sources/UITests/UITestsAppCoordinator.swift b/ElementX/Sources/UITests/UITestsAppCoordinator.swift index 2539bd30c..669b99f5f 100644 --- a/ElementX/Sources/UITests/UITestsAppCoordinator.swift +++ b/ElementX/Sources/UITests/UITestsAppCoordinator.swift @@ -207,7 +207,7 @@ class MockScreen: Identifiable { let context = LAContextMock() context.biometryTypeValue = UIDevice.current.isPhone ? .faceID : .touchID // (iPhone 14 & iPad 9th gen) context.evaluatePolicyReturnValue = true - context.evaluatedPolicyDomainStateValue = Data("😎".utf8) + context.biometricStateHashValue = Data("😎".utf8) let appLockService = AppLockService(keychainController: keychainController, appSettings: appSettings, @@ -263,7 +263,7 @@ class MockScreen: Identifiable { let context = LAContextMock() context.biometryTypeValue = UIDevice.current.isPhone ? .faceID : .touchID // (iPhone 14 & iPad 9th gen) context.evaluatePolicyReturnValue = true - context.evaluatedPolicyDomainStateValue = Data("😎".utf8) + context.biometricStateHashValue = Data("😎".utf8) let appLockService = AppLockService(keychainController: keychainController, appSettings: appSettings, diff --git a/UnitTests/Sources/AppLock/AppLockServiceTests.swift b/UnitTests/Sources/AppLock/AppLockServiceTests.swift index ee981159f..a8c1046d9 100644 --- a/UnitTests/Sources/AppLock/AppLockServiceTests.swift +++ b/UnitTests/Sources/AppLock/AppLockServiceTests.swift @@ -181,7 +181,7 @@ final class AppLockServiceTests { // Given a service with the PIN code already set. let context = LAContextMock() context.biometryTypeValue = .touchID - context.evaluatedPolicyDomainStateValue = Data("👆".utf8) + context.biometricStateHashValue = Data("👆".utf8) service = AppLockService(keychainController: keychainController, appSettings: appSettings, context: context) guard case .success = service.setupPINCode("2023") else { Issue.record("The PIN should be valid.") @@ -214,7 +214,7 @@ final class AppLockServiceTests { // Given a service with the PIN code already set. let context = LAContextMock() context.biometryTypeValue = .touchID - context.evaluatedPolicyDomainStateValue = Data("👆".utf8) + context.biometricStateHashValue = Data("👆".utf8) service = AppLockService(keychainController: keychainController, appSettings: appSettings, context: context) let pinCode = "2023" guard case .success = service.setupPINCode(pinCode) else { @@ -231,7 +231,7 @@ final class AppLockServiceTests { #expect(service.biometricUnlockTrusted, "Biometric unlock should be trusted.") // When the user changes biometric data. - context.evaluatedPolicyDomainStateValue = Data("👈".utf8) + context.biometricStateHashValue = Data("👈".utf8) // Then biometric lock should remain enabled but untrusted. #expect(service.isEnabled, "The service should remain enabled.") @@ -254,7 +254,7 @@ final class AppLockServiceTests { // Given a service with the PIN code already set. let context = LAContextMock() context.biometryTypeValue = .touchID - context.evaluatedPolicyDomainStateValue = Data("👆".utf8) + context.biometricStateHashValue = Data("👆".utf8) service = AppLockService(keychainController: keychainController, appSettings: appSettings, context: context) guard case .success = service.setupPINCode("2023") else { Issue.record("The PIN should be valid.") @@ -284,7 +284,7 @@ final class AppLockServiceTests { // Given a service with the PIN code already set. let context = LAContextMock() context.biometryTypeValue = .touchID - context.evaluatedPolicyDomainStateValue = Data("👆".utf8) + context.biometricStateHashValue = Data("👆".utf8) service = AppLockService(keychainController: keychainController, appSettings: appSettings, context: context) guard case .success = service.setupPINCode("2023") else { Issue.record("The PIN should be valid.")