mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
274 lines
12 KiB
Swift
274 lines
12 KiB
Swift
7 years ago
|
//
|
||
|
// 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)
|
||
7 years ago
|
case unexpectedFailure(error:String)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc public let screenLockTimeoutDefault = 15 * kMinuteInterval
|
||
7 years ago
|
@objc public let screenLockTimeouts = [
|
||
|
1 * kMinuteInterval,
|
||
|
5 * kMinuteInterval,
|
||
7 years ago
|
15 * kMinuteInterval,
|
||
|
30 * kMinuteInterval,
|
||
|
1 * kHourInterval,
|
||
7 years ago
|
0
|
||
|
]
|
||
|
|
||
7 years ago
|
@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"
|
||
7 years ago
|
private let OWSScreenLock_Key_ScreenLockTimeoutSeconds = "OWSScreenLock_Key_ScreenLockTimeoutSeconds"
|
||
7 years ago
|
|
||
7 years ago
|
// MARK: - Singleton class
|
||
7 years ago
|
|
||
|
@objc(sharedManager)
|
||
|
public static let shared = OWSScreenLock()
|
||
|
|
||
|
private override init() {
|
||
|
self.primaryStorage = OWSPrimaryStorage.shared()
|
||
|
self.dbConnection = self.primaryStorage.newDatabaseConnection()
|
||
|
|
||
|
super.init()
|
||
|
|
||
|
SwiftSingletons.register(self)
|
||
|
}
|
||
|
|
||
7 years ago
|
// MARK: - Properties
|
||
|
|
||
7 years ago
|
@objc public func isScreenLockEnabled() -> Bool {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
|
||
7 years ago
|
if !OWSStorage.isStorageReady() {
|
||
7 years ago
|
owsFailDebug("accessed screen lock state before storage is ready.")
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
7 years ago
|
return self.dbConnection.bool(forKey: OWSScreenLock_Key_IsScreenLockEnabled, inCollection: OWSScreenLock_Collection, defaultValue: false)
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc
|
||
|
public func setIsScreenLockEnabled(_ value: Bool) {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
assert(OWSStorage.isStorageReady())
|
||
7 years ago
|
|
||
|
self.dbConnection.setBool(value, forKey: OWSScreenLock_Key_IsScreenLockEnabled, inCollection: OWSScreenLock_Collection)
|
||
|
|
||
|
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
|
||
|
}
|
||
|
|
||
7 years ago
|
@objc public func screenLockTimeout() -> TimeInterval {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
|
||
|
if !OWSStorage.isStorageReady() {
|
||
7 years ago
|
owsFailDebug("accessed screen lock state before storage is ready.")
|
||
7 years ago
|
return 0
|
||
|
}
|
||
|
|
||
7 years ago
|
return self.dbConnection.double(forKey: OWSScreenLock_Key_ScreenLockTimeoutSeconds, inCollection: OWSScreenLock_Collection, defaultValue: screenLockTimeoutDefault)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
@objc public func setScreenLockTimeout(_ value: TimeInterval) {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
assert(OWSStorage.isStorageReady())
|
||
|
|
||
|
self.dbConnection.setDouble(value, forKey: OWSScreenLock_Key_ScreenLockTimeoutSeconds, inCollection: OWSScreenLock_Collection)
|
||
|
|
||
|
NotificationCenter.default.postNotificationNameAsync(OWSScreenLock.ScreenLockDidChange, object: nil)
|
||
|
}
|
||
|
|
||
|
// MARK: - Methods
|
||
|
|
||
7 years ago
|
// This method should only be called:
|
||
|
//
|
||
|
// * On the main thread.
|
||
|
//
|
||
|
// Exactly one of these completions will be performed:
|
||
|
//
|
||
|
// * Asynchronously.
|
||
|
// * On the main thread.
|
||
7 years ago
|
@objc public func tryToUnlockScreenLock(success: @escaping (() -> Void),
|
||
|
failure: @escaping ((Error) -> Void),
|
||
7 years ago
|
unexpectedFailure: @escaping ((Error) -> Void),
|
||
7 years ago
|
cancel: @escaping (() -> Void)) {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
|
||
7 years ago
|
tryToVerifyLocalAuthentication(localizedReason: NSLocalizedString("SCREEN_LOCK_REASON_UNLOCK_SCREEN_LOCK",
|
||
7 years ago
|
comment: "Description of how and why Signal iOS uses Touch ID/Face ID/Phone Passcode to unlock 'screen lock'."),
|
||
7 years ago
|
completion: { (outcome: OWSScreenLockOutcome) in
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
|
||
|
switch outcome {
|
||
|
case .failure(let error):
|
||
7 years ago
|
Logger.error("local authentication failed with error: \(error)")
|
||
7 years ago
|
failure(self.authenticationError(errorDescription: error))
|
||
7 years ago
|
case .unexpectedFailure(let error):
|
||
7 years ago
|
Logger.error("local authentication failed with unexpected error: \(error)")
|
||
7 years ago
|
unexpectedFailure(self.authenticationError(errorDescription: error))
|
||
7 years ago
|
case .success:
|
||
7 years ago
|
Logger.verbose("local authentication succeeded.")
|
||
7 years ago
|
success()
|
||
|
case .cancel:
|
||
7 years ago
|
Logger.verbose("local authentication cancelled.")
|
||
7 years ago
|
cancel()
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
7 years ago
|
// This method should only be called:
|
||
|
//
|
||
|
// * On the main thread.
|
||
|
//
|
||
|
// completionParam will be performed:
|
||
|
//
|
||
|
// * Asynchronously.
|
||
|
// * On the main thread.
|
||
7 years ago
|
private func tryToVerifyLocalAuthentication(localizedReason: String,
|
||
7 years ago
|
completion completionParam: @escaping ((OWSScreenLockOutcome) -> Void)) {
|
||
7 years ago
|
AssertIsOnMainThread()
|
||
7 years ago
|
|
||
7 years ago
|
let defaultErrorDescription = NSLocalizedString("SCREEN_LOCK_ENABLE_UNKNOWN_ERROR",
|
||
|
comment: "Indicates that an unknown error occurred while using Touch ID/Face ID/Phone Passcode.")
|
||
|
|
||
7 years ago
|
// 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?
|
||
7 years ago
|
let canEvaluatePolicy = context.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError)
|
||
7 years ago
|
if !canEvaluatePolicy || authError != nil {
|
||
7 years ago
|
Logger.error("could not determine if local authentication is supported: \(String(describing: authError))")
|
||
7 years ago
|
|
||
|
let outcome = self.outcomeForLAError(errorParam: authError,
|
||
|
defaultErrorDescription: defaultErrorDescription)
|
||
|
switch outcome {
|
||
|
case .success:
|
||
7 years ago
|
owsFailDebug("local authentication unexpected success")
|
||
7 years ago
|
completion(.failure(error:defaultErrorDescription))
|
||
7 years ago
|
case .cancel, .failure, .unexpectedFailure:
|
||
7 years ago
|
completion(outcome)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: localizedReason) { success, evaluateError in
|
||
7 years ago
|
|
||
7 years ago
|
if success {
|
||
7 years ago
|
Logger.info("local authentication succeeded.")
|
||
7 years ago
|
completion(.success)
|
||
|
} else {
|
||
|
let outcome = self.outcomeForLAError(errorParam: evaluateError,
|
||
|
defaultErrorDescription: defaultErrorDescription)
|
||
|
switch outcome {
|
||
|
case .success:
|
||
7 years ago
|
owsFailDebug("local authentication unexpected success")
|
||
7 years ago
|
completion(.failure(error:defaultErrorDescription))
|
||
7 years ago
|
case .cancel, .failure, .unexpectedFailure:
|
||
7 years ago
|
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:
|
||
7 years ago
|
Logger.error("local authentication error: biometryNotAvailable.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode are not available on this device."))
|
||
7 years ago
|
case .biometryNotEnrolled:
|
||
7 years ago
|
Logger.error("local authentication error: biometryNotEnrolled.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode is not configured on this device."))
|
||
7 years ago
|
case .biometryLockout:
|
||
7 years ago
|
Logger.error("local authentication error: biometryLockout.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode is 'locked out' on this device due to authentication failures."))
|
||
7 years ago
|
default:
|
||
|
// Fall through to second switch
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch laError.code {
|
||
|
case .authenticationFailed:
|
||
7 years ago
|
Logger.error("local authentication error: authenticationFailed.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_FAILED",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode authentication failed."))
|
||
7 years ago
|
case .userCancel, .userFallback, .systemCancel, .appCancel:
|
||
7 years ago
|
Logger.info("local authentication cancelled.")
|
||
7 years ago
|
return .cancel
|
||
|
case .passcodeNotSet:
|
||
7 years ago
|
Logger.error("local authentication error: passcodeNotSet.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_PASSCODE_NOT_SET",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode passcode is not set."))
|
||
7 years ago
|
case .touchIDNotAvailable:
|
||
7 years ago
|
Logger.error("local authentication error: touchIDNotAvailable.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_AVAILABLE",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode are not available on this device."))
|
||
7 years ago
|
case .touchIDNotEnrolled:
|
||
7 years ago
|
Logger.error("local authentication error: touchIDNotEnrolled.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_NOT_ENROLLED",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode is not configured on this device."))
|
||
7 years ago
|
case .touchIDLockout:
|
||
7 years ago
|
Logger.error("local authentication error: touchIDLockout.")
|
||
7 years ago
|
return .failure(error: NSLocalizedString("SCREEN_LOCK_ERROR_LOCAL_AUTHENTICATION_LOCKOUT",
|
||
7 years ago
|
comment: "Indicates that Touch ID/Face ID/Phone Passcode is 'locked out' on this device due to authentication failures."))
|
||
7 years ago
|
case .invalidContext:
|
||
7 years ago
|
owsFailDebug("context not valid.")
|
||
7 years ago
|
return .unexpectedFailure(error:defaultErrorDescription)
|
||
7 years ago
|
case .notInteractive:
|
||
7 years ago
|
owsFailDebug("context not interactive.")
|
||
7 years ago
|
return .unexpectedFailure(error:defaultErrorDescription)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
return .failure(error:defaultErrorDescription)
|
||
|
}
|
||
|
|
||
|
private func authenticationError(errorDescription: String) -> Error {
|
||
|
return OWSErrorWithCodeDescription(.localAuthenticationError,
|
||
|
errorDescription)
|
||
|
}
|
||
|
|
||
|
// MARK: - Context
|
||
|
|
||
|
private func screenLockContext() -> LAContext {
|
||
|
let context = LAContext()
|
||
|
|
||
7 years ago
|
// Never recycle biometric auth.
|
||
7 years ago
|
context.touchIDAuthenticationAllowableReuseDuration = TimeInterval(0)
|
||
7 years ago
|
|
||
7 years ago
|
if #available(iOS 11.0, *) {
|
||
|
assert(!context.interactionNotAllowed)
|
||
|
}
|
||
7 years ago
|
|
||
|
return context
|
||
|
}
|
||
|
}
|