| 1 | import Foundation |
| 2 | import Security |
| 3 | |
| 4 | /// Where a device keeps its key and credential. The Keychain in the apps; |
| 5 | /// memory in tests. |
| 6 | public protocol SecretStore: Sendable { |
| 7 | func read(_ account: String) -> Data? |
| 8 | func write(_ data: Data, account: String) throws |
| 9 | func remove(_ account: String) |
| 10 | } |
| 11 | |
| 12 | /// A generic-password item per account, this device only, after first |
| 13 | /// unlock (notification polling runs in the background). |
| 14 | public struct KeychainSecretStore: SecretStore { |
| 15 | public let service: String |
| 16 | public let accessGroup: String? |
| 17 | public static var shared: Self { Self(service: "works.oficina.equipo.device", accessGroup: "9U4A84C4WK.works.oficina.equipo") } |
| 18 | public init(service: String = "works.oficina.equipo.device", accessGroup: String? = nil) { self.service = service; self.accessGroup = accessGroup } |
| 19 | |
| 20 | func query(_ account: String) -> [String: Any] { |
| 21 | var result: [String: Any] = [kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: service, kSecAttrAccount as String: account] |
| 22 | if let accessGroup { result[kSecAttrAccessGroup as String] = accessGroup } |
| 23 | return result |
| 24 | } |
| 25 | public func read(_ account: String) -> Data? { |
| 26 | var q = query(account) |
| 27 | q[kSecReturnData as String] = true |
| 28 | q[kSecMatchLimit as String] = kSecMatchLimitOne |
| 29 | var result: CFTypeRef? |
| 30 | guard SecItemCopyMatching(q as CFDictionary, &result) == errSecSuccess else { return nil } |
| 31 | return result as? Data |
| 32 | } |
| 33 | /// iPhone: after the first unlock since it started, never in a backup or |
| 34 | /// on another device. Slice 2 works in the background: when the server |
| 35 | /// starts a task's Live Activity, iOS wakes the app, often on a locked |
| 36 | /// phone, to send the activity's token with the device credential. |
| 37 | /// (Slice 1 kept it to while unlocked; a write moves an older item over.) |
| 38 | /// On the Mac this lands in the login keychain, readable by this app only |
| 39 | /// (its default access list); the data protection keychain would need a |
| 40 | /// keychain-access-groups entitlement. |
| 41 | static var accessible: CFString { kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly } |
| 42 | |
| 43 | public func write(_ data: Data, account: String) throws { |
| 44 | let updated = SecItemUpdate(query(account) as CFDictionary, |
| 45 | [kSecValueData as String: data, kSecAttrAccessible as String: Self.accessible] as CFDictionary) |
| 46 | if updated == errSecSuccess { return } |
| 47 | guard updated == errSecItemNotFound else { throw StoreError.keychain(updated) } |
| 48 | var q = query(account) |
| 49 | q[kSecValueData as String] = data |
| 50 | q[kSecAttrAccessible as String] = Self.accessible |
| 51 | let added = SecItemAdd(q as CFDictionary, nil) |
| 52 | guard added == errSecSuccess else { throw StoreError.keychain(added) } |
| 53 | } |
| 54 | public func remove(_ account: String) { SecItemDelete(query(account) as CFDictionary) } |
| 55 | public enum StoreError: Error, Equatable { case keychain(OSStatus) } |
| 56 | } |
| 57 | |
| 58 | public final class MemorySecretStore: SecretStore, @unchecked Sendable { |
| 59 | private var items: [String: Data] = [:] |
| 60 | private let lock = NSLock() |
| 61 | public init() {} |
| 62 | public func read(_ account: String) -> Data? { lock.withLock { items[account] } } |
| 63 | public func write(_ data: Data, account: String) throws { lock.withLock { items[account] = data } } |
| 64 | public func remove(_ account: String) { lock.withLock { _ = items.removeValue(forKey: account) } } |
| 65 | public var accounts: [String] { lock.withLock { Array(items.keys) } } |
| 66 | } |
| 67 | |