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.

162 lines
4.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // BiometricAuthenticationViewController.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. enum BiometricAuthenticationPolicy {
  11. case deviceOwnerAuthentication
  12. case deviceOwnerAuthenticationWithBiometrics
  13. }
  14. enum BiometricAuthenticationError: Error {
  15. case authenticationFailed
  16. case userCancel
  17. case userFallback
  18. case biometryNotAvailable
  19. case biometryNotEnrolled
  20. case biometryLockout
  21. case notBeConfigured
  22. var message: String {
  23. let msg: String
  24. switch self {
  25. case .authenticationFailed:
  26. msg = "There was a problem verifying your identity."
  27. case .userCancel:
  28. msg = "You pressed cancel."
  29. case .userFallback:
  30. msg = "You pressed password."
  31. case .biometryNotAvailable:
  32. msg = "FaceID & TouchID is not available."
  33. case .biometryNotEnrolled:
  34. let path = "(Setting -> FaceID(TouchID) & Passcode"
  35. msg = "FaceID & TouchID is not set up.\nCheck your device setting.\n\(path)"
  36. case .biometryLockout:
  37. msg = "FaceID & TouchID is locked."
  38. default:
  39. msg = "FaceID & TouchID may not be configured"
  40. }
  41. return msg
  42. }
  43. }
  44. protocol BiometricAuthenticationViewControllerDelegate: class {
  45. func doSelectLocalAuthenticationPolicy(
  46. _ viewController: BiometricAuthenticationViewController
  47. ) -> BiometricAuthenticationPolicy
  48. func viewController(
  49. _ viewController: BiometricAuthenticationViewController,
  50. informationTitleLabel titleLabel: UILabel,
  51. authenticationButton button: UIButton
  52. )
  53. func didComplete(_ viewController: BiometricAuthenticationViewController)
  54. func viewController(
  55. _ viewController: BiometricAuthenticationViewController,
  56. didFailWithError error: BiometricAuthenticationError,
  57. errorMessage: String?
  58. )
  59. }
  60. extension BiometricAuthenticationViewControllerDelegate {
  61. func doSelectLocalAuthenticationPolicy(
  62. _ viewController: BiometricAuthenticationViewController
  63. ) -> BiometricAuthenticationPolicy {
  64. return .deviceOwnerAuthenticationWithBiometrics
  65. }
  66. }
  67. final class BiometricAuthenticationViewController: UIViewController, BiometricAuthenticationViewInterface {
  68. // MARK: Properties
  69. var presenter: BiometricAuthenticationModuleInterface?
  70. weak var delegate: BiometricAuthenticationViewControllerDelegate?
  71. private var isViewDidLoad = true
  72. private var buttonTitle: String = "bio_press_button_title_text".localized() {
  73. didSet {
  74. biometricAuthenticationImageButton.setTitle(buttonTitle, for: .normal)
  75. }
  76. }
  77. // MARK: IBOutlets
  78. @IBOutlet private weak var biometricAuthenticationImageButton: UIButton!
  79. @IBOutlet private weak var informationLabel: UILabel!
  80. @IBOutlet private weak var logoImageView: UIImageView!
  81. // MARK: VC's Life cycle
  82. override func viewDidLoad() {
  83. super.viewDidLoad()
  84. self.setup()
  85. }
  86. override func viewDidAppear(_ animated: Bool) {
  87. super.viewDidAppear(animated)
  88. if self.isViewDidLoad {
  89. self.showAuthentication()
  90. self.isViewDidLoad = false
  91. }
  92. }
  93. // MARK: IBActions
  94. @IBAction private func authenticationButtonTouch(_ sender: Any) {
  95. self.showAuthentication()
  96. }
  97. // MARK: Other Functions
  98. private func setup() {
  99. // all setup should be done here
  100. self.setUI()
  101. self.delegate?.viewController(
  102. self,
  103. informationTitleLabel: self.informationLabel,
  104. authenticationButton: self.biometricAuthenticationImageButton
  105. )
  106. }
  107. private func setUI() {
  108. logoImageView?.image = logoImageView.image?.withRenderingMode(.alwaysTemplate)
  109. logoImageView.tintColor = .themeWhiteRed
  110. biometricAuthenticationImageButton.layer.cornerRadius = 5
  111. biometricAuthenticationImageButton.setTitle(buttonTitle, for: .normal)
  112. biometricAuthenticationImageButton.backgroundColor = .themeBlue
  113. }
  114. private func showAuthentication() {
  115. #if DEBUG
  116. DispatchQueue.main.async {
  117. self.delegate?.didComplete(self)
  118. }
  119. #else
  120. guard
  121. let isUseBiometricAuth = KeyChain.shared.get(key: .biometricAuth),
  122. isUseBiometricAuth == "1" else {
  123. let error = BiometricAuthenticationError.biometryNotAvailable
  124. self.delegate?.viewController(
  125. self,
  126. didFailWithError: error,
  127. errorMessage: error.message
  128. )
  129. return
  130. }
  131. self.presenter?.showBiometricAuthentication()
  132. #endif
  133. }
  134. }