Skip to content

Commit 63f2543

Browse files
kinosangkevinchalet
authored andcommitted
Migrate the Untappd provider to ASP.NET Core 2.0
1 parent 27304fd commit 63f2543

8 files changed

+76
-214
lines changed

samples/Mvc.Client/Mvc.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.StackExchange\AspNet.Security.OAuth.StackExchange.csproj" />
4040
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Strava\AspNet.Security.OAuth.Strava.csproj" />
4141
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Twitch\AspNet.Security.OAuth.Twitch.csproj" />
42+
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Untappd\AspNet.Security.OAuth.Untappd.csproj" />
4243
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Vimeo\AspNet.Security.OAuth.Vimeo.csproj" />
4344
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Vkontakte\AspNet.Security.OAuth.Vkontakte.csproj" />
4445
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Weibo\AspNet.Security.OAuth.Weibo.csproj" />

src/AspNet.Security.OAuth.Untappd/AspNet.Security.OAuth.Untappd.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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>
@@ -12,10 +12,6 @@
1212
<PackageTags>aspnetcore;authentication;untappd;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.Untappd/UntappdAuthenticationDefaults.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.Untappd
1011
{
@@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.Untappd
1415
public static class UntappdAuthenticationDefaults
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 = "Untappd";
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 = "Untappd";
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 = "Untappd";
3031

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,76 @@
11
/*
22
* 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
3+
* See https://Untappd.com/aspnet-contrib/AspNet.Security.OAuth.Providers
44
* for more information concerning the license and the contributors participating to this project.
55
*/
66

77
using System;
88
using AspNet.Security.OAuth.Untappd;
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>
15-
/// Extension methods to add Untappd authentication capabilities to an HTTP application pipeline.
15+
/// Extension methods to add authentication capabilities to an HTTP application pipeline.
1616
/// </summary>
1717
public static class UntappdAuthenticationExtensions
1818
{
1919
/// <summary>
20-
/// Adds the <see cref="UntappdAuthenticationMiddleware"/> middleware to the specified
21-
/// <see cref="IApplicationBuilder"/>, which enables Untappd authentication capabilities.
20+
/// Adds <see cref="UntappdAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables Untappd 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="UntappdAuthenticationOptions"/> that specifies options for the middleware.</param>
25-
/// <returns>A reference to this instance after the operation has completed.</returns>
26-
public static IApplicationBuilder UseUntappdAuthentication(
27-
[NotNull] this IApplicationBuilder app,
28-
[NotNull] UntappdAuthenticationOptions options)
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddUntappd([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<UntappdAuthenticationMiddleware>(Options.Create(options));
27+
return builder.AddUntappd(UntappdAuthenticationDefaults.AuthenticationScheme, options => { });
4128
}
4229

4330
/// <summary>
44-
/// Adds the <see cref="UntappdAuthenticationMiddleware"/> middleware to the specified
45-
/// <see cref="IApplicationBuilder"/>, which enables Untappd authentication capabilities.
31+
/// Adds <see cref="UntappdAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables Untappd 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="UntappdAuthenticationOptions"/>.</param>
49-
/// <returns>A reference to this instance after the operation has completed.</returns>
50-
public static IApplicationBuilder UseUntappdAuthentication(
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 AddUntappd(
38+
[NotNull] this AuthenticationBuilder builder,
5239
[NotNull] Action<UntappdAuthenticationOptions> 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.AddUntappd(UntappdAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
6343

64-
var options = new UntappdAuthenticationOptions();
65-
configuration(options);
44+
/// <summary>
45+
/// Adds <see cref="UntappdAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables Untappd 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 Untappd options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddUntappd(
53+
[NotNull] this AuthenticationBuilder builder, [NotNull] string scheme,
54+
[NotNull] Action<UntappdAuthenticationOptions> configuration)
55+
{
56+
return builder.AddUntappd(scheme, UntappdAuthenticationDefaults.DisplayName, configuration);
57+
}
6658

67-
return app.UseMiddleware<UntappdAuthenticationMiddleware>(Options.Create(options));
59+
/// <summary>
60+
/// Adds <see cref="UntappdAuthenticationHandler"/> to the specified
61+
/// <see cref="AuthenticationBuilder"/>, which enables Untappd 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 Untappd options.</param>
67+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
68+
public static AuthenticationBuilder AddUntappd(
69+
[NotNull] this AuthenticationBuilder builder,
70+
[NotNull] string scheme, [CanBeNull] string name,
71+
[NotNull] Action<UntappdAuthenticationOptions> configuration)
72+
{
73+
return builder.AddOAuth<UntappdAuthenticationOptions, UntappdAuthenticationHandler>(scheme, name, configuration);
6874
}
6975
}
7076
}

src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHandler.cs

Lines changed: 11 additions & 14 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.Untappd
2020
{
2121
public class UntappdAuthenticationHandler : OAuthHandler<UntappdAuthenticationOptions>
2222
{
23-
public UntappdAuthenticationHandler([NotNull] HttpClient client)
24-
: base(client)
23+
public UntappdAuthenticationHandler(
24+
[NotNull] IOptionsMonitor<UntappdAuthenticationOptions> options,
25+
[NotNull] ILoggerFactory logger,
26+
[NotNull] UrlEncoder encoder,
27+
[NotNull] ISystemClock clock)
28+
: base(options, logger, encoder, clock)
2529
{
2630
}
2731

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

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

49-
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, UntappdAuthenticationHelper.GetIdentifier(payload), Options.ClaimsIssuer)
50-
.AddOptionalClaim(ClaimTypes.GivenName, UntappdAuthenticationHelper.GetFirstName(payload), Options.ClaimsIssuer)
51-
.AddOptionalClaim(ClaimTypes.Surname, UntappdAuthenticationHelper.GetLastName(payload), Options.ClaimsIssuer)
52-
.AddOptionalClaim(ClaimTypes.Name, UntappdAuthenticationHelper.GetUsername(payload), Options.ClaimsIssuer)
53-
.AddOptionalClaim(ClaimTypes.Webpage, UntappdAuthenticationHelper.GetUrl(payload), Options.ClaimsIssuer)
54-
.AddOptionalClaim("urn:untappd:link", UntappdAuthenticationHelper.GetAvatar(payload), Options.ClaimsIssuer);
55-
5653
var principal = new ClaimsPrincipal(identity);
57-
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.Value<JObject>("user"));
5856

59-
var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, payload);
6057
await Options.Events.CreatingTicket(context);
6158

62-
return context.Ticket;
59+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
6360
}
6461
}
6562
}

src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationHelper.cs

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

src/AspNet.Security.OAuth.Untappd/UntappdAuthenticationMiddleware.cs

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

0 commit comments

Comments
 (0)