File tree Expand file tree Collapse file tree 3 files changed +70
-0
lines changed
LibGit2Sharp.Tests/TestHelpers Expand file tree Collapse file tree 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,9 @@ public static class Constants
2222 // ... return new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
2323 //
2424 // Or:
25+ // ... return new SecureUsernamePasswordCredentials() { Username = "username", Password = StringToSecureString("swordfish") };
26+ //
27+ // Or:
2528 // public const string PrivateRepoUrl = "https://tfs.contoso.com/tfs/DefaultCollection/project/_git/project";
2629 // ... return new DefaultCredentials();
2730
@@ -68,5 +71,21 @@ public static string BuildPath()
6871 Trace . TraceInformation ( "Test working directory set to '{0}'" , testWorkingDirectory ) ;
6972 return testWorkingDirectory ;
7073 }
74+
75+ // To help with creating secure strings to test with.
76+ private static System . Security . SecureString StringToSecureString ( string str )
77+ {
78+ var chars = str . ToCharArray ( ) ;
79+
80+ var secure = new System . Security . SecureString ( ) ;
81+ for ( var i = 0 ; i < chars . Length ; i ++ )
82+ {
83+ secure . AppendChar ( chars [ i ] ) ;
84+ }
85+
86+ secure . MakeReadOnly ( ) ;
87+
88+ return secure ;
89+ }
7190 }
7291}
Original file line number Diff line number Diff line change 143143 <Compile Include =" RenameDetails.cs" />
144144 <Compile Include =" RevertResult.cs" />
145145 <Compile Include =" RevertOptions.cs" />
146+ <Compile Include =" SecureUsernamePasswordCredentials.cs" />
146147 <Compile Include =" StageOptions.cs" />
147148 <Compile Include =" StatusOptions.cs" />
148149 <Compile Include =" SimilarityOptions.cs" />
Original file line number Diff line number Diff line change 1+ using System ;
2+ using LibGit2Sharp . Core ;
3+ using System . Security ;
4+ using System . Runtime . InteropServices ;
5+
6+ namespace LibGit2Sharp
7+ {
8+ /// <summary>
9+ /// Class that uses <see cref="SecureString"/> to hold username and password credentials for remote repository access.
10+ /// </summary>
11+ public sealed class SecureUsernamePasswordCredentials : Credentials
12+ {
13+ /// <summary>
14+ /// Callback to acquire a credential object.
15+ /// </summary>
16+ /// <param name="cred">The newly created credential object.</param>
17+ /// <returns>0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired.</returns>
18+ protected internal override int GitCredentialHandler ( out IntPtr cred )
19+ {
20+ if ( Username == null || Password == null )
21+ {
22+ throw new InvalidOperationException ( "UsernamePasswordCredentials contains a null Username or Password." ) ;
23+ }
24+
25+ IntPtr passwordPtr = IntPtr . Zero ;
26+
27+ try
28+ {
29+ passwordPtr = Marshal . SecureStringToGlobalAllocUnicode ( Password ) ;
30+
31+ return NativeMethods . git_cred_userpass_plaintext_new ( out cred , Username , Marshal . PtrToStringUni ( passwordPtr ) ) ;
32+ }
33+ finally
34+ {
35+ Marshal . ZeroFreeGlobalAllocUnicode ( passwordPtr ) ;
36+ }
37+
38+ }
39+
40+ /// <summary>
41+ /// Username for username/password authentication (as in HTTP basic auth).
42+ /// </summary>
43+ public string Username { get ; set ; }
44+
45+ /// <summary>
46+ /// Password for username/password authentication (as in HTTP basic auth).
47+ /// </summary>
48+ public SecureString Password { get ; set ; }
49+ }
50+ }
You can’t perform that action at this time.
0 commit comments