Skip to content
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

[DOCS]: Example on how to create a GitHubClient with different authentication providers #122

Open
1 task done
ElanHasson opened this issue Sep 18, 2024 · 3 comments
Open
1 task done
Labels
Type: Documentation Improvements or additions to documentation

Comments

@ElanHasson
Copy link

Describe the need

I have a system where users can provide PATs to use the GH API via our system.

We would like to be able to create a new authentication provider each time, however since the the client factory is immutable it doesn't seem to be possible.

I'm probably missing something, any guidance is welcome!

SDK Version

No response

API Version

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@ElanHasson ElanHasson added Status: Triage This is being looked at and prioritized Type: Documentation Improvements or additions to documentation labels Sep 18, 2024
Copy link

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@kfcampbell kfcampbell moved this from 🆕 Triage to 🔥 Backlog in 🧰 Octokit Active Sep 27, 2024
@kfcampbell kfcampbell removed the Status: Triage This is being looked at and prioritized label Sep 27, 2024
@kfcampbell
Copy link
Member

Hmm...I'm not sure I understand what you're after, sorry! Do you mind providing a little more detail, and/or maybe an imaginative code snippet that would reflect what you're looking for?

@ElanHasson
Copy link
Author

ElanHasson commented Sep 30, 2024

Hi @kfcampbell!

Here are some details. I have a Multi-user system, users provide their own GH PAT.

I couldn't figure out how to modify the token directly for each request as the client disallows that.

So I cheated:

public class AsyncLocalTokenProvider : IAccessTokenProvider
{
    public static readonly AsyncLocal<string> Token = new(s => _ = s.CurrentValue);

    AllowedHostsValidator IAccessTokenProvider.AllowedHostsValidator => new();

    public Task<string> GetAuthorizationTokenAsync(Uri requestUri, Dictionary<string, object>? additionalAuthenticationContext = null, CancellationToken cancellationToken = default) => Task.FromResult(Token.Value ?? string.Empty);
}

Usage

  private static readonly HttpClientRequestAdapter Factory = new ClientFactory()
      .WithUserAgent($"InfinityFlow-{nameof(GitHubV1)}", System.Reflection.Assembly.GetAssembly(typeof(GitHubV1))?.GetName()?.Version?.ToString()
              ?? "0.0.0")
      .WithRequestTimeout(TimeSpan.FromSeconds(100))
      .WithAuthenticationProvider(new TokenAuthProvider(new GitHubAsyncLocalTokenProvider()))
      .WithBaseUrl("https://api.github.com")
      .Build();
    private static readonly GitHubClient GitHubClient = new(Factory);


 var credential = await Credential.Get();
 AsyncLocalTokenProvider.Token.Value = credential.Password;

There has to be a better way to do this with Kiota :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Documentation Improvements or additions to documentation
Projects
Status: 🔥 Backlog
Development

No branches or pull requests

2 participants