You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.6 KiB

  1. //
  2. // BiometricAuthenticationWireframe.swift
  3. // GME Remit
  4. //
  5. // Created by InKwon Kim on 30/03/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import LocalAuthentication
  10. class BiometricAuthenticationWireframe {
  11. weak var view: UIViewController!
  12. private var policy: LAPolicy = .deviceOwnerAuthenticationWithBiometrics
  13. private weak var delegate: BiometricAuthenticationViewControllerDelegate?
  14. }
  15. extension BiometricAuthenticationWireframe: BiometricAuthenticationWireframeInput {
  16. var storyboardName: String {return "BiometricAuthentication"}
  17. func getMainView() -> UIViewController {
  18. let service = BiometricAuthenticationService()
  19. let interactor = BiometricAuthenticationInteractor(service: service)
  20. let presenter = BiometricAuthenticationPresenter()
  21. let viewController = viewControllerFromStoryboard(of: BiometricAuthenticationViewController.self)
  22. viewController.presenter = presenter
  23. interactor.output = presenter
  24. presenter.interactor = interactor
  25. presenter.wireframe = self
  26. presenter.view = viewController
  27. self.view = viewController
  28. return viewController
  29. }
  30. func showBiometricAuthentication(completion: ((BiometricAuthenticationError?) -> Void)?) {
  31. let context = LAContext()
  32. var error: NSError?
  33. let msg = "bio_message_title_text".localized()
  34. let vc = self.view as? BiometricAuthenticationViewController ?? BiometricAuthenticationViewController()
  35. if let bioPolicy = delegate?.doSelectLocalAuthenticationPolicy(vc) {
  36. switch bioPolicy {
  37. case .deviceOwnerAuthentication:
  38. self.policy = .deviceOwnerAuthentication
  39. case .deviceOwnerAuthenticationWithBiometrics:
  40. self.policy = .deviceOwnerAuthenticationWithBiometrics
  41. }
  42. }
  43. if context.canEvaluatePolicy(self.policy, error: &error) {
  44. context.evaluatePolicy(self.policy, localizedReason: msg, reply: { (success, err) in
  45. if success {
  46. DispatchQueue.main.async {
  47. completion?(nil)
  48. self.delegate?.didComplete(vc)
  49. }
  50. } else {
  51. DispatchQueue.main.async {
  52. let bioError = self.setBiometricError(error: err)
  53. completion?(bioError)
  54. self.delegate?.viewController(vc, didFailWithError: bioError, errorMessage: bioError.message)
  55. }
  56. }
  57. })
  58. } else {
  59. KeyChain.shared.save(data: "0", key: .biometricAuth)
  60. DispatchQueue.main.async {
  61. let bioError = self.setBiometricError(error: error)
  62. completion?(bioError)
  63. self.delegate?.viewController(
  64. vc,
  65. didFailWithError: bioError,
  66. errorMessage: bioError.localizedDescription
  67. )
  68. }
  69. }
  70. }
  71. func openWithDelegate(
  72. on window: UIWindow,
  73. delegate: BiometricAuthenticationViewControllerDelegate
  74. ) {
  75. let vc = getMainView()
  76. self.delegate = delegate
  77. (vc as? BiometricAuthenticationViewController)?.delegate = delegate
  78. window.rootViewController = vc
  79. }
  80. func openWithDelegate(
  81. on viewConroller: UIViewController,
  82. delegate: BiometricAuthenticationViewControllerDelegate
  83. ) {
  84. let vc = getMainView()
  85. self.delegate = delegate
  86. (vc as? BiometricAuthenticationViewController)?.delegate = delegate
  87. viewConroller.present(vc, animated: true, completion: nil)
  88. }
  89. }
  90. extension BiometricAuthenticationWireframe {
  91. private func setBiometricError(error: Error?) -> BiometricAuthenticationError {
  92. let bioError: BiometricAuthenticationError
  93. if #available(iOS 11.0, *) {
  94. switch error {
  95. case LAError.authenticationFailed?:
  96. bioError = .authenticationFailed
  97. case LAError.userCancel?:
  98. bioError = .userCancel
  99. case LAError.userFallback?:
  100. bioError = .userFallback
  101. case LAError.biometryNotAvailable?:
  102. bioError = .biometryNotAvailable
  103. case LAError.biometryNotEnrolled?:
  104. bioError = .biometryNotEnrolled
  105. case LAError.biometryLockout?:
  106. bioError = .biometryLockout
  107. default:
  108. bioError = .notBeConfigured
  109. }
  110. } else {
  111. switch error {
  112. case LAError.authenticationFailed?:
  113. bioError = .authenticationFailed
  114. case LAError.userCancel?:
  115. bioError = .userCancel
  116. case LAError.userFallback?:
  117. bioError = .userFallback
  118. case LAError.touchIDNotAvailable?:
  119. bioError = .biometryNotAvailable
  120. case LAError.touchIDNotEnrolled?:
  121. bioError = .biometryNotEnrolled
  122. case LAError.touchIDLockout?:
  123. bioError = .biometryLockout
  124. default:
  125. bioError = .notBeConfigured
  126. }
  127. }
  128. return bioError
  129. }
  130. }