mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Initial service implementation for using a PIN code. (#1912)
* Initial service implementation for using a PIN code. * Tweak Danger for commit size 600-800 lines is perfectly normal for our PRs, up it to 1000.
This commit is contained in:
@@ -16,14 +16,33 @@
|
||||
|
||||
import LocalAuthentication
|
||||
|
||||
enum AppLockServiceError: Error {
|
||||
/// The operation failed to access the keychain.
|
||||
case keychainError
|
||||
/// The PIN code was rejected because it isn't long enough, or contains invalid characters.
|
||||
case invalidPIN
|
||||
/// The PIN code was rejected as an insecure choice.
|
||||
case weakPIN
|
||||
}
|
||||
|
||||
@MainActor
|
||||
protocol AppLockServiceProtocol {
|
||||
/// The app has been configured to automatically lock with a PIN code.
|
||||
var isEnabled: Bool { get }
|
||||
/// The app can additionally be unlocked using FaceID or TouchID.
|
||||
var supportsBiometrics: Bool { get }
|
||||
/// The app should be unlocked with a PIN code/biometrics before being presented.
|
||||
var needsUnlock: 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 }
|
||||
|
||||
/// Sets the user's PIN code used to unlock the app.
|
||||
func setupPINCode(_ pinCode: String) -> Result<Void, AppLockServiceError>
|
||||
/// Disables the App Lock feature, removing the user's stored PIN code.
|
||||
func disable()
|
||||
|
||||
/// Informs the service that the app has entered the background.
|
||||
func applicationDidEnterBackground()
|
||||
/// Decides whether the app should be unlocked with a PIN code/biometrics on foregrounding.
|
||||
func computeNeedsUnlock(willEnterForegroundAt date: Date) -> Bool
|
||||
|
||||
/// Attempt to unlock the app with the supplied PIN code.
|
||||
func unlock(with pinCode: String) -> Bool
|
||||
@@ -31,25 +50,80 @@ protocol AppLockServiceProtocol {
|
||||
func unlockWithBiometrics() -> Bool
|
||||
}
|
||||
|
||||
/// The service responsible for locking and unlocking the app.
|
||||
class AppLockService: AppLockServiceProtocol {
|
||||
private let keychainController: KeychainControllerProtocol
|
||||
private let appSettings: AppSettings
|
||||
private let context = LAContext()
|
||||
|
||||
var isEnabled: Bool { appSettings.appLockFlowEnabled }
|
||||
var supportsBiometrics: Bool { true }
|
||||
var needsUnlock: Bool { true }
|
||||
private let timer: AppLockTimer
|
||||
|
||||
var isEnabled: Bool {
|
||||
do {
|
||||
guard appSettings.appLockFlowEnabled else { return false }
|
||||
return try keychainController.containsPINCode()
|
||||
} catch {
|
||||
MXLog.error("Keychain access error: \(error)")
|
||||
MXLog.error("Locking the app.")
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
var biometryType: LABiometryType { context.biometryType }
|
||||
var biometricUnlockEnabled = false // Needs to be stored, not sure if in the keychain or defaults yet.
|
||||
|
||||
init(keychainController: KeychainControllerProtocol, appSettings: AppSettings) {
|
||||
self.keychainController = keychainController
|
||||
self.appSettings = appSettings
|
||||
timer = AppLockTimer(gracePeriod: appSettings.appLockGracePeriod)
|
||||
}
|
||||
|
||||
func setupPINCode(_ pinCode: String) -> Result<Void, AppLockServiceError> {
|
||||
guard validate(pinCode) else { return .failure(.invalidPIN) }
|
||||
guard !appSettings.appLockPINCodeBlockList.contains(pinCode) else { return .failure(.weakPIN) }
|
||||
|
||||
do {
|
||||
try keychainController.setPINCode(pinCode)
|
||||
return .success(())
|
||||
} catch {
|
||||
MXLog.error("Keychain access error: \(error)")
|
||||
return .failure(.keychainError)
|
||||
}
|
||||
}
|
||||
|
||||
func disable() {
|
||||
biometricUnlockEnabled = false
|
||||
keychainController.removePINCode()
|
||||
}
|
||||
|
||||
func applicationDidEnterBackground() {
|
||||
timer.applicationDidEnterBackground()
|
||||
}
|
||||
|
||||
func computeNeedsUnlock(willEnterForegroundAt date: Date) -> Bool {
|
||||
timer.computeLockState(willEnterForegroundAt: date)
|
||||
}
|
||||
|
||||
func unlock(with pinCode: String) -> Bool {
|
||||
true
|
||||
guard pinCode == keychainController.pinCode() else { return false }
|
||||
return completeUnlock()
|
||||
}
|
||||
|
||||
func unlockWithBiometrics() -> Bool {
|
||||
guard supportsBiometrics else { return false }
|
||||
guard biometryType != .none, biometricUnlockEnabled else { return false }
|
||||
return completeUnlock()
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// Ensures that a provided PIN code is long enough and only contains digits.
|
||||
private func validate(_ pinCode: String) -> Bool {
|
||||
pinCode.count == 4 && pinCode.allSatisfy(\.isNumber)
|
||||
}
|
||||
|
||||
/// Shared logic for completing an unlock via a PIN or biometry.
|
||||
private func completeUnlock() -> Bool {
|
||||
timer.registerUnlock()
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user