mirror of
https://github.com/we-promise/sure.git
synced 2026-09-06 07:11:14 +00:00
* Add Swift-native Sure app * Fix push subscriptions schema for CI * Address native app review feedback * Address remaining native app review feedback * Use Flutter app logo for native icon * Honor insight notification preferences and locale --------- Co-authored-by: Juan Jose Mata <2v8shcb6pz@privaterelay.appleid.com> Co-authored-by: sure-admin <sure-admin@splashblot.com>
50 lines
1.7 KiB
Swift
50 lines
1.7 KiB
Swift
import Foundation
|
|
import Security
|
|
|
|
enum KeychainStore {
|
|
private static var service: String { "am.sure.native" }
|
|
|
|
static func saveAPIKey(_ value: String) throws {
|
|
guard let data = value.data(using: .utf8) else { return }
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: "api-key"
|
|
]
|
|
SecItemDelete(query as CFDictionary)
|
|
var attributes = query
|
|
attributes[kSecValueData as String] = data
|
|
attributes[kSecAttrAccessible as String] = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
|
|
let status = SecItemAdd(attributes as CFDictionary, nil)
|
|
guard status == errSecSuccess else { throw KeychainError(status: status) }
|
|
}
|
|
|
|
static func readAPIKey() -> String? {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: "api-key",
|
|
kSecReturnData as String: true,
|
|
kSecMatchLimit as String: kSecMatchLimitOne
|
|
]
|
|
var result: AnyObject?
|
|
guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess,
|
|
let data = result as? Data else { return nil }
|
|
return String(data: data, encoding: .utf8)
|
|
}
|
|
|
|
static func deleteAPIKey() {
|
|
let query: [String: Any] = [
|
|
kSecClass as String: kSecClassGenericPassword,
|
|
kSecAttrService as String: service,
|
|
kSecAttrAccount as String: "api-key"
|
|
]
|
|
SecItemDelete(query as CFDictionary)
|
|
}
|
|
}
|
|
|
|
struct KeychainError: LocalizedError {
|
|
var status: OSStatus
|
|
var errorDescription: String? { "Could not securely save the API key (\(status))." }
|
|
}
|