Replace the deprecated biometric domain state in AppLockService (#6243)

This commit is contained in:
Mauro
2026-10-19 12:53:50 +02:00
committed by GitHub
parent 9f34176edc
commit 3216713c45
4 changed files with 25 additions and 16 deletions
@@ -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<Int, Never> {
@@ -94,7 +94,7 @@ class AppLockService: AppLockServiceProtocol {
func enableBiometricUnlock() -> Result<Void, AppLockServiceError> {
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
}
}
@@ -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
}
}
@@ -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,
@@ -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.")