diff --git a/src/Tgstation.Server.Host/Security/OAuth/GitHubOAuthValidator.cs b/src/Tgstation.Server.Host/Security/OAuth/GitHubOAuthValidator.cs
index 0ee4a21c8f..520baf0484 100644
--- a/src/Tgstation.Server.Host/Security/OAuth/GitHubOAuthValidator.cs
+++ b/src/Tgstation.Server.Host/Security/OAuth/GitHubOAuthValidator.cs
@@ -21,9 +21,9 @@ namespace Tgstation.Server.Host.Security.OAuth
public OAuthProvider Provider => OAuthProvider.GitHub;
///
- /// The for the .
+ /// The for the .
///
- readonly IGitHubClientFactory gitHubClientFactory;
+ readonly IGitHubServiceFactory gitHubServiceFactory;
///
/// The for the .
@@ -38,15 +38,15 @@ namespace Tgstation.Server.Host.Security.OAuth
///
/// Initializes a new instance of the class.
///
- /// The value of .
+ /// The value of .
/// The value of .
/// The value of .
public GitHubOAuthValidator(
- IGitHubClientFactory gitHubClientFactory,
+ IGitHubServiceFactory gitHubServiceFactory,
ILogger logger,
OAuthConfiguration oAuthConfiguration)
{
- this.gitHubClientFactory = gitHubClientFactory ?? throw new ArgumentNullException(nameof(gitHubClientFactory));
+ this.gitHubServiceFactory = gitHubServiceFactory ?? throw new ArgumentNullException(nameof(gitHubServiceFactory));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.oAuthConfiguration = oAuthConfiguration ?? throw new ArgumentNullException(nameof(oAuthConfiguration));
}
@@ -57,35 +57,21 @@ namespace Tgstation.Server.Host.Security.OAuth
if (code == null)
throw new ArgumentNullException(nameof(code));
- var client = gitHubClientFactory.CreateClient();
try
{
logger.LogTrace("Validating response code...");
- var response = await client
- .Oauth
- .CreateAccessToken(
- new OauthTokenRequest(
- oAuthConfiguration.ClientId,
- oAuthConfiguration.ClientSecret,
- code)
- {
- RedirectUri = oAuthConfiguration.RedirectUrl,
- })
- ;
- var token = response.AccessToken;
+ var gitHubService = gitHubServiceFactory.CreateService();
+ var token = await gitHubService.CreateOAuthAccessToken(oAuthConfiguration, code, cancellationToken);
if (token == null)
return null;
- var authenticatedClient = gitHubClientFactory.CreateClient(token);
+ var authenticatedClient = gitHubServiceFactory.CreateService(token);
logger.LogTrace("Getting user details...");
- var userDetails = await authenticatedClient
- .User
- .Current()
- ;
+ var userId = await authenticatedClient.GetCurrentUserId(cancellationToken);
- return userDetails.Id.ToString(CultureInfo.InvariantCulture);
+ return userId.ToString(CultureInfo.InvariantCulture);
}
catch (RateLimitExceededException)
{
diff --git a/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs b/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs
index 033dd97199..a7f4dc3c0a 100644
--- a/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs
+++ b/src/Tgstation.Server.Host/Security/OAuth/OAuthProviders.cs
@@ -25,12 +25,12 @@ namespace Tgstation.Server.Host.Security.OAuth
///
/// Initializes a new instance of the class.
///
- /// The to use.
+ /// The to use.
/// The to use.
/// The to use.
/// The containing the to use.
public OAuthProviders(
- IGitHubClientFactory gitHubClientFactory,
+ IGitHubServiceFactory gitHubServiceFactory,
IAbstractHttpClientFactory httpClientFactory,
ILoggerFactory loggerFactory,
IOptions securityConfigurationOptions)
@@ -49,7 +49,7 @@ namespace Tgstation.Server.Host.Security.OAuth
if (securityConfiguration.OAuth.TryGetValue(OAuthProvider.GitHub, out var gitHubConfig))
validatorsBuilder.Add(
new GitHubOAuthValidator(
- gitHubClientFactory,
+ gitHubServiceFactory,
loggerFactory.CreateLogger(),
gitHubConfig));
diff --git a/src/Tgstation.Server.Host/Utils/GitHub/GitHubService.cs b/src/Tgstation.Server.Host/Utils/GitHub/GitHubService.cs
index 00bff4973f..129f259d6a 100644
--- a/src/Tgstation.Server.Host/Utils/GitHub/GitHubService.cs
+++ b/src/Tgstation.Server.Host/Utils/GitHub/GitHubService.cs
@@ -44,6 +44,33 @@ namespace Tgstation.Server.Host.Utils.GitHub
this.updatesConfiguration = updatesConfiguration ?? throw new ArgumentNullException(nameof(updatesConfiguration));
}
+ ///
+ public async Task CreateOAuthAccessToken(OAuthConfiguration oAuthConfiguration, string code, CancellationToken cancellationToken)
+ {
+ if (oAuthConfiguration == null)
+ throw new ArgumentNullException(nameof(oAuthConfiguration));
+
+ if (code == null)
+ throw new ArgumentNullException(nameof(code));
+
+ logger.LogTrace("CreateOAuthAccessToken");
+
+ var response = await gitHubClient
+ .Oauth
+ .CreateAccessToken(
+ new OauthTokenRequest(
+ oAuthConfiguration.ClientId,
+ oAuthConfiguration.ClientSecret,
+ code)
+ {
+ RedirectUri = oAuthConfiguration.RedirectUrl,
+ })
+ .WithToken(cancellationToken);
+
+ var token = response.AccessToken;
+ return token;
+ }
+
///
public async Task> GetTgsReleases(CancellationToken cancellationToken)
{
@@ -90,5 +117,14 @@ namespace Tgstation.Server.Host.Utils.GitHub
return repoUrl;
}
+
+ ///
+ public async Task GetCurrentUserId(CancellationToken cancellationToken)
+ {
+ logger.LogTrace("CreateOAuthAccessToken");
+
+ var userDetails = await gitHubClient.User.Current().WithToken(cancellationToken);
+ return userDetails.Id;
+ }
}
}
diff --git a/src/Tgstation.Server.Host/Utils/GitHub/IGitHubService.cs b/src/Tgstation.Server.Host/Utils/GitHub/IGitHubService.cs
index 923217e87f..d9c518ab06 100644
--- a/src/Tgstation.Server.Host/Utils/GitHub/IGitHubService.cs
+++ b/src/Tgstation.Server.Host/Utils/GitHub/IGitHubService.cs
@@ -5,6 +5,8 @@ using System.Threading.Tasks;
using Octokit;
+using Tgstation.Server.Host.Configuration;
+
namespace Tgstation.Server.Host.Utils.GitHub
{
///
@@ -26,5 +28,21 @@ namespace Tgstation.Server.Host.Utils.GitHub
/// A resulting in a of TGS s keyed by their .
/// GitHub has been known to return incomplete results from the API with this call.
Task> GetTgsReleases(CancellationToken cancellationToken);
+
+ ///
+ /// Attempt to get an OAuth token from a given .
+ ///
+ /// The . Must have , and set.
+ /// The OAuth response code.
+ /// The for the operation.
+ /// A resulting in a representing the returned OAuth code from GitHub on success, otherwise.
+ Task CreateOAuthAccessToken(OAuthConfiguration oAuthConfiguration, string code, CancellationToken cancellationToken);
+
+ ///
+ /// Get the current user's ID.
+ ///
+ /// The for the operation.
+ /// A resulting in the current user's ID.
+ Task GetCurrentUserId(CancellationToken cancellationToken);
}
}