From f612914307aef71f26d0386aecc0f6344ed56359 Mon Sep 17 00:00:00 2001 From: Igor Kislyuk Date: Fri, 23 Mar 2018 16:37:03 +0300 Subject: [PATCH] Rename TouchID to biometrics --- .../View/BasePassCodeViewController.swift | 6 +---- .../ViewModel/BasePassCodeViewModel.swift | 27 +++++++++++++------ .../Services/BasePassCodeService.swift | 2 -- ...DService.swift => BiometricsService.swift} | 22 +++++++-------- 4 files changed, 29 insertions(+), 28 deletions(-) rename LeadKitAdditions/Sources/Services/{TouchIDService.swift => BiometricsService.swift} (74%) diff --git a/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift b/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift index b246456..5830e59 100644 --- a/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift +++ b/LeadKitAdditions/Sources/Controllers/PassCode/View/BasePassCodeViewController.swift @@ -154,11 +154,7 @@ open class BasePassCodeViewController: UIViewController, ConfigurableController return } - viewModel.touchIdService?.authenticateByTouchId(description: description) { [weak self] isSuccess in - if isSuccess { - self?.viewModel.authSucceed(.touchId) - } - } + viewModel.authenticateUsingBiometrics(with: description) } private func resetUI() { diff --git a/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift b/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift index 505c715..eedffb5 100644 --- a/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift +++ b/LeadKitAdditions/Sources/Controllers/PassCode/ViewModel/BasePassCodeViewModel.swift @@ -37,8 +37,8 @@ open class BasePassCodeViewModel: BaseViewModel { public let disposeBag = DisposeBag() - /// TouchId service, which can answer if user is authorized by finger - public let touchIdService: TouchIDService? + /// Service that can answer if user is authorized by biometrics + public let biometricsService = BiometricsService() /// Contains configuration for pass code operations public let passCodeConfiguration: PassCodeConfiguration @@ -59,13 +59,9 @@ open class BasePassCodeViewModel: BaseViewModel { private lazy var passCodeHolder: PassCodeHolderProtocol = PassCodeHolderBuilder.build(with: self.controllerType) - public init(controllerType: PassCodeControllerType, - passCodeConfiguration: PassCodeConfiguration, - touchIdService: TouchIDService? = nil) { - + public init(controllerType: PassCodeControllerType, passCodeConfiguration: PassCodeConfiguration) { self.controllerType = controllerType self.passCodeConfiguration = passCodeConfiguration - self.touchIdService = touchIdService bindViewModel() } @@ -125,6 +121,16 @@ open class BasePassCodeViewModel: BaseViewModel { passCodeHolder.reset() } + public func authenticateUsingBiometrics(with description: String) { + biometricsService.authenticateWithBiometrics(with: description) { [weak self] success, error in + if success { + self?.authSucceed(.touchId) + } else { + self?.authFailed(with: error) + } + } + } + // MARK: - HAVE TO OVERRIDE /// Override to check if entered pass code is equal to stored @@ -133,11 +139,16 @@ open class BasePassCodeViewModel: BaseViewModel { return false } - /// Handler called after successful authentication + /// Method is called after successful authentication open func authSucceed(_ type: PassCodeAuthType) { assertionFailure("You should override this method: authSucceed(_ type: PassCodeAuthType)") } + /// Called when authentication failed + open func authFailed(with: Error?) { + assertionFailure("You should override this method: authFailed(with: Error)") + } + // MARK: - Biometrics /// Posibility to use biometrics for authentication diff --git a/LeadKitAdditions/Sources/Services/BasePassCodeService.swift b/LeadKitAdditions/Sources/Services/BasePassCodeService.swift index b79d6ea..8686b1c 100644 --- a/LeadKitAdditions/Sources/Services/BasePassCodeService.swift +++ b/LeadKitAdditions/Sources/Services/BasePassCodeService.swift @@ -59,8 +59,6 @@ open class BasePassCodeService { return keychain[Keys.passCodeHash] } - - } public extension BasePassCodeService { diff --git a/LeadKitAdditions/Sources/Services/TouchIDService.swift b/LeadKitAdditions/Sources/Services/BiometricsService.swift similarity index 74% rename from LeadKitAdditions/Sources/Services/TouchIDService.swift rename to LeadKitAdditions/Sources/Services/BiometricsService.swift index 69e6325..925cb61 100644 --- a/LeadKitAdditions/Sources/Services/TouchIDService.swift +++ b/LeadKitAdditions/Sources/Services/BiometricsService.swift @@ -22,34 +22,30 @@ import LocalAuthentication -public typealias TouchIDServiceAuthHandler = (Bool) -> Void +public typealias BiometricsAuthHandler = (Bool, Error?) -> Void -/// Represents service that provides access to authentication via touch id -public class TouchIDService { +/// Service that provide access to authentication via biometric +public final class BiometricsService { - private lazy var laContext: LAContext = { - return LAContext() - }() + private lazy var laContext = LAContext() public init() {} /// Indicates is it possible to authenticate on this device via touch id - public var canAuthenticateByTouchId: Bool { + public var canAuthenticateWithBiometrics: Bool { return laContext.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil) } /** - Initiates system touch id authentication process + Initiates system biometrics authentication process - parameters: - description: prompt on the system alert that describes what for user should attach finger to device - authHandler: callback, with parameter, indicates if user authenticate successfuly */ - public func authenticateByTouchId(description: String, authHandler: @escaping TouchIDServiceAuthHandler) { - laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, - localizedReason: description) { success, _ in - - authHandler(success) + public func authenticateWithBiometrics(with description: String, authHandler: @escaping BiometricsAuthHandler) { + laContext.evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: description) { success, error in + authHandler(success, error) } }