Line data Source code
1 : // 2 : // SwiftySimpleKeychain+NewQuery.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 : internal extension SwiftySimpleKeychain { 30 19 : func queryNew(with key: String, value: Data? = nil) -> [String: Any] { 31 19 : var query = baseQuery(with: key) 32 19 : if let value = value { 33 19 : query[kSecValueData as String] = value 34 19 : } 35 19 : 36 19 : if useAccessControl { 37 0 : var accessControl: SecAccessControl? 38 0 : var error: Unmanaged<CFError>? 39 0 : 40 0 : let accessibility = accessibility() 41 0 : 42 0 : accessControl = SecAccessControlCreateWithFlags(kCFAllocatorDefault, accessibility, .userPresence, &error) 43 0 : 44 0 : if error == nil || accessControl != nil { 45 0 : query[kSecAttrAccessControl as String] = accessControl 46 0 : #if canImport(LocalAuthentication) 47 0 : query[kSecUseAuthenticationContext as String] = localAuthenticationContext 48 0 : #endif 49 0 : } 50 19 : } 51 19 : 52 19 : return query 53 19 : } 54 : 55 0 : fileprivate func accessibility() -> CFTypeRef { 56 0 : var accessibility = kSecAttrAccessibleWhenUnlocked 57 0 : if #available(iOS 12.0, *) { 58 0 : accessibility = kSecAttrAccessibleAfterFirstUnlock 59 0 : } 60 0 : 61 0 : switch self.defaultAccessiblity { 62 0 : case .afterFirstUnlock: 63 0 : accessibility = kSecAttrAccessibleAfterFirstUnlock 64 0 : case .always: 65 0 : #if targetEnvironment(macCatalyst) 66 0 : accessibility = kSecAttrAccessibleAfterFirstUnlock 67 0 : #else 68 0 : accessibility = kSecAttrAccessibleAlways 69 0 : #endif 70 0 : case .afterFirstUnlockThisDeviceOnly: 71 0 : accessibility = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly 72 0 : case .alwaysThisDeviceOnly: 73 0 : #if targetEnvironment(macCatalyst) 74 0 : accessibility = kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly 75 0 : #else 76 0 : accessibility = kSecAttrAccessibleAlwaysThisDeviceOnly 77 0 : #endif 78 0 : case .whenPasscodeSetThisDeviceOnly: 79 0 : #if os(iOS) 80 0 : accessibility = kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly 81 0 : #endif 82 0 : case .whenUnlocked: 83 0 : accessibility = kSecAttrAccessibleWhenUnlocked 84 0 : case .whenUnlockedThisDeviceOnly: 85 0 : accessibility = kSecAttrAccessibleWhenUnlockedThisDeviceOnly 86 0 : } 87 0 : return accessibility 88 0 : } 89 : 90 : }