2using System.Collections.Generic;
4using System.Net.Http.Headers;
7using System.Threading.Tasks;
9using Microsoft.Extensions.Logging;
11using Newtonsoft.Json.Linq;
12using Newtonsoft.Json.Serialization;
36 protected ILogger<GenericOAuthValidator>
Logger {
get; }
64 ContractResolver =
new DefaultContractResolver
66 NamingStrategy =
new SnakeCaseNamingStrategy(),
78 ILogger<GenericOAuthValidator> logger,
82 Logger = logger ??
throw new ArgumentNullException(nameof(logger));
83 OAuthConfiguration = oAuthConfiguration ??
throw new ArgumentNullException(nameof(oAuthConfiguration));
87 public async ValueTask<(
string? UserID,
string AccessCode)?>
ValidateResponseCode(
string code,
bool requireUserID, CancellationToken cancellationToken)
90 string? tokenResponsePayload =
null;
91 string? userInformationPayload =
null;
94 Logger.LogTrace(
"Validating response code...");
95 using var tokenRequest =
new HttpRequestMessage(HttpMethod.Post,
TokenUrl);
100 var tokenRequestJson = JsonConvert.SerializeObject(
104 var tokenRequestDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(tokenRequestJson)!;
105 tokenRequest.Content =
new FormUrlEncodedContent(tokenRequestDictionary);
107 using var tokenResponse = await httpClient.SendAsync(tokenRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
108 tokenResponse.EnsureSuccessStatusCode();
109 tokenResponsePayload = await tokenResponse.Content.ReadAsStringAsync(cancellationToken);
110 var tokenResponseJson = JObject.Parse(tokenResponsePayload);
113 if (accessToken ==
null)
115 Logger.LogTrace(
"No token from DecodeTokenPayload!");
120 return (
null, AccessCode: accessToken);
122 Logger.LogTrace(
"Getting user details...");
125 using var userInformationRequest =
new HttpRequestMessage(HttpMethod.Get, userInfoUrl);
126 userInformationRequest.Headers.Authorization =
new AuthenticationHeaderValue(
130 using var userInformationResponse = await httpClient.SendAsync(userInformationRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
131 userInformationResponse.EnsureSuccessStatusCode();
132 userInformationPayload = await userInformationResponse.Content.ReadAsStringAsync(cancellationToken);
134 var userInformationJson = JObject.Parse(userInformationPayload);
142 "Error while completing OAuth handshake! Payload:{newLine}{responsePayload}",
144 userInformationPayload ?? tokenResponsePayload);
189 httpClient.
DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
194 httpClient.Dispose();
Public information about a given OAuthProvider.
string? ClientId
The client ID.
OAuth configuration options.
Uri? UserInformationUrlOverride
User information URL override. Not supported by the Api.Models.OAuthProvider.GitHub provider.
Uri? RedirectUrl
The authentication server URL. Not used by all providers.
Uri? ServerUrl
The client redirect URL. Not used by all providers.
IOAuthValidator for generic OAuth2 endpoints.
GenericOAuthValidator(IAbstractHttpClientFactory httpClientFactory, ILogger< GenericOAuthValidator > logger, OAuthConfiguration oAuthConfiguration)
Initializes a new instance of the GenericOAuthValidator class.
OAuthProviderInfo GetProviderInfo()
Gets the OAuthProvider of validator.The client ID of the validator on success, null on failure.
string DecodeUserInformationPayload(dynamic responseJson)
Decode the user information payload responseJson .
ILogger< GenericOAuthValidator > Logger
The ILogger for the GenericOAuthValidator.
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...
Uri UserInformationUrl
Uri to HttpMethod.Get the user information payload from.
string DecodeTokenPayload(dynamic responseJson)
Decode the token payload responseJson .
OAuthProvider Provider
The OAuthProvider this validator is for.
Uri TokenUrl
Uri to HttpMethod.Post to to get the access token.
readonly IAbstractHttpClientFactory httpClientFactory
The IHttpClientFactory for the GenericOAuthValidator.
IHttpClient CreateHttpClient()
Create a new configured IHttpClient.
static JsonSerializerSettings SerializerSettings()
Gets JsonSerializerSettings that should be used.
OAuthTokenRequest CreateTokenRequest(string code)
Create the OAuthTokenRequest for a given code .
OAuthGatewayStatus GatewayStatus
The OAuthGatewayStatus for the IOAuthValidator.
Generic OAuth token request.
IHttpClient CreateClient()
Create a IHttpClient.
For sending HTTP requests.
HttpRequestHeaders DefaultRequestHeaders
The HttpRequestHeaders used on every request.
Validates OAuth responses for a given Provider.
OAuthProvider
List of OAuth providers supported by TGS.
OAuthGatewayStatus
Status of the OAuth gateway for a provider.