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
22
22
// ... return new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
23
23
//
24
24
// Or:
25
+ // ... return new SecureUsernamePasswordCredentials() { Username = "username", Password = StringToSecureString("swordfish") };
26
+ //
27
+ // Or:
25
28
// public const string PrivateRepoUrl = "https://tfs.contoso.com/tfs/DefaultCollection/project/_git/project";
26
29
// ... return new DefaultCredentials();
27
30
@@ -68,5 +71,21 @@ public static string BuildPath()
68
71
Trace . TraceInformation ( "Test working directory set to '{0}'" , testWorkingDirectory ) ;
69
72
return testWorkingDirectory ;
70
73
}
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
+ }
71
90
}
72
91
}
Original file line number Diff line number Diff line change 143
143
<Compile Include =" RenameDetails.cs" />
144
144
<Compile Include =" RevertResult.cs" />
145
145
<Compile Include =" RevertOptions.cs" />
146
+ <Compile Include =" SecureUsernamePasswordCredentials.cs" />
146
147
<Compile Include =" StageOptions.cs" />
147
148
<Compile Include =" StatusOptions.cs" />
148
149
<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