-
Notifications
You must be signed in to change notification settings - Fork 0
/
DYFSwiftKeychain.swift
executable file
·597 lines (509 loc) · 28.9 KB
/
DYFSwiftKeychain.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//
// DYFSwiftKeychain.swift
//
// Created by Tenfay on 2016/11/28. (https://github.com/itenfay/DYFSwiftKeychain)
// Copyright © 2016 Tenfay. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
import Security
/// The class for swift keychain wrapper.
open class DYFSwiftKeychain: NSObject {
/// Specifies an access group which is used to share keychain items between applications.
@objc public var accessGroup: String?
/// Specifies whether the item is synchronized to other devices through iCloud.
@objc public var synchronizable: Bool = false
/// The identifierfor for kSecAttrService.
@objc public var serviceIdentifier: String?
/// Records the query parameters of the last operation.
@objc public var queryDictionary: [String: Any]?
/// Records the status of the last operation result.
@objc public var osStatus: OSStatus = errSecSuccess
/// Instantiates an `DYFSwiftKeychain` object.
@objc public override init() {
super.init()
}
/// Instantiates a DYFSwiftKeychain object.
///
/// - Parameter serviceIdentifier: The identifier for service.
@objc public convenience init(serviceIdentifier: String?) {
self.init()
self.serviceIdentifier = serviceIdentifier
}
/// The lock prevents the code to be run simultaneously from multiple threads which may result in crashing.
private var lock: NSLock { return NSLock() }
/// Creates an instance of DYFSwiftKeychain with the class method.
///
/// - Returns: An instance of DYFSwiftKeychain.
@objc public class func defaultKeychain() -> DYFSwiftKeychain {
return DYFSwiftKeychain()
}
/// Returns a keychain that copies the current DYFSwiftKeychain instance.
///
/// - Returns: A DYFSwiftKeychain object.
@objc open override func copy() -> Any {
let keychain = DYFSwiftKeychain.defaultKeychain()
keychain.accessGroup = self.accessGroup
keychain.synchronizable = self.synchronizable
keychain.serviceIdentifier = self.serviceIdentifier
keychain.queryDictionary = self.queryDictionary
keychain.osStatus = self.osStatus
return keychain
}
/// Stores or updates the text value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The text value to be written to the keychain.
/// - key: The key which the text is stored in the keychain.
/// - Returns: True if the text was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func add(_ value: String?, forKey key: String) -> Bool {
let opts = DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked
return add(value, forKey: key, options: opts)
}
/// Stores or updates the text value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The text value to be written to the keychain.
/// - key: The key which the text is stored in the keychain.
/// - options: The options indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the text was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func add(_ value: String?, forKey key: String, options: DYFSwiftKeychainAccessOptions) -> Bool {
let v = value?.data(using: String.Encoding.utf8)
let opts = toOpts(options)
return set(v, forKey: key, withAccess: opts)
}
/// Stores or updates the text value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The text value to be written to the keychain.
/// - key: The key which the text is stored in the keychain.
/// - access: The parameter indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychain.AccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the text was successfully written to the keychain, false otherwise.
@discardableResult
public func set(_ value: String?, forKey key: String, withAccess access: DYFSwiftKeychain.AccessOptions? = nil) -> Bool {
let v = value?.data(using: String.Encoding.utf8)
return set(v, forKey: key, withAccess: access)
}
/// Stores or updates the data in the keychain item by the given key.
///
/// - Parameters:
/// - value: The data to be written to the keychain.
/// - key: The key which the data is stored in the keychain.
/// - Returns: True if the data was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func addData(_ value: Data?, forKey key: String) -> Bool {
let opts = DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked
return addData(value, forKey: key, options: opts)
}
/// Stores or updates the data in the keychain item by the given key.
///
/// - Parameters:
/// - value: The data to be written to the keychain.
/// - key: The key which the data is stored in the keychain.
/// - options: The options indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the data was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func addData(_ value: Data?, forKey key: String, options: DYFSwiftKeychainAccessOptions) -> Bool {
let opts = toOpts(options)
return set(value, forKey: key, withAccess: opts)
}
/// Stores or updates the data in the keychain item by the given key.
///
/// - Parameters:
/// - value: Stores or updates the data in the keychain item by the given key.
/// - key: The key which the data is stored in the keychain.
/// - access: The parameter indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychain.AccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the data was successfully written to the keychain, false otherwise.
@discardableResult
public func set(_ value: Data?, forKey key: String, withAccess access: DYFSwiftKeychain.AccessOptions? = nil) -> Bool {
// The lock prevents the code to be run simultaneously from multiple threads which may result in crashing.
lock.lock()
defer { lock.unlock() }
let accessible = access?.value ?? DYFSwiftKeychain.AccessOptions.defaultOption.value
var query: [String: Any] = supplyQueryDictionary()
query[DYFSwiftKeychain.Constants.account] = key
query[DYFSwiftKeychain.Constants.accessible] = accessible
queryDictionary = query
var ignore: CFTypeRef? = nil
let status = SecItemCopyMatching(query as CFDictionary, &ignore)
guard status == errSecSuccess else {
if let v = value {
query[DYFSwiftKeychain.Constants.valueData] = v
queryDictionary?[DYFSwiftKeychain.Constants.valueData] = v
osStatus = SecItemAdd(query as CFDictionary, nil)
} else {
osStatus = errSecInvalidPointer // -67675, An invalid pointer was encountered.
}
return osStatus == errSecSuccess
}
if let v = value {
let updatedDictionary: [String: Any] = [
DYFSwiftKeychain.Constants.valueData: v
]
osStatus = SecItemUpdate(query as CFDictionary, updatedDictionary as CFDictionary)
} else {
deleteWithoutLock(key)
osStatus = errSecInvalidPointer // -67675, An invalid pointer was encountered.
}
return osStatus == errSecSuccess
}
/// Stores or updates the boolean value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The boolean value to be written to the keychain.
/// - key: The key which the boolean value is stored in the keychain.
/// - Returns: True if the boolean value was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func addBool(_ value: Bool, forKey key: String) -> Bool {
let opts = DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked
return addBool(value, forKey: key, options: opts)
}
/// Stores or updates the boolean value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The boolean value to be written to the keychain.
/// - key: The key which the boolean value is stored in the keychain.
/// - options: The options indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychainAccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the boolean value was successfully written to the keychain, false otherwise.
@discardableResult
@objc public func addBool(_ value: Bool, forKey key: String, options: DYFSwiftKeychainAccessOptions) -> Bool {
let opts = toOpts(options)
return set(value, forKey: key, withAccess: opts)
}
/// Stores or updates the boolean value in the keychain item by the given key.
///
/// - Parameters:
/// - value: The boolean value to be written to the keychain.
/// - key: The key which the boolean value is stored in the keychain.
/// - access: The parameter indicates when you app needs access to the text in the keychain. By the default DYFSwiftKeychain.AccessOptions.accessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
/// - Returns: True if the boolean value was successfully written to the keychain, false otherwise.
@discardableResult
public func set(_ value: Bool, forKey key: String, withAccess access: DYFSwiftKeychain.AccessOptions? = nil) -> Bool {
let v = value ? "1" : "0"
let data = v.data(using: String.Encoding.utf8)
return set(data, forKey: key, withAccess: access)
}
/// Retrieves the text value from the keychain by the given key.
///
/// - Parameter key: The key that is used to read the keychain item.
/// - Returns: The text value from the keychain. Nil if unable to read the item.
@discardableResult
@objc public func get(_ key: String) -> String? {
if let data = getData(key) {
if let s = String(data: data, encoding: String.Encoding.utf8) {
return s
}
osStatus = errSecInvalidEncoding // -67853, the encoding was not valid.
}
return nil
}
/// Retrieves the data from the keychain by the given key.
///
/// - Parameter key: The key that is used to read the keychain item.
/// - Returns: The data from the keychain. Nil if unable to read the item.
@discardableResult
@objc public func getData(_ key: String) -> Data? {
return getData(key, asReference: false)
}
/// Retrieves the data from the keychain by the given key.
///
/// - Parameters:
/// - key: The key that is used to read the keychain item.
/// - asReference: If true, returns the data as reference (needed for things like NEVPNProtocol).
/// - Returns: The data from the keychain. Nil if unable to read the item.
@discardableResult
@objc public func getData(_ key: String, asReference: Bool = false) -> Data? {
lock.lock()
defer { lock.unlock() }
var query: [String: Any] = supplyQueryDictionary()
query[DYFSwiftKeychain.Constants.account] = key
query[DYFSwiftKeychain.Constants.matchLimit] = kSecMatchLimitOne
if asReference {
query[DYFSwiftKeychain.Constants.returnReference] = kCFBooleanTrue
} else {
query[DYFSwiftKeychain.Constants.returnData] = kCFBooleanTrue
}
queryDictionary = query
var result: AnyObject?
osStatus = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(query as CFDictionary, UnsafeMutablePointer($0))
}
if osStatus == errSecSuccess {
return result as? Data
}
return nil
}
/// Retrieves the boolean value from the keychain by the given key.
///
/// - Parameter key: The key that is used to read the keychain item.
/// - Returns: The boolean value from the keychain. False if unable to read the item.
@discardableResult
@objc public func getBool(_ key: String) -> Bool {
guard let bool = getBool(key) else {
return false
}
return bool
}
/// Retrieves the boolean value from the keychain by the given key.
///
/// - Parameter key: The key that is used to read the keychain item.
/// - Returns: The boolean value from the keychain. Nil if unable to read the item.
@discardableResult
public func getBool(_ key: String) -> Bool? {
guard let data = getData(key) else { return nil }
guard let s = String(data: data, encoding: String.Encoding.utf8) else {
return nil
}
return Int(s)! == 1
}
/// Deletes the single keychain item by the specified key.
///
/// - Parameter key: The key which is used to delete the keychain item.
/// - Returns: True if the item was successfully deleted, false otherwise.
@discardableResult
@objc public func delete(_ key: String) -> Bool {
lock.lock()
defer { lock.unlock() }
let result = deleteWithoutLock(key)
return result
}
/// Same as `delete`, but it is not thread safe.
///
/// - Parameter key: The key which is used to delete the keychain item.
/// - Returns: True if the item was successfully deleted, false otherwise.
@discardableResult
private func deleteWithoutLock(_ key: String) -> Bool {
var query: [String: Any] = supplyQueryDictionary()
query[DYFSwiftKeychain.Constants.account] = key
queryDictionary = query
osStatus = SecItemDelete(query as CFDictionary)
return osStatus == errSecSuccess
}
/// Deletes all keychain items used by the app. Note that this method deletes all items regardless of those used keys.
///
/// - Returns: True if all keychain items was successfully deleted, false otherwise.
@discardableResult
@objc public func clear() -> Bool {
lock.lock()
defer { lock.unlock() }
let query: [String: Any] = supplyQueryDictionary()
queryDictionary = query
osStatus = SecItemDelete(query as CFDictionary)
return osStatus == errSecSuccess // 0, no error.
}
/// Supplies a query dictionary to modify the keychain item.
///
/// - Parameter shouldAddItem: Use `true` when the dictionary will be used with `SecItemAdd` or `SecItemUpadte` method. For getting and deleting items, use `false`
/// - Returns: A query dictionary to modify the keychain item.
private func supplyQueryDictionary(shouldAddItem: Bool = false) -> [String: Any] {
var query: [String: Any] = [
DYFSwiftKeychain.Constants.kClass: kSecClassGenericPassword
]
if let accessGroup = accessGroup {
query[DYFSwiftKeychain.Constants.accessGroup] = accessGroup
}
if synchronizable {
let key = DYFSwiftKeychain.Constants.synchronizable
query[key] = shouldAddItem ? kCFBooleanTrue : kSecAttrSynchronizableAny
}
if let serviceId = serviceIdentifier {
query[DYFSwiftKeychain.Constants.service] = serviceId
}
return query
}
}
// MARK: - Extension DYFSwiftKeychain
extension DYFSwiftKeychain {
/// These options are used to determine when a keychain item should be readable.
public enum AccessOptions {
/// Converts a corresponding enumeration value to a string.
var value: String {
switch self {
case .accessibleWhenUnlocked:
return kSecAttrAccessibleWhenUnlocked as String
case .accessibleWhenUnlockedThisDeviceOnly:
return kSecAttrAccessibleWhenUnlockedThisDeviceOnly as String
case .accessibleAfterFirstUnlock:
return kSecAttrAccessibleAfterFirstUnlock as String
case .accessibleAfterFirstUnlockThisDeviceOnly:
return kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly as String
case .acessibleWhenPasscodeSetThisDeviceOnly:
return kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly as String
case .accessibleAlways:
return kSecAttrAccessibleAlways as String
case .accessibleAlwaysThisDeviceOnly:
return kSecAttrAccessibleAlwaysThisDeviceOnly as String
}
}
/// The default value is accessibleWhenUnlocked
static var defaultOption: AccessOptions {
return .accessibleWhenUnlocked
}
/// The data in the keychain item can be accessed only while the device is unlocked by the user.
///
/// 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.
/// This is the default value for keychain items added without explicitly setting an accessibility constant.
case accessibleWhenUnlocked
/// The data in the keychain item can be accessed only while the device is unlocked by the user.
///
/// 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.
case accessibleWhenUnlockedThisDeviceOnly
/// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
///
/// 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.
case accessibleAfterFirstUnlock
/// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
///
/// 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.
case accessibleAfterFirstUnlockThisDeviceOnly
/// The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
///
/// 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.
case acessibleWhenPasscodeSetThisDeviceOnly
/// The data in the keychain item can always be accessed regardless of whether the device is locked.
///
/// This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
case accessibleAlways
/// The data in the keychain item can always be accessed regardless of whether the device is locked.
///
/// 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.
case accessibleAlwaysThisDeviceOnly
}
/// Constants used by the library
public struct Constants {
/// Specifies an access group which is used to share keychain items between apps.
public static var accessGroup: String {
return toString(kSecAttrAccessGroup)
}
/// The value indicates when your app needs access to the data in a keychain item.
public static var accessible: String {
return toString(kSecAttrAccessible)
}
/// The value indicates whether the item is synchronized to other devices through iCloud.
///
/// Indicates whether the item in question is synchronized to other devices through iCloud. To add a new synchronizable item, or to obtain synchronizable results from a query, supply this key with a value of kCFBooleanTrue. If the key is not supplied, or has a value of kCFBooleanFalse, then no synchronizable items are added or returned. Use kSecAttrSynchronizableAny to query for both synchronizable and non-synchronizable results.
public static var synchronizable: String {
return toString(kSecAttrSynchronizable)
}
/// A value is a string indicating the item's account name.
public static var account: String {
return toString(kSecAttrAccount)
}
/// A key whose value is a string indicating the item's service.
///
/// Represents the service associated with this item. Items of class kSecClassGenericPassword have this attribute.
public static var service: String {
return toString(kSecAttrService)
}
/// A value is the item's class.
public static var kClass: String {
return toString(kSecClass)
}
/// A value indicates the match limit.
public static var matchLimit: String {
return toString(kSecMatchLimit)
}
/// A value is the item's data.
public static var valueData: String {
return toString(kSecValueData)
}
/// A value is a Boolean indicating whether or not to return item data.
public static var returnData: String {
return toString(kSecReturnData)
}
/// A value is a Boolean indicating whether or not to return a persistent reference to an item.
public static var returnReference: String {
return toString(kSecReturnPersistentRef)
}
/// Converts a CFString object to a string.
///
/// - Parameter value: A reference to a CFString object.
/// - Returns: A string.
static func toString(_ value: CFString) -> String {
return value as String
}
}
/// Converts a DYFSwiftKeychainAccessOptions value to a DYFSwiftKeychain.AccessOptions value.
///
/// - Parameter opts: A DYFSwiftKeychainAccessOptions value.
/// - Returns: A DYFSwiftKeychain.AccessOptions value.
private func toOpts(_ opts: DYFSwiftKeychainAccessOptions) -> DYFSwiftKeychain.AccessOptions? {
var options: DYFSwiftKeychain.AccessOptions? = nil
switch opts {
case .accessibleWhenUnlocked:
options = DYFSwiftKeychain.AccessOptions.accessibleWhenUnlocked
break
case .accessibleWhenUnlockedThisDeviceOnly:
options = DYFSwiftKeychain.AccessOptions.accessibleWhenUnlockedThisDeviceOnly
break
case .accessibleAfterFirstUnlock:
options = DYFSwiftKeychain.AccessOptions.accessibleAfterFirstUnlock
break
case .accessibleAfterFirstUnlockThisDeviceOnly:
options = DYFSwiftKeychain.AccessOptions.accessibleAfterFirstUnlockThisDeviceOnly
break
case .acessibleWhenPasscodeSetThisDeviceOnly:
options = DYFSwiftKeychain.AccessOptions.acessibleWhenPasscodeSetThisDeviceOnly
break
case .accessibleAlways:
options = DYFSwiftKeychain.AccessOptions.accessibleAlways
break
case .accessibleAlwaysThisDeviceOnly:
options = DYFSwiftKeychain.AccessOptions.accessibleAlwaysThisDeviceOnly
break
default: break
}
return options
}
}
/// Used to represent accessible access options.
@objc public enum DYFSwiftKeychainAccessOptions: UInt8 {
/// The data in the keychain item can be accessed only while the device is unlocked by the user.
///
/// 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.
/// This is the default value for keychain items added without explicitly setting an accessibility constant.
case accessibleWhenUnlocked
/// The data in the keychain item can be accessed only while the device is unlocked by the user.
///
/// 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.
case accessibleWhenUnlockedThisDeviceOnly
/// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
///
/// 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.
case accessibleAfterFirstUnlock
/// The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
///
/// 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.
case accessibleAfterFirstUnlockThisDeviceOnly
/// The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
///
/// 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.
case acessibleWhenPasscodeSetThisDeviceOnly
/// The data in the keychain item can always be accessed regardless of whether the device is locked.
///
/// This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
case accessibleAlways
/// The data in the keychain item can always be accessed regardless of whether the device is locked.
///
/// 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.
case accessibleAlwaysThisDeviceOnly
}