I am trying to get the connected wifi password from keychain access in one of the macOS application and am able to get the password using the following code:
There are two ways to get the wifi password:
1. Via Security Framework Api SecItemCopyMatching(_:_:)
let query = [
kSecClass as String: kSecClassGenericPassword as String,
kSecAttrAccount as String: accountName,
kSecReturnData as String: kCFBooleanTrue as Any,
kSecMatchLimit as String: kSecMatchLimitOne
] as [String : Any]
var dataTypeRef: AnyObject?
let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataTypeRef)
var data: Data?
if status == noErr{
data = dataTypeRef as? Data
print(data!)
print(String(decoding: data!, as: UTF8.self))
}
return data != nil ? NSString(data: data!, encoding: String.Encoding.utf8.rawValue)! as String : ""
2. Via CoreWLAN Framework Api CWKeychainFindWiFiPassword(_:_:_:), By passing the SSID data, we can get the wifi password.
func getPassword(data:Data) -> String {
var responseData:NSString? = nil
var thePassword:String = ""
var status:OSStatus?
if data.count > 0
{
status = CWKeychainFindWiFiPassword(CWKeychainDomain.system, data, &responseData)
if status == noErr {
thePassword = responseData! as String
}
}
return thePassword
}
Query:
Everytime while executing the either way keychain access is poping the modal window to provide admin credentials to acess the password.
Let me know if there is any way to bypass the above modal window by supplying the username and passoword in the code (background).