Repository Public
1//2// VQuant iOS3// File: VQuant/Auth/Keychain.swift4// Description: Keychain Services wrapper; Keychain-backed stores of the device session token and of the cache key.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import CryptoKit11import Foundation12import Security1314/// Generic-password items of this app. They never leave the device: `ThisDeviceOnly`, never synchronised to iCloud15/// Keychain, not restored onto another device from a backup.16struct Keychain: Sendable {17 let service: String18 /// AfterFirstUnlock lets the background refresh read the token while the phone is locked.19 var accessibility: CFString { kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly }2021 private func base(_ account: String) -> [String: Any] {22 [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account]23 }2425 func read(_ account: String) -> Data? {26 var q = base(account)27 q[kSecReturnData as String] = true28 q[kSecMatchLimit as String] = kSecMatchLimitOne29 var out: CFTypeRef?30 guard SecItemCopyMatching(q as CFDictionary, &out) == errSecSuccess else { return nil }31 return out as? Data32 }3334 @discardableResult35 func write(_ account: String, _ data: Data) -> Bool {36 let attrs: [String: Any] = [kSecValueData as String: data, kSecAttrAccessible as String: accessibility, kSecAttrSynchronizable as String: false]37 let status = SecItemUpdate(base(account) as CFDictionary, attrs as CFDictionary)38 if status == errSecItemNotFound { return SecItemAdd(base(account).merging(attrs) { $1 } as CFDictionary, nil) == errSecSuccess }39 return status == errSecSuccess40 }4142 @discardableResult43 func delete(_ account: String) -> Bool {44 let s = SecItemDelete(base(account) as CFDictionary)45 return s == errSecSuccess || s == errSecItemNotFound46 }47}4849/// The device session token (`vqn_…`) of one backend origin: an opaque random value issued by the server after a50/// passkey verification. It is the only credential the app stores; the passkey's private key stays in the Secure51/// Enclave / iCloud Keychain and is never visible to the app.52struct KeychainTokenStore: SessionTokenStore {53 private let keychain = Keychain(service: "ai.spboucher.vquant.session")54 let account: String55 init(origin: String) { account = origin }56 func load() -> String? { keychain.read(account).flatMap { String(data: $0, encoding: .utf8) } }57 func save(_ token: String) { keychain.write(account, Data(token.utf8)) }58 func clear() { keychain.delete(account) }59}6061/// 256-bit key of the on-device cache, generated on first use, kept in the Keychain of this device only. Signing62/// out deletes it: whatever cache files remain become undecipherable.63struct KeychainCacheKey: CacheKeyProvider {64 private let keychain = Keychain(service: "ai.spboucher.vquant.cache")65 private let account = "aes-gcm-256"66 func key() -> SymmetricKey {67 if let d = keychain.read(account), d.count == 32 { return SymmetricKey(data: d) }68 let k = SymmetricKey(size: .bits256)69 keychain.write(account, k.withUnsafeBytes { Data($0) })70 return k71 }72 func destroy() { keychain.delete(account) }73}74