SPB Git

Repository Public License

AppLock.swift 3.3 KB 88 lines swift

1// ORIX Secure2// File: App/Sources/Core/AppLock.swift3// Description: Optional App Lock: Face ID, Touch ID or the device passcode when the app opens or returns from the background.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import LocalAuthentication8import ORIXDesign9import SwiftUI1011@Observable final class AppLock {12    private let settings: AppSettings13    private(set) var isLocked: Bool14    private(set) var lastError: String?15    private var isAuthenticating = false1617    init(settings: AppSettings) {18        self.settings = settings19        isLocked = settings.appLockEnabled20    }2122    /// Whether the device can authenticate its owner at all (a passcode is set).23    static var isAvailable: Bool { LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) }2425    func lockIfEnabled() {26        if settings.appLockEnabled { isLocked = true }27    }2829    func unlock() async {30        guard isLocked, !isAuthenticating else { return }31        isAuthenticating = true32        defer { isAuthenticating = false }33        let context = LAContext()34        do {35            // Biometrics first, passcode as the fallback: biometrics are never the only way in.36            let ok = try await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: String(localized: "Unlock ORIX Secure"))37            if ok {38                isLocked = false39                lastError = nil40            }41        } catch {42            lastError = error.localizedDescription43        }44    }4546    /// Turning the lock on requires proving that authentication works, so the user cannot lock themselves out.47    func enable() async -> Bool {48        let context = LAContext()49        guard (try? await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: String(localized: "Turn on App Lock"))) == true else { return false }50        settings.appLockEnabled = true51        return true52    }5354    /// Turning the lock off also requires authentication: an unlocked phone left on a table must not be enough.55    func disable() async -> Bool {56        let context = LAContext()57        guard (try? await context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: String(localized: "Turn off App Lock"))) == true else { return false }58        forceDisable()59        return true60    }6162    private func forceDisable() {63        settings.appLockEnabled = false64        isLocked = false65    }66}6768struct LockScreen: View {69    let lock: AppLock70    var body: some View {71        VStack(spacing: ORIX.Space.lg) {72            ORIXMark().frame(width: 96, height: 96)73            Text("ORIX Secure is locked").font(.orixTitle).foregroundStyle(ORIX.Palette.textPrimary)74            if let error = lock.lastError {75                Text(verbatim: error).font(.orixCallout).foregroundStyle(ORIX.Palette.textSecondary).multilineTextAlignment(.center)76            }77            Button { Task { await lock.unlock() } } label: { Label("Unlock", systemImage: "lock.open") }78                .buttonStyle(.orixPrimary)79                .frame(maxWidth: 280)80                .accessibilityIdentifier("lock.unlock")81        }82        .padding(ORIX.Space.xl)83        .frame(maxWidth: .infinity, maxHeight: .infinity)84        .background(ORIX.Palette.canvas.ignoresSafeArea())85        .task { await lock.unlock() }86    }87}88
Files main
Menu

Theme