aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Utility/DAKeychain.swift
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2025-11-07 12:44:55 -0800
committerDustin Mierau <dustin@mierau.me>2025-11-07 12:44:55 -0800
commita55318fa8d643160900bec3e6b14e7404c63497a (patch)
treeab6a9eca9a368b35e1490becc70f94ebde6ac271 /Hotline/Utility/DAKeychain.swift
parent786a387d58fc66ae20716a9dee46b74dff8e6894 (diff)
Organize Library folder a bit. Update Tracker add/edit sheet design.
Diffstat (limited to 'Hotline/Utility/DAKeychain.swift')
-rw-r--r--Hotline/Utility/DAKeychain.swift81
1 files changed, 0 insertions, 81 deletions
diff --git a/Hotline/Utility/DAKeychain.swift b/Hotline/Utility/DAKeychain.swift
deleted file mode 100644
index 8031886..0000000
--- a/Hotline/Utility/DAKeychain.swift
+++ /dev/null
@@ -1,81 +0,0 @@
-//
-// DAKeychain.swift
-// DAKeychain
-//
-// Created by Dejan on 28/02/2017.
-// Copyright © 2017 Dejan. All rights reserved.
-//
-
-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 {
- 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]))
- logPrint("Update status: ", status)
- } else {
- let status = SecItemDelete(query)
- logPrint("Delete status: ", status)
- }
- } else {
- if let dictData = objectData {
- query.setValue(dictData, forKey: kSecValueData as String)
- let status = SecItemAdd(query, nil)
- logPrint("Update status: ", status)
- }
- }
- }
-
- 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,
- status == noErr
- else {
- logPrint("Load status: ", status)
- return nil
- }
- return String(data: resultsData, encoding: .utf8)
- }
-
- private func keychainQuery(withKey key: String) -> NSMutableDictionary {
- let result = NSMutableDictionary()
- result.setValue(kSecClassGenericPassword, forKey: kSecClass as String)
- result.setValue(key, forKey: kSecAttrService as String)
- result.setValue(kSecAttrAccessibleWhenUnlocked, forKey: kSecAttrAccessible as String)
- return result
- }
-
- private func logPrint(_ items: Any...) {
- if loggingEnabled {
- print(items)
- }
- }
-}