libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.12k stars 878 forks source link

Impossible to push to Azure repository using Person Access Token #2041

Open TackyTortoise opened 1 year ago

TackyTortoise commented 1 year ago

My problem is described perfectly here, but I don't think the solution is the correct one, since that would use the credentials stored on the computer as far as I'm aware.

When making git requests to an azure repository with a PAT, it is expected to be passed along as a custom header. (see documentation).

This is possible when doing a Clone operation, since CloneOptions contains FetchOptions, which allows us to set custom headers.

So a clone is perfectly possible as follows:

string encodedToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($":{credentials.PersonalAccessToken}"));
var fetchOptions = new FetchOptions
{
    CustomHeaders = new[] {
        $"Authorization: Basic {encodedToken}"
    }
};
var options = new CloneOptions
{
    CredentialsProvider = (url, usernameFromUrl, types) =>
                    new UsernamePasswordCredentials()
                    {
                        Username = credentials.UserName,
                        Password = credentials.PersonalAccessToken
                    },
    FetchOptions = fetchOptions,
    BranchName = branchName,
    Checkout = true,
    OnCheckoutProgress = (path, completedSteps, totalSteps) =>
        Logger.LogInformation("Clone progress {completed} / {total}", completedSteps, totalSteps),
    RepositoryOperationCompleted = (context) =>
        Logger.LogInformation("Clone completed"),
};

Repository.Clone(Remote, WorkingDirectory, options);

However, when trying to push to this same repository, PushOptions does not expose something similar to include the PAT as header, even though it is stored in the internal data structure since https://github.com/libgit2/libgit2sharp/pull/1233 .

Using the provided example from the wiki I get the following error: Unhandled exception: too many redirects or authentication replays | LibGit2Sharp.LibGit2SharpException | too many redirects or authentication replays Which is the exact same error I was getting when trying to clone without the added custom header.

As far as I can see, there is a PR open that would fix this, but it is not yet merged in: https://github.com/libgit2/libgit2sharp/pull/2011

Rpatil3g commented 1 year ago

Please check TLS protocol of your system. it should be 1.2, as Azure has removed support for TLS below 1.2. here is link to set TLS 1.2 https://learn.microsoft.com/en-us/mem/configmgr/core/plan-design/security/enable-tls-1-2-client

bording commented 8 months ago

Custom headers for PushOptions are now available in LibGit2Sharp 0.28.0.