|
| 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; |
| 8 | +using System.Net.Http.Headers; |
| 9 | +using System.Security.Claims; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using AspNet.Security.OAuth.Extensions; |
| 12 | +using JetBrains.Annotations; |
| 13 | +using Microsoft.AspNetCore.Authentication; |
| 14 | +using Microsoft.AspNetCore.Authentication.OAuth; |
| 15 | +using Microsoft.AspNetCore.Http.Authentication; |
| 16 | +using Microsoft.Extensions.Logging; |
| 17 | +using Newtonsoft.Json.Linq; |
| 18 | + |
| 19 | +namespace AspNet.Security.OAuth.MailChimp { |
| 20 | + public class MailChimpAuthenticationHandler : OAuthHandler<MailChimpAuthenticationOptions> { |
| 21 | + public MailChimpAuthenticationHandler([NotNull] HttpClient client) |
| 22 | + : base(client) { |
| 23 | + } |
| 24 | + |
| 25 | + protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull] ClaimsIdentity identity, |
| 26 | + [NotNull] AuthenticationProperties properties, [NotNull] OAuthTokenResponse tokens) { |
| 27 | + var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint); |
| 28 | + request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 29 | + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); |
| 30 | + |
| 31 | + var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); |
| 32 | + if (!response.IsSuccessStatusCode) { |
| 33 | + Logger.LogError("An error occurred when retrieving the user profile: the remote server " + |
| 34 | + "returned a {Status} response with the following payload: {Headers} {Body}.", |
| 35 | + /* Status: */ response.StatusCode, |
| 36 | + /* Headers: */ response.Headers.ToString(), |
| 37 | + /* Body: */ await response.Content.ReadAsStringAsync()); |
| 38 | + |
| 39 | + throw new HttpRequestException("An error occurred when retrieving the user profile."); |
| 40 | + } |
| 41 | + |
| 42 | + var payload = JObject.Parse(await response.Content.ReadAsStringAsync()); |
| 43 | + |
| 44 | + identity.AddOptionalClaim(ClaimTypes.NameIdentifier, MailChimpAuthenticationHelper.GetIdentifier(payload), Options.ClaimsIssuer) |
| 45 | + .AddOptionalClaim(ClaimTypes.Name, MailChimpAuthenticationHelper.GetName(payload), Options.ClaimsIssuer) |
| 46 | + .AddOptionalClaim(ClaimTypes.Email, MailChimpAuthenticationHelper.GetEmail(payload), Options.ClaimsIssuer) |
| 47 | + .AddOptionalClaim(ClaimTypes.Role, MailChimpAuthenticationHelper.GetRole(payload), Options.ClaimsIssuer) |
| 48 | + .AddOptionalClaim("urn:mailchimp:dc", MailChimpAuthenticationHelper.GetDataCenter(payload), Options.ClaimsIssuer) |
| 49 | + .AddOptionalClaim("urn:mailchimp:account_name", MailChimpAuthenticationHelper.GetAccountName(payload), Options.ClaimsIssuer) |
| 50 | + .AddOptionalClaim("urn:mailchimp:login_id", MailChimpAuthenticationHelper.GetLoginId(payload), Options.ClaimsIssuer) |
| 51 | + .AddOptionalClaim("urn:mailchimp:login_email", MailChimpAuthenticationHelper.GetLoginEmail(payload), Options.ClaimsIssuer) |
| 52 | + .AddOptionalClaim("urn:mailchimp:login_url", MailChimpAuthenticationHelper.GetLoginUrl(payload), Options.ClaimsIssuer) |
| 53 | + .AddOptionalClaim("urn:mailchimp:api_endpoint", MailChimpAuthenticationHelper.GetApiEndPoint(payload), Options.ClaimsIssuer); |
| 54 | + |
| 55 | + var principal = new ClaimsPrincipal(identity); |
| 56 | + var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme); |
| 57 | + |
| 58 | + var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, payload); |
| 59 | + await Options.Events.CreatingTicket(context); |
| 60 | + |
| 61 | + return context.Ticket; |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments