|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) |
| 3 | + * See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers |
| 4 | + * for more information concerning the license and the contributors participating to this project. |
| 5 | + */ |
| 6 | + |
| 7 | +using System.Net.Http.Headers; |
| 8 | +using System.Security.Claims; |
| 9 | +using System.Text.Encodings.Web; |
| 10 | +using System.Text.Json; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | +using Microsoft.Extensions.Options; |
| 13 | + |
| 14 | +namespace AspNet.Security.OAuth.Naver; |
| 15 | + |
| 16 | +public partial class NaverAuthenticationHandler : OAuthHandler<NaverAuthenticationOptions> |
| 17 | +{ |
| 18 | + public NaverAuthenticationHandler( |
| 19 | + [NotNull] IOptionsMonitor<NaverAuthenticationOptions> options, |
| 20 | + [NotNull] ILoggerFactory logger, |
| 21 | + [NotNull] UrlEncoder encoder, |
| 22 | + [NotNull] ISystemClock clock) |
| 23 | + : base(options, logger, encoder, clock) |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + protected override async Task<AuthenticationTicket> CreateTicketAsync( |
| 28 | + [NotNull] ClaimsIdentity identity, |
| 29 | + [NotNull] AuthenticationProperties properties, |
| 30 | + [NotNull] OAuthTokenResponse tokens) |
| 31 | + { |
| 32 | + using JsonDocument userProfile = await GetUserProfileAsync(tokens); |
| 33 | + var principal = new ClaimsPrincipal(identity); |
| 34 | + var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, userProfile.RootElement); |
| 35 | + context.RunClaimActions(userProfile.RootElement.GetProperty("response")); |
| 36 | + |
| 37 | + await Events.CreatingTicket(context); |
| 38 | + return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); |
| 39 | + } |
| 40 | + |
| 41 | + private async Task<JsonDocument> GetUserProfileAsync( |
| 42 | + [NotNull] OAuthTokenResponse tokens) |
| 43 | + { |
| 44 | + using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint); |
| 45 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 46 | + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); |
| 47 | + |
| 48 | + using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); |
| 49 | + if (!response.IsSuccessStatusCode) |
| 50 | + { |
| 51 | + await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted); |
| 52 | + throw new HttpRequestException("An error occurred while retrieving the user profile."); |
| 53 | + } |
| 54 | + |
| 55 | + return JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted)); |
| 56 | + } |
| 57 | + |
| 58 | + private static partial class Log |
| 59 | + { |
| 60 | + internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken) |
| 61 | + { |
| 62 | + UserProfileError( |
| 63 | + logger, |
| 64 | + response.StatusCode, |
| 65 | + response.Headers.ToString(), |
| 66 | + await response.Content.ReadAsStringAsync(cancellationToken)); |
| 67 | + } |
| 68 | + |
| 69 | + [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}.")] |
| 70 | + private static partial void UserProfileError( |
| 71 | + ILogger logger, |
| 72 | + System.Net.HttpStatusCode status, |
| 73 | + string headers, |
| 74 | + string body); |
| 75 | + } |
| 76 | +} |
0 commit comments