Bucksley / app · bucksley.ai / Sources / BucksleyKit / Keychain.swift
| 1 | import Foundation |
| 2 | import Security |
| 3 | |
| 4 | /// Credentials live only here, never in the workspace files, iCloud data or MCP arguments. |
| 5 | /// Items are synchronizable, so iCloud Keychain carries them to the user's other Macs. |
| 6 | public enum Keychain { |
| 7 | public enum Item: Sendable, Hashable { |
| 8 | case plaidClientID, plaidSecret |
| 9 | case claudeKey, openAIKey |
| 10 | /// A connection's own secret: the SimpleFIN access URL or a Plaid access token. |
| 11 | case connection(String) |
| 12 | |
| 13 | var account: String { |
| 14 | switch self { |
| 15 | case .plaidClientID: "plaid.client-id" |
| 16 | case .plaidSecret: "plaid.secret" |
| 17 | case .claudeKey: "ask.claude-key" |
| 18 | case .openAIKey: "ask.openai-key" |
| 19 | case .connection(let id): "connection.\(id)" |
| 20 | } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | public static let service = "com.protomota.bucksley" |
| 25 | /// Tests and the unsigned dev build use an in-memory store instead of the real Keychain. |
| 26 | nonisolated(unsafe) public static var memory: [String: String]? = ProcessInfo.processInfo.environment["BUCKSLEY_MEMORY_KEYCHAIN"] != nil ? [:] : nil |
| 27 | private static let memoryLock = NSLock() |
| 28 | |
| 29 | public static func get(_ item: Item) -> String? { |
| 30 | if memory != nil { memoryLock.lock(); defer { memoryLock.unlock() }; return memory?[item.account] } |
| 31 | var query = base(item) |
| 32 | query[kSecReturnData as String] = true |
| 33 | query[kSecMatchLimit as String] = kSecMatchLimitOne |
| 34 | var out: AnyObject? |
| 35 | guard SecItemCopyMatching(query as CFDictionary, &out) == errSecSuccess, let data = out as? Data else { return nil } |
| 36 | return String(data: data, encoding: .utf8) |
| 37 | } |
| 38 | |
| 39 | public static func has(_ item: Item) -> Bool { get(item) != nil } |
| 40 | |
| 41 | public static func set(_ value: String, for item: Item) throws { |
| 42 | if memory != nil { memoryLock.lock(); memory?[item.account] = value; memoryLock.unlock(); return } |
| 43 | let data = Data(value.utf8) |
| 44 | let status = SecItemUpdate(base(item) as CFDictionary, [kSecValueData as String: data] as CFDictionary) |
| 45 | if status == errSecItemNotFound { |
| 46 | var add = base(item); add[kSecValueData as String] = data |
| 47 | add[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock |
| 48 | let s = SecItemAdd(add as CFDictionary, nil) |
| 49 | guard s == errSecSuccess else { throw KeychainError(status: s) } |
| 50 | } else if status != errSecSuccess { |
| 51 | throw KeychainError(status: status) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | public static func remove(_ item: Item) { |
| 56 | if memory != nil { memoryLock.lock(); memory?[item.account] = nil; memoryLock.unlock(); return } |
| 57 | SecItemDelete(base(item) as CFDictionary) |
| 58 | } |
| 59 | |
| 60 | private static func base(_ item: Item) -> [String: Any] { |
| 61 | [kSecClass as String: kSecClassGenericPassword, |
| 62 | kSecAttrService as String: service, |
| 63 | kSecAttrAccount as String: item.account, |
| 64 | kSecAttrSynchronizable as String: kCFBooleanTrue as Any] |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | public struct KeychainError: Error, CustomStringConvertible { |
| 69 | public let status: OSStatus |
| 70 | public var description: String { "Keychain error \(status): \(SecCopyErrorMessageString(status, nil) as String? ?? "unknown")" } |
| 71 | } |