SPB Git

Repository Public

AppLock.swift 2.9 KB 54 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Security/AppLock.swift3// Description: Optional local lock of the app's window (Touch ID or the Mac's password, LocalAuthentication). It hides the screens; it authorizes NOTHING on the server: every operation still needs a passkey assertion bound to its intent.4import Foundation5import LocalAuthentication67@MainActor @Observable final class AppLock {8    static let enabledKey = "appLockEnabled", delayKey = "appLockDelaySeconds"9    private(set) var locked: Bool10    private(set) var lastError: String?11    private var resignedAt: Date?1213    var enabled: Bool { UserDefaults.standard.bool(forKey: Self.enabledKey) }14    var delay: TimeInterval { let v = UserDefaults.standard.double(forKey: Self.delayKey); return v > 0 ? v : 60 }1516    init() { locked = UserDefaults.standard.bool(forKey: Self.enabledKey) }1718    /// True when this Mac can verify its owner (Touch ID, Apple Watch or the account password).19    static var available: Bool { LAContext().canEvaluatePolicy(.deviceOwnerAuthentication, error: nil) }20    static var biometryName: String {21        let c = LAContext(); _ = c.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: nil)22        switch c.biometryType { case .touchID: return "Touch ID"; case .faceID: return "Face ID"; case .opticID: return "Optic ID"; default: return "le mot de passe du Mac" }23    }2425    func appDidResign() { resignedAt = Date() }26    func appDidBecomeActive() {27        guard enabled, !locked, let r = resignedAt else { return }28        if Date().timeIntervalSince(r) >= delay { locked = true }29    }30    func lockNow() { if enabled { locked = true } }3132    /// Offline consultation without a session: the Mac's owner is verified first, whatever the lock setting.33    private(set) var browseDenied = false34    func unlockForBrowse() async {35        let c = LAContext()36        browseDenied = (try? await c.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "consulter les dernières données de SP Wallet")) != true37    }3839    func unlock() async {40        let c = LAContext(); c.localizedCancelTitle = "Annuler"41        do {42            if try await c.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: "déverrouiller SP Wallet") { locked = false; lastError = nil; resignedAt = nil }43        } catch let e as LAError where e.code == .userCancel || e.code == .appCancel || e.code == .systemCancel { lastError = nil }44        catch { lastError = error.localizedDescription }45    }4647    /// Turning the lock on or off itself requires the owner of the Mac.48    func setEnabled(_ on: Bool) async -> Bool {49        let c = LAContext()50        guard (try? await c.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: on ? "activer le verrouillage de SP Wallet" : "désactiver le verrouillage de SP Wallet")) == true else { return false }51        UserDefaults.standard.set(on, forKey: Self.enabledKey); return true52    }53}54
Files main
Menu

Theme