Skip to content

Commit f801033

Browse files
igorskevinchalet
authored andcommitted
Add a MailChimp provider
1 parent c5d62fe commit f801033

12 files changed

+381
-1
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.Strav
8383
EndProject
8484
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.Untappd", "src\AspNet.Security.OAuth.Untappd\AspNet.Security.OAuth.Untappd.xproj", "{C17F7C35-4CAD-408F-9031-44EF1A3CC379}"
8585
EndProject
86+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNet.Security.OAuth.MailChimp", "src\AspNet.Security.OAuth.MailChimp\AspNet.Security.OAuth.MailChimp.xproj", "{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}"
87+
EndProject
8688
Global
8789
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8890
Debug|Any CPU = Debug|Any CPU
@@ -241,6 +243,10 @@ Global
241243
{C17F7C35-4CAD-408F-9031-44EF1A3CC379}.Debug|Any CPU.Build.0 = Debug|Any CPU
242244
{C17F7C35-4CAD-408F-9031-44EF1A3CC379}.Release|Any CPU.ActiveCfg = Release|Any CPU
243245
{C17F7C35-4CAD-408F-9031-44EF1A3CC379}.Release|Any CPU.Build.0 = Release|Any CPU
246+
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
247+
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
248+
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
249+
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF}.Release|Any CPU.Build.0 = Release|Any CPU
244250
EndGlobalSection
245251
GlobalSection(SolutionProperties) = preSolution
246252
HideSolutionNode = FALSE
@@ -284,5 +290,6 @@ Global
284290
{D0713EFF-8FBA-4145-8E57-5DF1CFBBC0EF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
285291
{F276EB53-7758-4CEE-8013-61BA537B94C6} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
286292
{C17F7C35-4CAD-408F-9031-44EF1A3CC379} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
293+
{337B29EB-54B4-43BF-8ADC-A5B3D11538AF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
287294
EndGlobalSection
288295
EndGlobal

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ We would love it if you could help contributing to this repository. Please look
3636
* [Dave Timmins](https://github.com/davetimmins)
3737
* [Eric Green](https://github.com/ericgreenmix)
3838
* [Ethan Celletti](https://github.com/Gekctek)
39+
* [Igor Simovic](https://github.com/igorsimovic)
3940
* [James Holcomb](https://github.com/jamesholcomb)
4041
* [Jason Loeffler](https://github.com/jmloeffler)
4142
* [Jerrie Pelser](https://github.com/jerriep)

samples/Mvc.Client/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void Configure(IApplicationBuilder app) {
4343
app.UseGitHubAuthentication(new GitHubAuthenticationOptions {
4444
ClientId = "49e302895d8b09ea5656",
4545
ClientSecret = "98f1bf028608901e9df91d64ee61536fe562064b",
46-
Scope = {"user:email"}
46+
Scope = { "user:email" }
4747
});
4848

4949
app.UseMvc();

samples/Mvc.Client/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"AspNet.Security.OAuth.Imgur": { "target": "project" },
3434
"AspNet.Security.OAuth.Instagram": { "target": "project" },
3535
"AspNet.Security.OAuth.LinkedIn": { "target": "project" },
36+
"AspNet.Security.OAuth.MailChimp": { "target": "project" },
3637
"AspNet.Security.OAuth.Myob": { "target": "project" },
3738
"AspNet.Security.OAuth.Onshape": { "target": "project" },
3839
"AspNet.Security.OAuth.Paypal": { "target": "project" },
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>337b29eb-54b4-43bf-8adc-a5b3d11538af</ProjectGuid>
10+
<RootNamespace>AspNet.Security.OAuth.MailChimp</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
</PropertyGroup>
14+
<PropertyGroup>
15+
<SchemaVersion>2.0</SchemaVersion>
16+
</PropertyGroup>
17+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
18+
</Project>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 Microsoft.AspNetCore.Builder;
8+
9+
namespace AspNet.Security.OAuth.MailChimp {
10+
/// <summary>
11+
/// Default values used by the MailChimp authentication middleware.
12+
/// </summary>
13+
public static class MailChimpAuthenticationDefaults {
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "MailChimp";
18+
19+
/// <summary>
20+
/// Default value for <see cref="RemoteAuthenticationOptions.DisplayName"/>.
21+
/// </summary>
22+
public const string DisplayName = "MailChimp";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public const string Issuer = "MailChimp";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public const string CallbackPath = "/signin-mailchimp";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public const string AuthorizationEndpoint = "https://login.mailchimp.com/oauth2/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public const string TokenEndpoint = "https://login.mailchimp.com/oauth2/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public const string UserInformationEndpoint = "https://login.mailchimp.com/oauth2/metadata";
48+
}
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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;
8+
using AspNet.Security.OAuth.MailChimp;
9+
using JetBrains.Annotations;
10+
using Microsoft.Extensions.Options;
11+
12+
namespace Microsoft.AspNetCore.Builder {
13+
/// <summary>
14+
/// Extension methods to add MailChimp authentication capabilities to an HTTP application pipeline.
15+
/// </summary>
16+
public static class MailChimpAuthenticationExtensions {
17+
/// <summary>
18+
/// Adds the <see cref="MailChimpAuthenticationMiddleware"/> middleware to the specified
19+
/// <see cref="IApplicationBuilder"/>, which enables MailChimp authentication capabilities.
20+
/// </summary>
21+
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
22+
/// <param name="options">A <see cref="MailChimpAuthenticationOptions"/> that specifies options for the middleware.</param>
23+
/// <returns>A reference to this instance after the operation has completed.</returns>
24+
public static IApplicationBuilder UseMailChimpAuthentication(
25+
[NotNull] this IApplicationBuilder app,
26+
[NotNull] MailChimpAuthenticationOptions options) {
27+
if (app == null) {
28+
throw new ArgumentNullException(nameof(app));
29+
}
30+
31+
if (options == null) {
32+
throw new ArgumentNullException(nameof(options));
33+
}
34+
35+
return app.UseMiddleware<MailChimpAuthenticationMiddleware>(Options.Create(options));
36+
}
37+
38+
/// <summary>
39+
/// Adds the <see cref="MailChimpAuthenticationMiddleware"/> middleware to the specified
40+
/// <see cref="IApplicationBuilder"/>, which enables MailChimp authentication capabilities.
41+
/// </summary>
42+
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
43+
/// <param name="configuration">An action delegate to configure the provided <see cref="MailChimpAuthenticationOptions"/>.</param>
44+
/// <returns>A reference to this instance after the operation has completed.</returns>
45+
public static IApplicationBuilder UseMailChimpAuthentication(
46+
[NotNull] this IApplicationBuilder app,
47+
[NotNull] Action<MailChimpAuthenticationOptions> configuration) {
48+
if (app == null) {
49+
throw new ArgumentNullException(nameof(app));
50+
}
51+
52+
if (configuration == null) {
53+
throw new ArgumentNullException(nameof(configuration));
54+
}
55+
56+
var options = new MailChimpAuthenticationOptions();
57+
configuration(options);
58+
59+
return app.UseMiddleware<MailChimpAuthenticationMiddleware>(Options.Create(options));
60+
}
61+
}
62+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 JetBrains.Annotations;
8+
using Newtonsoft.Json.Linq;
9+
10+
namespace AspNet.Security.OAuth.MailChimp {
11+
/// <summary>
12+
/// Contains static methods that allow to extract user's information from a <see cref="JObject"/>
13+
/// instance retrieved from MailChimp after a successful authentication process.
14+
/// </summary>
15+
public static class MailChimpAuthenticationHelper {
16+
/// <summary>
17+
/// Gets the identifier corresponding to the authenticated user.
18+
/// </summary>
19+
public static string GetIdentifier([NotNull] JObject payload) => payload.Value<string>("user_id");
20+
21+
/// <summary>
22+
/// Gets the data center corresponding to the authenticated user.
23+
/// </summary>
24+
public static string GetDataCenter([NotNull] JObject payload) => payload.Value<string>("dc");
25+
26+
/// <summary>
27+
/// Gets the account name corresponding to the authenticated user.
28+
/// </summary>
29+
public static string GetAccountName([NotNull] JObject payload) => payload.Value<string>("accountname");
30+
31+
/// <summary>
32+
/// Gets the role corresponding to the authenticated user.
33+
/// </summary>
34+
public static string GetRole(JObject payload) => payload.Value<string>("role");
35+
36+
/// <summary>
37+
/// Gets the email address corresponding to the authenticated user.
38+
/// </summary>
39+
public static string GetEmail([NotNull] JObject payload) => payload.Value<JObject>("login")
40+
?.Value<string>("email");
41+
42+
/// <summary>
43+
/// Gets the login id corresponding to the authenticated user.
44+
/// </summary>
45+
public static string GetLoginId([NotNull] JObject payload) => payload.Value<JObject>("login")
46+
?.Value<string>("login_id");
47+
48+
/// <summary>
49+
/// Gets the name corresponding to the authenticated user.
50+
/// </summary>
51+
public static string GetName([NotNull] JObject payload) => payload.Value<JObject>("login")
52+
?.Value<string>("login_name");
53+
54+
/// <summary>
55+
/// Gets the login email address corresponding to the authenticated user.
56+
/// </summary>
57+
public static string GetLoginEmail([NotNull] JObject payload) => payload.Value<JObject>("login")
58+
?.Value<string>("login_email");
59+
60+
/// <summary>
61+
/// Gets the login url address corresponding to the authenticated user.
62+
/// </summary>
63+
public static string GetLoginUrl([NotNull] JObject payload) => payload.Value<string>("login_url");
64+
65+
/// <summary>
66+
/// Gets the api endpoint address corresponding to the authenticated user.
67+
/// </summary>
68+
public static string GetApiEndPoint([NotNull] JObject payload) => payload.Value<string>("api_endpoint");
69+
}
70+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.Text.Encodings.Web;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Authentication.OAuth;
10+
using Microsoft.AspNetCore.DataProtection;
11+
using Microsoft.AspNetCore.Http;
12+
using Microsoft.Extensions.Logging;
13+
using Microsoft.Extensions.Options;
14+
using JetBrains.Annotations;
15+
16+
namespace AspNet.Security.OAuth.MailChimp {
17+
public class MailChimpAuthenticationMiddleware : OAuthMiddleware<MailChimpAuthenticationOptions> {
18+
public MailChimpAuthenticationMiddleware(
19+
[NotNull] RequestDelegate next,
20+
[NotNull] IDataProtectionProvider dataProtectionProvider,
21+
[NotNull] ILoggerFactory loggerFactory,
22+
[NotNull] UrlEncoder encoder,
23+
[NotNull] IOptions<SharedAuthenticationOptions> sharedOptions,
24+
[NotNull] IOptions<MailChimpAuthenticationOptions> options)
25+
: base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options) {
26+
}
27+
28+
protected override AuthenticationHandler<MailChimpAuthenticationOptions> CreateHandler() {
29+
return new MailChimpAuthenticationHandler(Backchannel);
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)