aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Utility/DAKeychain.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Hotline/Utility/DAKeychain.swift')
-rw-r--r--Hotline/Utility/DAKeychain.swift23
1 files changed, 12 insertions, 11 deletions
diff --git a/Hotline/Utility/DAKeychain.swift b/Hotline/Utility/DAKeychain.swift
index 8031886..aa71508 100644
--- a/Hotline/Utility/DAKeychain.swift
+++ b/Hotline/Utility/DAKeychain.swift
@@ -9,26 +9,27 @@
import Foundation
open class DAKeychain {
-
+
open var loggingEnabled = false
-
+
private init() {}
public static let shared = DAKeychain()
-
+
open subscript(key: String) -> String? {
get {
return load(withKey: key)
- } set {
+ }
+ set {
DispatchQueue.global().sync(flags: .barrier) {
self.save(newValue, forKey: key)
}
}
}
-
+
private func save(_ string: String?, forKey key: String) {
let query = keychainQuery(withKey: key)
let objectData: Data? = string?.data(using: .utf8, allowLossyConversion: false)
-
+
if SecItemCopyMatching(query, nil) == noErr {
if let dictData = objectData {
let status = SecItemUpdate(query, NSDictionary(dictionary: [kSecValueData: dictData]))
@@ -45,15 +46,15 @@ open class DAKeychain {
}
}
}
-
+
private func load(withKey key: String) -> String? {
let query = keychainQuery(withKey: key)
query.setValue(kCFBooleanTrue, forKey: kSecReturnData as String)
query.setValue(kCFBooleanTrue, forKey: kSecReturnAttributes as String)
-
+
var result: CFTypeRef?
let status = SecItemCopyMatching(query, &result)
-
+
guard
let resultsDict = result as? NSDictionary,
let resultsData = resultsDict.value(forKey: kSecValueData as String) as? Data,
@@ -64,7 +65,7 @@ open class DAKeychain {
}
return String(data: resultsData, encoding: .utf8)
}
-
+
private func keychainQuery(withKey key: String) -> NSMutableDictionary {
let result = NSMutableDictionary()
result.setValue(kSecClassGenericPassword, forKey: kSecClass as String)
@@ -72,7 +73,7 @@ open class DAKeychain {
result.setValue(kSecAttrAccessibleWhenUnlocked, forKey: kSecAttrAccessible as String)
return result
}
-
+
private func logPrint(_ items: Any...) {
if loggingEnabled {
print(items)