|
@@ -1,9 +1,26 @@
|
|
|
import UIKit
|
|
import UIKit
|
|
|
import Security
|
|
import Security
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+
|
|
|
|
|
+A collection of helper functions for saving text and data in the keychain.
|
|
|
|
|
+
|
|
|
|
|
+*/
|
|
|
public class TegKeychain {
|
|
public class TegKeychain {
|
|
|
|
|
|
|
|
- public class func set(key: String, value: String) -> Bool {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Store text value in the keychain item.
|
|
|
|
|
+
|
|
|
|
|
+ :param: key Key under which the text value is stored in the keychain.
|
|
|
|
|
+ :param: value Text string to be written to the keychain.
|
|
|
|
|
+ :param: 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 class func set(key: String, value: String,
|
|
|
|
|
+ withAccess access: TegKeychainAccessOptions? = nil) -> Bool {
|
|
|
|
|
+
|
|
|
if let data = value.dataUsingEncoding(NSUTF8StringEncoding) {
|
|
if let data = value.dataUsingEncoding(NSUTF8StringEncoding) {
|
|
|
return set(key, value: data)
|
|
return set(key, value: data)
|
|
|
}
|
|
}
|
|
@@ -11,11 +28,28 @@ public class TegKeychain {
|
|
|
return false
|
|
return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public class func set(key: String, value: NSData) -> Bool {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Store data in the keychain item.
|
|
|
|
|
+
|
|
|
|
|
+ :param: key Key under which the data is stored in the keychain.
|
|
|
|
|
+ :param: value Data to be written to the keychain.
|
|
|
|
|
+ :param: 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 class func set(key: String, value: NSData,
|
|
|
|
|
+ withAccess access: TegKeychainAccessOptions? = nil) -> Bool {
|
|
|
|
|
+
|
|
|
|
|
+ let accessible = access?.value ?? TegKeychainAccessOptions.defaultOption.value
|
|
|
|
|
+
|
|
|
let query = [
|
|
let query = [
|
|
|
TegKeychainConstants.klass : TegKeychainConstants.classGenericPassword,
|
|
TegKeychainConstants.klass : TegKeychainConstants.classGenericPassword,
|
|
|
TegKeychainConstants.attrAccount : key,
|
|
TegKeychainConstants.attrAccount : key,
|
|
|
- TegKeychainConstants.valueData : value ]
|
|
|
|
|
|
|
+ TegKeychainConstants.valueData : value,
|
|
|
|
|
+ TegKeychainConstants.accessible : accessible
|
|
|
|
|
+ ]
|
|
|
|
|
|
|
|
SecItemDelete(query as CFDictionaryRef)
|
|
SecItemDelete(query as CFDictionaryRef)
|
|
|
|
|
|
|
@@ -24,6 +58,14 @@ public class TegKeychain {
|
|
|
return status == noErr
|
|
return status == noErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Retrieves the text value from the keychain.
|
|
|
|
|
+
|
|
|
|
|
+ :param: 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 class func get(key: String) -> String? {
|
|
public class func get(key: String) -> String? {
|
|
|
if let data = getData(key),
|
|
if let data = getData(key),
|
|
|
let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
|
let currentString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String {
|
|
@@ -34,6 +76,14 @@ public class TegKeychain {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Retrieves the data from the keychain.
|
|
|
|
|
+
|
|
|
|
|
+ :param: 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 class func getData(key: String) -> NSData? {
|
|
public class func getData(key: String) -> NSData? {
|
|
|
let query = [
|
|
let query = [
|
|
|
TegKeychainConstants.klass : kSecClassGenericPassword,
|
|
TegKeychainConstants.klass : kSecClassGenericPassword,
|
|
@@ -52,6 +102,14 @@ public class TegKeychain {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Deletes the keychain item.
|
|
|
|
|
+
|
|
|
|
|
+ :param: key The key that is used to delete the keychain item.
|
|
|
|
|
+ :returns: True if the item was successfully deleted.
|
|
|
|
|
+
|
|
|
|
|
+ */
|
|
|
public class func delete(key: String) -> Bool {
|
|
public class func delete(key: String) -> Bool {
|
|
|
let query = [
|
|
let query = [
|
|
|
TegKeychainConstants.klass : kSecClassGenericPassword,
|
|
TegKeychainConstants.klass : kSecClassGenericPassword,
|
|
@@ -62,6 +120,13 @@ public class TegKeychain {
|
|
|
return status == noErr
|
|
return status == noErr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+
|
|
|
|
|
+ Deletes all keychain items used by the app.
|
|
|
|
|
+
|
|
|
|
|
+ :returns: True if the keychain items were successfully deleted.
|
|
|
|
|
+
|
|
|
|
|
+ */
|
|
|
public class func clear() -> Bool {
|
|
public class func clear() -> Bool {
|
|
|
let query = [ kSecClass as String : kSecClassGenericPassword ]
|
|
let query = [ kSecClass as String : kSecClassGenericPassword ]
|
|
|
|
|
|