Skip to content

Commit cd7c40a

Browse files
kinosangkevinchalet
authored andcommitted
Migrate the Yammer provider to ASP.NET Core 2.0
1 parent db3bdff commit cd7c40a

8 files changed

+74
-212
lines changed

samples/Mvc.Client/Mvc.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Weixin\AspNet.Security.OAuth.Weixin.csproj" />
4848
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.WordPress\AspNet.Security.OAuth.WordPress.csproj" />
4949
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Yahoo\AspNet.Security.OAuth.Yahoo.csproj" />
50+
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Yammer\AspNet.Security.OAuth.Yammer.csproj" />
5051
<ProjectReference Include="..\..\src\AspNet.Security.OAuth.Yandex\AspNet.Security.OAuth.Yandex.csproj" />
5152
</ItemGroup>
5253

src/AspNet.Security.OAuth.Yammer/AspNet.Security.OAuth.Yammer.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;oauth;security;yammer</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.Yammer/YammerAuthenticationDefaults.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.Yammer
1011
{
@@ -14,17 +15,17 @@ namespace AspNet.Security.OAuth.Yammer
1415
public static class YammerAuthenticationDefaults
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 = "Yammer";
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 = "Yammer";
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 = "Yammer";
3031

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationExtensions.cs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,64 +7,70 @@
77
using System;
88
using AspNet.Security.OAuth.Yammer;
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 Yammer authentication capabilities to an HTTP application pipeline.
1616
/// </summary>
1717
public static class YammerAuthenticationExtensions
1818
{
1919
/// <summary>
20-
/// Adds the <see cref="YammerAuthenticationMiddleware"/> middleware to the specified
21-
/// <see cref="IApplicationBuilder"/>, which enables Yammer authentication capabilities.
20+
/// Adds the <see cref="YammerAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables Yammer 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="YammerAuthenticationOptions"/> that specifies options for the middleware.</param>
23+
/// <param name="builder">The authentication builder.</param>
2524
/// <returns>A reference to this instance after the operation has completed.</returns>
26-
public static IApplicationBuilder UseYammerAuthentication(
27-
[NotNull] this IApplicationBuilder app,
28-
[NotNull] YammerAuthenticationOptions options)
25+
public static AuthenticationBuilder AddYammer([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<YammerAuthenticationMiddleware>(Options.Create(options));
27+
return builder.AddYammer(YammerAuthenticationDefaults.AuthenticationScheme, options => { });
4128
}
4229

4330
/// <summary>
44-
/// Adds the <see cref="YammerAuthenticationMiddleware"/> middleware to the specified
45-
/// <see cref="IApplicationBuilder"/>, which enables Yammer authentication capabilities.
31+
/// Adds the <see cref="YammerAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables Yammer 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="YammerAuthenticationOptions"/>.</param>
34+
/// <param name="builder">The authentication builder.</param>
35+
/// <param name="configuration">The delegate used to configure the OpenID 2.0 options.</param>
4936
/// <returns>A reference to this instance after the operation has completed.</returns>
50-
public static IApplicationBuilder UseYammerAuthentication(
51-
[NotNull] this IApplicationBuilder app,
37+
public static AuthenticationBuilder AddYammer(
38+
[NotNull] this AuthenticationBuilder builder,
5239
[NotNull] Action<YammerAuthenticationOptions> 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.AddYammer(YammerAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
6343

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

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

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHandler.cs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@
99
using System.Net.Http;
1010
using System.Net.Http.Headers;
1111
using System.Security.Claims;
12+
using System.Text.Encodings.Web;
1213
using System.Threading.Tasks;
13-
using AspNet.Security.OAuth.Extensions;
1414
using JetBrains.Annotations;
1515
using Microsoft.AspNetCore.Authentication;
1616
using Microsoft.AspNetCore.Authentication.OAuth;
17-
using Microsoft.AspNetCore.Http.Authentication;
1817
using Microsoft.Extensions.Logging;
18+
using Microsoft.Extensions.Options;
1919
using Newtonsoft.Json.Linq;
2020

2121
namespace AspNet.Security.OAuth.Yammer
2222
{
2323
public class YammerAuthenticationHandler : OAuthHandler<YammerAuthenticationOptions>
2424
{
25-
public YammerAuthenticationHandler(HttpClient client)
26-
: base(client)
25+
public YammerAuthenticationHandler(
26+
[NotNull] IOptionsMonitor<YammerAuthenticationOptions> options,
27+
[NotNull] ILoggerFactory logger,
28+
[NotNull] UrlEncoder encoder,
29+
[NotNull] ISystemClock clock)
30+
: base(options, logger, encoder, clock)
2731
{
2832
}
2933

@@ -47,21 +51,13 @@ protected override async Task<AuthenticationTicket> CreateTicketAsync([NotNull]
4751

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

50-
identity.AddOptionalClaim(ClaimTypes.NameIdentifier, YammerAuthenticationHelper.GetId(payload), Options.ClaimsIssuer)
51-
.AddOptionalClaim(ClaimTypes.GivenName, YammerAuthenticationHelper.GetFirstName(payload), Options.ClaimsIssuer)
52-
.AddOptionalClaim(ClaimTypes.Surname, YammerAuthenticationHelper.GetLastName(payload), Options.ClaimsIssuer)
53-
.AddOptionalClaim(ClaimTypes.Name, YammerAuthenticationHelper.GetName(payload), Options.ClaimsIssuer)
54-
.AddOptionalClaim(ClaimTypes.Email, YammerAuthenticationHelper.GetEmail(payload), Options.ClaimsIssuer)
55-
.AddOptionalClaim("urn:yammer:link", YammerAuthenticationHelper.GetLink(payload), Options.ClaimsIssuer)
56-
.AddOptionalClaim("urn:yammer:job_title", YammerAuthenticationHelper.GetJobTitle(payload), Options.ClaimsIssuer);
57-
5854
var principal = new ClaimsPrincipal(identity);
59-
var ticket = new AuthenticationTicket(principal, properties, Options.AuthenticationScheme);
55+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload);
56+
context.RunClaimActions(payload);
6057

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

64-
return context.Ticket;
60+
return new AuthenticationTicket(context.Principal, context.Properties, Scheme.Name);
6561
}
6662

6763
protected override async Task<OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri)

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationHelper.cs

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

src/AspNet.Security.OAuth.Yammer/YammerAuthenticationMiddleware.cs

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

0 commit comments

Comments
 (0)