|
| 1 | +import Configs |
| 2 | +import Foundation |
| 3 | +import Security |
| 4 | + |
| 5 | +public struct Keychain { |
| 6 | + let service = keychainService |
| 7 | + let accessGroup = keychainAccessGroup |
| 8 | + |
| 9 | + public enum Error: Swift.Error { |
| 10 | + case failedToDeleteFromKeyChain |
| 11 | + case failedToUpdateOrSetItem |
| 12 | + } |
| 13 | + |
| 14 | + public init() {} |
| 15 | + |
| 16 | + func query(_ key: String) -> [String: Any] { |
| 17 | + [ |
| 18 | + kSecClass as String: kSecClassGenericPassword as String, |
| 19 | + kSecAttrService as String: service, |
| 20 | + kSecAttrAccessGroup as String: accessGroup, |
| 21 | + kSecAttrAccount as String: key, |
| 22 | + kSecUseDataProtectionKeychain as String: true, |
| 23 | + ] |
| 24 | + } |
| 25 | + |
| 26 | + func set(_ value: String, key: String) throws { |
| 27 | + let query = query(key).merging([ |
| 28 | + kSecValueData as String: value.data(using: .utf8) ?? Data(), |
| 29 | + ], uniquingKeysWith: { _, b in b }) |
| 30 | + |
| 31 | + let result = SecItemAdd(query as CFDictionary, nil) |
| 32 | + |
| 33 | + switch result { |
| 34 | + case noErr: |
| 35 | + return |
| 36 | + default: |
| 37 | + throw Error.failedToUpdateOrSetItem |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public func update(_ value: String, key: String) throws { |
| 42 | + let query = query(key).merging([ |
| 43 | + kSecMatchLimit as String: kSecMatchLimitOne, |
| 44 | + kSecReturnData as String: true, |
| 45 | + ], uniquingKeysWith: { _, b in b }) |
| 46 | + |
| 47 | + let attributes: [String: Any] = |
| 48 | + [kSecValueData as String: value.data(using: .utf8) ?? Data()] |
| 49 | + |
| 50 | + let result = SecItemUpdate(query as CFDictionary, attributes as CFDictionary) |
| 51 | + |
| 52 | + switch result { |
| 53 | + case noErr: |
| 54 | + return |
| 55 | + case errSecItemNotFound: |
| 56 | + try set(value, key: key) |
| 57 | + default: |
| 58 | + throw Error.failedToUpdateOrSetItem |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public func get(_ key: String) throws -> String? { |
| 63 | + let query = query(key).merging([ |
| 64 | + kSecMatchLimit as String: kSecMatchLimitOne, |
| 65 | + kSecReturnData as String: true, |
| 66 | + kSecReturnAttributes as String: true, |
| 67 | + ], uniquingKeysWith: { _, b in b }) |
| 68 | + |
| 69 | + var item: CFTypeRef? |
| 70 | + if SecItemCopyMatching(query as CFDictionary, &item) == noErr { |
| 71 | + if let existingItem = item as? [String: Any], |
| 72 | + let passwordData = existingItem[kSecValueData as String] as? Data, |
| 73 | + let password = String(data: passwordData, encoding: .utf8) |
| 74 | + { |
| 75 | + return password |
| 76 | + } |
| 77 | + return nil |
| 78 | + } else { |
| 79 | + return nil |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public func remove(_ key: String) throws { |
| 84 | + if SecItemDelete(query(key) as CFDictionary) == noErr { |
| 85 | + return |
| 86 | + } |
| 87 | + throw Error.failedToDeleteFromKeyChain |
| 88 | + } |
| 89 | +} |
0 commit comments