diff options
author | Alan McGovern <alan.mcgovern@gmail.com> | 2012-01-05 18:33:52 +0400 |
---|---|---|
committer | Alan McGovern <alan.mcgovern@gmail.com> | 2012-01-05 18:33:52 +0400 |
commit | ef15b84e5e3a289215a4c63ac78ef78b53673daa (patch) | |
tree | 3c6e94d083e06f74c6874642ef7c2094f2b51b06 | |
parent | 219a0f8cde73cfcbeebd6fa6f59368936f66ebe4 (diff) |
[MacInterop] Add the ability to add/retrieve passwords from the keychain
This will be useful for storing passwords for various VCS.
-rw-r--r-- | main/src/addins/MacPlatform/MacInterop/Keychain.cs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/main/src/addins/MacPlatform/MacInterop/Keychain.cs b/main/src/addins/MacPlatform/MacInterop/Keychain.cs index 80c58136d3..ba4900fca3 100644 --- a/main/src/addins/MacPlatform/MacInterop/Keychain.cs +++ b/main/src/addins/MacPlatform/MacInterop/Keychain.cs @@ -52,6 +52,29 @@ namespace MonoDevelop.MacInterop } [DllImport (SecurityLib)] + static extern OSStatus SecKeychainItemFreeContent (IntPtr attrList, IntPtr data); + + [DllImport (SecurityLib)] + static extern OSStatus SecKeychainAddGenericPassword (IntPtr keychain, uint serviceNameLength, string serviceName, + uint accountNameLength, string accountName, uint passwordLength, + string passwordData, IntPtr itemRef); + [DllImport (SecurityLib)] + static extern OSStatus SecKeychainFindGenericPassword (IntPtr keychain, uint serviceNameLength, string serviceName, + uint accountNameLength, string accountName, out uint passwordLength, + out IntPtr passwordData, IntPtr itemRef); + + [DllImport (SecurityLib)] + static extern OSStatus SecKeychainAddInternetPassword (IntPtr keychain, uint serverNameLength, string serverName, uint securityDomainLength, + string securityDomain, uint accountNameLength, string accountName, uint pathLength, + string path, ushort port, int protocol, int authenticationType, + uint passwordLength, string passwordData, IntPtr itemRef); + [DllImport (SecurityLib)] + static extern OSStatus SecKeychainFindInternetPassword (IntPtr keychain, uint serverNameLength, string serverName, uint securityDomainLength, + string securityDomain, uint accountNameLength, string accountName, uint pathLength, + string path, ushort port, int protocol, int authenticationType, + out uint passwordLength, out IntPtr passwordData, IntPtr itemRef); + + [DllImport (SecurityLib)] static extern OSStatus SecKeychainSearchCreateFromAttributes (IntPtr keychainOrArray, SecItemClass itemClass, IntPtr attrList, out IntPtr searchRef); [DllImport (SecurityLib)] @@ -316,6 +339,31 @@ namespace MonoDevelop.MacInterop { return cert.GetNameInfo (X509NameType.SimpleName, false); } + + public static void AddInternetPassword (Uri uri, string userName, string password) + { + var result = SecKeychainAddInternetPassword (IntPtr.Zero, (uint) uri.Host.Length, uri.Host, 0, null, + (uint) userName.Length, userName, (uint) uri.PathAndQuery.Length, uri.PathAndQuery, + (ushort) uri.Port, 0, 0, (uint) password.Length, password, IntPtr.Zero); + if (result != OSStatus.Ok) + throw new Exception ("Could not add internet password to keychain: " + GetError (result)); + } + + public static string FindInternetPassword (Uri uri, string userName) + { + uint passwordLength; + IntPtr password; + var result = SecKeychainFindInternetPassword (IntPtr.Zero, (uint) uri.Host.Length, uri.Host, 0, null, + (uint) userName.Length, userName, (uint) uri.PathAndQuery.Length, uri.PathAndQuery, + (ushort) uri.Port, 0, 0, out passwordLength, out password, IntPtr.Zero); + if (result == OSStatus.ItemNotFound) + return null; + + if (result != OSStatus.Ok) + throw new Exception ("Could not find internet password: " + GetError (result)); + + return Marshal.PtrToStringAuto (password, (int) passwordLength); + } enum SecItemClass : uint { |