2 // KeychainOptions.swift
3 // SwiftKeychainWrapper
5 // Created by James Blair on 4/24/16.
6 // Copyright © 2016 Jason Rendel. All rights reserved.
8 // The MIT License (MIT)
10 // Permission is hereby granted, free of charge, to any person obtaining a copy
11 // of this software and associated documentation files (the "Software"), to deal
12 // in the Software without restriction, including without limitation the rights
13 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 // copies of the Software, and to permit persons to whom the Software is
15 // furnished to do so, subject to the following conditions:
17 // The above copyright notice and this permission notice shall be included in all
18 // copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 protocol KeychainAttrRepresentable {
31 var keychainAttrValue: CFString { get }
34 // MARK: - KeychainItemAccessibility
35 public enum KeychainItemAccessibility {
37 The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
39 After the first unlock, the data remains accessible until the next restart. This is recommended for items that need to be accessed by background applications. Items with this attribute migrate to a new device when using encrypted backups.
45 The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
47 After the first unlock, the data remains accessible until the next restart. This is recommended for items that need to be accessed by background applications. Items with this attribute do not migrate to a new device. Thus, after restoring from a backup of a different device, these items will not be present.
50 case afterFirstUnlockThisDeviceOnly
53 The data in the keychain item can always be accessed regardless of whether the device is locked.
55 This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
61 The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
63 This is recommended for items that only need to be accessible while the application is in the foreground. Items with this attribute never migrate to a new device. After a backup is restored to a new device, these items are missing. No items can be stored in this class on devices without a passcode. Disabling the device passcode causes all items in this class to be deleted.
66 case whenPasscodeSetThisDeviceOnly
69 The data in the keychain item can always be accessed regardless of whether the device is locked.
71 This is not recommended for application use. Items with this attribute do not migrate to a new device. Thus, after restoring from a backup of a different device, these items will not be present.
74 case alwaysThisDeviceOnly
77 The data in the keychain item can be accessed only while the device is unlocked by the user.
79 This is recommended for items that need to be accessible only while the application is in the foreground. Items with this attribute migrate to a new device when using encrypted backups.
81 This is the default value for keychain items added without explicitly setting an accessibility constant.
87 The data in the keychain item can be accessed only while the device is unlocked by the user.
89 This is recommended for items that need to be accessible only while the application is in the foreground. Items with this attribute do not migrate to a new device. Thus, after restoring from a backup of a different device, these items will not be present.
92 case whenUnlockedThisDeviceOnly
94 static func accessibilityForAttributeValue(_ keychainAttrValue: CFString) -> KeychainItemAccessibility? {
95 for (key, value) in keychainItemAccessibilityLookup {
96 if value == keychainAttrValue {
105 private let keychainItemAccessibilityLookup: [KeychainItemAccessibility:CFString] = {
106 var lookup: [KeychainItemAccessibility:CFString] = [
107 .afterFirstUnlock: kSecAttrAccessibleAfterFirstUnlock,
108 .afterFirstUnlockThisDeviceOnly: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
109 .always: kSecAttrAccessibleAlways,
110 .whenPasscodeSetThisDeviceOnly: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
111 .alwaysThisDeviceOnly : kSecAttrAccessibleAlwaysThisDeviceOnly,
112 .whenUnlocked: kSecAttrAccessibleWhenUnlocked,
113 .whenUnlockedThisDeviceOnly: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
119 extension KeychainItemAccessibility : KeychainAttrRepresentable {
120 internal var keychainAttrValue: CFString {
121 return keychainItemAccessibilityLookup[self]!