import Foundation import Security /// Secrets kept as generic-password keychain items, keyed by account and /// readable after first unlock. enum GenericPasswordKeychain { /// Replaces any item under `reference`. False when the keychain refuses. static func save(_ secret: String, reference: String) -> Bool { delete(reference: reference) let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: reference, kSecValueData as String: Data(secret.utf8), kSecAttrAccessible as String: kSecAttrAccessibleAfterFirstUnlock ] return SecItemAdd(query as CFDictionary, nil) == errSecSuccess } static func secret(reference: String) -> String? { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: reference, kSecReturnData as String: true, kSecMatchLimit as String: kSecMatchLimitOne ] var result: CFTypeRef? guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, let data = result as? Data else { return nil } return String(data: data, encoding: .utf8) } static func delete(reference: String) { let query: [String: Any] = [ kSecClass as String: kSecClassGenericPassword, kSecAttrAccount as String: reference ] SecItemDelete(query as CFDictionary) } }