session-ios/SignalMessaging/utils/OWSScreenLock.swift

277 lines
13 KiB
Swift
Raw Normal View History

2018-03-21 18:25:39 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import LocalAuthentication
@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
}
2018-03-22 21:18:13 +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")
let primaryStorage: OWSPrimaryStorage
let dbConnection: YapDatabaseConnection
private let OWSScreenLock_Collection = "OWSScreenLock_Collection"
private let OWSScreenLock_Key_IsScreenLockEnabled = "OWSScreenLock_Key_IsScreenLockEnabled"
2018-03-21 20:12:00 +01:00
private let OWSScreenLock_Key_ScreenLockTimeoutSeconds = "OWSScreenLock_Key_ScreenLockTimeoutSeconds"
2018-03-21 18:25:39 +01:00
// We temporarily resign any first responder while the Screen Lock is presented.
weak var firstResponderBeforeLockscreen: UIResponder?
2018-03-21 18:25:39 +01:00
// MARK - Singleton class
@objc(sharedManager)
public static let shared = OWSScreenLock()
private override init() {
self.primaryStorage = OWSPrimaryStorage.shared()
self.dbConnection = self.primaryStorage.newDatabaseConnection()
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 {
2018-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-21 18:25:39 +01:00
2018-03-21 20:12:00 +01:00
if !OWSStorage.isStorageReady() {
2018-03-22 21:10:38 +01:00
owsFail("\(logTag) accessed screen lock state before storage is ready.")
2018-03-21 20:12:00 +01:00
return false
}
2018-03-21 18:25:39 +01:00
return self.dbConnection.bool(forKey: OWSScreenLock_Key_IsScreenLockEnabled, inCollection: OWSScreenLock_Collection, defaultValue: false)
}
2018-04-12 18:23:11 +02:00
@objc
public func setIsScreenLockEnabled(_ value: Bool) {
2018-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-21 20:12:00 +01:00
assert(OWSStorage.isStorageReady())
2018-03-21 18:25:39 +01:00
self.dbConnection.setBool(value, forKey: OWSScreenLock_Key_IsScreenLockEnabled, inCollection: OWSScreenLock_Collection)
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
}
2018-03-21 20:12:00 +01:00
@objc public func screenLockTimeout() -> TimeInterval {
2018-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-21 20:12:00 +01:00
if !OWSStorage.isStorageReady() {
2018-03-22 21:10:38 +01:00
owsFail("\(logTag) accessed screen lock state before storage is ready.")
2018-03-21 20:12:00 +01:00
return 0
}
2018-03-22 21:18:13 +01:00
return self.dbConnection.double(forKey: OWSScreenLock_Key_ScreenLockTimeoutSeconds, inCollection: OWSScreenLock_Collection, defaultValue: screenLockTimeoutDefault)
2018-03-21 20:12:00 +01:00
}
2018-03-21 22:11:38 +01:00
@objc public func setScreenLockTimeout(_ value: TimeInterval) {
2018-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-21 20:12:00 +01:00
assert(OWSStorage.isStorageReady())
self.dbConnection.setDouble(value, forKey: OWSScreenLock_Key_ScreenLockTimeoutSeconds, inCollection: OWSScreenLock_Collection)
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
}
// 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)) {
2018-04-19 17:45:13 +02:00
SwiftAssertIsOnMainThread(#function)
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-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-21 20:12:00 +01:00
switch outcome {
case .failure(let error):
2018-04-19 17:45:13 +02:00
Logger.error("\(self.logTag) local authentication failed with error: \(error)")
2018-03-21 20:12:00 +01:00
failure(self.authenticationError(errorDescription: error))
case .unexpectedFailure(let error):
2018-04-19 17:45:13 +02:00
Logger.error("\(self.logTag) local authentication failed with unexpected error: \(error)")
unexpectedFailure(self.authenticationError(errorDescription: error))
2018-03-21 20:12:00 +01:00
case .success:
2018-04-19 17:45:13 +02:00
Logger.verbose("\(self.logTag) local authentication succeeded.")
2018-03-21 20:12:00 +01:00
success()
case .cancel:
2018-04-19 17:45:13 +02:00
Logger.verbose("\(self.logTag) 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-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
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-03-22 21:10:38 +01:00
Logger.error("\(logTag) 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-03-22 21:10:38 +01:00
owsFail("\(self.logTag) 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-03-22 21:10:38 +01:00
Logger.info("\(self.logTag) 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-03-22 21:10:38 +01:00
owsFail("\(self.logTag) 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)
}
if #available(iOS 11.0, *) {
switch laError.code {
case .biometryNotAvailable:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) 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-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) 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-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) 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:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) local authentication error: authenticationFailed.")
2018-03-21 18:25:39 +01:00
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_FAILED",
2018-03-22 21:10:38 +01:00
comment: "Indicates that Touch ID/Face ID/Phone Passcode authentication failed."))
2018-03-21 18:25:39 +01:00
case .userCancel, .userFallback, .systemCancel, .appCancel:
2018-03-22 21:10:38 +01:00
Logger.info("\(self.logTag) local authentication cancelled.")
2018-03-21 18:25:39 +01:00
return .cancel
case .passcodeNotSet:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) local authentication error: passcodeNotSet.")
2018-03-21 18:25:39 +01:00
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_PASSCODE_NOT_SET",
2018-03-22 21:10:38 +01:00
comment: "Indicates that Touch ID/Face ID/Phone Passcode passcode is not set."))
2018-03-21 18:25:39 +01:00
case .touchIDNotAvailable:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) local authentication error: touchIDNotAvailable.")
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 .touchIDNotEnrolled:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) local authentication error: touchIDNotEnrolled.")
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 .touchIDLockout:
2018-03-22 21:10:38 +01:00
Logger.error("\(self.logTag) local authentication error: touchIDLockout.")
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
case .invalidContext:
2018-03-22 21:10:38 +01:00
owsFail("\(self.logTag) context not valid.")
return .unexpectedFailure(error:defaultErrorDescription)
2018-03-21 18:25:39 +01:00
case .notInteractive:
2018-03-22 21:10:38 +01:00
owsFail("\(self.logTag) context not interactive.")
return .unexpectedFailure(error:defaultErrorDescription)
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)
2018-03-27 21:55:31 +02:00
if #available(iOS 11.0, *) {
assert(!context.interactionNotAllowed)
}
2018-03-21 18:25:39 +01:00
return context
}
}