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

feat: Adds a prototype for token based auth #34

Merged
merged 3 commits into from
Nov 9, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.Kiota.Abstractions;
using Microsoft.Kiota.Abstractions.Authentication;

namespace GitHubAuthentication;

// TODO: Consider implementing `Bearer` authentication scheme as a separate class
// TODO: Consider implementing `Basic` authentication scheme as a separate class
// TODO: Consider implementing `Anonymous` authentication scheme as a separate class
// TODO: Consider implementing `AccessToken/Device` authentication scheme as a separate class
// TODO: Consider implementing `GitHub Apps` authentication scheme as a separate class
public class GitHubTokenAuthenticationProvider : IAuthenticationProvider
{
private const string AuthorizationHeaderKey = "Authorization";

public IAuthenticationProvider TokenAuthProvider {get; private set;}
public string ClientId { get; set; }
public string Token { get; set; }

/// <summary>
///
/// </summary>
/// <param name="clientId"></param>
/// <param name="token"></param>
/// <exception cref="ArgumentNullException"></exception>
public GitHubTokenAuthenticationProvider(string clientId, string token)
{
if (string.IsNullOrEmpty(clientId)) throw new ArgumentNullException(nameof(clientId));
if (string.IsNullOrEmpty(token)) throw new ArgumentNullException(nameof(token));

ClientId = clientId;
Token = token;

TokenAuthProvider = this;
}

/// <summary>
/// Authenticates the application request.
/// This currently acts as a synchronous (no await) method - but will change as we do more work on the authentication library.
/// </summary>
/// <param name="request"></param>
/// <param name="additionalAuthenticationContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public async Task AuthenticateRequestAsync(RequestInformation request, Dictionary<string, object>? additionalAuthenticationContext = null, CancellationToken cancellationToken = default)
{

if(request == null) throw new ArgumentNullException(nameof(request));

// This needs to be abstracted out into a configuration option but still set on each request
request.Headers.Add("X-GitHub-Api-Version", "2022-11-28");
request.Headers.Add("User-Agent", ClientId);

if (Token == string.Empty)
{
throw new ArgumentNullException(nameof(Token));
}

if(!request.Headers.ContainsKey(AuthorizationHeaderKey))
{
request.Headers.Add(AuthorizationHeaderKey, Token);
}
}
}
34 changes: 27 additions & 7 deletions generated/csharp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
using Octokit.Client;
using Microsoft.Kiota.Abstractions.Authentication;
using GitHubAuthentication;
using Microsoft.Kiota.Http.HttpClientLibrary;

// API requires no authentication, so use the anonymous
var authProvider = new AnonymousAuthenticationProvider();
var adapter = new HttpClientRequestAdapter(authProvider);
var client = new OctokitClient(adapter);
var token = Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? "";
var githubRequestAdapter = new HttpClientRequestAdapter(new GitHubTokenAuthenticationProvider("Octokit.Gen",token));

var user = await client.Users["nickfloyd"].GetAsync();
user?.AdditionalData.ToList().ForEach(x => Console.WriteLine(x.Key + ": " + x.Value));
var gitHubClient = new OctokitClient(githubRequestAdapter);
var pullRequests = await gitHubClient.Repos["octokit"]["octokit.net"].Pulls.GetAsync();

if (pullRequests == null)
{
Console.WriteLine("No pull requests found.");
return;
}

foreach(var pullRequest in pullRequests)
{
Console.WriteLine($"#{pullRequest.Number} - {pullRequest.Title}");

var pullRequestComnments = await gitHubClient.Repos["octokit"]["octokit.net"].Pulls[pullRequest.Number.Value].Comments.GetAsync();
if (pullRequestComnments == null)
{
Console.WriteLine($"#{pullRequest.Number} - {pullRequest.Title} - No reviews found.");
continue;
} else {
foreach(var comments in pullRequestComnments) {
Console.WriteLine($"#{comments.Body}\n");
}
}
}