Files
bundesmessenger-ios/Riot/Modules/SetPinCode/PinCodePreferences.swift
T
JanNiklas Grabowski b298dedc22 chore: update from foss 1.11.19 (MESSENGER-6656)
Merge commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1' into feature/foss_update_1_11_19

* commit 'f823ab9aae70e8d15ed7cc079210dd9bbbb6c8e1':
  finish version++
  version++
  comments
  update submodule
  remove obsolete tests
  removed unused code
  update submodule
  fix
  Libolm removal
  update license macro
  update license
  Prepare for new sprint

# Conflicts:
#	Config/AppVersion.xcconfig
#	IDETemplateMacros.plist
#	LICENSE
#	README.md
#	Riot/Categories/MXSession+Riot.m
#	Riot/Managers/EncryptionKeyManager/EncryptionKeyManager.swift
#	Riot/Managers/KeyValueStorage/Extensions/Keychain.swift
#	Riot/Managers/KeyValueStorage/KeyValueStore.swift
#	Riot/Managers/KeyValueStorage/KeychainStore.swift
#	Riot/Managers/KeyValueStorage/MemoryStore.swift
#	Riot/Managers/PushNotification/PushNotificationService.m
#	Riot/Managers/Settings/RiotSettings.swift
#	Riot/Managers/Settings/Shared/RiotSharedSettings.swift
#	Riot/Modules/Analytics/AnalyticsUIElement.swift
#	Riot/Modules/Application/AppCoordinator.swift
#	Riot/Modules/Application/LegacyAppDelegate.h
#	Riot/Modules/Application/LegacyAppDelegate.m
#	Riot/Modules/Authentication/Legacy/AuthenticationViewController.h
#	Riot/Modules/Authentication/Legacy/AuthenticationViewController.m
#	Riot/Modules/Authentication/Legacy/Views/AuthInputsView.h
#	Riot/Modules/Authentication/Legacy/Views/AuthInputsView.m
#	Riot/Modules/Common/Recents/DataSources/RecentsDataSource.m
#	Riot/Modules/Common/Recents/RecentsViewController.m
#	Riot/Modules/Common/WebViewController/WebViewViewController.m
#	Riot/Modules/Contacts/Details/ContactDetailsViewController.m
#	Riot/Modules/Contacts/Views/ContactTableViewCell.m
#	Riot/Modules/Favorites/FavouritesViewController.h
#	Riot/Modules/Favorites/FavouritesViewController.m
#	Riot/Modules/GlobalSearch/UnifiedSearchViewController.m
#	Riot/Modules/People/PeopleViewController.h
#	Riot/Modules/People/PeopleViewController.m
#	Riot/Modules/Room/ContextualMenu/ReactionsMenu/ReactionsMenuViewModel.swift
#	Riot/Modules/Room/DataSources/RoomDataSource.m
#	Riot/Modules/Room/Files/RoomFilesViewController.m
#	Riot/Modules/Room/Members/Detail/RoomMemberDetailsViewController.m
#	Riot/Modules/Room/Members/RoomParticipantsViewController.m
#	Riot/Modules/Room/RoomViewController.m
#	Riot/Modules/Room/Settings/RoomSettingsViewController.m
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCell.swift
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroCellContentView.swift
#	Riot/Modules/Room/TimelineCells/RoomCreationIntro/RoomCreationIntroViewData.swift
#	Riot/Modules/Room/TimelineCells/RoomTimelineCellIdentifier.h
#	Riot/Modules/Rooms/RoomsViewController.h
#	Riot/Modules/Rooms/ShowDirectory/Cells/Network/DirectoryNetworkTableHeaderFooterView.swift
#	Riot/Modules/Rooms/ShowDirectory/Cells/Room/DirectoryRoomTableViewCell.swift
#	Riot/Modules/Rooms/ShowDirectory/PublicRoomsDirectoryViewModel.swift
#	Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyCoordinator.swift
#	Riot/Modules/Secrets/Recover/RecoverWithKey/SecretsRecoveryWithKeyViewController.swift
#	Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseCoordinator.swift
#	Riot/Modules/Secrets/Recover/RecoverWithPassphrase/SecretsRecoveryWithPassphraseViewController.swift
#	Riot/Modules/Secrets/Recover/SecretsRecoveryCoordinator.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewController.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModel.swift
#	Riot/Modules/SecureBackup/Setup/Intro/SecureBackupSetupIntroViewModelType.swift
#	Riot/Modules/SetPinCode/PinCodePreferences.swift
#	Riot/Modules/SetPinCode/SetupBiometrics/BiometricsAuthenticationPresenter.swift
#	Riot/Modules/Settings/Security/ManageSession/ManageSessionViewController.m
#	Riot/Modules/Settings/Security/SecurityViewController.m
#	Riot/Modules/Settings/SettingsViewController.m
#	Riot/Modules/SplitView/SplitViewCoordinator.swift
#	Riot/Modules/SplitView/SplitViewCoordinatorType.swift
#	Riot/Modules/StartChat/StartChatViewController.m
#	Riot/Modules/TabBar/MasterTabBarController.h
#	Riot/Modules/TabBar/MasterTabBarController.m
#	Riot/Utils/EventFormatter.m
#	Riot/Utils/HTMLFormatter.swift
#	Riot/Utils/Tools.m
#	RiotNSE/NotificationService.swift
2024-10-18 15:45:54 +02:00

274 lines
9.0 KiB
Swift

/*
Copyright 2018-2024 New Vector Ltd.
Copyright (c) 2021 BWI GmbH
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/
import Foundation
import KeychainAccess
import LocalAuthentication
import MatrixSDK
extension Notification.Name {
/// Posted when fallbacking to PIN from a biometrics protection
static let biometricsDidFallbackToPin = Notification.Name("BiometricsDidFallbackToPin")
}
/// Pin code preferences.
@objcMembers
final class PinCodePreferences: NSObject {
// MARK: - Constants
private struct PinConstants {
static let pinCodeKeychainService: String = BuildSettings.baseBundleIdentifier + ".pin-service"
}
private struct StoreKeys {
static let pin: String = "pin"
static let biometricsEnabled: String = "biometricsEnabled"
static let canUseBiometricsToUnlock: String = "canUseBiometricsToUnlock"
static let numberOfPinFailures: String = "numberOfPinFailures"
static let numberOfBiometricsFailures: String = "numberOfBiometricsFailures"
}
static let shared = PinCodePreferences()
/// Store. Defaults to `SecureFileStore`
private let secureStore: SecureFileStorage
override private init() {
secureStore = SecureFileStorage.shared
super.init()
migrateOldData()
}
private func migrateOldData() {
let oldStore = KeychainVault(Keychain(service: PinConstants.pinCodeKeychainService,
accessGroup: BuildSettings.keychainAccessGroup))
if let oldPin = try? oldStore.string(forKey: StoreKeys.pin) {
do {
// migrate
pin = oldPin
// clear old pin
try oldStore.removeObject(forKey: StoreKeys.pin)
} catch let error {
MXLog.error("[PinCodePreferences] Error when migrating old user pin:")
}
}
if let oldBiometricsEnabled = try? oldStore.bool(forKey: StoreKeys.biometricsEnabled) {
biometricsEnabled = oldBiometricsEnabled
try? oldStore.removeObject(forKey: StoreKeys.biometricsEnabled)
}
if let oldCanUseBiometricsToUnlock = try? oldStore.bool(forKey: StoreKeys.canUseBiometricsToUnlock) {
canUseBiometricsToUnlock = oldCanUseBiometricsToUnlock
try? oldStore.removeObject(forKey: StoreKeys.canUseBiometricsToUnlock)
}
if let oldNumberOfPinFailures = try? oldStore.integer(forKey: StoreKeys.numberOfPinFailures) {
numberOfPinFailures = oldNumberOfPinFailures
try? oldStore.removeObject(forKey: StoreKeys.numberOfPinFailures)
}
if let oldNumberOfBiometricsFailures = try? oldStore.integer(forKey: StoreKeys.numberOfBiometricsFailures) {
numberOfBiometricsFailures = oldNumberOfBiometricsFailures
try? oldStore.removeObject(forKey: StoreKeys.numberOfBiometricsFailures)
}
}
// MARK: - Public
/// Setting to force protection by pin code
var forcePinProtection: Bool {
return BWIBuildSettings.shared.forcePinProtection
}
/// Not allowed pin codes. User won't be able to select one of the pin in the list.
var notAllowedPINs: [String] {
return BWIBuildSettings.shared.notAllowedPINs
}
/// 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 {
let context = LAContext()
var error: NSError?
let result = LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error)
// While in lockout they're still techincally available
if error?.code == LAError.Code.biometryLockout.rawValue {
return true
}
if result == true {
return context.biometryType == .touchID || context.biometryType == .faceID
}
return false
}
/// Allowed number of PIN trials before showing forgot help alert
let allowedNumberOfTrialsBeforeAlert: Int = 5
/// Max allowed time to continue using the app without prompting PIN
var graceTimeInSeconds: TimeInterval {
return BWIBuildSettings.shared.pinCodeGraceTimeInSeconds
}
/// Number of digits for the PIN
let numberOfDigits: Int = 4
/// Is user has set a pin
var isPinSet: Bool {
return secureStore.objectExists(withKey: StoreKeys.pin)
}
/// Saved user PIN
var pin: String? {
get {
do {
return try secureStore.string(forKey: StoreKeys.pin)
} catch let error {
MXLog.debug("[PinCodePreferences] Error when reading user pin from store: \(error)")
return nil
}
} set {
do {
if let newPin = newValue {
if !secureStore.locked {
try secureStore.update(passphrase: newPin)
} else {
try secureStore.unlock(passphrase: newPin)
}
}
try secureStore.set(newValue, forKey: StoreKeys.pin)
} catch let error {
MXLog.debug("[PinCodePreferences] Error when storing user pin to the store: \(error)")
}
}
}
func pinsMatch(_ pinCode: String) -> Bool {
do {
try secureStore.unlock(passphrase: pinCode)
let pin = try secureStore.string(forKey: StoreKeys.pin)
return pinCode == pin
} catch let error {
MXLog.error("[PinCodePreferences] Error when checking user pin")
return false
}
}
var biometricsEnabled: Bool {
get {
return RiotSettings.shared.biometricsEnabled
} set {
RiotSettings.shared.biometricsEnabled = newValue
}
}
var canUseBiometricsToUnlock: Bool? {
get {
guard isBiometricsAvailable == true else {
return false
}
do {
return try secureStore.bool(forKey: StoreKeys.canUseBiometricsToUnlock)
} catch {
MXLog.debug("[PinCodePreferences] Error when reading canUseBiometricsToUnlock from store")
return nil
}
} set {
do {
try secureStore.set(newValue, forKey: StoreKeys.canUseBiometricsToUnlock)
} catch {
MXLog.debug("[PinCodePreferences] Error when storing canUseBiometricsToUnlock to the store")
}
}
}
var numberOfPinFailures: Int {
get {
return RiotSettings.shared.numberOfPinFailures
} set {
RiotSettings.shared.numberOfPinFailures = newValue
}
}
var numberOfBiometricsFailures: Int {
get {
return RiotSettings.shared.numberOfBiometricsFailures
} set {
RiotSettings.shared.numberOfBiometricsFailures = newValue
}
}
var isBiometricsSet: Bool {
return biometricsEnabled && (canUseBiometricsToUnlock ?? true) && isBiometricsAvailable
}
func localizedBiometricsName() -> String? {
if isBiometricsAvailable {
let context = LAContext()
// 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()
// 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
}
/// Resets user PIN
func reset() {
pin = nil
biometricsEnabled = false
canUseBiometricsToUnlock = true
resetCounters()
}
/// Reset number of failures for both pin and biometrics
func resetCounters() {
numberOfPinFailures = 0
numberOfBiometricsFailures = 0
}
}