Skip to content

Added secure string support for credentials #1050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public static class Constants
// ... return new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
//
// Or:
// ... return new SecureUsernamePasswordCredentials() { Username = "username", Password = StringToSecureString("swordfish") };
//
// Or:
// public const string PrivateRepoUrl = "https://tfs.contoso.com/tfs/DefaultCollection/project/_git/project";
// ... return new DefaultCredentials();

Expand Down Expand Up @@ -68,5 +71,21 @@ public static string BuildPath()
Trace.TraceInformation("Test working directory set to '{0}'", testWorkingDirectory);
return testWorkingDirectory;
}

// To help with creating secure strings to test with.
private static System.Security.SecureString StringToSecureString(string str)
{
var chars = str.ToCharArray();

var secure = new System.Security.SecureString();
for (var i = 0; i < chars.Length; i++)
{
secure.AppendChar(chars[i]);
}

secure.MakeReadOnly();

return secure;
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
<Compile Include="RenameDetails.cs" />
<Compile Include="RevertResult.cs" />
<Compile Include="RevertOptions.cs" />
<Compile Include="SecureUsernamePasswordCredentials.cs" />
<Compile Include="StageOptions.cs" />
<Compile Include="StatusOptions.cs" />
<Compile Include="SimilarityOptions.cs" />
Expand Down
50 changes: 50 additions & 0 deletions LibGit2Sharp/SecureUsernamePasswordCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using LibGit2Sharp.Core;
using System.Security;
using System.Runtime.InteropServices;

namespace LibGit2Sharp
{
/// <summary>
/// Class that uses <see cref="SecureString"/> to hold username and password credentials for remote repository access.
/// </summary>
public sealed class SecureUsernamePasswordCredentials : Credentials
{
/// <summary>
/// Callback to acquire a credential object.
/// </summary>
/// <param name="cred">The newly created credential object.</param>
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
protected internal override int GitCredentialHandler(out IntPtr cred)
{
if (Username == null || Password == null)
{
throw new InvalidOperationException("UsernamePasswordCredentials contains a null Username or Password.");
}

IntPtr passwordPtr = IntPtr.Zero;

try
{
passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(Password);

return NativeMethods.git_cred_userpass_plaintext_new(out cred, Username, Marshal.PtrToStringUni(passwordPtr));
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr);
}

}

/// <summary>
/// Username for username/password authentication (as in HTTP basic auth).
/// </summary>
public string Username { get; set; }

/// <summary>
/// Password for username/password authentication (as in HTTP basic auth).
/// </summary>
public SecureString Password { get; set; }
}
}