Skip to content

Commit 19c71c0

Browse files
Therzoksevoku
authored andcommitted
Introduce SSH functionality
Added SshAgentCredentials for querying ssh-agent, SshUserKeyCredentials for authenticating with a given ssh key-pair. Introduced UsernameQueryCredentials which returns the supported credential types. Authentication exceptions are now translated from libgit2.
1 parent 616c3e4 commit 19c71c0

8 files changed

+226
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Runtime.Serialization;
3+
using LibGit2Sharp.Core;
4+
5+
namespace LibGit2Sharp
6+
{
7+
/// <summary>
8+
/// The exception that is thrown when an operation which requires an
9+
/// authentication fails.
10+
/// </summary>
11+
[Serializable]
12+
public class AuthenticationException : LibGit2SharpException
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="LibGit2Sharp.AuthenticationException"/> class.
16+
/// </summary>
17+
public AuthenticationException()
18+
{
19+
}
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="LibGit2Sharp.AuthenticationException"/> class with a specified error message.
23+
/// </summary>
24+
/// <param name="message">A message that describes the error.</param>
25+
public AuthenticationException(string message)
26+
: base(message)
27+
{
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="LibGit2Sharp.AuthenticationException"/> class with a specified error message and a reference to the inner exception that is the cause of this exception.
32+
/// </summary>
33+
/// <param name="message">The error message that explains the reason for the exception.</param>
34+
/// <param name="innerException">The exception that is the cause of the current exception. If the <paramref name="innerException"/> parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.</param>
35+
public AuthenticationException(string message, Exception innerException)
36+
: base(message, innerException)
37+
{
38+
}
39+
40+
/// <summary>
41+
/// Initializes a new instance of the <see cref="LibGit2Sharp.AuthenticationException"/> class with a serialized data.
42+
/// </summary>
43+
/// <param name="info">The <see cref="SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
44+
/// <param name="context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param>
45+
protected AuthenticationException(SerializationInfo info, StreamingContext context)
46+
: base(info, context)
47+
{
48+
}
49+
50+
internal AuthenticationException(string message, GitErrorCode code, GitErrorCategory category)
51+
: base(message, code, category)
52+
{
53+
}
54+
}
55+
}

LibGit2Sharp/Core/Ensure.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitE
127127
{ GitErrorCode.Conflict, (m, r, c) => new CheckoutConflictException(m, r, c) },
128128
{ GitErrorCode.LockedFile, (m, r, c) => new LockedFileException(m, r, c) },
129129
{ GitErrorCode.NotFound, (m, r, c) => new NotFoundException(m, r, c) },
130-
{ GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
130+
{ GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
131+
{ GitErrorCode.Auth, (m, r, c) => new AuthenticationException(m, r, c) },
131132
};
132133

133134
private static unsafe void HandleError(int result)

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,24 @@ internal static extern int git_cred_userpass_plaintext_new(
512512
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
513513
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string password);
514514

515+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
516+
internal static extern int git_cred_ssh_key_new(
517+
out IntPtr cred,
518+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
519+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey,
520+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey,
521+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase);
522+
523+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
524+
internal static extern int git_cred_ssh_key_from_agent(
525+
out IntPtr cred,
526+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
527+
528+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
529+
internal static extern int git_cred_username_new(
530+
out IntPtr cred,
531+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
532+
515533
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
516534
internal static extern void git_cred_free(IntPtr cred);
517535

LibGit2Sharp/RemoteCallbacks.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ private int GitCredentialHandler(
284284
{
285285
types |= SupportedCredentialTypes.Default;
286286
}
287+
if (credTypes.HasFlag(GitCredentialType.SshKey))
288+
{
289+
types |= SupportedCredentialTypes.Ssh;
290+
}
291+
if (credTypes.HasFlag(GitCredentialType.Username))
292+
{
293+
types |= SupportedCredentialTypes.UsernameQuery;
294+
}
287295

288296
ptr = IntPtr.Zero;
289297
try

LibGit2Sharp/SshAgentCredentials.cs

Lines changed: 36 additions & 0 deletions
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+
/// <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 (!GlobalSettings.Version.Features.HasFlag(BuiltInFeatures.Ssh))
19+
{
20+
throw new InvalidOperationException("LibGit2 was not built with SSH support.");
21+
}
22+
23+
if (Username == null)
24+
{
25+
throw new InvalidOperationException("SshAgentCredentials contains a null Username.");
26+
}
27+
28+
return NativeMethods.git_cred_ssh_key_from_agent(out cred, Username);
29+
}
30+
31+
/// <summary>
32+
/// Username for SSH authentication.
33+
/// </summary>
34+
public string Username { get; set; }
35+
}
36+
}

LibGit2Sharp/SshUserKeyCredentials.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 (!GlobalSettings.Version.Features.HasFlag(BuiltInFeatures.Ssh))
19+
{
20+
throw new InvalidOperationException("LibGit2 was not built with SSH support.");
21+
}
22+
23+
if (Username == null)
24+
{
25+
throw new InvalidOperationException("SshUserKeyCredentials contains a null Username.");
26+
}
27+
28+
if (Passphrase == null)
29+
{
30+
throw new InvalidOperationException("SshUserKeyCredentials contains a null Passphrase.");
31+
}
32+
33+
if (PublicKey == null)
34+
{
35+
throw new InvalidOperationException("SshUserKeyCredentials contains a null PublicKey.");
36+
}
37+
38+
if (PrivateKey == null)
39+
{
40+
throw new InvalidOperationException("SshUserKeyCredentials contains a null PrivateKey.");
41+
}
42+
43+
return NativeMethods.git_cred_ssh_key_new(out cred, Username, PublicKey, PrivateKey, Passphrase);
44+
}
45+
46+
/// <summary>
47+
/// Username for SSH authentication.
48+
/// </summary>
49+
public string Username { get; set; }
50+
51+
/// <summary>
52+
/// Public key file location for SSH authentication.
53+
/// </summary>
54+
public string PublicKey { get; set; }
55+
56+
/// <summary>
57+
/// Private key file location for SSH authentication.
58+
/// </summary>
59+
public string PrivateKey { get; set; }
60+
61+
/// <summary>
62+
/// Passphrase for SSH authentication.
63+
/// </summary>
64+
public string Passphrase { get; set; }
65+
}
66+
}

LibGit2Sharp/SupportedCredentialTypes.cs

Lines changed: 10 additions & 0 deletions
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 keys. (SshUserKeyCredentials, SshAgentCredentials).
24+
/// </summary>
25+
Ssh = (1 << 2),
26+
27+
/// <summary>
28+
/// Queries the server with the given username, then later returns the supported credential types.
29+
/// </summary>
30+
UsernameQuery = (1 << 3),
2131
}
2232
}
Lines changed: 31 additions & 0 deletions
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 UsernameQueryCredentials : 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("UsernameQueryCredentials 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)