2022-05-20 09:58:39 +02:00
|
|
|
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
2018-03-21 18:25:39 +01:00
|
|
|
|
|
|
|
import Foundation
|
2022-05-20 09:58:39 +02:00
|
|
|
import GRDB
|
2018-03-21 18:25:39 +01:00
|
|
|
import LocalAuthentication
|
2022-05-20 09:58:39 +02:00
|
|
|
import SessionMessagingKit
|
2018-03-21 18:25:39 +01:00
|
|
|
|
2022-05-20 09:58:39 +02: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
|
2022-05-20 09:58:39 +02:00
|
|
|
case failure(error: String)
|
|
|
|
case unexpectedFailure(error: String)
|
2018-03-21 18:25:39 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 09:58:39 +02: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 {
|
2022-07-01 05:08:45 +02:00
|
|
|
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) {
|
2022-07-01 05:08:45 +02:00
|
|
|
Storage.shared.writeAsync(
|
2022-05-20 09:58:39 +02:00
|
|
|
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 {
|
2022-07-01 05:08:45 +02:00
|
|
|
return Storage.shared[.screenLockTimeoutSeconds]
|
2022-05-20 09:58:39 +02:00
|
|
|
.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) {
|
2022-07-01 05:08:45 +02:00
|
|
|
Storage.shared.writeAsync(
|
2022-05-20 09:58:39 +02:00
|
|
|
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),
|
2018-03-28 18:32:57 +02:00
|
|
|
unexpectedFailure: @escaping ((Error) -> Void),
|
2018-03-21 20:12:00 +01:00
|
|
|
cancel: @escaping (() -> Void)) {
|
2018-08-22 19:44:22 +02:00
|
|
|
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
|
2018-08-22 19:44:22 +02:00
|
|
|
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))
|
2018-03-28 18:32:57 +02:00
|
|
|
case .unexpectedFailure(let error):
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.error("local authentication failed with unexpected error: \(error)")
|
2018-03-28 18:32:57 +02:00
|
|
|
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)) {
|
2018-08-22 19:44:22 +02:00
|
|
|
AssertIsOnMainThread()
|
2018-03-21 18:25:39 +01:00
|
|
|
|
2022-07-15 10:15:28 +02:00
|
|
|
let defaultErrorDescription = "SCREEN_LOCK_ENABLE_UNKNOWN_ERROR".localized()
|
2018-04-19 17:45:13 +02:00
|
|
|
|
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")
|
2022-07-15 10:15:28 +02:00
|
|
|
completion(.failure(error: defaultErrorDescription))
|
2018-03-28 18:32:57 +02:00
|
|
|
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))
|
2018-03-28 18:32:57 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-01 05:33:00 +02:00
|
|
|
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.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE".localized())
|
2018-03-21 18:25:39 +01:00
|
|
|
case .biometryNotEnrolled:
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.error("local authentication error: biometryNotEnrolled.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED".localized())
|
2018-03-21 18:25:39 +01:00
|
|
|
case .biometryLockout:
|
2018-08-23 16:37:34 +02:00
|
|
|
Logger.error("local authentication error: biometryLockout.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT".localized())
|
2018-03-21 18:25:39 +01:00
|
|
|
default:
|
|
|
|
// Fall through to second switch
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
switch laError.code {
|
2022-07-01 05:33:00 +02:00
|
|
|
case .authenticationFailed:
|
|
|
|
Logger.error("local authentication error: authenticationFailed.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_FAILED".localized())
|
2022-07-01 05:33:00 +02:00
|
|
|
case .userCancel, .userFallback, .systemCancel, .appCancel:
|
|
|
|
Logger.info("local authentication cancelled.")
|
|
|
|
return .cancel
|
|
|
|
case .passcodeNotSet:
|
|
|
|
Logger.error("local authentication error: passcodeNotSet.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_PASSCODE_NOT_SET".localized())
|
2022-07-01 05:33:00 +02:00
|
|
|
case .touchIDNotAvailable:
|
|
|
|
Logger.error("local authentication error: touchIDNotAvailable.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE".localized())
|
2022-07-01 05:33:00 +02:00
|
|
|
case .touchIDNotEnrolled:
|
|
|
|
Logger.error("local authentication error: touchIDNotEnrolled.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED".localized())
|
2022-07-01 05:33:00 +02:00
|
|
|
case .touchIDLockout:
|
|
|
|
Logger.error("local authentication error: touchIDLockout.")
|
2022-07-15 10:15:28 +02:00
|
|
|
return .failure(error: "SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT".localized())
|
2022-07-01 05:33:00 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2022-07-01 05:33:00 +02: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)
|
2022-07-01 05:33:00 +02:00
|
|
|
assert(!context.interactionNotAllowed)
|
2018-03-21 18:25:39 +01:00
|
|
|
|
|
|
|
return context
|
|
|
|
}
|
|
|
|
}
|