SPB Git

Repository Public

BiometricLock.swift 2.9 KB 84 lines swift

1// SP Drive iOS2// File: App/Security/BiometricLock.swift3// Description: Optional app lock with Face ID / Touch ID (device passcode as fallback). Biometric data never leaves the Secure Enclave.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import LocalAuthentication9import Observation1011@MainActor @Observable12final class BiometricLock {13    enum Kind { case faceID, touchID, opticID, passcode, none }1415    private(set) var isLocked: Bool16    private(set) var lastError: String?17    var isEnabled: Bool {18        didSet { UserDefaults.standard.set(isEnabled, forKey: Self.key); if !isEnabled { isLocked = false } }19    }20    /// Seconds in the background after which the lock engages again.21    var gracePeriod: TimeInterval = 6022    private var backgroundedAt: Date?23    private static let key = "lock.enabled"2425    init() {26        let enabled = UserDefaults.standard.bool(forKey: Self.key)27        isEnabled = enabled28        isLocked = enabled29    }3031    var kind: Kind {32        let ctx = LAContext()33        var err: NSError?34        if ctx.canEvaluatePolicy(.deviceOwnerAuthenticationWithBiometrics, error: &err) {35            switch ctx.biometryType {36            case .faceID: return .faceID37            case .touchID: return .touchID38            case .opticID: return .opticID39            default: return .passcode40            }41        }42        return ctx.canEvaluatePolicy(.deviceOwnerAuthentication, error: &err) ? .passcode : .none43    }4445    var kindLabel: String {46        switch kind {47        case .faceID: "Face ID"48        case .touchID: "Touch ID"49        case .opticID: "Optic ID"50        case .passcode: "Code de l’appareil"51        case .none: "Indisponible"52        }53    }5455    func didEnterBackground() { backgroundedAt = .now }5657    func willEnterForeground() {58        guard isEnabled, let t = backgroundedAt else { return }59        if Date.now.timeIntervalSince(t) > gracePeriod { isLocked = true }60        backgroundedAt = nil61    }6263    /// Asks the system to verify the owner. Returns true when verified.64    @discardableResult65    func unlock(reason: String = "Déverrouiller SP Drive") async -> Bool {66        let ctx = LAContext()67        ctx.localizedCancelTitle = "Annuler"68        do {69            let ok = try await ctx.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason)70            if ok { isLocked = false; lastError = nil }71            return ok72        } catch {73            lastError = (error as? LAError)?.code == .userCancel ? nil : error.localizedDescription74            return false75        }76    }7778    /// Turning the lock on requires proving ownership first, so nobody can lock the owner out or in.79    func setEnabled(_ on: Bool) async {80        guard on != isEnabled else { return }81        if await unlock(reason: on ? "Activer le verrouillage de SP Drive" : "Désactiver le verrouillage de SP Drive") { isEnabled = on }82    }83}84
Files main
Menu

Theme