tgstation-server 6.12.0
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
13
15{
20 {
23
25 public OAuthGatewayStatus GatewayStatus => oAuthConfiguration.Gateway ?? default;
26
31
35 readonly ILogger<GitHubOAuthValidator> logger;
36
41
50 ILogger<GitHubOAuthValidator> logger,
52 {
53 this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
54 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
55 this.oAuthConfiguration = oAuthConfiguration ?? throw new ArgumentNullException(nameof(oAuthConfiguration));
56 }
57
59 public async ValueTask<(string? UserID, string AccessCode)?> ValidateResponseCode(string code, bool requireUserID, CancellationToken cancellationToken)
60 {
61 ArgumentNullException.ThrowIfNull(code);
62
63 try
64 {
65 logger.LogTrace("Validating response code...");
66
67 var gitHubService = await gitHubServiceFactory.CreateService(cancellationToken);
68 var token = await gitHubService.CreateOAuthAccessToken(oAuthConfiguration, code, cancellationToken);
69 if (token == null)
70 return null;
71
72 if (!requireUserID)
73 return (null, AccessCode: token);
74
75 var authenticatedClient = await gitHubServiceFactory.CreateService(token, cancellationToken);
76
77 logger.LogTrace("Getting user details...");
78 var userId = await authenticatedClient.GetCurrentUserId(cancellationToken);
79
80 return (userId.ToString(CultureInfo.InvariantCulture), AccessCode: token);
81 }
82 catch (RateLimitExceededException)
83 {
84 throw;
85 }
86 catch (ApiException ex)
87 {
88 logger.LogWarning(ex, "API error while completing OAuth handshake!");
89 return null;
90 }
91 }
92
95 => new()
96 {
98 RedirectUri = oAuthConfiguration.RedirectUrl,
99 GatewayOnly = GatewayStatus.ToBoolean(),
100 };
101 }
102}
Public information about a given OAuthProvider.
Uri? RedirectUrl
The authentication server URL. Not used by all providers.
OAuthGatewayStatus GatewayStatus
The OAuthGatewayStatus for the IOAuthValidator.
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.
readonly OAuthConfiguration oAuthConfiguration
The OAuthConfiguration for the GitHubOAuthValidator.
async ValueTask<(string? UserID, string AccessCode)?> ValidateResponseCode(string code, bool requireUserID, CancellationToken cancellationToken)
Validate a given OAuth response code .A ValueTask<TResult> resulting in null if authentication failed...
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.
ValueTask< IGitHubService > CreateService(CancellationToken cancellationToken)
Create a IGitHubService.
OAuthProvider
List of OAuth providers supported by TGS.
OAuthGatewayStatus
Status of the OAuth gateway for a provider.