Skip to content

Instantly share code, notes, and snippets.

@obyknovenius
Created August 3, 2016 15:47
Show Gist options
  • Save obyknovenius/58a7fdabc115d922b270dcd497e33446 to your computer and use it in GitHub Desktop.
Save obyknovenius/58a7fdabc115d922b270dcd497e33446 to your computer and use it in GitHub Desktop.
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