KeychainSwiftAccessOptions.swift 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Security
  2. /**
  3. These options are used to determine when a keychain item should be readable. The default value is AccessibleWhenUnlocked.
  4. */
  5. public enum KeychainSwiftAccessOptions {
  6. /**
  7. The data in the keychain item can be accessed only while the device is unlocked by the user.
  8. 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.
  9. This is the default value for keychain items added without explicitly setting an accessibility constant.
  10. */
  11. case accessibleWhenUnlocked
  12. /**
  13. The data in the keychain item can be accessed only while the device is unlocked by the user.
  14. 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.
  15. */
  16. case accessibleWhenUnlockedThisDeviceOnly
  17. /**
  18. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  19. 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.
  20. */
  21. case accessibleAfterFirstUnlock
  22. /**
  23. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  24. 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.
  25. */
  26. case accessibleAfterFirstUnlockThisDeviceOnly
  27. /**
  28. The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
  29. 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.
  30. */
  31. case accessibleWhenPasscodeSetThisDeviceOnly
  32. static var defaultOption: KeychainSwiftAccessOptions {
  33. return .accessibleWhenUnlocked
  34. }
  35. var value: String {
  36. switch self {
  37. case .accessibleWhenUnlocked:
  38. return toString(kSecAttrAccessibleWhenUnlocked)
  39. case .accessibleWhenUnlockedThisDeviceOnly:
  40. return toString(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)
  41. case .accessibleAfterFirstUnlock:
  42. return toString(kSecAttrAccessibleAfterFirstUnlock)
  43. case .accessibleAfterFirstUnlockThisDeviceOnly:
  44. return toString(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)
  45. case .accessibleWhenPasscodeSetThisDeviceOnly:
  46. return toString(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly)
  47. }
  48. }
  49. func toString(_ value: CFString) -> String {
  50. return KeychainSwiftConstants.toString(value)
  51. }
  52. }