2024-02-29 18:54:33 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2024-04-05 07:24:35 +00:00
|
|
|
class SettingsStore: ObservableObject {
|
|
|
|
let service = "com.maxgoedjen.Secretive"
|
2024-02-29 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension SettingsStore {
|
2024-04-05 07:24:35 +00:00
|
|
|
subscript(key: String) -> String? {
|
|
|
|
set(value) {
|
|
|
|
guard let valueData = value?.data(using: String.Encoding.utf8)! else {
|
|
|
|
return
|
2024-02-29 18:54:33 +00:00
|
|
|
}
|
|
|
|
|
2024-04-05 07:24:35 +00:00
|
|
|
if let keyVal = self[key] {
|
|
|
|
if keyVal == value {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let updateQuery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
|
|
|
|
kSecAttrServer as String: service]
|
|
|
|
let attributes: [String: Any] = [kSecAttrAccount as String: key,
|
|
|
|
kSecValueData as String: valueData]
|
|
|
|
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1393617-secitemupdate
|
|
|
|
let status = SecItemUpdate(updateQuery as CFDictionary, attributes as CFDictionary)
|
|
|
|
guard status == errSecSuccess else {
|
|
|
|
print("Couldn't update item in keychain. " + status.description)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let addquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
|
|
|
|
kSecAttrAccount as String: key,
|
|
|
|
kSecAttrServer as String: service,
|
|
|
|
kSecValueData as String: valueData]
|
|
|
|
// FIXME: Make this non-blocking as described here: https://developer.apple.com/documentation/security/1401659-secitemadd
|
|
|
|
let status = SecItemAdd(addquery as CFDictionary, nil)
|
|
|
|
guard status == errSecSuccess else {
|
|
|
|
print("Couldn't add item to keychain. " + status.description)
|
|
|
|
return
|
|
|
|
}
|
2024-02-29 18:54:33 +00:00
|
|
|
}
|
2024-04-05 07:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get {
|
|
|
|
let getquery: [String: Any] = [kSecClass as String: kSecClassGenericPassword,
|
2024-02-29 18:54:33 +00:00
|
|
|
kSecAttrAccount as String: key,
|
|
|
|
kSecAttrServer as String: service,
|
2024-04-05 07:24:35 +00:00
|
|
|
kSecMatchLimit as String: kSecMatchLimitOne,
|
|
|
|
kSecReturnData as String: true]
|
|
|
|
var item: CFTypeRef?
|
|
|
|
let status = SecItemCopyMatching(getquery as CFDictionary, &item)
|
|
|
|
|
|
|
|
return status == errSecSuccess ? String(decoding: item as! Data, as: UTF8.self) : nil
|
2024-02-29 18:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|