Skip to content

Commit 1101b10

Browse files
haydenhancockkevinchalet
authored andcommitted
Migrate the MailChimp provider to ASP.NET Core 2.0
1 parent 11764ea commit 1101b10

8 files changed

+72
-257
lines changed

samples/Mvc.Client/Mvc.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Imgur\AspNet.Security.OAuth.Imgur.csproj" />
3030
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Instagram\AspNet.Security.OAuth.Instagram.csproj" />
3131
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.LinkedIn\AspNet.Security.OAuth.LinkedIn.csproj" />
32+
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.MailChimp\AspNet.Security.OAuth.MailChimp.csproj" />
3233
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Myob\AspNet.Security.OAuth.Myob.csproj" />
3334
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Onshape\AspNet.Security.OAuth.Onshape.csproj" />
3435
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Patreon\AspNet.Security.OAuth.Patreon.csproj" />

src/AspNet.Security.OAuth.MailChimp/AspNet.Security.OAuth.MailChimp.csproj

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,15 @@
33
<Import Project="..\..\build\packages.props" />
44

55
<PropertyGroup>
6-
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
6+
<TargetFramework>netstandard2.0</TargetFramework>
77
</PropertyGroup>
88

99
<PropertyGroup>
1010
<Description>ASP.NET Core security middleware enabling MailChimp authentication.</Description>
11-
<Authors>Igor Simovic</Authors>
11+
<Authors>Igor Simovic;Hayden Hancock</Authors>
1212
<PackageTags>aspnetcore;authentication;mailchimp;oauth;security</PackageTags>
1313
</PropertyGroup>
1414

15-
<ItemGroup>
16-
<Compile Include="..\..\shared\AspNet.Security.OAuth.Extensions\*.cs" />
17-
</ItemGroup>
18-
1915
<ItemGroup>
2016
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
2117
<PackageReference Include="Microsoft.AspNetCore.Authentication.OAuth" Version="$(AspNetCoreVersion)" />

src/AspNet.Security.OAuth.MailChimp/MailChimpAuthenticationDefaults.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

7-
using Microsoft.AspNetCore.Builder;
7+
using Microsoft.AspNetCore.Authentication;
8+
using Microsoft.AspNetCore.Authentication.OAuth;
89

910
namespace AspNet.Security.OAuth.MailChimp
1011
{
@@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.MailChimp
1415
public static class MailChimpAuthenticationDefaults
1516
{
1617
/// <summary>
17-
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>.
18+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
1819
/// </summary>
1920
public const string AuthenticationScheme = "MailChimp";
2021

2122
/// <summary>
22-
/// Default value for <see cref="RemoteAuthenticationOptions.DisplayName"/>.
23+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
2324
/// </summary>
2425
public const string DisplayName = "MailChimp";
2526

2627
/// <summary>
27-
/// Default value for <see cref="AuthenticationOptions.ClaimsIssuer"/>.
28+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
2829
/// </summary>
2930
public const string Issuer = "MailChimp";
3031

src/AspNet.Security.OAuth.MailChimp/MailChimpAuthenticationExtensions.cs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,70 @@
77
using System;
88
using AspNet.Security.OAuth.MailChimp;
99
using JetBrains.Annotations;
10-
using Microsoft.Extensions.Options;
10+
using Microsoft.AspNetCore.Authentication;
1111

12-
namespace Microsoft.AspNetCore.Builder
12+
namespace Microsoft.Extensions.DependencyInjection
1313
{
1414
/// <summary>
1515
/// Extension methods to add MailChimp authentication capabilities to an HTTP application pipeline.
1616
/// </summary>
1717
public static class MailChimpAuthenticationExtensions
1818
{
1919
/// <summary>
20-
/// Adds the <see cref="MailChimpAuthenticationMiddleware"/> middleware to the specified
21-
/// <see cref="IApplicationBuilder"/>, which enables MailChimp authentication capabilities.
20+
/// Adds <see cref="MailChimpAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables MailChimp authentication capabilities.
2222
/// </summary>
23-
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
24-
/// <param name="options">A <see cref="MailChimpAuthenticationOptions"/> that specifies options for the middleware.</param>
25-
/// <returns>A reference to this instance after the operation has completed.</returns>
26-
public static IApplicationBuilder UseMailChimpAuthentication(
27-
[NotNull] this IApplicationBuilder app,
28-
[NotNull] MailChimpAuthenticationOptions options)
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddMailChimp([NotNull] this AuthenticationBuilder builder)
2926
{
30-
if (app == null)
31-
{
32-
throw new ArgumentNullException(nameof(app));
33-
}
34-
35-
if (options == null)
36-
{
37-
throw new ArgumentNullException(nameof(options));
38-
}
39-
40-
return app.UseMiddleware<MailChimpAuthenticationMiddleware>(Options.Create(options));
27+
return builder.AddMailChimp(MailChimpAuthenticationDefaults.AuthenticationScheme, options => { });
4128
}
4229

4330
/// <summary>
44-
/// Adds the <see cref="MailChimpAuthenticationMiddleware"/> middleware to the specified
45-
/// <see cref="IApplicationBuilder"/>, which enables MailChimp authentication capabilities.
31+
/// Adds <see cref="MailChimpAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables MailChimp authentication capabilities.
4633
/// </summary>
47-
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
48-
/// <param name="configuration">An action delegate to configure the provided <see cref="MailChimpAuthenticationOptions"/>.</param>
49-
/// <returns>A reference to this instance after the operation has completed.</returns>
50-
public static IApplicationBuilder UseMailChimpAuthentication(
51-
[NotNull] this IApplicationBuilder app,
34+
/// <param name="builder">The authentication builder.</param>
35+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
36+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
37+
public static AuthenticationBuilder AddMailChimp(
38+
[NotNull] this AuthenticationBuilder builder,
5239
[NotNull] Action<MailChimpAuthenticationOptions> configuration)
5340
{
54-
if (app == null)
55-
{
56-
throw new ArgumentNullException(nameof(app));
57-
}
58-
59-
if (configuration == null)
60-
{
61-
throw new ArgumentNullException(nameof(configuration));
62-
}
41+
return builder.AddMailChimp(MailChimpAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
6343

64-
var options = new MailChimpAuthenticationOptions();
65-
configuration(options);
44+
/// <summary>
45+
/// Adds <see cref="MailChimpAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables MailChimp authentication capabilities.
47+
/// </summary>
48+
/// <param name="builder">The authentication builder.</param>
49+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
50+
/// <param name="configuration">The delegate used to configure the MailChimp options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddMailChimp(
53+
[NotNull] this AuthenticationBuilder builder, string scheme,
54+
[NotNull] Action<MailChimpAuthenticationOptions> configuration)
55+
{
56+
return builder.AddMailChimp(scheme, MailChimpAuthenticationDefaults.DisplayName, configuration);
57+
}
6658

67-
return app.UseMiddleware<MailChimpAuthenticationMiddleware>(Options.Create(options));
59+
/// <summary>
60+
/// Adds <see cref="MailChimpAuthenticationHandler"/> to the specified
61+
/// <see cref="AuthenticationBuilder"/>, which enables MailChimp authentication capabilities.
62+
/// </summary>
63+
/// <param name="builder">The authentication builder.</param>
64+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
65+
/// <param name="name">The optional display name associated with this instance.</param>
66+
/// <param name="configuration">The delegate used to configure the MailChimp options.</param>
67+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
68+
public static AuthenticationBuilder AddMailChimp(
69+
[NotNull] this AuthenticationBuilder builder,
70+
[NotNull] string scheme, [CanBeNull] string name,
71+
[NotNull] Action<MailChimpAuthenticationOptions> configuration)
72+
{
73+
return builder.AddOAuth<MailChimpAuthenticationOptions, MailChimpAuthenticationHandler>(scheme, name, configuration);
6874
}
6975
}
7076
}

src/AspNet.Security.OAuth.MailChimp/MailChimpAuthenticationHandler.cs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@
77
using System.Net.Http;
88
using System.Net.Http.Headers;
99
using System.Security.Claims;
10+
using System.Text.Encodings.Web;
1011
using System.Threading.Tasks;
11-
using AspNet.Security.OAuth.Extensions;
1212
using JetBrains.Annotations;
1313
using Microsoft.AspNetCore.Authentication;
1414
using Microsoft.AspNetCore.Authentication.OAuth;
15-
using Microsoft.AspNetCore.Http.Authentication;
1615
using Microsoft.Extensions.Logging;
16+
using Microsoft.Extensions.Options;
1717
using Newtonsoft.Json.Linq;
1818

1919
namespace AspNet.Security.OAuth.MailChimp
2020
{
2121
public class MailChimpAuthenticationHandler : OAuthHandler<MailChimpAuthenticationOptions>
2222
{
23-
public MailChimpAuthenticationHandler([NotNull] HttpClient client)
24-
: base(client)
23+
public MailChimpAuthenticationHandler(
24+
[NotNull] IOptionsMonitor<MailChimpAuthenticationOptions> options,
25+
[NotNull] ILoggerFactory logger,
26+
[NotNull] UrlEncoder encoder,
27+
[NotNull] ISystemClock clock)
28+
: base(options, logger, encoder, clock)
2529
{
2630
}
2731

@@ -46,24 +50,12 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
4650

4751
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
4852

49-
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, MailChimpAuthenticationHelper.GetIdentifier(payload), Options.ClaimsIssuer)
50-
.AddOptionalClaim(ClaimTypes.Name, MailChimpAuthenticationHelper.GetName(payload), Options.ClaimsIssuer)
51-
.AddOptionalClaim(ClaimTypes.Email, MailChimpAuthenticationHelper.GetEmail(payload), Options.ClaimsIssuer)
52-
.AddOptionalClaim(ClaimTypes.Role, MailChimpAuthenticationHelper.GetRole(payload), Options.ClaimsIssuer)
53-
.AddOptionalClaim("urn:mailchimp:dc", MailChimpAuthenticationHelper.GetDataCenter(payload), Options.ClaimsIssuer)
54-
.AddOptionalClaim("urn:mailchimp:account_name", MailChimpAuthenticationHelper.GetAccountName(payload), Options.ClaimsIssuer)
55-
.AddOptionalClaim("urn:mailchimp:login_id", MailChimpAuthenticationHelper.GetLoginId(payload), Options.ClaimsIssuer)
56-
.AddOptionalClaim("urn:mailchimp:login_email", MailChimpAuthenticationHelper.GetLoginEmail(payload), Options.ClaimsIssuer)
57-
.AddOptionalClaim("urn:mailchimp:login_url", MailChimpAuthenticationHelper.GetLoginUrl(payload), Options.ClaimsIssuer)
58-
.AddOptionalClaim("urn:mailchimp:api_endpoint", MailChimpAuthenticationHelper.GetApiEndPoint(payload), Options.ClaimsIssuer);
59-
6053
var principal = new ClaimsPrincipal(identity);
61-
var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme);
54+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
55+
context.RunClaimActions(payload);
6256

63-
var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, payload);
6457
await Options.Events.CreatingTicket(context);
65-
66-
return context.Ticket;
58+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
6759
}
6860
}
6961
}

src/AspNet.Security.OAuth.MailChimp/MailChimpAuthenticationHelper.cs

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)