Skip to content

Commit d7eb9d1

Browse files
authored
Add ServiceChannel provider (#616)
* Add ServiceChannel provider
1 parent 8df6ff8 commit d7eb9d1

File tree

10 files changed

+411
-2
lines changed

10 files changed

+411
-2
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Quick
265265
EndProject
266266
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Ebay", "src\AspNet.Security.OAuth.Ebay\AspNet.Security.OAuth.Ebay.csproj", "{574A52D9-E7A5-4E11-8F36-5C8AA7033287}"
267267
EndProject
268+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.ServiceChannel", "src\AspNet.Security.OAuth.ServiceChannel\AspNet.Security.OAuth.ServiceChannel.csproj", "{57633BE6-C7AD-4197-A75A-F38A2312A4D9}"
269+
EndProject
268270
Global
269271
GlobalSection(SolutionConfigurationPlatforms) = preSolution
270272
Debug|Any CPU = Debug|Any CPU
@@ -599,6 +601,10 @@ Global
599601
{574A52D9-E7A5-4E11-8F36-5C8AA7033287}.Debug|Any CPU.Build.0 = Debug|Any CPU
600602
{574A52D9-E7A5-4E11-8F36-5C8AA7033287}.Release|Any CPU.ActiveCfg = Release|Any CPU
601603
{574A52D9-E7A5-4E11-8F36-5C8AA7033287}.Release|Any CPU.Build.0 = Release|Any CPU
604+
{57633BE6-C7AD-4197-A75A-F38A2312A4D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
605+
{57633BE6-C7AD-4197-A75A-F38A2312A4D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
606+
{57633BE6-C7AD-4197-A75A-F38A2312A4D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
607+
{57633BE6-C7AD-4197-A75A-F38A2312A4D9}.Release|Any CPU.Build.0 = Release|Any CPU
602608
EndGlobalSection
603609
GlobalSection(SolutionProperties) = preSolution
604610
HideSolutionNode = FALSE
@@ -692,6 +698,7 @@ Global
692698
{C90BA18B-E6C4-4E84-AFCC-58A4EE13BA40} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
693699
{815DA59A-E884-4BAD-B16C-D0B550B40A8D} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
694700
{574A52D9-E7A5-4E11-8F36-5C8AA7033287} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
701+
{57633BE6-C7AD-4197-A75A-F38A2312A4D9} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
695702
EndGlobalSection
696703
GlobalSection(ExtensibilityGlobals) = postSolution
697704
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

samples/Mvc.Client/Properties/launchSettings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"launchUrl": "https://localhost:5001/",
2222
"environmentVariables": {
2323
"ASPNETCORE_ENVIRONMENT": "Development"
24-
}
24+
},
25+
"applicationUrl": ""
2526
}
2627
}
27-
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<PropertyGroup>
8+
<Description>ASP.NET Core security middleware enabling ServiceChannel authentication.</Description>
9+
<Authors>Hang Yang</Authors>
10+
<PackageTags>servicechannel;aspnetcore;authentication;oauth;security</PackageTags>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
15+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
16+
</ItemGroup>
17+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
namespace AspNet.Security.OAuth.ServiceChannel
8+
{
9+
/// <summary>
10+
/// Contains constants specific to the <see cref="ServiceChannelAuthenticationHandler"/>.
11+
/// </summary>
12+
public static class ServiceChannelAuthenticationConstants
13+
{
14+
public static class Claims
15+
{
16+
public const string ProviderId = "urn:servicechannel:providerId";
17+
public const string ProviderName = "urn:servicechannel:providerName";
18+
}
19+
}
20+
}
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.Authentication;
8+
using Microsoft.AspNetCore.Authentication.OAuth;
9+
10+
namespace AspNet.Security.OAuth.ServiceChannel
11+
{
12+
public static class ServiceChannelAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "ServiceChannel";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "ServiceChannel";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "ServiceChannel";
28+
29+
/// <summary>
30+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
31+
/// </summary>
32+
public static readonly string CallbackPath = "/signin-servicechannel";
33+
34+
/// <summary>
35+
/// Default value for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
36+
/// </summary>
37+
public static readonly string AuthorizationEndpoint = "https://login.servicechannel.com/oauth/authorize";
38+
39+
/// <summary>
40+
/// Default value for <see cref="OAuthOptions.TokenEndpoint"/>.
41+
/// </summary>
42+
public static readonly string TokenEndpoint = "https://login.servicechannel.com/oauth/token";
43+
44+
/// <summary>
45+
/// Default value for <see cref="OAuthOptions.UserInformationEndpoint"/>.
46+
/// </summary>
47+
public static readonly string UserInformationEndpoint = "https://api.servicechannel.com/v3/users/current/profile";
48+
}
49+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.ServiceChannel;
9+
using JetBrains.Annotations;
10+
using Microsoft.AspNetCore.Authentication;
11+
12+
namespace Microsoft.Extensions.DependencyInjection
13+
{
14+
/// <summary>
15+
/// Extension methods to add ServiceChannel authentication capabilities to an HTTP application pipeline.
16+
/// </summary>
17+
public static class ServiceChannelAuthenticationExtensions
18+
{
19+
/// <summary>
20+
/// Adds <see cref="ServiceChannelAuthenticationHandler"/> to the specified
21+
/// <see cref="AuthenticationBuilder"/>, which enables ServiceChannel authentication capabilities.
22+
/// </summary>
23+
/// <param name="builder">The authentication builder.</param>
24+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
25+
public static AuthenticationBuilder AddServiceChannel([NotNull] this AuthenticationBuilder builder)
26+
{
27+
return builder.AddServiceChannel(ServiceChannelAuthenticationDefaults.AuthenticationScheme, options => { });
28+
}
29+
30+
/// <summary>
31+
/// Adds <see cref="ServiceChannelAuthenticationHandler"/> to the specified
32+
/// <see cref="AuthenticationBuilder"/>, which enables ServiceChannel authentication capabilities.
33+
/// </summary>
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 AddServiceChannel(
38+
[NotNull] this AuthenticationBuilder builder,
39+
[NotNull] Action<ServiceChannelAuthenticationOptions> configuration)
40+
{
41+
return builder.AddServiceChannel(ServiceChannelAuthenticationDefaults.AuthenticationScheme, configuration);
42+
}
43+
44+
/// <summary>
45+
/// Adds <see cref="ServiceChannelAuthenticationHandler"/> to the specified
46+
/// <see cref="AuthenticationBuilder"/>, which enables ServiceChannel 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 ServiceChannel options.</param>
51+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
52+
public static AuthenticationBuilder AddServiceChannel(
53+
[NotNull] this AuthenticationBuilder builder,
54+
[NotNull] string scheme,
55+
[NotNull] Action<ServiceChannelAuthenticationOptions> configuration)
56+
{
57+
return builder.AddServiceChannel(scheme, ServiceChannelAuthenticationDefaults.DisplayName, configuration);
58+
}
59+
60+
/// <summary>
61+
/// Adds <see cref="ServiceChannelAuthenticationHandler"/> to the specified
62+
/// <see cref="AuthenticationBuilder"/>, which enables ServiceChannel authentication capabilities.
63+
/// </summary>
64+
/// <param name="builder">The authentication builder.</param>
65+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
66+
/// <param name="caption">The optional display name associated with this instance.</param>
67+
/// <param name="configuration">The delegate used to configure the ServiceChannel options.</param>
68+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
69+
public static AuthenticationBuilder AddServiceChannel(
70+
[NotNull] this AuthenticationBuilder builder,
71+
[NotNull] string scheme,
72+
[CanBeNull] string caption,
73+
[NotNull] Action<ServiceChannelAuthenticationOptions> configuration)
74+
{
75+
return builder.AddOAuth<ServiceChannelAuthenticationOptions, ServiceChannelAuthenticationHandler>(scheme, caption, configuration);
76+
}
77+
}
78+
}
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.Text.Encodings.Web;
11+
using System.Text.Json;
12+
using System.Threading.Tasks;
13+
using JetBrains.Annotations;
14+
using Microsoft.AspNetCore.Authentication;
15+
using Microsoft.AspNetCore.Authentication.OAuth;
16+
using Microsoft.AspNetCore.WebUtilities;
17+
using Microsoft.Extensions.Logging;
18+
using Microsoft.Extensions.Options;
19+
20+
namespace AspNet.Security.OAuth.ServiceChannel
21+
{
22+
public class ServiceChannelAuthenticationHandler : OAuthHandler<ServiceChannelAuthenticationOptions>
23+
{
24+
public ServiceChannelAuthenticationHandler(
25+
[NotNull] IOptionsMonitor<ServiceChannelAuthenticationOptions> options,
26+
[NotNull] ILoggerFactory logger,
27+
[NotNull] UrlEncoder encoder,
28+
[NotNull] ISystemClock clock)
29+
: base(options, logger, encoder, clock)
30+
{
31+
}
32+
33+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
34+
[NotNull] ClaimsIdentity identity,
35+
[NotNull] AuthenticationProperties properties,
36+
[NotNull] OAuthTokenResponse tokens)
37+
{
38+
using var request = new HttpRequestMessage(HttpMethod.Get, Options.UserInformationEndpoint);
39+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
40+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
41+
42+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
43+
if (!response.IsSuccessStatusCode)
44+
{
45+
Logger.LogError("An error occurred while retrieving the user profile: the remote server " +
46+
"returned a {Status} response with the following payload: {Headers} {Body}.",
47+
/* Status: */ response.StatusCode,
48+
/* Headers: */ response.Headers.ToString(),
49+
/* Body: */ await response.Content.ReadAsStringAsync(Context.RequestAborted));
50+
51+
throw new HttpRequestException("An error occurred while retrieving the user profile.");
52+
}
53+
54+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
55+
56+
var principal = new ClaimsPrincipal(identity);
57+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
58+
context.RunClaimActions();
59+
60+
await Events.CreatingTicket(context);
61+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
62+
}
63+
}
64+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.Security.Claims;
8+
using Microsoft.AspNetCore.Authentication;
9+
using Microsoft.AspNetCore.Authentication.OAuth;
10+
using static AspNet.Security.OAuth.ServiceChannel.ServiceChannelAuthenticationConstants;
11+
12+
namespace AspNet.Security.OAuth.ServiceChannel
13+
{
14+
/// <summary>
15+
/// Defines a set of options used by <see cref="ServiceChannelAuthenticationHandler"/>.
16+
/// </summary>
17+
public class ServiceChannelAuthenticationOptions : OAuthOptions
18+
{
19+
public ServiceChannelAuthenticationOptions()
20+
{
21+
ClaimsIssuer = ServiceChannelAuthenticationDefaults.Issuer;
22+
CallbackPath = ServiceChannelAuthenticationDefaults.CallbackPath;
23+
24+
AuthorizationEndpoint = ServiceChannelAuthenticationDefaults.AuthorizationEndpoint;
25+
TokenEndpoint = ServiceChannelAuthenticationDefaults.TokenEndpoint;
26+
UserInformationEndpoint = ServiceChannelAuthenticationDefaults.UserInformationEndpoint;
27+
28+
ClaimActions.MapJsonSubKey(ClaimTypes.NameIdentifier, "UserProfile", "UserId");
29+
ClaimActions.MapJsonSubKey(ClaimTypes.Name, "UserProfile", "UserName");
30+
ClaimActions.MapJsonSubKey(ClaimTypes.Email, "UserProfile", "Email");
31+
ClaimActions.MapJsonSubKey(Claims.ProviderId, "UserProfile", "ProviderId");
32+
ClaimActions.MapJsonSubKey(Claims.ProviderName, "UserProfile", "ProviderName");
33+
}
34+
}
35+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.Security.Claims;
8+
using System.Threading.Tasks;
9+
using AspNet.Security.OAuth.ServiceChannel;
10+
using Microsoft.AspNetCore.Authentication;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using Xunit;
13+
using Xunit.Abstractions;
14+
using static AspNet.Security.OAuth.ServiceChannel.ServiceChannelAuthenticationConstants;
15+
16+
namespace AspNet.Security.OAuth.ServiceChannel
17+
{
18+
public class ServiceChannelTests : OAuthTests<ServiceChannelAuthenticationOptions>
19+
{
20+
public ServiceChannelTests(ITestOutputHelper outputHelper)
21+
{
22+
OutputHelper = outputHelper;
23+
}
24+
25+
public override string DefaultScheme => ServiceChannelAuthenticationDefaults.AuthenticationScheme;
26+
27+
protected internal override void RegisterAuthentication(AuthenticationBuilder builder)
28+
{
29+
builder.AddServiceChannel(options =>
30+
{
31+
ConfigureDefaults(builder, options);
32+
});
33+
}
34+
35+
[Theory]
36+
[InlineData(ClaimTypes.NameIdentifier, "4034383")]
37+
[InlineData(ClaimTypes.Name, "[email protected]")]
38+
[InlineData(ClaimTypes.Email, "[email protected]")]
39+
[InlineData(Claims.ProviderId, "2000156703")]
40+
[InlineData(Claims.ProviderName, "uicccf")]
41+
public async Task Can_Sign_In_Using_ServiceChannel(string claimType, string claimValue)
42+
{
43+
// Arrange
44+
using var server = CreateTestServer();
45+
46+
// Act
47+
var claims = await AuthenticateUserAsync(server);
48+
49+
// Assert
50+
AssertClaim(claims, claimType, claimValue);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)