KeychainCoordinator.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*****************************************************************************
  2. * KeychainCoordinator.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2017 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors:Carola Nitz <caro # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. import Foundation
  13. import LocalAuthentication
  14. @objc(VLCKeychainCoordinator)
  15. class KeychainCoordinator: NSObject, PAPasscodeViewControllerDelegate {
  16. @objc class var passcodeLockEnabled: Bool {
  17. return UserDefaults.standard.bool(forKey: kVLCSettingPasscodeOnKey)
  18. }
  19. // Since FaceID and TouchID are both set to 1 when the defaults are registered
  20. // we have to double check for the biometry type to not return true even though the setting is not visible
  21. // and that type is not supported by the device
  22. private var touchIDEnabled: Bool {
  23. var touchIDEnabled = UserDefaults.standard.bool(forKey: kVLCSettingPasscodeAllowTouchID)
  24. let laContext = LAContext()
  25. if #available(iOS 11.0.1, *), laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
  26. touchIDEnabled = touchIDEnabled && laContext.biometryType == .touchID
  27. }
  28. return touchIDEnabled
  29. }
  30. private var faceIDEnabled: Bool {
  31. var faceIDEnabled = UserDefaults.standard.bool(forKey: kVLCSettingPasscodeAllowFaceID)
  32. let laContext = LAContext()
  33. if #available(iOS 11.0.1, *), laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
  34. faceIDEnabled = faceIDEnabled && laContext.biometryType == .faceID
  35. }
  36. return faceIDEnabled
  37. }
  38. static let passcodeService = "org.videolan.vlc-ios.passcode"
  39. var completion: (() -> Void)?
  40. private var avoidPromptingTouchOrFaceID = false
  41. private lazy var passcodeLockController: PAPasscodeViewController = {
  42. let passcodeController = PAPasscodeViewController(for: PasscodeActionEnter)
  43. passcodeController!.delegate = self
  44. return passcodeController!
  45. }()
  46. override init() {
  47. super.init()
  48. NotificationCenter.default.addObserver(self, selector: #selector(appInForeground), name: UIApplication.didBecomeActiveNotification, object: nil)
  49. }
  50. @objc class func setPasscode(passcode: String?) throws {
  51. guard let passcode = passcode else {
  52. do {
  53. try XKKeychainGenericPasswordItem.removeItems(forService: passcodeService)
  54. } catch let error {
  55. throw error
  56. }
  57. return
  58. }
  59. let keychainItem = XKKeychainGenericPasswordItem()
  60. keychainItem.service = passcodeService
  61. keychainItem.account = passcodeService
  62. keychainItem.secret.stringValue = passcode
  63. do {
  64. try keychainItem.save()
  65. } catch let error {
  66. throw error
  67. }
  68. }
  69. @objc func validatePasscode(completion: @escaping () -> Void) {
  70. passcodeLockController.passcode = passcodeFromKeychain()
  71. self.completion = completion
  72. guard let rootViewController = UIApplication.shared.delegate?.window??.rootViewController, passcodeLockController.passcode != "" else {
  73. self.completion?()
  74. self.completion = nil
  75. return
  76. }
  77. //if we have no video displayed we should use the current rootViewController
  78. var presentingViewController = rootViewController
  79. if let presentedViewController = rootViewController.presentedViewController {
  80. //but if a video is playing we have the MovieViewController presented and want to show it above
  81. presentingViewController = presentedViewController
  82. }
  83. let navigationController = UINavigationController(rootViewController: passcodeLockController)
  84. navigationController.modalPresentationStyle = .fullScreen
  85. navigationController.modalTransitionStyle = .crossDissolve
  86. presentingViewController.present(navigationController, animated: true) {
  87. [weak self] in
  88. if self?.touchIDEnabled == true || self?.faceIDEnabled == true {
  89. self?.touchOrFaceIDQuery()
  90. }
  91. }
  92. }
  93. @objc private func appInForeground(notification: Notification) {
  94. if let navigationController = UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController as? UINavigationController, navigationController.topViewController is PAPasscodeViewController,
  95. touchIDEnabled || faceIDEnabled {
  96. touchOrFaceIDQuery()
  97. }
  98. }
  99. private func touchOrFaceIDQuery() {
  100. if avoidPromptingTouchOrFaceID || UIApplication.shared.applicationState != .active {
  101. return
  102. }
  103. let laContext = LAContext()
  104. if laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) {
  105. avoidPromptingTouchOrFaceID = true
  106. laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics,
  107. localizedReason: NSLocalizedString("BIOMETRIC_UNLOCK", comment: ""),
  108. reply: { [weak self] success, _ in
  109. DispatchQueue.main.async {
  110. if success {
  111. UIApplication.shared.delegate?.window??.rootViewController?.dismiss(animated: true, completion: {
  112. self?.completion?()
  113. self?.completion = nil
  114. self?.avoidPromptingTouchOrFaceID = false
  115. })
  116. } else {
  117. // user hit cancel and wants to enter the passcode
  118. self?.avoidPromptingTouchOrFaceID = true
  119. }
  120. }
  121. })
  122. }
  123. }
  124. private func passcodeFromKeychain() -> String {
  125. do {
  126. let item = try XKKeychainGenericPasswordItem(forService: KeychainCoordinator.passcodeService, account: KeychainCoordinator.passcodeService)
  127. return item.secret.stringValue
  128. } catch let error {
  129. assert(false, "Couldn't retrieve item from Keychain! If passcodeLockEnabled we should have an item and secret. Error was \(error)")
  130. return ""
  131. }
  132. }
  133. // MARK: PAPassCodeDelegate
  134. func paPasscodeViewControllerDidEnterPasscode(_ controller: PAPasscodeViewController!) {
  135. avoidPromptingTouchOrFaceID = false
  136. if let navigationController = UIApplication.shared.delegate?.window??.rootViewController?.presentedViewController as? UINavigationController,
  137. let passcodeController = navigationController.topViewController?.presentedViewController as? PAPasscodeViewController ??
  138. navigationController.topViewController {
  139. //either dismiss the papasscode presented from movieVC or as topViewController
  140. passcodeController.dismiss(animated: true, completion: {
  141. [weak self] in
  142. self?.completion?()
  143. self?.completion = nil
  144. })
  145. }
  146. }
  147. }