Repository Public
1//2// SP Chat iOS3// File: SPChat-iOS/Core/Security/Keychain.swift4// Description: Keychain Services wrapper and the Keychain-backed store of the server session token.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//910import Foundation11import Security1213/// Generic-password items of this app. Items never leave the device (`ThisDeviceOnly`: no iCloud Keychain sync, not14/// restored onto another device from a backup).15struct Keychain: Sendable {16 let service: String1718 func read(_ account: String) -> Data? {19 let q: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account,20 kSecReturnData as String: true, kSecMatchLimit as String: kSecMatchLimitOne]21 var out: CFTypeRef?22 guard SecItemCopyMatching(q as CFDictionary, &out) == errSecSuccess else { return nil }23 return out as? Data24 }2526 @discardableResult27 func write(_ account: String, _ data: Data) -> Bool {28 let base: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account]29 // AfterFirstUnlock: the background refresh task may run while the device is locked. Never synchronizable.30 let attrs: [String: Any] = [kSecValueData as String: data, kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly, kSecAttrSynchronizable as String: false]31 let status = SecItemUpdate(base as CFDictionary, attrs as CFDictionary)32 if status == errSecItemNotFound { return SecItemAdd(base.merging(attrs) { $1 } as CFDictionary, nil) == errSecSuccess }33 return status == errSecSuccess34 }3536 @discardableResult37 func delete(_ account: String) -> Bool {38 let q: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account]39 let s = SecItemDelete(q as CFDictionary)40 return s == errSecSuccess || s == errSecItemNotFound41 }42}4344/// The session token of one backend origin. The token is an opaque random value issued by the server after a passkey45/// verification; it is the only secret the app stores.46struct KeychainSessionTokenStore: SessionTokenStore {47 private let keychain = Keychain(service: "ai.spboucher.spchat.session")48 let account: String49 init(origin: String) { account = origin }50 func load() -> String? { keychain.read(account).flatMap { String(data: $0, encoding: .utf8) } }51 func save(_ token: String) { keychain.write(account, Data(token.utf8)) }52 func clear() { keychain.delete(account) }53}5455/// In-memory store for unit tests and previews.56final class MemorySessionTokenStore: SessionTokenStore, @unchecked Sendable {57 private let lock = NSLock()58 private var token: String?59 init(_ token: String? = nil) { self.token = token }60 func load() -> String? { lock.withLock { token } }61 func save(_ token: String) { lock.withLock { self.token = token } }62 func clear() { lock.withLock { token = nil } }63}64