/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers * for more information concerning the license and the contributors participating to this project. */ using System.Net.Http; using System.Net.Http.Headers; using System.Security.Claims; using System.Text.Encodings.Web; using System.Text.Json; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace AspNet.Security.OAuth.<%= name %>; /// /// Defines a handler for authentication using <%= name %>. /// public partial class <%= name %>AuthenticationHandler : OAuthHandler<<%= name %>AuthenticationOptions> { /// /// Initializes a new instance of the class. /// /// The authentication options. /// The logger to use. /// The URL encoder to use. public <%= name %>AuthenticationHandler( [NotNull] IOptionsMonitor<<%= name %>AuthenticationOptions> options, [NotNull] ILoggerFactory logger, [NotNull] UrlEncoder encoder) : base(options, logger, encoder) { } /// protected override async Task CreateTicketAsync( [NotNull] ClaimsIdentity identity, [NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens) { var endpoint = Options.UserInformationEndpoint; // TODO Append any additional query string parameters required ////endpoint = QueryHelpers.AddQueryString(endpoint, "token", tokens.AccessToken); using var request = new HttpRequestMessage(HttpMethod.Get, endpoint); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // TODO Add any HTTP request headers required ////request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); if (!response.IsSuccessStatusCode) { await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted); throw new HttpRequestException("An error occurred while retrieving the user profile from <%= name %>."); } using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync()); var principal = new ClaimsPrincipal(identity); var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement); context.RunClaimActions(); await Events.CreatingTicket(context); return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); } private static partial class Log { internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken) { UserProfileError( logger, response.StatusCode, response.Headers.ToString(), await response.Content.ReadAsStringAsync(cancellationToken)); } [LoggerMessage(1, LogLevel.Error, "An error occurred while retrieving the user profile: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")] private static partial void UserProfileError( ILogger logger, System.Net.HttpStatusCode status, string headers, string body); } }