Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit aa94e9d

Browse files
author
Edward Thomson
committed
Introduce default credentials
Abstract Credentials, providing UsernamePasswordCredentials and DefaultCredentials.
1 parent 35e6cbe commit aa94e9d

File tree

10 files changed

+136
-29
lines changed

10 files changed

+136
-29
lines changed

LibGit2Sharp.Tests/CloneFixture.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ public void CanCloneWithCredentials()
142142
string clonedRepoPath = Repository.Clone(Constants.PrivateRepoUrl, scd.DirectoryPath,
143143
new CloneOptions()
144144
{
145-
Credentials = new Credentials
146-
{
147-
Username = Constants.PrivateRepoUsername,
148-
Password = Constants.PrivateRepoPassword
149-
}
145+
Credentials = Constants.PrivateRepoCredentials
150146
});
151147

152148

LibGit2Sharp.Tests/FetchFixture.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,7 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials()
6060
// Perform the actual fetch
6161
repo.Network.Fetch(remote, new FetchOptions
6262
{
63-
Credentials = new Credentials
64-
{
65-
Username = Constants.PrivateRepoUsername,
66-
Password = Constants.PrivateRepoPassword
67-
}
63+
Credentials = Constants.PrivateRepoCredentials
6864
});
6965
}
7066
}

LibGit2Sharp.Tests/TestHelpers/Constants.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,21 @@ public static class Constants
88
public const string UnknownSha = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
99
public static readonly Signature Signature = new Signature("A. U. Thor", "[email protected]", new DateTimeOffset(2011, 06, 16, 10, 58, 27, TimeSpan.FromHours(2)));
1010

11-
// Populate these to turn on live credential tests
11+
// Populate these to turn on live credential tests: set the
12+
// PrivateRepoUrl to the URL of a repository that requires
13+
// authentication. Set PrivateRepoCredentials to an instance of
14+
// UsernamePasswordCredentials (for HTTP Basic authentication) or
15+
// DefaultCredentials (for NTLM/Negotiate authentication).
16+
//
17+
// For example:
18+
// public const string PrivateRepoUrl = "https://github.com/username/PrivateRepo";
19+
// public static readonly Credentials PrivateRepoCredentials = new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
20+
//
21+
// Or:
22+
// public const string PrivateRepoUrl = "https://tfs.contoso.com/tfs/DefaultCollection/project/_git/project";
23+
// public static readonly Credentials PrivateRepoCredentials = new DefaultCredentials();
24+
1225
public const string PrivateRepoUrl = "";
13-
public const string PrivateRepoUsername = "";
14-
public const string PrivateRepoPassword = "";
26+
public static readonly Credentials PrivateRepoCredentials;
1527
}
1628
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
3+
namespace LibGit2Sharp.Core
4+
{
5+
/// <summary>
6+
/// Authentication type requested.
7+
/// </summary>
8+
[Flags]
9+
public enum GitCredentialType
10+
{
11+
/// <summary>
12+
/// A plaintext username and password.
13+
/// </summary>
14+
UserPassPlaintext = (1 << 0),
15+
16+
/// <summary>
17+
/// A ssh key from disk.
18+
/// </summary>
19+
SshKey = (1 << 1),
20+
21+
/// <summary>
22+
/// A key with a custom signature function.
23+
/// </summary>
24+
SshCustom = (1 << 2),
25+
26+
/// <summary>
27+
/// A key for NTLM/Kerberos "default" credentials.
28+
/// </summary>
29+
Default = (1 << 3),
30+
}
31+
}
32+

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,12 @@ internal delegate int git_cred_acquire_cb(
389389
out IntPtr cred,
390390
IntPtr url,
391391
IntPtr username_from_url,
392-
uint allowed_types,
392+
GitCredentialType allowed_types,
393393
IntPtr payload);
394394

395+
[DllImport(libgit2)]
396+
internal static extern int git_cred_default_new(out IntPtr cred);
397+
395398
[DllImport(libgit2)]
396399
internal static extern int git_cred_userpass_plaintext_new(
397400
out IntPtr cred,

LibGit2Sharp/Credentials.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
namespace LibGit2Sharp
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
25
{
36
/// <summary>
47
/// Class that holds credentials for remote repository access.
58
/// </summary>
6-
public sealed class Credentials
9+
public abstract class Credentials
710
{
811
/// <summary>
9-
/// Username for username/password authentication (as in HTTP basic auth).
10-
/// </summary>
11-
public string Username { get; set; }
12-
13-
/// <summary>
14-
/// Password for username/password authentication (as in HTTP basic auth).
12+
/// Callback to acquire a credential object.
1513
/// </summary>
16-
public string Password { get; set; }
14+
/// <param name="cred">The newly created credential object.</param>
15+
/// <param name="url">The resource for which we are demanding a credential.</param>
16+
/// <param name="usernameFromUrl">The username that was embedded in a "user@host"</param>
17+
/// <param name="types">A bitmask stating which cred types are OK to return.</param>
18+
/// <param name="payload">The payload provided when specifying this callback.</param>
19+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
20+
protected internal abstract int GitCredentialHandler(out IntPtr cred, IntPtr url, IntPtr usernameFromUrl, GitCredentialType types, IntPtr payload);
1721
}
1822
}

LibGit2Sharp/DefaultCredentials.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// A credential object that will provide the "default" credentials
8+
/// (logged-in user information) via NTLM or SPNEGO authentication.
9+
/// </summary>
10+
public sealed class DefaultCredentials : Credentials
11+
{
12+
/// <summary>
13+
/// Callback to acquire a credential object.
14+
/// </summary>
15+
/// <param name="cred">The newly created credential object.</param>
16+
/// <param name="url">The resource for which we are demanding a credential.</param>
17+
/// <param name="usernameFromUrl">The username that was embedded in a "user@host"</param>
18+
/// <param name="types">A bitmask stating which cred types are OK to return.</param>
19+
/// <param name="payload">The payload provided when specifying this callback.</param>
20+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
21+
protected internal override int GitCredentialHandler(out IntPtr cred, IntPtr url, IntPtr usernameFromUrl, GitCredentialType types, IntPtr payload)
22+
{
23+
return NativeMethods.git_cred_default_new(out cred);
24+
}
25+
}
26+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
<Compile Include="CommitSortStrategies.cs" />
7878
<Compile Include="CompareOptions.cs" />
7979
<Compile Include="ContentChangeStats.cs" />
80+
<Compile Include="Core\GitCredentialType.cs" />
8081
<Compile Include="Core\Handles\PatchSafeHandle.cs" />
8182
<Compile Include="Core\IntPtrExtensions.cs" />
83+
<Compile Include="DefaultCredentials.cs" />
8284
<Compile Include="FetchOptions.cs" />
8385
<Compile Include="MergeResult.cs" />
8486
<Compile Include="PatchStats.cs" />
@@ -303,6 +305,7 @@
303305
<Compile Include="TreeEntry.cs" />
304306
<Compile Include="TreeEntryDefinition.cs" />
305307
<Compile Include="UserCanceledException.cs" />
308+
<Compile Include="UsernamePasswordCredentials.cs" />
306309
<Compile Include="VoidReference.cs" />
307310
<Compile Include="Core\RawContentStream.cs" />
308311
</ItemGroup>

LibGit2Sharp/RemoteCallbacks.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal GitRemoteCallbacks GenerateCallbacks()
7474

7575
if (Credentials != null)
7676
{
77-
callbacks.acquire_credentials = GitCredentialHandler;
77+
callbacks.acquire_credentials = Credentials.GitCredentialHandler;
7878
}
7979

8080
if (DownloadTransferProgress != null)
@@ -153,11 +153,6 @@ private int GitDownloadTransferProgressHandler(ref GitTransferProgress progress,
153153
return Proxy.ConvertResultToCancelFlag(shouldContinue);
154154
}
155155

156-
private int GitCredentialHandler(out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload)
157-
{
158-
return NativeMethods.git_cred_userpass_plaintext_new(out cred, Credentials.Username, Credentials.Password);
159-
}
160-
161156
#endregion
162157
}
163158
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// Class that holds username and password credentials for remote repository access.
8+
/// </summary>
9+
public sealed class UsernamePasswordCredentials : Credentials
10+
{
11+
/// <summary>
12+
/// Callback to acquire a credential object.
13+
/// </summary>
14+
/// <param name="cred">The newly created credential object.</param>
15+
/// <param name="url">The resource for which we are demanding a credential.</param>
16+
/// <param name="usernameFromUrl">The username that was embedded in a "user@host"</param>
17+
/// <param name="types">A bitmask stating which cred types are OK to return.</param>
18+
/// <param name="payload">The payload provided when specifying this callback.</param>
19+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
20+
protected internal override int GitCredentialHandler(out IntPtr cred, IntPtr url, IntPtr usernameFromUrl, GitCredentialType types, IntPtr payload)
21+
{
22+
if (Username == null || Password == null)
23+
{
24+
throw new InvalidOperationException("UsernamePasswordCredentials contains a null Username or Password.");
25+
}
26+
27+
return NativeMethods.git_cred_userpass_plaintext_new(out cred, Username, Password);
28+
}
29+
30+
/// <summary>
31+
/// Username for username/password authentication (as in HTTP basic auth).
32+
/// </summary>
33+
public string Username { get; set; }
34+
35+
/// <summary>
36+
/// Password for username/password authentication (as in HTTP basic auth).
37+
/// </summary>
38+
public string Password { get; set; }
39+
}
40+
}

0 commit comments

Comments
 (0)