added iOS source code
[wl-app.git] / iOS / Pods / SwiftKeychainWrapper / SwiftKeychainWrapper / KeychainItemAccessibility.swift
1 //
2 //  KeychainOptions.swift
3 //  SwiftKeychainWrapper
4 //
5 //  Created by James Blair on 4/24/16.
6 //  Copyright © 2016 Jason Rendel. All rights reserved.
7 //
8 //    The MIT License (MIT)
9 //
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:
16 //
17 //    The above copyright notice and this permission notice shall be included in all
18 //    copies or substantial portions of the Software.
19 //
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
26 //    SOFTWARE.
27
28 import Foundation
29
30 protocol KeychainAttrRepresentable {
31     var keychainAttrValue: CFString { get }
32 }
33
34 // MARK: - KeychainItemAccessibility
35 public enum KeychainItemAccessibility {
36     /**
37      The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
38      
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.
40     */
41     @available(iOS 4, *)
42     case afterFirstUnlock
43     
44     /**
45      The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
46      
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.
48      */
49     @available(iOS 4, *)
50     case afterFirstUnlockThisDeviceOnly
51     
52     /**
53      The data in the keychain item can always be accessed regardless of whether the device is locked.
54      
55      This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
56      */
57     @available(iOS 4, *)
58     case always
59     
60     /**
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.
62      
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.
64      */
65     @available(iOS 8, *)
66     case whenPasscodeSetThisDeviceOnly
67     
68     /**
69      The data in the keychain item can always be accessed regardless of whether the device is locked.
70      
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.
72      */
73     @available(iOS 4, *)
74     case alwaysThisDeviceOnly
75     
76     /**
77      The data in the keychain item can be accessed only while the device is unlocked by the user.
78      
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.
80      
81      This is the default value for keychain items added without explicitly setting an accessibility constant.
82      */
83     @available(iOS 4, *)
84     case whenUnlocked
85     
86     /**
87      The data in the keychain item can be accessed only while the device is unlocked by the user.
88      
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.
90      */
91     @available(iOS 4, *)
92     case whenUnlockedThisDeviceOnly
93     
94     static func accessibilityForAttributeValue(_ keychainAttrValue: CFString) -> KeychainItemAccessibility? {
95         for (key, value) in keychainItemAccessibilityLookup {
96             if value == keychainAttrValue {
97                 return key
98             }
99         }
100         
101         return nil
102     }
103 }
104
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
114     ]
115
116     return lookup
117 }()
118
119 extension KeychainItemAccessibility : KeychainAttrRepresentable {
120     internal var keychainAttrValue: CFString {
121         return keychainItemAccessibilityLookup[self]!
122     }
123 }