| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- import Security
- import Foundation
- /**
- A collection of helper functions for saving text and data in the keychain.
- */
- public class KeychainSwift {
-
- var lastQueryParameters: [String: NSObject]? // Used by the unit tests
- var keyPrefix = "" // Can be useful in test.
-
- public init() { }
-
- /**
-
- - parameter keyPrefix: a prefix that is added before the key in get/set methods. Note that `clear` method still clears everything from the Keychain.
- */
- public init(keyPrefix: String) {
- self.keyPrefix = keyPrefix
- }
-
- /**
-
- Stores the text value in the keychain item under the given key.
-
- - parameter key: Key under which the text value is stored in the keychain.
- - parameter value: Text string to be written to the keychain.
- - 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.
- */
- public func set(value: String, forKey key: String,
- withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
-
- if let value = value.dataUsingEncoding(NSUTF8StringEncoding) {
- return set(value, forKey: key, withAccess: access)
- }
-
- return false
- }
- /**
-
- Stores the data in the keychain item under the given key.
-
- - parameter key: Key under which the data is stored in the keychain.
- - parameter value: Data to be written to the keychain.
- - 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.
-
- - returns: True if the text was successfully written to the keychain.
-
- */
- public func set(value: NSData, forKey key: String,
- withAccess access: KeychainSwiftAccessOptions? = nil) -> Bool {
- let accessible = access?.value ?? KeychainSwiftAccessOptions.defaultOption.value
-
- let prefixedKey = keyWithPrefix(key)
-
- let query = [
- KeychainSwiftConstants.klass : KeychainSwiftConstants.classGenericPassword,
- KeychainSwiftConstants.attrAccount : prefixedKey,
- KeychainSwiftConstants.valueData : value,
- KeychainSwiftConstants.accessible : accessible
- ]
-
- lastQueryParameters = query
-
- SecItemDelete(query as CFDictionaryRef)
-
- let status: OSStatus = SecItemAdd(query as CFDictionaryRef, nil)
-
- return status == noErr
- }
- /**
-
- Retrieves the text value from the keychain that corresponds to the given key.
-
- - parameter key: The key that is used to read the keychain item.
- - returns: The text value from the keychain. Returns nil if unable to read the item.
-
- */
- public func get(key: String) -> String? {
- if let data = getData(key),
- let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
- return currentString
- }
- return nil
- }
- /**
-
- Retrieves the data from the keychain that corresponds to the given key.
-
- - parameter key: The key that is used to read the keychain item.
- - returns: The text value from the keychain. Returns nil if unable to read the item.
-
- */
- public func getData(key: String) -> NSData? {
- let prefixedKey = keyWithPrefix(key)
-
- let query = [
- KeychainSwiftConstants.klass : kSecClassGenericPassword,
- KeychainSwiftConstants.attrAccount : prefixedKey,
- KeychainSwiftConstants.returnData : kCFBooleanTrue,
- KeychainSwiftConstants.matchLimit : kSecMatchLimitOne ]
-
- var result: AnyObject?
-
- let status = withUnsafeMutablePointer(&result) {
- SecItemCopyMatching(query, UnsafeMutablePointer($0))
- }
-
- if status == noErr { return result as? NSData }
-
- return nil
- }
- /**
-
- Deletes the single keychain item specified by the key.
-
- - parameter key: The key that is used to delete the keychain item.
- - returns: True if the item was successfully deleted.
-
- */
- public func delete(key: String) -> Bool {
- let prefixedKey = keyWithPrefix(key)
- let query = [
- KeychainSwiftConstants.klass : kSecClassGenericPassword,
- KeychainSwiftConstants.attrAccount : prefixedKey ]
-
- let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
-
- return status == noErr
- }
- /**
-
- Deletes all Keychain items used by the app. Note that this method deletes all items regardless of the prefix settings used for initializing the class.
-
- - returns: True if the keychain items were successfully deleted.
-
- */
- public func clear() -> Bool {
- let query = [ kSecClass as String : kSecClassGenericPassword ]
-
- let status: OSStatus = SecItemDelete(query as CFDictionaryRef)
-
- return status == noErr
- }
-
- /// Returns the key with currently set prefix.
- func keyWithPrefix(key: String) -> String {
- return "\(keyPrefix)\(key)"
- }
- }
|