libgit2 / libgit2/libgit2sharp

Impossible to push to Azure repository using Person Access Token

Open
#2,041 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
3.5k
Forks
925
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing PushOptions and comparing its available configuration with FetchOptions, then read pull request #2011 and the git-push wiki example. The work is done when pushing to an Azure repository can pass the PAT as a custom authorization header without authentication replay errors; add or update the relevant tests if the existing subsystem provides them.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp, git
Domain
authentication, devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.