mirror of
https://github.com/block/buzz.git
synced 2026-10-31 14:31:02 +00:00
41 lines
1.5 KiB
Swift
41 lines
1.5 KiB
Swift
#if BUZZ_PUSH_ENABLED
|
|
import Foundation
|
|
import Security
|
|
|
|
enum BuzzPushKeychain {
|
|
static let service = "buzz.push.nse.signing"
|
|
|
|
static func replace(signingKeys: [String: String], accessGroup: String?) throws {
|
|
var query = baseQuery(accessGroup: accessGroup)
|
|
SecItemDelete(query as CFDictionary)
|
|
for (communityID, privateKeyHex) in signingKeys {
|
|
query[kSecAttrAccount as String] = communityID
|
|
query[kSecValueData as String] = Data(privateKeyHex.utf8)
|
|
query[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlock
|
|
let status = SecItemAdd(query as CFDictionary, nil)
|
|
guard status == errSecSuccess else {
|
|
SecItemDelete(baseQuery(accessGroup: accessGroup) as CFDictionary)
|
|
throw NSError(
|
|
domain: NSOSStatusErrorDomain, code: Int(status),
|
|
userInfo: [NSLocalizedDescriptionKey: SecCopyErrorMessageString(status, nil) ?? "Keychain write failed" as CFString]
|
|
)
|
|
}
|
|
query.removeValue(forKey: kSecValueData as String)
|
|
query.removeValue(forKey: kSecAttrAccessible as String)
|
|
query.removeValue(forKey: kSecAttrAccount as String)
|
|
}
|
|
}
|
|
|
|
private static func baseQuery(accessGroup: String?) -> [String: Any] {
|
|
var query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
]
|
|
if let accessGroup, !accessGroup.isEmpty {
|
|
query[kSecAttrAccessGroup as String] = accessGroup
|
|
}
|
|
return query
|
|
}
|
|
}
|
|
#endif
|