diff --git a/LibGit2Sharp/AuthenticationException.cs b/LibGit2Sharp/AuthenticationException.cs
new file mode 100644
index 000000000..acbf331ff
--- /dev/null
+++ b/LibGit2Sharp/AuthenticationException.cs
@@ -0,0 +1,55 @@
+using System;
+using System.Runtime.Serialization;
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ ///
+ /// The exception that is thrown when an operation which requires an
+ /// authentication fails.
+ ///
+ [Serializable]
+ public class AuthenticationException : LibGit2SharpException
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ public AuthenticationException()
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class with a specified error message.
+ ///
+ /// A message that describes the error.
+ public AuthenticationException(string message)
+ : base(message)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception.
+ ///
+ /// The error message that explains the reason for the exception.
+ /// The exception that is the cause of the current exception. If the parameter is not a null reference, the current exception is raised in a catch block that handles the inner exception.
+ public AuthenticationException(string message, Exception innerException)
+ : base(message, innerException)
+ {
+ }
+
+ ///
+ /// Initializes a new instance of the class with a serialized data.
+ ///
+ /// The that holds the serialized object data about the exception being thrown.
+ /// The that contains contextual information about the source or destination.
+ protected AuthenticationException(SerializationInfo info, StreamingContext context)
+ : base(info, context)
+ {
+ }
+
+ internal AuthenticationException(string message, GitErrorCode code, GitErrorCategory category)
+ : base(message, code, category)
+ {
+ }
+ }
+}
diff --git a/LibGit2Sharp/Core/Ensure.cs b/LibGit2Sharp/Core/Ensure.cs
index e0da6413b..627968993 100644
--- a/LibGit2Sharp/Core/Ensure.cs
+++ b/LibGit2Sharp/Core/Ensure.cs
@@ -127,7 +127,8 @@ private static readonly Dictionary new CheckoutConflictException(m, r, c) },
{ GitErrorCode.LockedFile, (m, r, c) => new LockedFileException(m, r, c) },
{ GitErrorCode.NotFound, (m, r, c) => new NotFoundException(m, r, c) },
- { GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
+ { GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
+ { GitErrorCode.Auth, (m, r, c) => new AuthenticationException(m, r, c) },
};
private static void HandleError(int result)
diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs
index a1b5b5cf1..3b98c9c4a 100644
--- a/LibGit2Sharp/Core/NativeMethods.cs
+++ b/LibGit2Sharp/Core/NativeMethods.cs
@@ -459,6 +459,24 @@ internal static extern int git_cred_userpass_plaintext_new(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string password);
+ [DllImport(libgit2)]
+ internal static extern int git_cred_ssh_key_new(
+ out IntPtr cred,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string publickey,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string privatekey,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string passphrase);
+
+ [DllImport(libgit2)]
+ internal static extern int git_cred_ssh_key_from_agent(
+ out IntPtr cred,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
+
+ [DllImport(libgit2)]
+ internal static extern int git_cred_username_new(
+ out IntPtr cred,
+ [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string username);
+
[DllImport(libgit2)]
internal static extern int git_describe_commit(
out DescribeResultSafeHandle describe,
diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj
index 561ad7676..51351469c 100644
--- a/LibGit2Sharp/LibGit2Sharp.csproj
+++ b/LibGit2Sharp/LibGit2Sharp.csproj
@@ -47,6 +47,7 @@
+
@@ -167,6 +168,8 @@
+
+
@@ -363,6 +366,7 @@
+
diff --git a/LibGit2Sharp/RemoteCallbacks.cs b/LibGit2Sharp/RemoteCallbacks.cs
index 42037a22e..e57756892 100644
--- a/LibGit2Sharp/RemoteCallbacks.cs
+++ b/LibGit2Sharp/RemoteCallbacks.cs
@@ -284,6 +284,14 @@ private int GitCredentialHandler(
{
types |= SupportedCredentialTypes.Default;
}
+ if (credTypes.HasFlag(GitCredentialType.SshKey))
+ {
+ types |= SupportedCredentialTypes.Ssh;
+ }
+ if (credTypes.HasFlag(GitCredentialType.Username))
+ {
+ types |= SupportedCredentialTypes.UsernameQuery;
+ }
var cred = CredentialsProvider(url, username, types);
diff --git a/LibGit2Sharp/SshAgentCredentials.cs b/LibGit2Sharp/SshAgentCredentials.cs
new file mode 100644
index 000000000..5812df2d3
--- /dev/null
+++ b/LibGit2Sharp/SshAgentCredentials.cs
@@ -0,0 +1,36 @@
+using System;
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ ///
+ /// Class that holds SSH agent credentials for remote repository access.
+ ///
+ public sealed class SshAgentCredentials : Credentials
+ {
+ ///
+ /// Callback to acquire a credential object.
+ ///
+ /// The newly created credential object.
+ /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired.
+ protected internal override int GitCredentialHandler(out IntPtr cred)
+ {
+ if (!GlobalSettings.Version.Features.HasFlag(BuiltInFeatures.Ssh))
+ {
+ throw new InvalidOperationException("LibGit2 was not built with SSH support.");
+ }
+
+ if (Username == null)
+ {
+ throw new InvalidOperationException("SshAgentCredentials contains a null Username.");
+ }
+
+ return NativeMethods.git_cred_ssh_key_from_agent(out cred, Username);
+ }
+
+ ///
+ /// Username for SSH authentication.
+ ///
+ public string Username { get; set; }
+ }
+}
diff --git a/LibGit2Sharp/SshUserKeyCredentials.cs b/LibGit2Sharp/SshUserKeyCredentials.cs
new file mode 100644
index 000000000..e5c9e4701
--- /dev/null
+++ b/LibGit2Sharp/SshUserKeyCredentials.cs
@@ -0,0 +1,66 @@
+using System;
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ ///
+ /// Class that holds SSH username with key credentials for remote repository access.
+ ///
+ public sealed class SshUserKeyCredentials : Credentials
+ {
+ ///
+ /// Callback to acquire a credential object.
+ ///
+ /// The newly created credential object.
+ /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired.
+ protected internal override int GitCredentialHandler(out IntPtr cred)
+ {
+ if (!GlobalSettings.Version.Features.HasFlag(BuiltInFeatures.Ssh))
+ {
+ throw new InvalidOperationException("LibGit2 was not built with SSH support.");
+ }
+
+ if (Username == null)
+ {
+ throw new InvalidOperationException("SshUserKeyCredentials contains a null Username.");
+ }
+
+ if (Passphrase == null)
+ {
+ throw new InvalidOperationException("SshUserKeyCredentials contains a null Passphrase.");
+ }
+
+ if (PublicKey == null)
+ {
+ throw new InvalidOperationException("SshUserKeyCredentials contains a null PublicKey.");
+ }
+
+ if (PrivateKey == null)
+ {
+ throw new InvalidOperationException("SshUserKeyCredentials contains a null PrivateKey.");
+ }
+
+ return NativeMethods.git_cred_ssh_key_new(out cred, Username, PublicKey, PrivateKey, Passphrase);
+ }
+
+ ///
+ /// Username for SSH authentication.
+ ///
+ public string Username { get; set; }
+
+ ///
+ /// Public key file location for SSH authentication.
+ ///
+ public string PublicKey { get; set; }
+
+ ///
+ /// Private key file location for SSH authentication.
+ ///
+ public string PrivateKey { get; set; }
+
+ ///
+ /// Passphrase for SSH authentication.
+ ///
+ public string Passphrase { get; set; }
+ }
+}
diff --git a/LibGit2Sharp/SupportedCredentialTypes.cs b/LibGit2Sharp/SupportedCredentialTypes.cs
index bc38a259e..077597011 100644
--- a/LibGit2Sharp/SupportedCredentialTypes.cs
+++ b/LibGit2Sharp/SupportedCredentialTypes.cs
@@ -18,5 +18,15 @@ public enum SupportedCredentialTypes
/// Ask Windows to provide its default credentials for the current user (e.g. NTLM)
///
Default = (1 << 1),
+
+ ///
+ /// SSH with username and public/private keys. (SshUserKeyCredentials, SshAgentCredentials).
+ ///
+ Ssh = (1 << 2),
+
+ ///
+ /// Queries the server with the given username, then later returns the supported credential types.
+ ///
+ UsernameQuery = (1 << 3),
}
}
diff --git a/LibGit2Sharp/UsernameQueryCredentials.cs b/LibGit2Sharp/UsernameQueryCredentials.cs
new file mode 100644
index 000000000..14981d74e
--- /dev/null
+++ b/LibGit2Sharp/UsernameQueryCredentials.cs
@@ -0,0 +1,31 @@
+using System;
+using LibGit2Sharp.Core;
+
+namespace LibGit2Sharp
+{
+ ///
+ /// Class that holds username query credentials for remote repository access.
+ ///
+ public sealed class UsernameQueryCredentials : Credentials
+ {
+ ///
+ /// Callback to acquire a credential object.
+ ///
+ /// The newly created credential object.
+ /// 0 for success, < 0 to indicate an error, > 0 to indicate no credential was acquired.
+ protected internal override int GitCredentialHandler(out IntPtr cred)
+ {
+ if (Username == null)
+ {
+ throw new InvalidOperationException("UsernameQueryCredentials contains a null Username.");
+ }
+
+ return NativeMethods.git_cred_username_new(out cred, Username);
+ }
+
+ ///
+ /// Username for querying the server for supported authentication.
+ ///
+ public string Username { get; set; }
+ }
+}