Line data Source code
1 : // 2 : // SwiftySimpleKeychain+Setters.swift 3 : // SwiftySimpleKeychain 4 : // 5 : // Copyright (c) 2022 Ezequiel (Kimi) Aceto (https://eaceto.dev). Based on Auth0's SimpleKeychain 6 : // 7 : // Permission is hereby granted, free of charge, to any person obtaining a copy 8 : // of this software and associated documentation files (the "Software"), to deal 9 : // in the Software without restriction, including without limitation the rights 10 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 : // copies of the Software, and to permit persons to whom the Software is 12 : // furnished to do so, subject to the following conditions: 13 : // 14 : // The above copyright notice and this permission notice shall be included in 15 : // all copies or substantial portions of the Software. 16 : // 17 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 : // THE SOFTWARE. 24 : 25 : #if canImport(Foundation) 26 : import Foundation 27 : #endif 28 : 29 : public extension SwiftySimpleKeychain { 30 : 31 : /** 32 : * Sets a **Data** for the given key 33 : * 34 : * Usage: 35 : * ```swift 36 : * _ = keychain.set(data: aData, for: "aKey") 37 : * ``` 38 : * 39 : * - Parameter data: The **Data** to store. If nill, it delets the entry. 40 : * - Parameter key: Identifier of the key 41 : * - Parameter promptMessage: A message to show to the user in case the Keychain should be unlocked 42 : * - Returns **VoidErrorResult** 43 : */ 44 23 : func set(data: Data?, for key: String, with promptMessage: String? = nil) -> VoidErrorResult { 45 23 : if key.isEmpty { return .failure(SwiftySimpleKeychainError.wrongParameter) } 46 23 : 47 23 : let query = queryFind(by: key, promptMessage: promptMessage) 48 23 : 49 23 : // Touch ID case 50 23 : if useAccessControl && defaultAccessiblity == .whenPasscodeSetThisDeviceOnly { 51 0 : // TouchId case. Doesn't support updating keychain items 52 0 : // see Known Issues: https://developer.apple.com/library/ios/releasenotes/General/RN-iOSSDK-8.0/ 53 0 : // We need to delete old and add a new item. This can fail 54 0 : 55 0 : var status = SecItemDelete(query as CFDictionary) 56 0 : if status == errSecSuccess || status == errSecItemNotFound { 57 0 : let newQuery = queryNew(with: key, value: data) as CFDictionary 58 0 : status = SecItemAdd(newQuery, nil) 59 0 : if status == errSecSuccess { 60 0 : return .success(()) 61 0 : } 62 0 : return .failure(SwiftySimpleKeychainError.from(status)) 63 0 : } 64 23 : } 65 23 : 66 23 : var item: CFTypeRef? 67 23 : var status = SecItemCopyMatching(query as CFDictionary, &item) 68 23 : if status == errSecSuccess { 69 4 : if let data = data { 70 2 : // set data for key 71 2 : let updateQuery = queryUpdate(with: data, promptMessage: promptMessage) as CFDictionary 72 2 : status = SecItemUpdate(query as CFDictionary, updateQuery) 73 4 : } else { 74 2 : // delete key 75 2 : status = SecItemDelete(query as CFDictionary) 76 4 : } 77 23 : } else { 78 19 : // add a new item for key 79 19 : let newQuery = queryNew(with: key, value: data) 80 19 : status = SecItemAdd(newQuery as CFDictionary, nil) 81 23 : } 82 23 : 83 23 : if status == errSecSuccess { 84 23 : return .success(()) 85 23 : } 86 0 : return .failure(SwiftySimpleKeychainError.from(status)) 87 23 : } 88 : 89 : /** 90 : * Sets a **String** for the given key 91 : * 92 : * Usage: 93 : * ```swift 94 : * _ = keychain.set(string: "value", for: "aKey") 95 : * ``` 96 : * 97 : * - Parameter data: The **String** to store. If nill, it delets the entry. 98 : * - Parameter key: Identifier of the key 99 : * - Parameter promptMessage: A message to show to the user in case the Keychain should be unlocked 100 : * - Returns **VoidErrorResult** 101 : */ 102 18 : func set(string: String? = nil, for key: String, with promptMessage: String? = nil) -> VoidErrorResult { 103 18 : var data: Data? 104 18 : if let string = string { 105 17 : data = string.data(using: .utf8) 106 18 : } 107 18 : return set(data: data, for: key, with: promptMessage) 108 18 : } 109 : }