import Foundation import Security /// Where a device keeps its key and credential. The Keychain in the apps; /// memory in tests. public protocol SecretStore: Sendable { func read(_ account: String) -> Data? func write(_ data: Data, account: String) throws func remove(_ account: String) } /// A generic-password item per account, this device only, after first /// unlock (notification polling runs in the background). public struct KeychainSecretStore: SecretStore { public let service: String public let accessGroup: String? public static var shared: Self { Self(service: "works.oficina.equipo.device", accessGroup: "9U4A84C4WK.works.oficina.equipo") } public init(service: String = "works.oficina.equipo.device", accessGroup: String? = nil) { self.service = service; self.accessGroup = accessGroup } func query(_ account: String) -> [String: Any] { var result: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account] if let accessGroup { result[kSecAttrAccessGroup as String] = accessGroup } return result } public func read(_ account: String) -> Data? { var q = query(account) q[kSecReturnData as String] = true q[kSecMatchLimit as String] = kSecMatchLimitOne var result: CFTypeRef? guard SecItemCopyMatching(q as CFDictionary, &result) == errSecSuccess else { return nil } return result as? Data } /// iPhone: after the first unlock since it started, never in a backup or /// on another device. Slice 2 works in the background: when the server /// starts a task's Live Activity, iOS wakes the app, often on a locked /// phone, to send the activity's token with the device credential. /// (Slice 1 kept it to while unlocked; a write moves an older item over.) /// On the Mac this lands in the login keychain, readable by this app only /// (its default access list); the data protection keychain would need a /// keychain-access-groups entitlement. static var accessible: CFString { kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly } public func write(_ data: Data, account: String) throws { let updated = SecItemUpdate(query(account) as CFDictionary, [kSecValueData as String: data, kSecAttrAccessible as String: Self.accessible] as CFDictionary) if updated == errSecSuccess { return } guard updated == errSecItemNotFound else { throw StoreError.keychain(updated) } var q = query(account) q[kSecValueData as String] = data q[kSecAttrAccessible as String] = Self.accessible let added = SecItemAdd(q as CFDictionary, nil) guard added == errSecSuccess else { throw StoreError.keychain(added) } } public func remove(_ account: String) { SecItemDelete(query(account) as CFDictionary) } public enum StoreError: Error, Equatable { case keychain(OSStatus) } } public final class MemorySecretStore: SecretStore, @unchecked Sendable { private var items: [String: Data] = [:] private let lock = NSLock() public init() {} public func read(_ account: String) -> Data? { lock.withLock { items[account] } } public func write(_ data: Data, account: String) throws { lock.withLock { items[account] = data } } public func remove(_ account: String) { lock.withLock { _ = items.removeValue(forKey: account) } } public var accounts: [String] { lock.withLock { Array(items.keys) } } }