Skip to content

Commit

Permalink
Migrate the Slack provider to ASP.NET Core 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kinosang authored and kevinchalet committed Nov 4, 2017
1 parent 207d4f3 commit 0814591
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 256 deletions.
1 change: 1 addition & 0 deletions samples/Mvc.Client/Mvc.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Paypal\AspNet.Security.OAuth.Paypal.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Reddit\AspNet.Security.OAuth.Reddit.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Salesforce\AspNet.Security.OAuth.Salesforce.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Slack\AspNet.Security.OAuth.Slack.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Strava\AspNet.Security.OAuth.Strava.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Twitch\AspNet.Security.OAuth.Twitch.csproj" />
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Vimeo\AspNet.Security.OAuth.Vimeo.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="..\..\build\packages.props" />

<PropertyGroup>
<TargetFrameworks>net451;netstandard1.3</TargetFrameworks>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -12,10 +12,6 @@
<PackageTags>aspnetcore;authentication;oauth;security;slack</PackageTags>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\..\shared\AspNet.Security.OAuth.Extensions\*.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="$(JetBrainsVersion)" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OAuth" Version="$(AspNetCoreVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* for more information concerning the license and the contributors participating to this project.
*/

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;

namespace AspNet.Security.OAuth.Slack
{
Expand All @@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.Slack
public static class SlackAuthenticationDefaults
{
/// <summary>
/// Default value for <see cref="AuthenticationOptions.AuthenticationScheme"/>.
/// Default value for <see cref="AuthenticationScheme.Name"/>.
/// </summary>
public const string AuthenticationScheme = "Slack";

/// <summary>
/// Default value for <see cref="RemoteAuthenticationOptions.DisplayName"/>.
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
/// </summary>
public const string DisplayName = "Slack";

/// <summary>
/// Default value for <see cref="AuthenticationOptions.ClaimsIssuer"/>.
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
/// </summary>
public const string Issuer = "Slack";

Expand Down
86 changes: 46 additions & 40 deletions src/AspNet.Security.OAuth.Slack/SlackAuthenticationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,70 @@
using System;
using AspNet.Security.OAuth.Slack;
using JetBrains.Annotations;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication;

namespace Microsoft.AspNetCore.Builder
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods to add Slack authentication capabilities to an HTTP application pipeline.
/// </summary>
public static class SlackAuthenticationExtensions
{
/// <summary>
/// Adds the <see cref="SlackAuthenticationMiddleware"/> middleware to the specified
/// <see cref="IApplicationBuilder"/>, which enables Slack authentication capabilities.
/// Adds <see cref="SlackAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Slack authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="options">A <see cref="SlackAuthenticationOptions"/> that specifies options for the middleware.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseSlackAuthentication(
[NotNull] this IApplicationBuilder app,
[NotNull] SlackAuthenticationOptions options)
/// <param name="builder">The authentication builder.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSlack([NotNull] this AuthenticationBuilder builder)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}

if (options == null)
{
throw new ArgumentNullException(nameof(options));
}

return app.UseMiddleware<SlackAuthenticationMiddleware>(Options.Create(options));
return builder.AddSlack(SlackAuthenticationDefaults.AuthenticationScheme, options => { });
}

/// <summary>
/// Adds the <see cref="SlackAuthenticationMiddleware"/> middleware to the specified
/// <see cref="IApplicationBuilder"/>, which enables Slack authentication capabilities.
/// Adds <see cref="SlackAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Slack authentication capabilities.
/// </summary>
/// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
/// <param name="configuration">An action delegate to configure the provided <see cref="SlackAuthenticationOptions"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IApplicationBuilder UseSlackAuthentication(
[NotNull] this IApplicationBuilder app,
/// <param name="builder">The authentication builder.</param>
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSlack(
[NotNull] this AuthenticationBuilder builder,
[NotNull] Action<SlackAuthenticationOptions> configuration)
{
if (app == null)
{
throw new ArgumentNullException(nameof(app));
}

if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
return builder.AddSlack(SlackAuthenticationDefaults.AuthenticationScheme, configuration);
}

var options = new SlackAuthenticationOptions();
configuration(options);
/// <summary>
/// Adds <see cref="SlackAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Slack authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Slack options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSlack(
[NotNull] this AuthenticationBuilder builder, [NotNull] string scheme,
[NotNull] Action<SlackAuthenticationOptions> configuration)
{
return builder.AddSlack(scheme, SlackAuthenticationDefaults.DisplayName, configuration);
}

return app.UseMiddleware<SlackAuthenticationMiddleware>(Options.Create(options));
/// <summary>
/// Adds <see cref="SlackAuthenticationHandler"/> to the specified
/// <see cref="AuthenticationBuilder"/>, which enables Slack authentication capabilities.
/// </summary>
/// <param name="builder">The authentication builder.</param>
/// <param name="scheme">The authentication scheme associated with this instance.</param>
/// <param name="name">The optional display name associated with this instance.</param>
/// <param name="configuration">The delegate used to configure the Slack options.</param>
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
public static AuthenticationBuilder AddSlack(
[NotNull] this AuthenticationBuilder builder,
[NotNull] string scheme, [CanBeNull] string name,
[NotNull] Action<SlackAuthenticationOptions> configuration)
{
return builder.AddOAuth<SlackAuthenticationOptions, SlackAuthenticationHandler>(scheme, name, configuration);
}
}
}
30 changes: 11 additions & 19 deletions src/AspNet.Security.OAuth.Slack/SlackAuthenticationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using AspNet.Security.OAuth.Extensions;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json.Linq;

namespace AspNet.Security.OAuth.Slack
{
public class SlackAuthenticationHandler : OAuthHandler<SlackAuthenticationOptions>
{
public SlackAuthenticationHandler([NotNull] HttpClient client)
: base(client)
public SlackAuthenticationHandler(
[NotNull] IOptionsMonitor<SlackAuthenticationOptions> options,
[NotNull] ILoggerFactory logger,
[NotNull] UrlEncoder encoder,
[NotNull] ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

Expand All @@ -48,24 +52,12 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]

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

identity.AddOptionalClaim(ClaimTypes.NameIdentifier, SlackAuthenticationHelper.GetUserIdentifier(payload), Options.ClaimsIssuer)
.AddOptionalClaim(ClaimTypes.Name, SlackAuthenticationHelper.GetUserName(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:team_id", SlackAuthenticationHelper.GetTeamIdentifier(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:team_name", SlackAuthenticationHelper.GetTeamName(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:team_url", SlackAuthenticationHelper.GetTeamLink(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:bot:user_id", SlackAuthenticationHelper.GetBotUserId(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:bot:access_token", SlackAuthenticationHelper.GetBotAccessToken(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:webhook:channel", SlackAuthenticationHelper.GetWebhookChannel(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:webhook:url", SlackAuthenticationHelper.GetWebhookURL(payload), Options.ClaimsIssuer)
.AddOptionalClaim("urn:slack:webhook:configuration_url", SlackAuthenticationHelper.GetWebhookConfigurationURL(payload), Options.ClaimsIssuer);

var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme);
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
context.RunClaimActions(payload);

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

return context.Ticket;
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
}
}
}
149 changes: 0 additions & 149 deletions src/AspNet.Security.OAuth.Slack/SlackAuthenticationHelper.cs

This file was deleted.

Loading

0 comments on commit 0814591

Please sign in to comment.