NCPasscode.swift 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // NCPasscode.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/02/24.
  6. // Copyright © 2024 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import LocalAuthentication
  25. public protocol NCPasscodeDelegate: AnyObject {
  26. func evaluatePolicy(_ passcodeViewController: TOPasscodeViewController, isCorrectCode: Bool)
  27. func passcodeReset(_ passcodeViewController: TOPasscodeViewController)
  28. func requestedAccount(controller: UIViewController?)
  29. }
  30. // optional func
  31. public extension NCPasscodeDelegate {
  32. func evaluatePolicy(_ passcodeViewController: TOPasscodeViewController, isCorrectCode: Bool) {}
  33. func passcodeReset() {}
  34. func requestedAccount(controller: UIViewController?) {}
  35. }
  36. class NCPasscode: NSObject, TOPasscodeViewControllerDelegate {
  37. public static let shared: NCPasscode = {
  38. let instance = NCPasscode()
  39. return instance
  40. }()
  41. var isPasscodeReset: Bool {
  42. let passcodeCounterFailReset = NCKeychain().passcodeCounterFailReset
  43. return NCKeychain().resetAppCounterFail && passcodeCounterFailReset >= NCBrandOptions.shared.resetAppPasscodeAttempts
  44. }
  45. var isPasscodeCounterFail: Bool {
  46. let passcodeCounterFail = NCKeychain().passcodeCounterFail
  47. return passcodeCounterFail > 0 && passcodeCounterFail.isMultiple(of: 3)
  48. }
  49. var passcodeViewController: TOPasscodeViewController!
  50. var delegate: NCPasscodeDelegate?
  51. var viewController: UIViewController?
  52. func presentPasscode(viewController: UIViewController, delegate: NCPasscodeDelegate?, completion: @escaping () -> Void) {
  53. var error: NSError?
  54. self.delegate = delegate
  55. self.viewController = viewController
  56. passcodeViewController = TOPasscodeViewController(passcodeType: .sixDigits, allowCancel: false)
  57. passcodeViewController.delegate = self
  58. passcodeViewController.keypadButtonShowLettering = false
  59. if NCKeychain().touchFaceID, LAContext().canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &error) {
  60. if error == nil {
  61. if LAContext().biometryType == .faceID {
  62. passcodeViewController.biometryType = .faceID
  63. } else if LAContext().biometryType == .touchID {
  64. passcodeViewController.biometryType = .touchID
  65. }
  66. passcodeViewController.allowBiometricValidation = true
  67. passcodeViewController.automaticallyPromptForBiometricValidation = false
  68. }
  69. }
  70. viewController.presentedViewController?.dismiss(animated: false)
  71. viewController.present(passcodeViewController, animated: true, completion: {
  72. self.openAlert(passcodeViewController: self.passcodeViewController)
  73. completion()
  74. })
  75. }
  76. func enableTouchFaceID() {
  77. guard NCKeychain().touchFaceID,
  78. NCKeychain().presentPasscode,
  79. !isPasscodeCounterFail,
  80. let passcodeViewController
  81. else { return }
  82. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  83. LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: NCBrandOptions.shared.brand) { success, evaluateError in
  84. if success {
  85. DispatchQueue.main.async {
  86. passcodeViewController.dismiss(animated: true) {
  87. NCKeychain().passcodeCounterFail = 0
  88. NCKeychain().passcodeCounterFailReset = 0
  89. self.delegate?.evaluatePolicy(passcodeViewController, isCorrectCode: true)
  90. if NCKeychain().accountRequest {
  91. self.delegate?.requestedAccount(controller: self.viewController)
  92. }
  93. }
  94. }
  95. } else {
  96. if let error = evaluateError {
  97. switch error._code {
  98. case LAError.userFallback.rawValue, LAError.authenticationFailed.rawValue:
  99. if LAContext().biometryType == .faceID {
  100. NCKeychain().passcodeCounterFail = 2
  101. NCKeychain().passcodeCounterFailReset += 2
  102. } else {
  103. NCKeychain().passcodeCounterFail = 3
  104. NCKeychain().passcodeCounterFailReset += 3
  105. }
  106. self.openAlert(passcodeViewController: passcodeViewController)
  107. case LAError.biometryLockout.rawValue:
  108. LAContext().evaluatePolicy(LAPolicy.deviceOwnerAuthentication, localizedReason: NSLocalizedString("_deviceOwnerAuthentication_", comment: ""), reply: { success, _ in
  109. if success {
  110. DispatchQueue.main.async {
  111. NCKeychain().passcodeCounterFail = 0
  112. self.enableTouchFaceID()
  113. }
  114. }
  115. })
  116. case LAError.userCancel.rawValue:
  117. NCKeychain().passcodeCounterFail += 1
  118. NCKeychain().passcodeCounterFailReset += 1
  119. default:
  120. break
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. func didInputCorrectPasscode(in passcodeViewController: TOPasscodeViewController) {
  128. DispatchQueue.main.async {
  129. passcodeViewController.dismiss(animated: true) {
  130. NCKeychain().passcodeCounterFail = 0
  131. NCKeychain().passcodeCounterFailReset = 0
  132. if NCKeychain().accountRequest {
  133. self.delegate?.requestedAccount(controller: self.viewController)
  134. }
  135. }
  136. }
  137. }
  138. func passcodeViewController(_ passcodeViewController: TOPasscodeViewController, isCorrectCode code: String) -> Bool {
  139. if code == NCKeychain().passcode {
  140. self.delegate?.evaluatePolicy(passcodeViewController, isCorrectCode: true)
  141. return true
  142. } else {
  143. NCKeychain().passcodeCounterFail += 1
  144. NCKeychain().passcodeCounterFailReset += 1
  145. openAlert(passcodeViewController: passcodeViewController)
  146. self.delegate?.evaluatePolicy(passcodeViewController, isCorrectCode: false)
  147. return false
  148. }
  149. }
  150. func didPerformBiometricValidationRequest(in passcodeViewController: TOPasscodeViewController) {
  151. enableTouchFaceID()
  152. }
  153. func openAlert(passcodeViewController: TOPasscodeViewController) {
  154. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  155. if self.isPasscodeReset {
  156. passcodeViewController.setContentHidden(true, animated: true)
  157. let alertController = UIAlertController(title: NSLocalizedString("_reset_wrong_passcode_", comment: ""), message: nil, preferredStyle: .alert)
  158. passcodeViewController.present(alertController, animated: true, completion: { })
  159. self.delegate?.passcodeReset()
  160. } else if self.isPasscodeCounterFail {
  161. passcodeViewController.setContentHidden(true, animated: true)
  162. let alertController = UIAlertController(title: NSLocalizedString("_passcode_counter_fail_", comment: ""), message: nil, preferredStyle: .alert)
  163. passcodeViewController.present(alertController, animated: true, completion: { })
  164. var seconds = NCBrandOptions.shared.passcodeSecondsFail
  165. _ = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
  166. alertController.message = "\(seconds) " + NSLocalizedString("_seconds_", comment: "")
  167. seconds -= 1
  168. if seconds < 0 {
  169. timer.invalidate()
  170. alertController.dismiss(animated: true)
  171. passcodeViewController.setContentHidden(false, animated: true)
  172. NCKeychain().passcodeCounterFail = 0
  173. self.enableTouchFaceID()
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }