[ABANDONED] Mastodon iOS client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
5.7 KiB

6 years ago
  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. import Foundation
  28. protocol KeychainAttrRepresentable {
  29. var keychainAttrValue: CFString { get }
  30. }
  31. // MARK: - KeychainItemAccessibility
  32. public enum KeychainItemAccessibility {
  33. /**
  34. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  35. 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.
  36. */
  37. @available(iOS 4, *)
  38. case afterFirstUnlock
  39. /**
  40. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  41. 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.
  42. */
  43. @available(iOS 4, *)
  44. case afterFirstUnlockThisDeviceOnly
  45. /**
  46. The data in the keychain item can always be accessed regardless of whether the device is locked.
  47. This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
  48. */
  49. @available(iOS 4, *)
  50. case always
  51. /**
  52. The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
  53. 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.
  54. */
  55. @available(iOS 8, *)
  56. case whenPasscodeSetThisDeviceOnly
  57. /**
  58. The data in the keychain item can always be accessed regardless of whether the device is locked.
  59. 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.
  60. */
  61. @available(iOS 4, *)
  62. case alwaysThisDeviceOnly
  63. /**
  64. The data in the keychain item can be accessed only while the device is unlocked by the user.
  65. 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.
  66. This is the default value for keychain items added without explicitly setting an accessibility constant.
  67. */
  68. @available(iOS 4, *)
  69. case whenUnlocked
  70. /**
  71. The data in the keychain item can be accessed only while the device is unlocked by the user.
  72. 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.
  73. */
  74. @available(iOS 4, *)
  75. case whenUnlockedThisDeviceOnly
  76. static func accessibilityForAttributeValue(_ keychainAttrValue: CFString) -> KeychainItemAccessibility? {
  77. for (key, value) in keychainItemAccessibilityLookup {
  78. if value == keychainAttrValue {
  79. return key
  80. }
  81. }
  82. return nil
  83. }
  84. }
  85. private let keychainItemAccessibilityLookup: [KeychainItemAccessibility:CFString] = {
  86. var lookup: [KeychainItemAccessibility:CFString] = [
  87. .afterFirstUnlock: kSecAttrAccessibleAfterFirstUnlock,
  88. .afterFirstUnlockThisDeviceOnly: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
  89. .always: kSecAttrAccessibleAlways,
  90. .whenPasscodeSetThisDeviceOnly: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
  91. .alwaysThisDeviceOnly : kSecAttrAccessibleAlwaysThisDeviceOnly,
  92. .whenUnlocked: kSecAttrAccessibleWhenUnlocked,
  93. .whenUnlockedThisDeviceOnly: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
  94. ]
  95. return lookup
  96. }()
  97. extension KeychainItemAccessibility : KeychainAttrRepresentable {
  98. internal var keychainAttrValue: CFString {
  99. return keychainItemAccessibilityLookup[self]!
  100. }
  101. }