TouchID: What is it?
It's a fingerprint recognition feature and is only available on the iPhone 5S and plus. Fingerprint data is stored on the secure enclave of the Apple A7 processor that is inside the device itself. To read more the existing and mysterious topic of secure enclave check apple security paper here.If the user's phone has been rebooted, or has not been unlocked for 48 hours, only the user's passcode, not a fingerprint, can be used to unlock the phone.
Since iOS8, you can programatically use touchID APIs to:
Keychain Access with TouchID
The existing ACL attributes on keychain:func createQueryForAddItemWithTouchID(# key: String, value: String? = nil) -> NSMutableDictionary {
var dataFromString: NSData? = value?.dataUsingEncoding(NSUTF8StringEncoding)
var error: Unmanaged?
var sac: Unmanaged
sac = SecAccessControlCreateWithFlags(kCFAllocatorDefault,
kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, .UserPresence, &error)
let retrievedData = Unmanaged.fromOpaque(sac.toOpaque()).takeUnretainedValue()
var keychainQuery = NSMutableDictionary()
keychainQuery[kSecClass] = kSecClassGenericPassword
keychainQuery[kSecAttrService] = self.serviceIdentifier
keychainQuery[kSecAttrAccount] = key
keychainQuery[kSecAttrAccessControl] = retrievedData
keychainQuery[kSecUseNoAuthenticationUI] = true
if let unwrapped = dataFromString {
keychainQuery[kSecValueData] = unwrapped
}
return keychainQuery
}
Line 4, we create an AccessControl object with the policy set to kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly. Note the flag item is set to the only possible UserPresence. we set the attribute kSecUseNoAuthenticationUI because we don't want to be prompted on Add.
Read Keychain with TouchID popping up
For the read, we only need to customize the pop-up window content with the attribute kSecUseOperationPrompt.
func createQueryForReadItemWithTouchID(# key: String, value: String? = nil) -> NSMutableDictionary {
var dataFromString: NSData? = value?.dataUsingEncoding(NSUTF8StringEncoding)
var keychainQuery = NSMutableDictionary()
keychainQuery[kSecClass] = kSecClassGenericPassword
keychainQuery[kSecAttrService] = self.serviceIdentifier
keychainQuery[kSecAttrAccount] = key
keychainQuery[kSecUseOperationPrompt] = "Do you really want to access the item?"
keychainQuery[kSecReturnData] = true
return keychainQuery
}
Things to remember
Accessibility and AccessControl work together. One key implication of using TouchID and Keychain is: the user has to authenticate using standard UI, therefore the app must be in foreground. Be aware that broad queries on Keychain may request items that need user auth. Last but not least, the keychains items stored using kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly are not synchronised or back-up on iCloud.
That's all for today, next blog post we can see how to use TouchID for LocalAuthentication and how to fallback when touchID is not available. Stay tuned!
Happy iOS8, Happy Swift!
Tweet
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.