Add a state machine to AppLockFlowCoordinator. (#2166)

Fixes a bug when cancelling Face ID would loop back round to presenting Face ID.
This commit is contained in:
Doug
2023-11-24 16:03:38 +00:00
committed by GitHub
parent 81373b0d98
commit 9a52dcf9ac
9 changed files with 188 additions and 64 deletions
@@ -38,6 +38,9 @@ class AppLockService: AppLockServiceProtocol {
}
}
private var isEnabledSubject: PassthroughSubject<Bool, Never> = .init()
var isEnabledPublisher: AnyPublisher<Bool, Never> { isEnabledSubject.eraseToAnyPublisher() }
var biometryType: LABiometryType {
updateBiometrics()
guard context.evaluatedPolicyDomainState != nil else { return .none }
@@ -71,6 +74,7 @@ class AppLockService: AppLockServiceProtocol {
do {
try keychainController.setPINCode(pinCode)
isEnabledSubject.send(true)
return .success(())
} catch {
MXLog.error("Keychain access error: \(error)")
@@ -105,6 +109,7 @@ class AppLockService: AppLockServiceProtocol {
keychainController.removePINCode()
keychainController.removePINCodeBiometricState()
appSettings.appLockNumberOfPINAttempts = 0
isEnabledSubject.send(false)
}
func applicationDidEnterBackground() {
@@ -128,30 +133,35 @@ class AppLockService: AppLockServiceProtocol {
_ = enableBiometricUnlock()
}
return completeUnlock()
completeUnlock()
return true
}
func unlockWithBiometrics() async -> Bool {
func unlockWithBiometrics() async -> AppLockServiceBiometricResult {
guard biometryType != .none, biometricUnlockEnabled else {
MXLog.error("Biometric unlock not setup.")
return false
return .failed
}
guard biometricUnlockTrusted else {
MXLog.error("Biometrics have changed. PIN should be shown.")
return false
return .failed
}
do {
let context = unlockContext()
guard try await context.evaluatePolicy(unlockPolicy, localizedReason: L10n.screenAppLockBiometricUnlockReasonIos) else {
MXLog.warning("\(context.biometryType) failed without error.")
return false
return .failed
}
return completeUnlock()
completeUnlock()
return .unlocked
} catch LAError.systemCancel {
MXLog.error("\(context.biometryType) failed: The system cancelled.")
return .interrupted
} catch {
MXLog.error("\(context.biometryType) failed: \(error)")
return false
return .failed
}
}
@@ -180,9 +190,8 @@ class AppLockService: AppLockServiceProtocol {
}
/// Shared logic for completing an unlock via a PIN or biometrics.
private func completeUnlock() -> Bool {
private func completeUnlock() {
timer.registerUnlock()
appSettings.appLockNumberOfPINAttempts = 0
return true
}
}
@@ -30,6 +30,18 @@ enum AppLockServiceError: Error {
case biometricUnlockNotSupported
}
/// The result of an attempt to unlock the app using Touch ID or Face ID.
enum AppLockServiceBiometricResult {
/// Biometric lock was successful.
case unlocked
/// Biometric lock failed to authenticate the user. This represents any failure
/// other than the app being backgrounded during the authentication.
case failed
/// Biometric lock was interrupted by the system and did not complete. The expected
/// cause for this that the app was backgrounded whilst the request was in progress.
case interrupted
}
@MainActor
protocol AppLockServiceProtocol: AnyObject {
/// The use of a PIN code is mandatory for this device.
@@ -37,6 +49,9 @@ protocol AppLockServiceProtocol: AnyObject {
/// The app has been configured to automatically lock with a PIN code.
var isEnabled: Bool { get }
/// A publisher that advertises when the service has been enabled or disabled.
var isEnabledPublisher: AnyPublisher<Bool, Never> { get }
/// The type of biometric authentication supported by the device.
var biometryType: LABiometryType { get }
/// Whether or not the user has enabled unlock via TouchID, FaceID or (possibly) OpticID.
@@ -64,7 +79,7 @@ protocol AppLockServiceProtocol: AnyObject {
/// Attempt to unlock the app with the supplied PIN code.
func unlock(with pinCode: String) -> Bool
/// Attempt to unlock the app using FaceID or TouchID.
func unlockWithBiometrics() async -> Bool
func unlockWithBiometrics() async -> AppLockServiceBiometricResult
/// The number of attempts the user had made to unlock with a PIN code.
///