Skip to content

Restore old Credentials API as Obsolete #767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/CloneFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void CanCloneWithCredentials()
string clonedRepoPath = Repository.Clone(Constants.PrivateRepoUrl, scd.DirectoryPath,
new CloneOptions()
{
CredentialsProvider = (_url, _user, _cred) => Constants.PrivateRepoCredentials
CredentialsProvider = Constants.PrivateRepoCredentials
});


Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/FetchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials()
// Perform the actual fetch
repo.Network.Fetch(remote, new FetchOptions
{
CredentialsProvider = (_url, _user, _credtype) => Constants.PrivateRepoCredentials
CredentialsProvider = Constants.PrivateRepoCredentials
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/MetaFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
}

[Fact]
public void LibGit2SharpInterfacesCoverAllPublicMembers()
public void LibGit2SharpPublicInterfacesCoverAllPublicMembers()
{
var methodsMissingFromInterfaces =
from t in Assembly.GetAssembly(typeof(IRepository)).GetExportedTypes()
where !t.IsInterface
where t.GetInterfaces().Any(i => i.Namespace == typeof(IRepository).Namespace)
where t.GetInterfaces().Any(i => i.IsPublic && i.Namespace == typeof(IRepository).Namespace)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Travis seems unhappy with the LibGit2SharpInterfacesCoverAllPublicMembers() meta test though.

Not sure how I didn't notice this before... ICredentialsProvider is our first non-public interface; no need to test it.

let interfaceTargetMethods = from i in t.GetInterfaces()
from im in t.GetInterfaceMap(i).TargetMethods
select im
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/NetworkFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void CanListRemoteReferencesWithCredentials()
{
Remote remote = repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);

var references = repo.Network.ListReferences(remote, (_url, _user, _credtype) => Constants.PrivateRepoCredentials);
var references = repo.Network.ListReferences(remote, Constants.PrivateRepoCredentials);

foreach (var directReference in references)
{
Expand Down
13 changes: 9 additions & 4 deletions LibGit2Sharp.Tests/TestHelpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ public static class Constants

// Populate these to turn on live credential tests: set the
// PrivateRepoUrl to the URL of a repository that requires
// authentication. Set PrivateRepoCredentials to an instance of
// authentication. Define PrivateRepoCredentials to return an instance of
// UsernamePasswordCredentials (for HTTP Basic authentication) or
// DefaultCredentials (for NTLM/Negotiate authentication).
//
// For example:
// public const string PrivateRepoUrl = "https://github.com/username/PrivateRepo";
// public static readonly Credentials PrivateRepoCredentials = new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
// ... return new UsernamePasswordCredentials { Username = "username", Password = "swordfish" };
//
// Or:
// public const string PrivateRepoUrl = "https://tfs.contoso.com/tfs/DefaultCollection/project/_git/project";
// public static readonly Credentials PrivateRepoCredentials = new DefaultCredentials();
// ... return new DefaultCredentials();

public const string PrivateRepoUrl = "";
public static readonly Credentials PrivateRepoCredentials;

public static Credentials PrivateRepoCredentials(string url, string usernameFromUrl,
SupportedCredentialTypes types)
{
return null;
}
}
}
8 changes: 7 additions & 1 deletion LibGit2Sharp/CloneOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LibGit2Sharp
/// <summary>
/// Options to define clone behaviour
/// </summary>
public sealed class CloneOptions : IConvertableToGitCheckoutOpts
public sealed class CloneOptions : IConvertableToGitCheckoutOpts, ICredentialsProvider
{
/// <summary>
/// Creates default <see cref="CloneOptions"/> for a non-bare clone
Expand Down Expand Up @@ -41,6 +41,12 @@ public CloneOptions()
/// <summary>
/// Credentials to use for user/pass authentication
/// </summary>
[Obsolete("This will be removed in future release. Use CredentialsProvider.")]
public Credentials Credentials { get; set; }

/// <summary>
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
/// </summary>
public CredentialsHandler CredentialsProvider { get; set; }

#region IConvertableToGitCheckoutOpts
Expand Down
38 changes: 38 additions & 0 deletions LibGit2Sharp/Credentials.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Handlers;

namespace LibGit2Sharp
{
Expand All @@ -19,4 +20,41 @@ public abstract class Credentials
/// <returns>0 for success, &lt; 0 to indicate an error, &gt; 0 to indicate no credential was acquired.</returns>
protected internal abstract int GitCredentialHandler(out IntPtr cred, IntPtr url, IntPtr usernameFromUrl, GitCredentialType types, IntPtr payload);
}

internal interface ICredentialsProvider
{
/// <summary>
/// The <see cref="Credentials"/> to authenticate with during the push.
/// </summary>
[Obsolete("This will be removed in future release. Use CredentialsProvider.")]
Credentials Credentials { get; }

/// <summary>
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
/// </summary>
CredentialsHandler CredentialsProvider { get; }
}

internal static class CredentialsProviderExtensions
{
public static CredentialsHandler GetCredentialsHandler(this ICredentialsProvider provider)
{
if (provider == null)
{
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some braces here and below please?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some braces here and below please?

Fixed

}

if (provider.CredentialsProvider != null)
{
return provider.CredentialsProvider;
}

if (provider.Credentials == null)
{
return null;
}

return (url, user, type) => provider.Credentials;
}
}
}
8 changes: 7 additions & 1 deletion LibGit2Sharp/FetchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace LibGit2Sharp
/// <summary>
/// Collection of parameters controlling Fetch behavior.
/// </summary>
public sealed class FetchOptions
public sealed class FetchOptions : ICredentialsProvider
{
/// <summary>
/// Specifies the tag-following behavior of the fetch operation.
Expand Down Expand Up @@ -42,6 +42,12 @@ public sealed class FetchOptions
/// <summary>
/// Credentials to use for username/password authentication.
/// </summary>
[Obsolete("This will be removed in future release. Use CredentialsProvider.")]
public Credentials Credentials { get; set; }

/// <summary>
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
/// </summary>
public CredentialsHandler CredentialsProvider { get; set; }
}
}
2 changes: 1 addition & 1 deletion LibGit2Sharp/Handlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
/// <summary>
/// Delegate definition for the credentials retrieval callback
/// </summary>
/// <param name="usernameFromUrl">Username which was extracted form the url, if any</param>
/// <param name="url">The url</param>
/// <param name="usernameFromUrl">Username which was extracted from the url, if any</param>
/// <param name="types">Credential types which the server accepts</param>
public delegate Credentials CredentialsHandler(string url, string usernameFromUrl, SupportedCredentialTypes types);

Expand Down
21 changes: 20 additions & 1 deletion LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ public virtual RemoteCollection Remotes
get { return remotes.Value; }
}

/// <summary>
/// List references in a <see cref="Remote"/> repository.
/// <para>
/// When the remote tips are ahead of the local ones, the retrieved
/// <see cref="DirectReference"/>s may point to non existing
/// <see cref="GitObject"/>s in the local repository. In that
/// case, <see cref="DirectReference.Target"/> will return <c>null</c>.
/// </para>
/// </summary>
/// <param name="remote">The <see cref="Remote"/> to list from.</param>
/// <param name="credentials">The optional <see cref="Credentials"/> used to connect to remote repository.</param>
/// <returns>The references in the <see cref="Remote"/> repository.</returns>
[Obsolete("This will be removed in future release. Use the overload ListReferences(Remote, CredentialsHandler).")]
public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Credentials credentials)
{
return ListReferences(remote,
credentials == null ? null : new CredentialsHandler((url, user, type) => credentials));
}

/// <summary>
/// List references in a <see cref="Remote"/> repository.
/// <para>
Expand Down Expand Up @@ -272,7 +291,7 @@ public virtual void Push(
// Load the remote.
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
{
var callbacks = new RemoteCallbacks(null, null, null, pushOptions.CredentialsProvider);
var callbacks = new RemoteCallbacks(null, null, null, pushOptions);
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);

Expand Down
10 changes: 8 additions & 2 deletions LibGit2Sharp/PushOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ namespace LibGit2Sharp
/// <summary>
/// Collection of parameters controlling Push behavior.
/// </summary>
public sealed class PushOptions
public sealed class PushOptions : ICredentialsProvider
{
/// <summary>
/// The <see cref="Credentials"/> to authenticate with during the push.
/// The <see cref="LibGit2Sharp.Credentials"/> to authenticate with during the push.
/// </summary>
[Obsolete("This will be removed in future release. Use CredentialsProvider.")]
public Credentials Credentials { get; set; }

/// <summary>
/// Handler to generate <see cref="LibGit2Sharp.Credentials"/> for authentication.
/// </summary>
public CredentialsHandler CredentialsProvider { get; set; }

Expand Down
14 changes: 13 additions & 1 deletion LibGit2Sharp/RemoteCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ namespace LibGit2Sharp
/// </summary>
internal class RemoteCallbacks
{
internal RemoteCallbacks(
ProgressHandler onProgress = null,
TransferProgressHandler onDownloadProgress = null,
UpdateTipsHandler onUpdateTips = null,
ICredentialsProvider credentialsProvider = null)
{
Progress = onProgress;
DownloadTransferProgress = onDownloadProgress;
UpdateTips = onUpdateTips;
CredentialsProvider = credentialsProvider.GetCredentialsHandler();
}

internal RemoteCallbacks(
ProgressHandler onProgress = null,
TransferProgressHandler onDownloadProgress = null,
Expand All @@ -30,7 +42,7 @@ internal RemoteCallbacks(FetchOptions fetchOptions)
Progress = fetchOptions.OnProgress;
DownloadTransferProgress = fetchOptions.OnTransferProgress;
UpdateTips = fetchOptions.OnUpdateTips;
CredentialsProvider = fetchOptions.CredentialsProvider;
CredentialsProvider = fetchOptions.GetCredentialsHandler();
}

#region Delegates
Expand Down