Created
August 3, 2016 15:47
-
-
Save obyknovenius/58a7fdabc115d922b270dcd497e33446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func make(withKeychainItem keychainItem: SecKeychainItem) -> Credentials? { | |
var attributeTags = [SecItemAttr.accountItemAttr.rawValue] | |
var formatConstants = [UInt32(CSSM_DB_ATTRIBUTE_FORMAT_STRING)] | |
var attributeInfo = SecKeychainAttributeInfo(count: 1, tag: &attributeTags, format: &formatConstants) | |
var attributeList: UnsafeMutablePointer<SecKeychainAttributeList>? = nil | |
var passwordLength: UInt32 = 0 | |
var passwordPointer: UnsafeMutablePointer<Void>? = nil | |
let status = SecKeychainItemCopyAttributesAndData(keychainItem, | |
&attributeInfo, nil, &attributeList, | |
&passwordLength, &passwordPointer) | |
guard status == errSecSuccess else { | |
return nil | |
} | |
let accountAttribute = attributeList?.pointee.attr.pointee | |
let account = String(bytesNoCopy: (accountAttribute?.data)!, length: Int((accountAttribute?.length)!), | |
encoding: String.Encoding.utf8, freeWhenDone: false)! | |
let password = String(bytesNoCopy: passwordPointer!, length: Int(passwordLength), | |
encoding: String.Encoding.utf8, freeWhenDone: false)! | |
SecKeychainItemFreeAttributesAndData(attributeList, passwordPointer) | |
return Credentials(email: account, password: password) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment