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;
32 protected ILogger<GenericOAuthValidator>
Logger {
get; }
60 ContractResolver =
new DefaultContractResolver
62 NamingStrategy =
new SnakeCaseNamingStrategy(),
74 ILogger<GenericOAuthValidator> logger,
78 Logger = logger ??
throw new ArgumentNullException(nameof(logger));
79 OAuthConfiguration = oAuthConfiguration ??
throw new ArgumentNullException(nameof(oAuthConfiguration));
86 string tokenResponsePayload =
null;
87 string userInformationPayload =
null;
90 Logger.LogTrace(
"Validating response code...");
91 using var tokenRequest =
new HttpRequestMessage(HttpMethod.Post,
TokenUrl);
96 var tokenRequestJson = JsonConvert.SerializeObject(
100 var tokenRequestDictionary = JsonConvert.DeserializeObject<Dictionary<string, string>>(tokenRequestJson);
101 tokenRequest.Content =
new FormUrlEncodedContent(tokenRequestDictionary);
103 using var tokenResponse = await httpClient.SendAsync(tokenRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
104 tokenResponse.EnsureSuccessStatusCode();
105 tokenResponsePayload = await tokenResponse.Content.ReadAsStringAsync(cancellationToken);
106 var tokenResponseJson = JObject.Parse(tokenResponsePayload);
109 if (accessToken ==
null)
111 Logger.LogTrace(
"No token from DecodeTokenPayload!");
115 Logger.LogTrace(
"Getting user details...");
118 using var userInformationRequest =
new HttpRequestMessage(HttpMethod.Get, userInfoUrl);
119 userInformationRequest.Headers.Authorization =
new AuthenticationHeaderValue(
123 using var userInformationResponse = await httpClient.SendAsync(userInformationRequest, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
124 userInformationResponse.EnsureSuccessStatusCode();
125 userInformationPayload = await userInformationResponse.Content.ReadAsStringAsync(cancellationToken);
127 var userInformationJson = JObject.Parse(userInformationPayload);
135 "Error while completing OAuth handshake! Payload:{newLine}{responsePayload}",
137 userInformationPayload ?? tokenResponsePayload);
143 public Task<OAuthProviderInfo>
GetProviderInfo(CancellationToken cancellationToken) => Task.FromResult(
181 httpClient.
DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue(MediaTypeNames.Application.Json));
186 httpClient.Dispose();
Public information about a given OAuthProvider.
string ClientId
The client ID.
OAuth configuration options.
Uri RedirectUrl
The authentication server URL. Not used by all providers.
Uri UserInformationUrlOverride
User information URL override. Not supported by the Api.Models.OAuthProvider.GitHub provider.
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.
async Task< string > ValidateResponseCode(string code, CancellationToken cancellationToken)
Validate a given OAuth response code . A Task<TResult> resulting in null if authentication failed,...
abstract OAuthProvider Provider
The OAuthProvider this validator is for.
abstract OAuthTokenRequest CreateTokenRequest(string code)
Create the OAuthTokenRequest for a given code .
abstract string DecodeTokenPayload(dynamic responseJson)
Decode the token payload responseJson .
ILogger< GenericOAuthValidator > Logger
The ILogger for the GenericOAuthValidator.
abstract Uri UserInformationUrl
Uri to HttpMethod.Get the user information payload from.
abstract Uri TokenUrl
Uri to HttpMethod.Post to to get the access token.
abstract string DecodeUserInformationPayload(dynamic responseJson)
Decode the user information payload responseJson .
Task< OAuthProviderInfo > GetProviderInfo(CancellationToken cancellationToken)
Gets the OAuthProvider of validator. A Task<TResult> resulting in the client ID of the validator on s...
readonly IAbstractHttpClientFactory httpClientFactory
The IHttpClientFactory for the GenericOAuthValidator.
IHttpClient CreateHttpClient()
Create a new configured IHttpClient.
static JsonSerializerSettings SerializerSettings()
Gets JsonSerializerSettings that should be used.
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.