KeychainSwiftDistrib.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //n// Keychain helper for iOS/Swift.n//n// https://github.com/exchangegroup/keychain-swiftn//n// This file was automatically generated by combining multiple Swift source files.n//
  2. // ----------------------------
  3. //
  4. // KeychainSwift.swift
  5. //
  6. // ----------------------------
  7. import UIKit
  8. import Security
  9. /**
  10. A collection of helper functions for saving text and data in the keychain.
  11. */
  12. public class KeychainSwift {
  13. static var lastQueryParameters: [String: NSObject]? // Used by unit tests
  14. /**
  15. Stores the text value in the keychain item under the given key.
  16. - parameter key: Key under which the text value is stored in the keychain.
  17. - parameter value: Text string to be written to the keychain.
  18. - parameter withAccess: Value that indicates when your app needs access to the text in the keychain item. By default the .AccessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
  19. */
  20. public class func set(value: String, forKey key: String,
  21. withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
  22. if let value = value.dataUsingEncoding(NSUTF8StringEncoding) {
  23. return set(value, forKey: key, withAccess: access)
  24. }
  25. return false
  26. }
  27. /**
  28. Stores the data in the keychain item under the given key.
  29. - parameter key: Key under which the data is stored in the keychain.
  30. - parameter value: Data to be written to the keychain.
  31. - parameter withAccess: Value that indicates when your app needs access to the text in the keychain item. By default the .AccessibleWhenUnlocked option is used that permits the data to be accessed only while the device is unlocked by the user.
  32. - returns: True if the text was successfully written to the keychain.
  33. */
  34. public class func set(value: NSData, forKey key: String,
  35. withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
  36. let accessible = access?.value ?? KeychainSwiftAccessOptions.defaultOption.value
  37. let query = [
  38. KeychainSwiftConstants.klass : KeychainSwiftConstants.classGenericPassword,
  39. KeychainSwiftConstants.attrAccount : key,
  40. KeychainSwiftConstants.valueData : value,
  41. KeychainSwiftConstants.accessible : accessible
  42. ]
  43. lastQueryParameters = query
  44. SecItemDelete(query as CFDictionaryRef)
  45. let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
  46. return status == noErr
  47. }
  48. /**
  49. Retrieves the text value from the keychain that corresponds to the given key.
  50. - parameter key: The key that is used to read the keychain item.
  51. - returns: The text value from the keychain. Returns nil if unable to read the item.
  52. */
  53. public class func get(key: String) -> String? {
  54. if let data = getData(key),
  55. let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
  56. return currentString
  57. }
  58. return nil
  59. }
  60. /**
  61. Retrieves the data from the keychain that corresponds to the given key.
  62. - parameter key: The key that is used to read the keychain item.
  63. - returns: The text value from the keychain. Returns nil if unable to read the item.
  64. */
  65. public class func getData(key: String) -> NSData? {
  66. let query = [
  67. KeychainSwiftConstants.klass : kSecClassGenericPassword,
  68. KeychainSwiftConstants.attrAccount : key,
  69. KeychainSwiftConstants.returnData : kCFBooleanTrue,
  70. KeychainSwiftConstants.matchLimit : kSecMatchLimitOne ]
  71. var result: AnyObject?
  72. let status = withUnsafeMutablePointer(&result) {
  73. SecItemCopyMatching(query, UnsafeMutablePointer($0))
  74. }
  75. if status == noErr { return result as? NSData }
  76. return nil
  77. }
  78. /**
  79. Deletes the single keychain item specified by the key.
  80. - parameter key: The key that is used to delete the keychain item.
  81. - returns: True if the item was successfully deleted.
  82. */
  83. public class func delete(key: String) -> Bool {
  84. let query = [
  85. KeychainSwiftConstants.klass : kSecClassGenericPassword,
  86. KeychainSwiftConstants.attrAccount : key ]
  87. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  88. return status == noErr
  89. }
  90. /**
  91. Deletes all keychain items used by the app.
  92. - returns: True if the keychain items were successfully deleted.
  93. */
  94. public class func clear() -> Bool {
  95. let query = [ kSecClass as String : kSecClassGenericPassword ]
  96. let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
  97. return status == noErr
  98. }
  99. }
  100. // ----------------------------
  101. //
  102. // KeychainSwiftAccessOptions.swift
  103. //
  104. // ----------------------------
  105. import Security
  106. /**
  107. These options are used to determine when a keychain item should be readable. The default value is AccessibleWhenUnlocked.
  108. */
  109. public enum KeychainSwiftAccessOptions {
  110. /**
  111. The data in the keychain item can be accessed only while the device is unlocked by the user.
  112. 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.
  113. This is the default value for keychain items added without explicitly setting an accessibility constant.
  114. */
  115. case AccessibleWhenUnlocked
  116. /**
  117. The data in the keychain item can be accessed only while the device is unlocked by the user.
  118. 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.
  119. */
  120. case AccessibleWhenUnlockedThisDeviceOnly
  121. /**
  122. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  123. 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.
  124. */
  125. case AccessibleAfterFirstUnlock
  126. /**
  127. The data in the keychain item cannot be accessed after a restart until the device has been unlocked once by the user.
  128. 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.
  129. */
  130. case AccessibleAfterFirstUnlockThisDeviceOnly
  131. /**
  132. The data in the keychain item can always be accessed regardless of whether the device is locked.
  133. This is not recommended for application use. Items with this attribute migrate to a new device when using encrypted backups.
  134. */
  135. case AccessibleAlways
  136. /**
  137. The data in the keychain can only be accessed when the device is unlocked. Only available if a passcode is set on the device.
  138. 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.
  139. */
  140. case AccessibleWhenPasscodeSetThisDeviceOnly
  141. /**
  142. The data in the keychain item can always be accessed regardless of whether the device is locked.
  143. 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.
  144. */
  145. case AccessibleAlwaysThisDeviceOnly
  146. static var defaultOption: KeychainSwiftAccessOptions {
  147. return .AccessibleWhenUnlocked
  148. }
  149. var value: String {
  150. switch self {
  151. case .AccessibleWhenUnlocked:
  152. return toString(kSecAttrAccessibleWhenUnlocked)
  153. case .AccessibleWhenUnlockedThisDeviceOnly:
  154. return toString(kSecAttrAccessibleWhenUnlockedThisDeviceOnly)
  155. case .AccessibleAfterFirstUnlock:
  156. return toString(kSecAttrAccessibleAfterFirstUnlock)
  157. case .AccessibleAfterFirstUnlockThisDeviceOnly:
  158. return toString(kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly)
  159. case .AccessibleAlways:
  160. return toString(kSecAttrAccessibleAlways)
  161. case .AccessibleWhenPasscodeSetThisDeviceOnly:
  162. return toString(kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly)
  163. case .AccessibleAlwaysThisDeviceOnly:
  164. return toString(kSecAttrAccessibleAlwaysThisDeviceOnly)
  165. }
  166. }
  167. func toString(value: CFStringRef) -> String {
  168. return KeychainSwiftConstants.toString(value)
  169. }
  170. }
  171. // ----------------------------
  172. //
  173. // TegKeychainConstants.swift
  174. //
  175. // ----------------------------
  176. import Foundation
  177. import Security
  178. public struct KeychainSwiftConstants {
  179. public static var klass: String { return toString(kSecClass) }
  180. public static var classGenericPassword: String { return toString(kSecClassGenericPassword) }
  181. public static var attrAccount: String { return toString(kSecAttrAccount) }
  182. public static var valueData: String { return toString(kSecValueData) }
  183. public static var returnData: String { return toString(kSecReturnData) }
  184. public static var matchLimit: String { return toString(kSecMatchLimit) }
  185. /**
  186. A value that indicates when your app needs access to the data in a keychain item. The default value is AccessibleWhenUnlocked. For a list of possible values, see KeychainSwiftAccessOptions.
  187. */
  188. public static var accessible: String { return toString(kSecAttrAccessible) }
  189. static func toString(value: CFStringRef) -> String {
  190. return value as String
  191. }
  192. }