tgstation-server 6.6.1
The /tg/station 13 server suite
Loading...
Searching...
No Matches
GitHubOAuthValidator.cs
Go to the documentation of this file.
1using System;
2using System.Globalization;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Microsoft.Extensions.Logging;
7using Octokit;
8
12
14{
19 {
22
27
31 readonly ILogger<GitHubOAuthValidator> logger;
32
37
46 ILogger<GitHubOAuthValidator> logger,
48 {
49 this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
50 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
51 this.oAuthConfiguration = oAuthConfiguration ?? throw new ArgumentNullException(nameof(oAuthConfiguration));
52 }
53
55 public async ValueTask<string?> ValidateResponseCode(string code, CancellationToken cancellationToken)
56 {
57 ArgumentNullException.ThrowIfNull(code);
58
59 try
60 {
61 logger.LogTrace("Validating response code...");
62
63 var gitHubService = gitHubServiceFactory.CreateService();
64 var token = await gitHubService.CreateOAuthAccessToken(oAuthConfiguration, code, cancellationToken);
65 if (token == null)
66 return null;
67
68 var authenticatedClient = gitHubServiceFactory.CreateService(token);
69
70 logger.LogTrace("Getting user details...");
71 var userId = await authenticatedClient.GetCurrentUserId(cancellationToken);
72
73 return userId.ToString(CultureInfo.InvariantCulture);
74 }
75 catch (RateLimitExceededException)
76 {
77 throw;
78 }
79 catch (ApiException ex)
80 {
81 logger.LogWarning(ex, "API error while completing OAuth handshake!");
82 return null;
83 }
84 }
85
88 => new()
89 {
91 RedirectUri = oAuthConfiguration.RedirectUrl,
92 };
93 }
94}
Public information about a given OAuthProvider.
Uri? RedirectUrl
The authentication server URL. Not used by all providers.
GitHubOAuthValidator(IGitHubServiceFactory gitHubServiceFactory, ILogger< GitHubOAuthValidator > logger, OAuthConfiguration oAuthConfiguration)
Initializes a new instance of the GitHubOAuthValidator class.
readonly ILogger< GitHubOAuthValidator > logger
The ILogger for the GitHubOAuthValidator.
OAuthProvider Provider
The OAuthProvider this validator is for.
readonly IGitHubServiceFactory gitHubServiceFactory
The IGitHubServiceFactory for the GitHubOAuthValidator.
async ValueTask< string?> ValidateResponseCode(string code, CancellationToken cancellationToken)
Validate a given OAuth response code . A ValueTask<TResult> resulting in null if authentication faile...
readonly OAuthConfiguration oAuthConfiguration
The OAuthConfiguration for the GitHubOAuthValidator.
OAuthProviderInfo GetProviderInfo()
Gets the OAuthProvider of validator. The client ID of the validator on success, null on failure.
Validates OAuth responses for a given Provider.
IGitHubService CreateService()
Create a IGitHubService.
ValueTask< string > CreateOAuthAccessToken(OAuthConfiguration oAuthConfiguration, string code, CancellationToken cancellationToken)
Attempt to get an OAuth token from a given code .
OAuthProvider
List of OAuth providers supported by TGS.