element-ios/Riot/Modules/SetPinCode/PinCodePreferences.swift

230 lines
7.6 KiB
Swift
Raw Normal View History

2020-07-21 13:15:08 +00:00
/*
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import Foundation
2020-07-22 14:07:54 +00:00
import KeychainAccess
import LocalAuthentication
2020-07-21 13:15:08 +00:00
/// Pin code preferences.
@objcMembers
final class PinCodePreferences: NSObject {
// MARK: - Constants
2020-07-22 14:07:54 +00:00
private struct PinConstants {
2020-08-05 14:11:46 +00:00
static let pinCodeKeychainService: String = BuildSettings.baseBundleIdentifier + ".pin-service"
2020-07-22 14:07:54 +00:00
}
private struct StoreKeys {
static let pin: String = "pin"
static let biometricsEnabled: String = "biometricsEnabled"
2020-09-25 14:59:29 +00:00
static let canUseBiometricsToUnlock: String = "canUseBiometricsToUnlock"
2020-09-28 13:19:51 +00:00
static let numberOfPinFailures: String = "numberOfPinFailures"
2020-09-28 13:44:50 +00:00
static let numberOfBiometricsFailures: String = "numberOfBiometricsFailures"
2020-07-21 13:15:08 +00:00
}
static let shared = PinCodePreferences()
/// Store. Defaults to `KeychainStore`
private let store: KeyValueStore
2020-09-28 13:19:51 +00:00
override private init() {
2020-07-22 14:07:54 +00:00
store = KeychainStore(withKeychain: Keychain(service: PinConstants.pinCodeKeychainService,
2020-08-05 14:11:46 +00:00
accessGroup: BuildSettings.keychainAccessGroup))
2020-07-22 14:47:45 +00:00
super.init()
}
2020-07-21 13:15:08 +00:00
// MARK: - Public
/// Setting to force protection by pin code
var forcePinProtection: Bool {
return BuildSettings.forcePinProtection
2020-07-21 13:15:08 +00:00
}
2020-07-21 13:26:22 +00:00
2020-09-17 12:55:41 +00:00
/// Not allowed pin codes. User won't be able to select one of the pin in the list.
var notAllowedPINs: [String] {
return BuildSettings.notAllowedPINs
2020-09-17 11:01:29 +00:00
}
2020-09-28 15:12:56 +00:00
/// Maximum number of allowed pin failures when unlocking, before force logging out the user
var maxAllowedNumberOfPinFailures: Int {
return BuildSettings.maxAllowedNumberOfPinFailures
}
/// Maximum number of allowed biometrics failures when unlocking, before fallbacking the user to the pin
var maxAllowedNumberOfBiometricsFailures: Int {
return BuildSettings.maxAllowedNumberOfBiometricsFailures
}
var isBiometricsAvailable: Bool {
return LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
}
2020-07-21 13:26:22 +00:00
/// Allowed number of PIN trials before showing forgot help alert
2020-07-21 13:15:08 +00:00
let allowedNumberOfTrialsBeforeAlert: Int = 5
2020-07-21 13:26:22 +00:00
/// Max allowed time to continue using the app without prompting PIN
2020-09-07 10:41:52 +00:00
var graceTimeInSeconds: TimeInterval {
return BuildSettings.pinCodeGraceTimeInSeconds
}
2020-07-21 13:15:08 +00:00
2020-07-21 16:32:48 +00:00
/// Number of digits for the PIN
let numberOfDigits: Int = 4
2020-07-21 13:15:08 +00:00
/// Is user has set a pin
var isPinSet: Bool {
return pin != nil
}
/// Saved user PIN
var pin: String? {
get {
2020-07-22 14:07:54 +00:00
do {
2020-07-28 15:21:39 +00:00
return try store.string(forKey: StoreKeys.pin)
2020-07-22 14:07:54 +00:00
} catch let error {
NSLog("[PinCodePreferences] Error when reading user pin from store: \(error)")
return nil
}
2020-07-21 13:15:08 +00:00
} set {
2020-07-22 14:07:54 +00:00
do {
try store.set(newValue, forKey: StoreKeys.pin)
2020-07-22 14:07:54 +00:00
} catch let error {
NSLog("[PinCodePreferences] Error when storing user pin to the store: \(error)")
}
2020-07-21 13:15:08 +00:00
}
}
var biometricsEnabled: Bool? {
get {
do {
2020-07-28 15:21:39 +00:00
return try store.bool(forKey: StoreKeys.biometricsEnabled)
} catch let error {
NSLog("[PinCodePreferences] Error when reading biometrics enabled from store: \(error)")
return nil
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.biometricsEnabled)
} catch let error {
NSLog("[PinCodePreferences] Error when storing biometrics enabled to the store: \(error)")
}
}
}
2020-09-25 14:59:29 +00:00
var canUseBiometricsToUnlock: Bool? {
get {
do {
return try store.bool(forKey: StoreKeys.canUseBiometricsToUnlock)
2020-09-25 14:59:29 +00:00
} catch let error {
NSLog("[PinCodePreferences] Error when reading canUseBiometricsToUnlock from store: \(error)")
return nil
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.canUseBiometricsToUnlock)
} catch let error {
NSLog("[PinCodePreferences] Error when storing canUseBiometricsToUnlock to the store: \(error)")
}
}
}
2020-09-28 13:19:51 +00:00
var numberOfPinFailures: Int {
get {
do {
return try store.integer(forKey: StoreKeys.numberOfPinFailures) ?? 0
} catch let error {
NSLog("[PinCodePreferences] Error when reading numberOfPinFailures from store: \(error)")
return 0
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.numberOfPinFailures)
} catch let error {
NSLog("[PinCodePreferences] Error when storing numberOfPinFailures to the store: \(error)")
}
}
}
2020-09-28 13:44:50 +00:00
var numberOfBiometricsFailures: Int {
get {
do {
return try store.integer(forKey: StoreKeys.numberOfBiometricsFailures) ?? 0
} catch let error {
NSLog("[PinCodePreferences] Error when reading numberOfBiometricsFailures from store: \(error)")
return 0
}
} set {
do {
try store.set(newValue, forKey: StoreKeys.numberOfBiometricsFailures)
} catch let error {
NSLog("[PinCodePreferences] Error when storing numberOfBiometricsFailures to the store: \(error)")
}
}
}
var isBiometricsSet: Bool {
return biometricsEnabled == true && canUseBiometricsToUnlock == true
}
func localizedBiometricsName() -> String? {
if isBiometricsAvailable {
let context = LAContext()
2020-08-19 12:37:05 +00:00
// canEvaluatePolicy should be called for biometryType to be set
_ = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
switch context.biometryType {
case .touchID:
return VectorL10n.biometricsModeTouchId
case .faceID:
return VectorL10n.biometricsModeFaceId
default:
return nil
}
}
return nil
}
func biometricsIcon() -> UIImage? {
if isBiometricsAvailable {
let context = LAContext()
2020-08-19 12:37:05 +00:00
// canEvaluatePolicy should be called for biometryType to be set
_ = context.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)
switch context.biometryType {
case .touchID:
return Asset.Images.touchidIcon.image
case .faceID:
return Asset.Images.faceidIcon.image
default:
return nil
}
}
return nil
}
2020-07-21 13:15:08 +00:00
/// Resets user PIN
func reset() {
pin = nil
biometricsEnabled = nil
2020-09-28 13:19:51 +00:00
canUseBiometricsToUnlock = nil
2020-09-28 13:44:50 +00:00
resetCounters()
}
/// Reset number of failures for both pin and biometrics
func resetCounters() {
2020-09-28 13:19:51 +00:00
numberOfPinFailures = 0
2020-09-28 13:44:50 +00:00
numberOfBiometricsFailures = 0
2020-07-21 13:15:08 +00:00
}
}