Initial implementation for AppLockSetupFlowCoordinator. (#1949)

Rename AppLockSettingsScreen to AppLockSetupSettingsScreen.
Implement forced logout and persist PIN entry attempts.
This commit is contained in:
Doug
2023-10-25 12:03:37 +01:00
committed by GitHub
parent 30b18031a0
commit c6e4dc2a37
53 changed files with 773 additions and 234 deletions
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import LocalAuthentication
/// The service responsible for locking and unlocking the app.
@@ -24,6 +25,8 @@ class AppLockService: AppLockServiceProtocol {
private let timer: AppLockTimer
var isMandatory: Bool { appSettings.appLockIsMandatory }
var isEnabled: Bool {
do {
guard appSettings.appLockFlowEnabled else { return false }
@@ -38,6 +41,12 @@ class AppLockService: AppLockServiceProtocol {
var biometryType: LABiometryType { context.biometryType }
var biometricUnlockEnabled = false // Needs to be stored, not sure if in the keychain or defaults yet.
var numberOfPINAttempts: AnyPublisher<Int, Never> { appSettings.$appLockNumberOfPINAttempts }
var numberOfBiometricAttempts: AnyPublisher<Int, Never> { appSettings.$appLockNumberOfBiometricAttempts }
private var disabledSubject: PassthroughSubject<Void, Never> = .init()
var disabledPublisher: AnyPublisher<Void, Never> { disabledSubject.eraseToAnyPublisher() }
init(keychainController: KeychainControllerProtocol, appSettings: AppSettings) {
self.keychainController = keychainController
self.appSettings = appSettings
@@ -68,6 +77,9 @@ class AppLockService: AppLockServiceProtocol {
func disable() {
biometricUnlockEnabled = false
keychainController.removePINCode()
appSettings.appLockNumberOfPINAttempts = 0
appSettings.appLockNumberOfBiometricAttempts = 0
disabledSubject.send()
}
func applicationDidEnterBackground() {
@@ -79,12 +91,20 @@ class AppLockService: AppLockServiceProtocol {
}
func unlock(with pinCode: String) -> Bool {
guard pinCode == keychainController.pinCode() else { return false }
guard pinCode == keychainController.pinCode() else {
MXLog.warning("Wrong PIN entered.")
appSettings.appLockNumberOfPINAttempts += 1
return false
}
return completeUnlock()
}
func unlockWithBiometrics() -> Bool {
guard biometryType != .none, biometricUnlockEnabled else { return false }
guard biometryType != .none, biometricUnlockEnabled else {
MXLog.warning("\(biometryType) failed.")
appSettings.appLockNumberOfBiometricAttempts += 1
return false
}
return completeUnlock()
}
@@ -103,6 +123,8 @@ class AppLockService: AppLockServiceProtocol {
/// Shared logic for completing an unlock via a PIN or biometry.
private func completeUnlock() -> Bool {
timer.registerUnlock()
appSettings.appLockNumberOfPINAttempts = 0
appSettings.appLockNumberOfBiometricAttempts = 0
return true
}
}
@@ -14,6 +14,7 @@
// limitations under the License.
//
import Combine
import LocalAuthentication
enum AppLockServiceError: Error {
@@ -27,13 +28,19 @@ enum AppLockServiceError: Error {
@MainActor
protocol AppLockServiceProtocol: AnyObject {
/// The use of a PIN code is mandatory for this device.
var isMandatory: Bool { get }
/// The app has been configured to automatically lock with a PIN code.
var isEnabled: Bool { 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.
var biometricUnlockEnabled: Bool { get set }
/// A publisher that advertises when the service has been disabled.
var disabledPublisher: AnyPublisher<Void, Never> { get }
/// Sets the user's PIN code used to unlock the app.
func setupPINCode(_ pinCode: String) -> Result<Void, AppLockServiceError>
/// Validates the supplied PIN code is long enough, only contains digits and isn't a weak choice.
@@ -50,15 +57,23 @@ protocol AppLockServiceProtocol: AnyObject {
func unlock(with pinCode: String) -> Bool
/// Attempt to unlock the app using FaceID or TouchID.
func unlockWithBiometrics() -> Bool
/// The number of attempts the user had made to unlock with a PIN code.
var numberOfPINAttempts: AnyPublisher<Int, Never> { get }
/// The number of attempts the user has made to unlock with Touch/Face ID.
var numberOfBiometricAttempts: AnyPublisher<Int, Never> { get }
}
// sourcery: AutoMockable
extension AppLockServiceProtocol { }
extension AppLockServiceMock {
static func mock(pinCode: String? = "2023", biometryType: LABiometryType = .faceID) -> AppLockServiceMock {
static func mock(pinCode: String? = "2023", isMandatory: Bool = false, biometryType: LABiometryType = .faceID) -> AppLockServiceMock {
let mock = AppLockServiceMock()
mock.isEnabled = pinCode != nil
mock.isMandatory = isMandatory
mock.numberOfPINAttempts = PassthroughSubject<Int, Never>().eraseToAnyPublisher()
mock.numberOfBiometricAttempts = PassthroughSubject<Int, Never>().eraseToAnyPublisher()
mock.underlyingBiometryType = biometryType
mock.underlyingBiometricUnlockEnabled = biometryType != .none
mock.unlockWithClosure = { $0 == pinCode }