Skip to content

Commit f2be82d

Browse files
drewkill32Andrew Killionmartincostello
authored
Add PingOne provider (#758)
* Adding PingOne Provider * updating documentation * fixing url for tests * adding test with custom domain * adding new version to package validation Co-authored-by: Martin Costello <[email protected]> * fixing casing the exception string Co-authored-by: Martin Costello <[email protected]> * updating changes from PR review * updating docs to reflect code change * Update documentation Minor documentation updates. --------- Co-authored-by: Andrew Killion <[email protected]> Co-authored-by: Martin Costello <[email protected]>
1 parent bfa6e42 commit f2be82d

14 files changed

+736
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Smart
295295
EndProject
296296
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Huawei", "src\AspNet.Security.OAuth.Huawei\AspNet.Security.OAuth.Huawei.csproj", "{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}"
297297
EndProject
298+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.PingOne", "src\AspNet.Security.OAuth.PingOne\AspNet.Security.OAuth.PingOne.csproj", "{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}"
299+
EndProject
298300
Global
299301
GlobalSection(SolutionConfigurationPlatforms) = preSolution
300302
Debug|Any CPU = Debug|Any CPU
@@ -677,6 +679,10 @@ Global
677679
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Debug|Any CPU.Build.0 = Debug|Any CPU
678680
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Release|Any CPU.ActiveCfg = Release|Any CPU
679681
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E}.Release|Any CPU.Build.0 = Release|Any CPU
682+
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
683+
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
684+
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
685+
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Release|Any CPU.Build.0 = Release|Any CPU
680686
EndGlobalSection
681687
GlobalSection(SolutionProperties) = preSolution
682688
HideSolutionNode = FALSE
@@ -782,6 +788,7 @@ Global
782788
{8E42EF81-A630-4BDB-B642-3F20C863F9BE} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
783789
{68862DC5-65B7-4517-B909-AB334F9FCF6E} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
784790
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
791+
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
785792
EndGlobalSection
786793
GlobalSection(ExtensibilityGlobals) = postSolution
787794
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ We would love it if you could help contributing to this repository.
5656
* [CoCo Lin](https://github.com/linmasaki)
5757
* [Dave Timmins](https://github.com/davetimmins)
5858
* [Dmitry Popov](https://github.com/justdmitry)
59+
* [Drew Killion](https://github.com/drewkill32)
5960
* [Elan Hasson](https://github.com/ElanHasson)
6061
* [Eric Green](https://github.com/ericgreenmix)
6162
* [Ethan Celletti](https://github.com/Gekctek)
@@ -180,6 +181,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
180181
| Onshape | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Onshape?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Onshape/ "Download AspNet.Security.OAuth.Onshape from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Onshape?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Onshape "Download AspNet.Security.OAuth.Onshape from MyGet.org") | N/A |
181182
| Patreon | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Patreon?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Patreon/ "Download AspNet.Security.OAuth.Patreon from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Patreon?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Patreon "Download AspNet.Security.OAuth.Patreon from MyGet.org") | [Documentation](https://docs.patreon.com/#oauth "Patreon developer documentation") |
182183
| Paypal | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Paypal?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Paypal/ "Download AspNet.Security.OAuth.Paypal from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Paypal?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Paypal "Download AspNet.Security.OAuth.Paypal from MyGet.org") | [Documentation](https://developer.paypal.com/docs/api-basics/#oauth-20-authorization-protocol "Paypal developer documentation") |
184+
| PingOne | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.PingOne?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.PingOne/ "Download AspNet.Security.OAuth.PingOne from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.PingOne?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.PingOne "Download AspNet.Security.OAuth.PingOne from MyGet.org") | [Documentation](https://apidocs.pingidentity.com/pingone/platform/v1/api/#openid-connectoauth-2 "PingOne developer documentation") |
183185
| QQ | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.QQ?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.QQ/ "Download AspNet.Security.OAuth.QQ from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.QQ?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.QQ "Download AspNet.Security.OAuth.QQ from MyGet.org") | [Documentation](https://developers.e.qq.com/docs/apilist/auth/oauth2 "QQ developer documentation") |
184186
| QuickBooks | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.QuickBooks?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.QuickBooks/ "Download AspNet.Security.OAuth.QuickBooks from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.QuickBooks?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.QuickBooks "Download AspNet.Security.OAuth.QuickBooks from MyGet.org") | [Documentation](https://www.developer.intuit.com/app/developer/qbo/docs/develop/authentication-and-authorization/oauth-2.0-playground "QuickBooks developer documentation") |
185187
| Reddit | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Reddit?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Reddit/ "Download AspNet.Security.OAuth.Reddit from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Reddit?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Reddit "Download AspNet.Security.OAuth.Reddit from MyGet.org") | [Documentation](https://github.com/reddit-archive/reddit/wiki/oauth2 "Reddit developer documentation") |

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ covered by the section above.
6161
| Odnoklassniki | _Optional_ | [Documentation](odnoklassniki.md "Odnoklassniki provider documentation") |
6262
| Okta | **Required** | [Documentation](okta.md "Okta provider documentation") |
6363
| Patreon | _Optional_ | [Documentation](patreon.md "Patreon provider documentation") |
64+
| PingOne | _Optional_ | [Documentation](pingone.md "PingOne provider documentation") |
6465
| QQ | _Optional_ | [Documentation](qq.md "QQ provider documentation") |
6566
| Reddit | _Optional_ | [Documentation](reddit.md "Reddit provider documentation") |
6667
| Salesforce | _Optional_ | [Documentation](salesforce.md "Salesforce provider documentation") |

docs/pingone.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Integrating the PingOne Provider
2+
3+
## Example
4+
5+
```csharp
6+
services.AddAuthentication(options => /* Auth configuration */)
7+
.AddPingOne(options =>
8+
{
9+
options.ClientId = "my-client-id";
10+
options.ClientSecret = "my-client-secret";
11+
options.EnvironmentId = "63e9d5c3-5bb8-462d-8f71-8e6b2592e516";
12+
});
13+
```
14+
15+
## Required Additional Settings
16+
17+
| Property Name | Property Type | Description | Default Value |
18+
|:--|:--|:--|:--|
19+
| `EnvironmentId` | `string` | The PingOne EnvironmentId to use for authentication. This can be found on the `environment.properties` page of the PingOne admin portal for your account. | `""` |
20+
21+
## Optional Settings
22+
23+
| Property Name | Property Type | Description | Default Value |
24+
|:--|:--|:--|:--|
25+
| `Domain` | `string?` | The PingOne domain to use for authentication. Can be a custom domain configured in PingOne or one of the following: `auth.pingone.com` for the United States region, `auth.pingone.ca` for the Canada region, `auth.pingone.eu` for the European Union region, or `auth.pingone.asia` for the Asia-Pacific region. | `"auth.pingone.com"` |
26+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
8+
<!-- TODO Remove once published to NuGet.org -->
9+
<PropertyGroup>
10+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
11+
<PackageValidationBaselineVersion>7.0.1</PackageValidationBaselineVersion>
12+
</PropertyGroup>
13+
14+
<PropertyGroup>
15+
<Description>ASP.NET Core security provider enabling PingOne authentication.</Description>
16+
<Authors>Drew Killion</Authors>
17+
<PackageTags>pingone;aspnetcore;authentication;oauth;security</PackageTags>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
22+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
23+
</ItemGroup>
24+
25+
</Project>
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+
namespace AspNet.Security.OAuth.PingOne;
8+
9+
/// <summary>
10+
/// Default values used by the PingOne authentication provider.
11+
/// </summary>
12+
public static class PingOneAuthenticationDefaults
13+
{
14+
/// <summary>
15+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
16+
/// </summary>
17+
public const string AuthenticationScheme = "PingOne";
18+
19+
/// <summary>
20+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
21+
/// </summary>
22+
public static readonly string DisplayName = "PingOne";
23+
24+
/// <summary>
25+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
26+
/// </summary>
27+
public static readonly string Issuer = "PingOne";
28+
29+
/// <summary>
30+
/// Default value for PingOne domain.
31+
/// </summary>
32+
public static readonly string Domain = "auth.pingone.com";
33+
34+
/// <summary>
35+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
36+
/// </summary>
37+
public static readonly string CallbackPath = "/signin-pingone";
38+
39+
/// <summary>
40+
/// Default path format to use for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
41+
/// </summary>
42+
public static readonly string AuthorizationEndpointPathFormat = "/{0}/as/authorize";
43+
44+
/// <summary>
45+
/// Default path format to use for <see cref="OAuthOptions.TokenEndpoint"/>.
46+
/// </summary>
47+
public static readonly string TokenEndpointPathFormat = "/{0}/as/token";
48+
49+
/// <summary>
50+
/// Default path format to use for <see cref="OAuthOptions.UserInformationEndpoint"/>.
51+
/// </summary>
52+
public static readonly string UserInformationEndpointPathFormat = "/{0}/as/userinfo";
53+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 AspNet.Security.OAuth.PingOne;
8+
using Microsoft.Extensions.DependencyInjection.Extensions;
9+
using Microsoft.Extensions.Options;
10+
11+
namespace Microsoft.Extensions.DependencyInjection;
12+
13+
/// <summary>
14+
/// Extension methods to add PingOne authentication capabilities to an HTTP application pipeline.
15+
/// </summary>
16+
public static class PingOneAuthenticationExtensions
17+
{
18+
/// <summary>
19+
/// Adds <see cref="PingOneAuthenticationHandler"/> to the specified
20+
/// <see cref="AuthenticationBuilder"/>, which enables PingOne authentication capabilities.
21+
/// </summary>
22+
/// <param name="builder">The authentication builder.</param>
23+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
24+
public static AuthenticationBuilder AddPingOne([NotNull] this AuthenticationBuilder builder)
25+
{
26+
return builder.AddPingOne(PingOneAuthenticationDefaults.AuthenticationScheme, options => { });
27+
}
28+
29+
/// <summary>
30+
/// Adds <see cref="PingOneAuthenticationHandler"/> to the specified
31+
/// <see cref="AuthenticationBuilder"/>, which enables PingOne authentication capabilities.
32+
/// </summary>
33+
/// <param name="builder">The authentication builder.</param>
34+
/// <param name="configuration">The delegate used to configure the PingOne options.</param>
35+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
36+
public static AuthenticationBuilder AddPingOne(
37+
[NotNull] this AuthenticationBuilder builder,
38+
[NotNull] Action<PingOneAuthenticationOptions> configuration)
39+
{
40+
return builder.AddPingOne(PingOneAuthenticationDefaults.AuthenticationScheme, configuration);
41+
}
42+
43+
/// <summary>
44+
/// Adds <see cref="PingOneAuthenticationHandler"/> to the specified
45+
/// <see cref="AuthenticationBuilder"/>, which enables PingOne authentication capabilities.
46+
/// </summary>
47+
/// <param name="builder">The authentication builder.</param>
48+
/// <param name="scheme">The authentication scheme associated with this instance.</param>
49+
/// <param name="configuration">The delegate used to configure the PingOne options.</param>
50+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
51+
public static AuthenticationBuilder AddPingOne(
52+
[NotNull] this AuthenticationBuilder builder,
53+
[NotNull] string scheme,
54+
[NotNull] Action<PingOneAuthenticationOptions> configuration)
55+
{
56+
return builder.AddPingOne(scheme, PingOneAuthenticationDefaults.DisplayName, configuration);
57+
}
58+
59+
/// <summary>
60+
/// Adds <see cref="PingOneAuthenticationHandler"/> to the specified
61+
/// <see cref="AuthenticationBuilder"/>, which enables PingOne 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="caption">The optional display name associated with this instance.</param>
66+
/// <param name="configuration">The delegate used to configure the PingOne options.</param>
67+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
68+
public static AuthenticationBuilder AddPingOne(
69+
[NotNull] this AuthenticationBuilder builder,
70+
[NotNull] string scheme,
71+
[CanBeNull] string caption,
72+
[NotNull] Action<PingOneAuthenticationOptions> configuration)
73+
{
74+
builder.Services.TryAddSingleton<IPostConfigureOptions<PingOneAuthenticationOptions>, PingOnePostConfigureOptions>();
75+
return builder.AddOAuth<PingOneAuthenticationOptions, PingOneAuthenticationHandler>(scheme, caption, configuration);
76+
}
77+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.Headers;
8+
using System.Security.Claims;
9+
using System.Text.Encodings.Web;
10+
using System.Text.Json;
11+
using Microsoft.Extensions.Logging;
12+
using Microsoft.Extensions.Options;
13+
14+
namespace AspNet.Security.OAuth.PingOne;
15+
16+
/// <summary>
17+
/// Defines a handler for authentication using PingOne.
18+
/// </summary>
19+
public partial class PingOneAuthenticationHandler : OAuthHandler<PingOneAuthenticationOptions>
20+
{
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="PingOneAuthenticationHandler"/> class.
23+
/// </summary>
24+
/// <param name="options">The authentication options.</param>
25+
/// <param name="logger">The logger to use.</param>
26+
/// <param name="encoder">The URL encoder to use.</param>
27+
/// <param name="clock">The system clock to use.</param>
28+
public PingOneAuthenticationHandler(
29+
[NotNull] IOptionsMonitor<PingOneAuthenticationOptions> options,
30+
[NotNull] ILoggerFactory logger,
31+
[NotNull] UrlEncoder encoder,
32+
[NotNull] ISystemClock clock)
33+
: base(options, logger, encoder, clock)
34+
{
35+
}
36+
37+
/// <inheritdoc />
38+
protected override async Task<AuthenticationTicket> CreateTicketAsync(
39+
[NotNull] ClaimsIdentity identity,
40+
[NotNull] AuthenticationProperties properties,
41+
[NotNull] OAuthTokenResponse tokens)
42+
{
43+
string endpoint = Options.UserInformationEndpoint;
44+
45+
using var request = new HttpRequestMessage(HttpMethod.Get, endpoint);
46+
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
47+
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken);
48+
49+
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted);
50+
if (!response.IsSuccessStatusCode)
51+
{
52+
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted);
53+
throw new HttpRequestException("An error occurred while retrieving the user profile from PingOne.");
54+
}
55+
56+
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted));
57+
58+
var principal = new ClaimsPrincipal(identity);
59+
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement);
60+
context.RunClaimActions();
61+
62+
await Events.CreatingTicket(context);
63+
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name);
64+
}
65+
66+
private static partial class Log
67+
{
68+
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken)
69+
{
70+
UserProfileError(
71+
logger,
72+
response.StatusCode,
73+
response.Headers.ToString(),
74+
await response.Content.ReadAsStringAsync(cancellationToken));
75+
}
76+
77+
[LoggerMessage(1, LogLevel.Error, "An error occurred while retrieving the user profile: the remote server returned a {Status} response with the following payload: {Headers} {Body}.")]
78+
private static partial void UserProfileError(
79+
ILogger logger,
80+
System.Net.HttpStatusCode status,
81+
string headers,
82+
string body);
83+
}
84+
}

0 commit comments

Comments
 (0)