SPB Git

Repository Public

AutoFillVault.swift 2.9 KB 56 lines swift

1// SP Vault AutoFill — offline, read-only access to the vault from the extension.2// One LAContext (biometrics or device passcode) covers both device keys; the SQLite store is opened read-only and3// closed again; no network request is ever made from this process.4import Foundation5import LocalAuthentication6import SharedVaultKit78/// What the extension holds after an unlock. Plaintext: dropped when the request ends (the process is short-lived).9struct UnlockedVault: Sendable {10    /// Items of the vault (not in the trash) plus the ones still waiting in the pending queue.11    let items: [ItemView]12    let identity: DeviceIdentity13    /// X9.63 public key of this device's encryption key (new items are sealed to it, see PendingItems.swift).14    let deviceEncPub: Data15}1617enum AutoFillVault {18    /// True when the user dismissed the system prompt (not an error to report).19    static func isUserCancel(_ error: Error) -> Bool {20        if error is CancellationError { return true }21        if let e = error as? LAError { return [.userCancel, .appCancel, .systemCancel].contains(e.code) }22        return false23    }2425    /// Asks for user presence once, then decrypts the local copy of the vault. Dev builds (software keys) never prompt.26    nonisolated static func unlock(reason: String) async throws -> UnlockedVault {27        let keyStore: any DeviceKeyStore28        do { keyStore = try AppEnvironment.makeKeyStore() } catch { throw AutoFillError.notEnrolled }29        guard let identity = try? keyStore.loadIdentity(), identity.arkEnvelope != nil else { throw AutoFillError.notEnrolled }30        #if SPV_DEV31        let ctx: LAContext? = nil32        #else33        let ctx: LAContext? = LAContext()34        if let ctx {35            ctx.touchIDAuthenticationAllowableReuseDuration = 1036            ctx.localizedCancelTitle = "Cancel"37            try await ctx.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason)38        }39        #endif40        let store: SQLiteStore41        do { store = try AppEnvironment.makeStore(readOnly: true) } catch { throw AutoFillError.notEnrolled }42        defer { store.close() }43        let session: VaultReader.Session44        do { session = try VaultReader(store: store, keyStore: keyStore).unlock(context: ctx) }45        catch KeyStoreError.keysInvalidated { throw AutoFillError.keysInvalidated }46        catch EngineError.notEnrolled, EngineError.noCachedEnvelope { throw AutoFillError.notEnrolled }47        let keys: DeviceKeys48        do {49            guard let k = try keyStore.loadKeys(context: ctx) else { throw AutoFillError.notEnrolled }50            keys = k51        } catch let e as AutoFillError { throw e } catch { throw AutoFillError.keysInvalidated }52        let pending = (try? PendingItems())?.load(identity: identity, keys: keys) ?? []53        return UnlockedVault(items: AutoFillCore.merge(items: session.items(), pending: pending), identity: identity, deviceEncPub: keys.enc.publicKey)54    }55}56
Files main
Menu

Theme