taler-ios

iOS apps for GNU Taler (wallet)
Log | Files | Refs | README | LICENSE

commit 0a36ba7163bb40172df42437741f0a685d9e68b8
parent b1674fb5a3f902de1363564823fea3a551855ff4
Author: Marc Stibane <marc@taler.net>
Date:   Sat,  5 Jul 2025 11:16:28 +0200

authentication only if available

Diffstat:
MTalerWallet1/Controllers/BiometricService.swift | 42+++++++++++++++++++++++++-----------------
MTalerWallet1/Views/Main/MainView.swift | 4++++
2 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/TalerWallet1/Controllers/BiometricService.swift b/TalerWallet1/Controllers/BiometricService.swift @@ -11,31 +11,39 @@ import LocalAuthentication class BiometricService: ObservableObject { static let shared = BiometricService() + @Published var canAuthenticate: Bool = true @Published var isAuthenticated = false @Published var authenticationError: String? + @AppStorage("useAuthentication") var useAuthentication: Bool = false + private var context: LAContext? func authenticateUser() { let reason = resetContext() - guard let context = context else { - authenticationError = String(localized: "Failed to initialize authentication context.", comment: "FaceID") - return - } - guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) else { - authenticationError = String(localized: "Authentication is not available on this device.", comment: "FaceID") - return - } - context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, authenticationError in - DispatchQueue.main.async { - if success { - self?.isAuthenticated = true - self?.authenticationError = nil - } else if let error = authenticationError as? LAError { - self?.authenticationError = self?.errorMessage(for: error) - } else { - self?.authenticationError = String(localized: "Unknown authentication error occurred.", comment: "FaceID") + if useAuthentication { + guard let context = context else { + authenticationError = String(localized: "Failed to initialize authentication context.", comment: "FaceID") + canAuthenticate = false + return + } + guard context.canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) else { + authenticationError = String(localized: "Authentication is not available on this device.", comment: "FaceID") + canAuthenticate = false + return + } + canAuthenticate = true + context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason) { [weak self] success, authenticationError in + DispatchQueue.main.async { + if success { + self?.isAuthenticated = true + self?.authenticationError = nil + } else if let error = authenticationError as? LAError { + self?.authenticationError = self?.errorMessage(for: error) + } else { + self?.authenticationError = String(localized: "Unknown authentication error occurred.", comment: "FaceID") + } } } } diff --git a/TalerWallet1/Views/Main/MainView.swift b/TalerWallet1/Views/Main/MainView.swift @@ -122,6 +122,10 @@ struct MainView: View { .onAppear { DispatchQueue.main.asyncAfter(deadline: .now() + 3) { biometricService.authenticationError = nil + if !biometricService.canAuthenticate { + let _ = print("authentication not available") + useAuthentication = false // switch off + } } } } else {