session-ios/SignalUtilitiesKit/Screen Lock/OWSScreenLock.swift

248 lines
11 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
2018-03-21 18:25:39 +01:00
import Foundation
import GRDB
2018-03-21 18:25:39 +01:00
import LocalAuthentication
import SessionMessagingKit
2018-03-21 18:25:39 +01:00
// FIXME: Refactor this once the 'PrivacySettingsTableViewController' and 'OWSScreenLockUI' have been refactored
2018-03-21 18:25:39 +01:00
@objc public class OWSScreenLock: NSObject {
public enum OWSScreenLockOutcome {
case success
case cancel
case failure(error: String)
case unexpectedFailure(error: String)
2018-03-21 18:25:39 +01:00
}
@objc public let screenLockTimeoutDefault = (15 * kMinuteInterval)
2018-03-21 21:06:25 +01:00
@objc public let screenLockTimeouts = [
1 * kMinuteInterval,
5 * kMinuteInterval,
2018-03-22 21:18:13 +01:00
15 * kMinuteInterval,
30 * kMinuteInterval,
1 * kHourInterval,
2018-03-21 21:06:25 +01:00
0
]
2018-03-21 18:25:39 +01:00
@objc public static let ScreenLockDidChange = Notification.Name("ScreenLockDidChange")
2018-08-23 16:37:34 +02:00
// MARK: - Singleton class
2018-03-21 18:25:39 +01:00
@objc(sharedManager)
public static let shared = OWSScreenLock()
private override init() {
super.init()
SwiftSingletons.register(self)
}
2018-03-21 20:12:00 +01:00
// MARK: - Properties
2018-03-21 18:25:39 +01:00
@objc public func isScreenLockEnabled() -> Bool {
return Storage.shared[.isScreenLockEnabled]
2018-03-21 18:25:39 +01:00
}
2018-04-12 18:23:11 +02:00
@objc
public func setIsScreenLockEnabled(_ value: Bool) {
Storage.shared.writeAsync(
updates: { db in db[.isScreenLockEnabled] = value },
completion: { _, _ in
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
}
)
2018-03-21 18:25:39 +01:00
}
2018-03-21 20:12:00 +01:00
@objc public func screenLockTimeout() -> TimeInterval {
return Storage.shared[.screenLockTimeoutSeconds]
.defaulting(to: screenLockTimeoutDefault)
2018-03-21 20:12:00 +01:00
}
2018-03-21 22:11:38 +01:00
@objc public func setScreenLockTimeout(_ value: TimeInterval) {
Storage.shared.writeAsync(
updates: { db in db[.screenLockTimeoutSeconds] = value },
completion: { _, _ in
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
}
)
2018-03-21 20:12:00 +01:00
}
// MARK: - Methods
2018-04-19 17:45:13 +02:00
// This method should only be called:
//
// * On the main thread.
//
// Exactly one of these completions will be performed:
//
// * Asynchronously.
// * On the main thread.
2018-03-21 20:12:00 +01:00
@objc public func tryToUnlockScreenLock(success: @escaping (() -> Void),
failure: @escaping ((Error) -> Void),
unexpectedFailure: @escaping ((Error) -> Void),
2018-03-21 20:12:00 +01:00
cancel: @escaping (() -> Void)) {
AssertIsOnMainThread()
2018-03-22 22:28:05 +01:00
2018-03-22 21:10:38 +01:00
tryToVerifyLocalAuthentication(localizedReason: NSLocalizedString("SCREEN_LOCK_REASON_UNLOCK_SCREEN_LOCK",
2018-04-12 18:23:11 +02:00
comment: "Description of how and why Signal iOS uses Touch ID/Face ID/Phone Passcode to unlock 'screen lock'."),
2018-03-21 20:12:00 +01:00
completion: { (outcome: OWSScreenLockOutcome) in
AssertIsOnMainThread()
2018-03-21 20:12:00 +01:00
switch outcome {
case .failure(let error):
2018-08-23 16:37:34 +02:00
Logger.error("local authentication failed with error: \(error)")
2018-03-21 20:12:00 +01:00
failure(self.authenticationError(errorDescription: error))
case .unexpectedFailure(let error):
2018-08-23 16:37:34 +02:00
Logger.error("local authentication failed with unexpected error: \(error)")
unexpectedFailure(self.authenticationError(errorDescription: error))
2018-03-21 20:12:00 +01:00
case .success:
2018-08-23 16:37:34 +02:00
Logger.verbose("local authentication succeeded.")
2018-03-21 20:12:00 +01:00
success()
case .cancel:
2018-08-23 16:37:34 +02:00
Logger.verbose("local authentication cancelled.")
2018-03-21 20:12:00 +01:00
cancel()
}
})
}
2018-04-19 17:45:13 +02:00
// This method should only be called:
//
// * On the main thread.
//
// completionParam will be performed:
//
// * Asynchronously.
// * On the main thread.
2018-03-22 21:10:38 +01:00
private func tryToVerifyLocalAuthentication(localizedReason: String,
2018-03-22 20:50:59 +01:00
completion completionParam: @escaping ((OWSScreenLockOutcome) -> Void)) {
AssertIsOnMainThread()
2018-03-21 18:25:39 +01:00
2018-04-19 17:45:13 +02:00
let defaultErrorDescription = NSLocalizedString("SCREEN_LOCK_ENABLE_UNKNOWN_ERROR",
comment: "Indicates that an unknown error occurred while using Touch ID/Face ID/Phone Passcode.")
2018-03-21 18:25:39 +01:00
// Ensure completion is always called on the main thread.
let completion = { (outcome: OWSScreenLockOutcome) in
DispatchQueue.main.async {
completionParam(outcome)
}
}
let context = screenLockContext()
var authError: NSError?
2018-03-22 21:10:38 +01:00
let canEvaluatePolicy = context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError)
2018-03-21 18:25:39 +01:00
if !canEvaluatePolicy || authError != nil {
2018-08-23 16:37:34 +02:00
Logger.error("could not determine if local authentication is supported: \(String(describing: authError))")
2018-03-21 18:25:39 +01:00
let outcome = self.outcomeForLAError(errorParam: authError,
defaultErrorDescription: defaultErrorDescription)
switch outcome {
case .success:
2018-08-27 16:27:48 +02:00
owsFailDebug("local authentication unexpected success")
2018-03-21 18:25:39 +01:00
completion(.failure(error:defaultErrorDescription))
case .cancel, .failure, .unexpectedFailure:
2018-03-21 18:25:39 +01:00
completion(outcome)
}
return
}
2018-03-22 21:10:38 +01:00
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: localizedReason) { success, evaluateError in
2018-03-22 22:28:05 +01:00
2018-03-21 18:25:39 +01:00
if success {
2018-08-23 16:37:34 +02:00
Logger.info("local authentication succeeded.")
2018-03-21 18:25:39 +01:00
completion(.success)
} else {
let outcome = self.outcomeForLAError(errorParam: evaluateError,
defaultErrorDescription: defaultErrorDescription)
switch outcome {
case .success:
2018-08-27 16:27:48 +02:00
owsFailDebug("local authentication unexpected success")
2018-03-21 18:25:39 +01:00
completion(.failure(error:defaultErrorDescription))
case .cancel, .failure, .unexpectedFailure:
2018-03-21 18:25:39 +01:00
completion(outcome)
}
}
}
}
// MARK: - Outcome
private func outcomeForLAError(errorParam: Error?, defaultErrorDescription: String) -> OWSScreenLockOutcome {
if let error = errorParam {
guard let laError = error as? LAError else {
return .failure(error:defaultErrorDescription)
}
switch laError.code {
2018-03-21 18:25:39 +01:00
case .biometryNotAvailable:
2018-08-23 16:37:34 +02:00
Logger.error("local authentication error: biometryNotAvailable.")
2018-03-21 18:25:39 +01:00
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE",
2018-03-22 21:10:38 +01:00
comment: "Indicates that Touch ID/Face ID/Phone Passcode are not available on this device."))
2018-03-21 18:25:39 +01:00
case .biometryNotEnrolled:
2018-08-23 16:37:34 +02:00
Logger.error("local authentication error: biometryNotEnrolled.")
2018-03-21 18:25:39 +01:00
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED",
2018-03-22 21:10:38 +01:00
comment: "Indicates that Touch ID/Face ID/Phone Passcode is not configured on this device."))
2018-03-21 18:25:39 +01:00
case .biometryLockout:
2018-08-23 16:37:34 +02:00
Logger.error("local authentication error: biometryLockout.")
2018-03-21 18:25:39 +01:00
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT",
2018-03-22 21:10:38 +01:00
comment: "Indicates that Touch ID/Face ID/Phone Passcode is 'locked out' on this device due to authentication failures."))
2018-03-21 18:25:39 +01:00
default:
// Fall through to second switch
break
}
switch laError.code {
case .authenticationFailed:
Logger.error("local authentication error: authenticationFailed.")
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_FAILED",
comment: "Indicates that Touch ID/Face ID/Phone Passcode authentication failed."))
case .userCancel, .userFallback, .systemCancel, .appCancel:
Logger.info("local authentication cancelled.")
return .cancel
case .passcodeNotSet:
Logger.error("local authentication error: passcodeNotSet.")
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_PASSCODE_NOT_SET",
comment: "Indicates that Touch ID/Face ID/Phone Passcode passcode is not set."))
case .touchIDNotAvailable:
Logger.error("local authentication error: touchIDNotAvailable.")
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE",
comment: "Indicates that Touch ID/Face ID/Phone Passcode are not available on this device."))
case .touchIDNotEnrolled:
Logger.error("local authentication error: touchIDNotEnrolled.")
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED",
comment: "Indicates that Touch ID/Face ID/Phone Passcode is not configured on this device."))
case .touchIDLockout:
Logger.error("local authentication error: touchIDLockout.")
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT",
comment: "Indicates that Touch ID/Face ID/Phone Passcode is 'locked out' on this device due to authentication failures."))
case .invalidContext:
owsFailDebug("context not valid.")
return .unexpectedFailure(error:defaultErrorDescription)
case .notInteractive:
owsFailDebug("context not interactive.")
return .unexpectedFailure(error:defaultErrorDescription)
2018-03-21 18:25:39 +01:00
}
}
2018-03-21 18:25:39 +01:00
return .failure(error:defaultErrorDescription)
}
private func authenticationError(errorDescription: String) -> Error {
return OWSErrorWithCodeDescription(.localAuthenticationError,
errorDescription)
}
// MARK: - Context
private func screenLockContext() -> LAContext {
let context = LAContext()
2018-04-12 18:23:11 +02:00
// Never recycle biometric auth.
2018-04-10 16:35:53 +02:00
context.touchIDAuthenticationAllowableReuseDuration = TimeInterval(0)
assert(!context.interactionNotAllowed)
2018-03-21 18:25:39 +01:00
return context
}
}