|
| 1 | +// |
| 2 | +// Copyright © 2022 Iterable. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import Foundation |
| 6 | + |
| 7 | +/// Basic wrapper for keychain |
| 8 | +/// This should have no dependency on Iterable classes |
| 9 | +class KeychainWrapper { |
| 10 | + init(serviceName: String = Const.Keychain.serviceName) { |
| 11 | + self.serviceName = serviceName |
| 12 | + } |
| 13 | + |
| 14 | + @discardableResult |
| 15 | + func set(_ value: Data, forKey key: String) -> Bool { |
| 16 | + var keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key) |
| 17 | + |
| 18 | + keychainQueryDictionary[SecValueData] = value |
| 19 | + |
| 20 | + // Assign default protection - Protect the keychain entry so it's only valid when the device is unlocked |
| 21 | + keychainQueryDictionary[SecAttrAccessible] = SecAttrAccessibleWhenUnlocked |
| 22 | + |
| 23 | + let status: OSStatus = SecItemAdd(keychainQueryDictionary as CFDictionary, nil) |
| 24 | + |
| 25 | + if status == errSecSuccess { |
| 26 | + return true |
| 27 | + } else if status == errSecDuplicateItem { |
| 28 | + return update(value, forKey: key) |
| 29 | + } else { |
| 30 | + return false |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + func data(forKey key: String) -> Data? { |
| 35 | + var keychainQueryDictionary = setupKeychainQueryDictionary(forKey: key) |
| 36 | + |
| 37 | + // Limit search results to one |
| 38 | + keychainQueryDictionary[SecMatchLimit] = SecMatchLimitOne |
| 39 | + |
| 40 | + // Specify we want Data/CFData returned |
| 41 | + keychainQueryDictionary[SecReturnData] = CFBooleanTrue |
| 42 | + |
| 43 | + // Search |
| 44 | + var result: AnyObject? |
| 45 | + let status = SecItemCopyMatching(keychainQueryDictionary as CFDictionary, &result) |
| 46 | + |
| 47 | + return status == noErr ? result as? Data : nil |
| 48 | + } |
| 49 | + |
| 50 | + @discardableResult |
| 51 | + func removeValue(forKey key: String) -> Bool { |
| 52 | + let keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key) |
| 53 | + |
| 54 | + // Delete |
| 55 | + let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary) |
| 56 | + |
| 57 | + return status == errSecSuccess |
| 58 | + } |
| 59 | + |
| 60 | + @discardableResult |
| 61 | + func removeAll() -> Bool { |
| 62 | + var keychainQueryDictionary: [String: Any] = [SecClass: SecClassGenericPassword] |
| 63 | + |
| 64 | + keychainQueryDictionary[SecAttrService] = serviceName |
| 65 | + |
| 66 | + let status: OSStatus = SecItemDelete(keychainQueryDictionary as CFDictionary) |
| 67 | + |
| 68 | + return status == errSecSuccess |
| 69 | + } |
| 70 | + |
| 71 | + private let serviceName: String |
| 72 | + |
| 73 | + private func setupKeychainQueryDictionary(forKey key: String) -> [String: Any] { |
| 74 | + // Setup default access as generic password (rather than a certificate, internet password, etc) |
| 75 | + var keychainQueryDictionary: [String: Any] = [SecClass: SecClassGenericPassword] |
| 76 | + |
| 77 | + // Uniquely identify this keychain accessor |
| 78 | + keychainQueryDictionary[SecAttrService] = serviceName |
| 79 | + |
| 80 | + // Uniquely identify the account who will be accessing the keychain |
| 81 | + let encodedIdentifier: Data? = key.data(using: .utf8) |
| 82 | + |
| 83 | + keychainQueryDictionary[SecAttrGeneric] = encodedIdentifier |
| 84 | + |
| 85 | + keychainQueryDictionary[SecAttrAccount] = encodedIdentifier |
| 86 | + |
| 87 | + keychainQueryDictionary[SecAttrSynchronizable] = CFBooleanFalse |
| 88 | + |
| 89 | + return keychainQueryDictionary |
| 90 | + } |
| 91 | + |
| 92 | + private func update(_ value: Data, forKey key: String) -> Bool { |
| 93 | + let keychainQueryDictionary: [String: Any] = setupKeychainQueryDictionary(forKey: key) |
| 94 | + let updateDictionary = [SecValueData: value] |
| 95 | + |
| 96 | + // Update |
| 97 | + let status: OSStatus = SecItemUpdate(keychainQueryDictionary as CFDictionary, updateDictionary as CFDictionary) |
| 98 | + |
| 99 | + return status == errSecSuccess |
| 100 | + } |
| 101 | + |
| 102 | + private let SecValueData = kSecValueData as String |
| 103 | + private let SecAttrAccessible: String = kSecAttrAccessible as String |
| 104 | + private let SecAttrAccessibleWhenUnlocked = kSecAttrAccessibleWhenUnlocked |
| 105 | + private let SecClass: String = kSecClass as String |
| 106 | + private let SecClassGenericPassword = kSecClassGenericPassword |
| 107 | + private let SecAttrService: String = kSecAttrService as String |
| 108 | + private let SecAttrGeneric: String = kSecAttrGeneric as String |
| 109 | + private let SecAttrAccount: String = kSecAttrAccount as String |
| 110 | + private let SecAttrSynchronizable: String = kSecAttrSynchronizable as String |
| 111 | + private let CFBooleanTrue = kCFBooleanTrue |
| 112 | + private let CFBooleanFalse = kCFBooleanFalse |
| 113 | + private let SecMatchLimit: String = kSecMatchLimit as String |
| 114 | + private let SecMatchLimitOne = kSecMatchLimitOne |
| 115 | + private let SecReturnData: String = kSecReturnData as String |
| 116 | +} |
0 commit comments