Skip to content

Commit 11764ea

Browse files
haydenhancockkevinchalet
authored andcommitted
Migrate the Gitter provider to ASP.NET Core 2.0
1 parent cd7c40a commit 11764ea

8 files changed

+73
-204
lines changed

samples/Mvc.Client/Mvc.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Fitbit\AspNet.Security.OAuth.Fitbit.csproj" />
2525
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Foursquare\AspNet.Security.OAuth.Foursquare.csproj" />
2626
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.GitHub\AspNet.Security.OAuth.GitHub.csproj" />
27+
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Gitter\AspNet.Security.OAuth.Gitter.csproj" />
2728
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.HealthGraph\AspNet.Security.OAuth.HealthGraph.csproj" />
2829
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Imgur\AspNet.Security.OAuth.Imgur.csproj" />
2930
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Instagram\AspNet.Security.OAuth.Instagram.csproj" />

src/AspNet.Security.OAuth.Gitter/AspNet.Security.OAuth.Gitter.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 Gitter authentication.</Description>
11-
<Authors>Maxime Roussin-Bélanger</Authors>
11+
<Authors>Maxime Roussin-Bélanger;Hayden Hancock</Authors>
1212
<PackageTags>aspnetcore;authentication;gitter;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.Gitter/GitterAuthenticationDefaults.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.Gitter
1011
{
@@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.Gitter
1415
public static class GitterAuthenticationDefaults
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 = "Gitter";
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 = "Gitter";
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 = "Gitter";
3031

src/AspNet.Security.OAuth.Gitter/GitterAuthenticationExtensions.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.Gitter;
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 Gitter authentication capabilities to an HTTP application pipeline.
1616
/// </summary>
1717
public static class GitterAuthenticationExtensions
1818
{
1919
/// <summary>
20-
/// Adds the <see cref="GitterAuthenticationMiddleware"/> middleware to the specified
21-
/// <see cref="IApplicationBuilder"/>, which enables GitHub authentication capabilities.
20+
/// Adds <see cref="GitterAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables Gitter 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="GitterAuthenticationOptions"/> that specifies options for the middleware.</param>
25-
/// <returns>A reference to this instance after the operation has completed.</returns>
26-
public static IApplicationBuilder UseGitterAuthentication(
27-
[NotNull] this IApplicationBuilder app,
28-
[NotNull] GitterAuthenticationOptions options)
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddGitter([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<GitterAuthenticationMiddleware>(Options.Create(options));
27+
return builder.AddGitter(GitterAuthenticationDefaults.AuthenticationScheme, options => { });
4128
}
4229

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

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

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

src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHandler.cs

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

@@ -30,7 +34,7 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
3034
{
3135
var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
3236
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
33-
request.Headers.Authorization = new AuthenticationHeaderValue(tokens.TokenType, tokens.AccessToken);
37+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
3438

3539
var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
3640
if (!response.IsSuccessStatusCode)
@@ -44,23 +48,14 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
4448
throw new HttpRequestException("An error occurred while retrieving the user profile.");
4549
}
4650

47-
var payload = JArray.Parse(await response.Content.ReadAsStringAsync());
48-
var user = (JObject) payload[0];
49-
50-
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, GitterAuthenticationHelper.GetIdentifier(user), Options.ClaimsIssuer)
51-
.AddOptionalClaim(ClaimTypes.Name, GitterAuthenticationHelper.GetUsername(user), Options.ClaimsIssuer)
52-
.AddOptionalClaim(ClaimTypes.Webpage, GitterAuthenticationHelper.GetLink(user), Options.ClaimsIssuer)
53-
.AddOptionalClaim("urn:gitter:displayname", GitterAuthenticationHelper.GetDisplayName(user), Options.ClaimsIssuer)
54-
.AddOptionalClaim("urn:gitter:avatarurlsmall", GitterAuthenticationHelper.GetAvatarUrlSmall(user), Options.ClaimsIssuer)
55-
.AddOptionalClaim("urn:gitter:avatarurlmedium", GitterAuthenticationHelper.GetAvatarUrlMedium(user), Options.ClaimsIssuer);
51+
var payload = JObject.Parse(await response.Content.ReadAsStringAsync());
5652

5753
var principal = new ClaimsPrincipal(identity);
58-
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);
5956

60-
var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, user);
6157
await Options.Events.CreatingTicket(context);
62-
63-
return context.Ticket;
58+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
6459
}
6560
}
6661
}

src/AspNet.Security.OAuth.Gitter/GitterAuthenticationHelper.cs

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

src/AspNet.Security.OAuth.Gitter/GitterAuthenticationMiddleware.cs

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

0 commit comments

Comments
 (0)