KeychainItemAccessibility.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import Foundation
  2. protocol KeychainAttrRepresentable {
  3. var keychainAttrValue: CFString { get }
  4. }
  5. // MARK: - KeychainItemAccessibility
  6. public enum KeychainItemAccessibility {
  7. /**
  8. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  9. 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.
  10. */
  11. case afterFirstUnlock
  12. /**
  13. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  14. 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.
  15. */
  16. case afterFirstUnlockThisDeviceOnly
  17. /**
  18. The data in the keychain item can always be accessed regardless of whether the device is locked.
  19. This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
  20. */
  21. case whenPasscodeSetThisDeviceOnly
  22. /**
  23. The data in the keychain item can always be accessed regardless of whether the device is locked.
  24. 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.
  25. */
  26. case whenUnlocked
  27. /**
  28. The data in the keychain item can be accessed only while the device is unlocked by the user.
  29. 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.
  30. */
  31. case whenUnlockedThisDeviceOnly
  32. static func accessibilityForAttributeValue(_ keychainAttrValue: CFString) -> KeychainItemAccessibility? {
  33. let firstResult = keychainItemAccessibilityLookup.enumerated().first { $0.element.value == keychainAttrValue }
  34. return firstResult?.element.key
  35. }
  36. }
  37. private let keychainItemAccessibilityLookup: [KeychainItemAccessibility: CFString] = {
  38. var lookup: [KeychainItemAccessibility: CFString] = [
  39. .afterFirstUnlock: kSecAttrAccessibleAfterFirstUnlock,
  40. .afterFirstUnlockThisDeviceOnly: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
  41. .whenPasscodeSetThisDeviceOnly: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
  42. .whenUnlocked: kSecAttrAccessibleWhenUnlocked,
  43. .whenUnlockedThisDeviceOnly: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
  44. ]
  45. return lookup
  46. }()
  47. extension KeychainItemAccessibility: KeychainAttrRepresentable {
  48. internal var keychainAttrValue: CFString {
  49. keychainItemAccessibilityLookup[self]!
  50. }
  51. }