mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-18 23:48:29 +02:00
Introduce BiometricsAuthenticationPresenter
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Copyright 2020 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import LocalAuthentication
|
||||
|
||||
enum BiometricsAuthenticationPresenterError: Error {
|
||||
case unknown
|
||||
}
|
||||
|
||||
/// Presenter for user authentication with biometry.
|
||||
final class BiometricsAuthenticationPresenter {
|
||||
|
||||
/// Whether the presenter currently showing the biometrics setup or unlock dialog.
|
||||
/// Showing biometrics dialog will cause the app to resign active.
|
||||
/// This property can be used in order to distinguish real resignations and biometrics case.
|
||||
static private(set) var isPresentingBiometricsAuth: Bool = false
|
||||
|
||||
/// Presents the user authentication with biometry.
|
||||
/// - Parameters:
|
||||
/// - message: The app-provided reason for requesting authentication, which displays in the authentication dialog presented to the user.
|
||||
/// - completion: A closure that is executed when policy evaluation finishes. Will be called in main thread.
|
||||
func present(with message: String, completion: @escaping (Result<Void, Error>) -> Void) {
|
||||
// do not present if already presented
|
||||
guard !BiometricsAuthenticationPresenter.isPresentingBiometricsAuth else {
|
||||
return
|
||||
}
|
||||
BiometricsAuthenticationPresenter.isPresentingBiometricsAuth = true
|
||||
|
||||
LAContext().evaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, localizedReason: message) { (success, error) in
|
||||
if success {
|
||||
// Complete after a little delay
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
BiometricsAuthenticationPresenter.isPresentingBiometricsAuth = false
|
||||
completion(.success(Void()))
|
||||
}
|
||||
} else {
|
||||
let finalError: Error
|
||||
|
||||
if let error = error {
|
||||
finalError = error
|
||||
} else {
|
||||
finalError = BiometricsAuthenticationPresenterError.unknown
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
BiometricsAuthenticationPresenter.isPresentingBiometricsAuth = false
|
||||
completion(.failure(finalError))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,7 @@ final class SetupBiometricsViewModel: SetupBiometricsViewModelType {
|
||||
private let viewMode: SetPinCoordinatorViewMode
|
||||
private let pinCodePreferences: PinCodePreferences
|
||||
private let localAuthenticationService: LocalAuthenticationService
|
||||
private let biometricsAuthenticationPresenter: BiometricsAuthenticationPresenter
|
||||
|
||||
// MARK: Public
|
||||
|
||||
@@ -42,6 +43,7 @@ final class SetupBiometricsViewModel: SetupBiometricsViewModelType {
|
||||
self.viewMode = viewMode
|
||||
self.pinCodePreferences = pinCodePreferences
|
||||
self.localAuthenticationService = LocalAuthenticationService(pinCodePreferences: pinCodePreferences)
|
||||
self.biometricsAuthenticationPresenter = BiometricsAuthenticationPresenter()
|
||||
}
|
||||
|
||||
deinit {
|
||||
@@ -76,34 +78,26 @@ final class SetupBiometricsViewModel: SetupBiometricsViewModelType {
|
||||
// MARK: - Private
|
||||
|
||||
private func enableDisableBiometrics() {
|
||||
LocalAuthenticationService.isShowingBiometrics = true
|
||||
LAContext().evaluatePolicy(.deviceOwnerAuthentication, localizedReason: VectorL10n.biometricsUsageReason) { (success, error) in
|
||||
if success {
|
||||
// complete after a little delay
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
self.pinCodePreferences.canUseBiometricsToUnlock = nil
|
||||
self.pinCodePreferences.resetCounters()
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
|
||||
LocalAuthenticationService.isShowingBiometrics = false
|
||||
}
|
||||
} else {
|
||||
LocalAuthenticationService.isShowingBiometrics = false
|
||||
biometricsAuthenticationPresenter.present(with: VectorL10n.biometricsUsageReason) { (response) in
|
||||
switch response {
|
||||
case .success:
|
||||
self.pinCodePreferences.canUseBiometricsToUnlock = nil
|
||||
self.pinCodePreferences.resetCounters()
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
|
||||
case .failure:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func unlockWithBiometrics() {
|
||||
LocalAuthenticationService.isShowingBiometrics = true
|
||||
LAContext().evaluatePolicy(.deviceOwnerAuthentication, localizedReason: VectorL10n.biometricsUsageReason) { (success, error) in
|
||||
if success {
|
||||
// complete after a little delay
|
||||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
|
||||
self.pinCodePreferences.canUseBiometricsToUnlock = nil
|
||||
self.pinCodePreferences.resetCounters()
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
|
||||
LocalAuthenticationService.isShowingBiometrics = false
|
||||
}
|
||||
} else {
|
||||
biometricsAuthenticationPresenter.present(with: VectorL10n.biometricsUsageReason) { (response) in
|
||||
switch response {
|
||||
case .success:
|
||||
self.pinCodePreferences.canUseBiometricsToUnlock = nil
|
||||
self.pinCodePreferences.resetCounters()
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidComplete(self)
|
||||
case .failure(let error):
|
||||
guard let error = error as NSError? else {
|
||||
return
|
||||
}
|
||||
@@ -111,13 +105,9 @@ final class SetupBiometricsViewModel: SetupBiometricsViewModelType {
|
||||
if self.localAuthenticationService.shouldLogOutUser() {
|
||||
// biometrics can't be used until further unlock with pin or a new log in
|
||||
self.pinCodePreferences.canUseBiometricsToUnlock = false
|
||||
DispatchQueue.main.async {
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidCompleteWithReset(self, dueToTooManyErrors: true)
|
||||
LocalAuthenticationService.isShowingBiometrics = false
|
||||
}
|
||||
self.coordinatorDelegate?.setupBiometricsViewModelDidCompleteWithReset(self, dueToTooManyErrors: true)
|
||||
} else if error.code == LAError.Code.userCancel.rawValue || error.code == LAError.Code.userFallback.rawValue {
|
||||
self.userCancelledUnlockWithBiometrics()
|
||||
LocalAuthenticationService.isShowingBiometrics = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user