Skip to content

Commit

Permalink
Add function to load dataRef from keychain.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexrus committed Jan 21, 2015
1 parent 930401e commit 03ef4f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions VPNOnKit/VPNKeychainWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class VPNKeychainWrapper
class func passwordForVPNID(VPNID: String) -> NSData? {
let key = NSURL(string: VPNID)!.lastPathComponent!
KeychainWrapper.serviceName = kKeychainServiceName
return KeychainWrapper.dataForKey(key)
return KeychainWrapper.dataRefForKey(key)
}

class func secretForVPNID(VPNID: String) -> NSData? {
let key = NSURL(string: VPNID)!.lastPathComponent!
KeychainWrapper.serviceName = kKeychainServiceName
return KeychainWrapper.dataForKey("\(key)psk")
return KeychainWrapper.dataRefForKey("\(key)psk")
}

class func destoryKeyForVPNID(VPNID: String) {
Expand Down
25 changes: 22 additions & 3 deletions VPNOnKit/Vendor/KeychainWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,28 @@ class KeychainWrapper {
keychainQueryDictionary[SecMatchLimit] = kSecMatchLimitOne

// Specify we want NSData/CFData returned
// keychainQueryDictionary[SecReturnData] = kCFBooleanTrue
keychainQueryDictionary[SecReturnData] = kCFBooleanTrue

// Search
var searchResultRef: Unmanaged<AnyObject>?
var keychainValue: NSData?

let status: OSStatus = SecItemCopyMatching(keychainQueryDictionary, &searchResultRef)

if status == noErr {
if let resultRef = searchResultRef {
keychainValue = resultRef.takeUnretainedValue() as? NSData
}
}

return keychainValue;
}

class func dataRefForKey(keyName: String) -> NSData? {
var keychainQueryDictionary = self.setupKeychainQueryDictionaryForKey(keyName)

// Limit search results to one
keychainQueryDictionary[SecMatchLimit] = kSecMatchLimitOne

keychainQueryDictionary[SecReturnPersistentRef] = kCFBooleanTrue

Expand Down Expand Up @@ -175,8 +196,6 @@ class KeychainWrapper {

keychainQueryDictionary[SecAttrGeneric] = encodedIdentifier

keychainQueryDictionary[SecReturnPersistentRef] = kCFBooleanTrue

keychainQueryDictionary[SecAttrAccount] = encodedIdentifier

return keychainQueryDictionary
Expand Down

0 comments on commit 03ef4f2

Please sign in to comment.