Skip to content

Commit 86f25a5

Browse files
committed
Introduce SshAgentCredentials, SshUserKeyCredentials and SshUserCredentials.
1 parent 8000ec8 commit 86f25a5

8 files changed

+153
-0
lines changed

Diff for: LibGit2Sharp/Core/GitCredentialType.cs

+5
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ internal enum GitCredentialType
3232
/// TODO
3333
/// </summary>
3434
SshInteractive = (1 << 4),
35+
36+
/// <summary>
37+
/// Username only information.
38+
/// </summary>
39+
SshUsername = (1 << 5),
3540
}
3641
}

Diff for: LibGit2Sharp/Core/NativeMethods.cs

+18
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,24 @@ internal static extern int git_cred_userpass_plaintext_new(
398398
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof (StrictUtf8Marshaler))] string username,
399399
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof (StrictUtf8Marshaler))] string password);
400400

401+
[DllImport(libgit2)]
402+
internal static extern int git_cred_ssh_key_new(
403+
out IntPtr cred,
404+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
405+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey,
406+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey,
407+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase);
408+
409+
[DllImport(libgit2)]
410+
internal static extern int git_cred_username_new(
411+
out IntPtr cred,
412+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
413+
414+
[DllImport(libgit2)]
415+
internal static extern int git_cred_ssh_key_from_agent(
416+
out IntPtr cred,
417+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
418+
401419
[DllImport(libgit2)]
402420
internal static extern int git_describe_commit(
403421
out DescribeResultSafeHandle describe,

Diff for: LibGit2Sharp/LibGit2Sharp.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@
340340
<Compile Include="Core\RawContentStream.cs" />
341341
<Compile Include="Core\Handles\OdbStreamSafeHandle.cs" />
342342
<Compile Include="SupportedCredentialTypes.cs" />
343+
<Compile Include="SshUserKeyCredentials.cs" />
344+
<Compile Include="SshAgentCredentials.cs" />
345+
<Compile Include="UsernameQueryCredentials.cs" />
343346
</ItemGroup>
344347
<ItemGroup>
345348
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

Diff for: LibGit2Sharp/RemoteCallbacks.cs

+8
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,14 @@ private int GitCredentialHandler(out IntPtr ptr, IntPtr cUrl, IntPtr usernameFro
256256
{
257257
types |= SupportedCredentialTypes.Default;
258258
}
259+
if (credTypes.HasFlag(GitCredentialType.SshKey))
260+
{
261+
types |= SupportedCredentialTypes.Ssh;
262+
}
263+
if (credTypes.HasFlag(GitCredentialType.SshUsername))
264+
{
265+
types |= SupportedCredentialTypes.SshUsername;
266+
}
259267

260268
var cred = CredentialsProvider(url, username, types);
261269

Diff for: LibGit2Sharp/SshAgentCredentials.cs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// Class that holds SSH agent credentials for remote repository access.
8+
/// </summary>
9+
public sealed class SshAgentCredentials : Credentials
10+
{
11+
/// <summary>
12+
/// Callback to acquire a credential object.
13+
/// </summary>
14+
/// <param name="cred">The newly created credential object.</param>
15+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
16+
protected internal override int GitCredentialHandler(out IntPtr cred)
17+
{
18+
if (Username == null)
19+
{
20+
throw new InvalidOperationException("SshAgentCredentials contains a null Username.");
21+
}
22+
23+
return NativeMethods.git_cred_ssh_key_from_agent(out cred, Username);
24+
}
25+
26+
/// <summary>
27+
/// Username for SSH authentication.
28+
/// </summary>
29+
public string Username { get; set; }
30+
}
31+
}
32+

Diff for: LibGit2Sharp/SshUserKeyCredentials.cs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// Class that holds SSH username with key credentials for remote repository access.
8+
/// </summary>
9+
public sealed class SshUserKeyCredentials : Credentials
10+
{
11+
/// <summary>
12+
/// Callback to acquire a credential object.
13+
/// </summary>
14+
/// <param name="cred">The newly created credential object.</param>
15+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
16+
protected internal override int GitCredentialHandler(out IntPtr cred)
17+
{
18+
if (Username == null || PublicKey == null || PrivateKey == null || Passphrase == null)
19+
{
20+
throw new InvalidOperationException("SshCredentials contains a null Username or Key or Passphrase.");
21+
}
22+
23+
return NativeMethods.git_cred_ssh_key_new(out cred, Username, PublicKey, PrivateKey, Passphrase);
24+
}
25+
26+
/// <summary>
27+
/// Username for SSH authentication.
28+
/// </summary>
29+
public string Username { get; set; }
30+
31+
/// <summary>
32+
/// Public key file location for SSH authentication.
33+
/// </summary>
34+
public string PublicKey { get; set; }
35+
36+
/// <summary>
37+
/// Private key file location for SSH authentication.
38+
/// </summary>
39+
public string PrivateKey { get; set; }
40+
41+
/// <summary>
42+
/// Passphrase for SSH authentication.
43+
/// </summary>
44+
public string Passphrase { get; set; }
45+
}
46+
}

Diff for: LibGit2Sharp/SupportedCredentialTypes.cs

+10
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@ public enum SupportedCredentialTypes
1818
/// Ask Windows to provide its default credentials for the current user (e.g. NTLM)
1919
/// </summary>
2020
Default = (1 << 1),
21+
22+
/// <summary>
23+
/// SSH with username and public/private key. (SshUserKeyCredentials, SshAgentCredentials)
24+
/// </summary>
25+
Ssh = (1 << 2),
26+
27+
/// <summary>
28+
/// Queries the server with the specified username, then later returns the supported credential types.
29+
/// </summary>
30+
UsernameQuery = (1 << 3),
2131
}
2232
}

Diff for: LibGit2Sharp/UsernameQueryCredentials.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// Class that holds username query credentials for remote repository access.
8+
/// </summary>
9+
public sealed class SshUserCredentials : Credentials
10+
{
11+
/// <summary>
12+
/// Callback to acquire a credential object.
13+
/// </summary>
14+
/// <param name="cred">The newly created credential object.</param>
15+
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
16+
protected internal override int GitCredentialHandler(out IntPtr cred)
17+
{
18+
if (Username == null)
19+
{
20+
throw new InvalidOperationException("SshUserCredentials contains a null Username.");
21+
}
22+
23+
return NativeMethods.git_cred_username_new(out cred, Username);
24+
}
25+
26+
/// <summary>
27+
/// Username for querying the server for supported authentication.
28+
/// </summary>
29+
public string Username { get; set; }
30+
}
31+
}

0 commit comments

Comments
 (0)