Skip to content

Commit 07b6b1a

Browse files
committed
Introduce SshAgentCredentials, SshUserKeyCredentials and SshUserCredentials.
1 parent 7e84a94 commit 07b6b1a

8 files changed

+165
-0
lines changed

LibGit2Sharp/Core/GitCredentialType.cs

+5
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ public 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
}

LibGit2Sharp/Core/NativeMethods.cs

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

423+
[DllImport(libgit2)]
424+
internal static extern int git_cred_ssh_key_new(
425+
out IntPtr cred,
426+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
427+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey,
428+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey,
429+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase);
430+
431+
[DllImport(libgit2)]
432+
internal static extern int git_cred_username_new(
433+
out IntPtr cred,
434+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
435+
436+
[DllImport(libgit2)]
437+
internal static extern int git_cred_ssh_key_from_agent(
438+
out IntPtr cred,
439+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
440+
423441
[DllImport(libgit2)]
424442
internal static extern void git_diff_free(IntPtr diff);
425443

LibGit2Sharp/LibGit2Sharp.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,9 @@
337337
<Compile Include="Core\RawContentStream.cs" />
338338
<Compile Include="Core\Handles\OdbStreamSafeHandle.cs" />
339339
<Compile Include="SupportedCredentialTypes.cs" />
340+
<Compile Include="SshUserCredentials.cs" />
341+
<Compile Include="SshUserKeyCredentials.cs" />
342+
<Compile Include="SshAgentCredentials.cs" />
340343
</ItemGroup>
341344
<ItemGroup>
342345
<CodeAnalysisDictionary Include="CustomDictionary.xml" />

LibGit2Sharp/RemoteCallbacks.cs

+8
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ private int GitCredentialHandler(out IntPtr ptr, IntPtr cUrl, IntPtr usernameFro
179179
{
180180
types |= SupportedCredentialTypes.Default;
181181
}
182+
if (credTypes.HasFlag(GitCredentialType.SshKey))
183+
{
184+
types |= SupportedCredentialTypes.Ssh;
185+
}
186+
if (credTypes.HasFlag(GitCredentialType.SshUsername))
187+
{
188+
types |= SupportedCredentialTypes.SshUsername;
189+
}
182190

183191
var cred = CredentialsProvider(url, username, types);
184192

LibGit2Sharp/SshAgentCredentials.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
/// <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)
23+
{
24+
throw new InvalidOperationException("SshCredentials contains a null Username.");
25+
}
26+
27+
return NativeMethods.git_cred_ssh_key_from_agent(out cred, Username);
28+
}
29+
30+
/// <summary>
31+
/// Username for SSH authentication.
32+
/// </summary>
33+
public string Username { get; set; }
34+
}
35+
}
36+

LibGit2Sharp/SshUserCredentials.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using LibGit2Sharp.Core;
3+
4+
namespace LibGit2Sharp
5+
{
6+
/// <summary>
7+
/// Class that holds ssh username 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+
/// <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)
23+
{
24+
throw new InvalidOperationException("SshCredentials contains a null Username.");
25+
}
26+
27+
return NativeMethods.git_cred_username_new(out cred, Username);
28+
}
29+
30+
/// <summary>
31+
/// Username for SSH authentication.
32+
/// </summary>
33+
public string Username { get; set; }
34+
}
35+
}

LibGit2Sharp/SshUserKeyCredentials.cs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/// <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 || PublicKey == null || PrivateKey == null || Passphrase == null)
23+
{
24+
throw new InvalidOperationException("SshCredentials contains a null Username or Key or Passphrase.");
25+
}
26+
27+
return NativeMethods.git_cred_ssh_key_new(out cred, Username, PublicKey, PrivateKey, Passphrase);
28+
}
29+
30+
/// <summary>
31+
/// Username for SSH authentication.
32+
/// </summary>
33+
public string Username { get; set; }
34+
35+
/// <summary>
36+
/// Public key file location for SSH authentication.
37+
/// </summary>
38+
public string PublicKey { get; set; }
39+
40+
/// <summary>
41+
/// Private key file location for SSH authentication.
42+
/// </summary>
43+
public string PrivateKey { get; set; }
44+
45+
/// <summary>
46+
/// Passphrase for SSH authentication.
47+
/// </summary>
48+
public string Passphrase { get; set; }
49+
}
50+
}

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+
/// Ssh with username for supported methods querying.
29+
/// </summary>
30+
SshUsername = (1 << 3),
2131
}
2232
}

0 commit comments

Comments
 (0)