Skip to content

Commit 2fac680

Browse files
Add JumpCloud provider (#797)
Add a provider for JumpCloud.
1 parent a5c1db8 commit 2fac680

13 files changed

+619
-0
lines changed

AspNet.Security.OAuth.Providers.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.Kook"
296296
EndProject
297297
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AspNet.Security.OAuth.PingOne", "src\AspNet.Security.OAuth.PingOne\AspNet.Security.OAuth.PingOne.csproj", "{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}"
298298
EndProject
299+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNet.Security.OAuth.JumpCloud", "src\AspNet.Security.OAuth.JumpCloud\AspNet.Security.OAuth.JumpCloud.csproj", "{8AF5DDBE-2631-4E71-9045-73A6356CE86B}"
300+
EndProject
299301
Global
300302
GlobalSection(SolutionConfigurationPlatforms) = preSolution
301303
Debug|Any CPU = Debug|Any CPU
@@ -678,6 +680,10 @@ Global
678680
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
679681
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
680682
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF}.Release|Any CPU.Build.0 = Release|Any CPU
683+
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
684+
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Debug|Any CPU.Build.0 = Debug|Any CPU
685+
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.ActiveCfg = Release|Any CPU
686+
{8AF5DDBE-2631-4E71-9045-73A6356CE86B}.Release|Any CPU.Build.0 = Release|Any CPU
681687
EndGlobalSection
682688
GlobalSection(SolutionProperties) = preSolution
683689
HideSolutionNode = FALSE
@@ -783,6 +789,7 @@ Global
783789
{E3CF7FFC-56A0-4033-87A9-BB3080CF030E} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
784790
{101681FB-569F-4941-B943-2AD380039BE0} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
785791
{CF8C4235-6AE6-404E-B572-4FF4E85AB5FF} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
792+
{8AF5DDBE-2631-4E71-9045-73A6356CE86B} = {C1352FD3-AE8B-43EE-B45B-F6E0B3FBAC6D}
786793
EndGlobalSection
787794
GlobalSection(ExtensibilityGlobals) = postSolution
788795
SolutionGuid = {C7B54DE2-6407-4802-AD9C-CE54BF414C8C}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ We would love it if you could help contributing to this repository.
4242

4343
**Special thanks to our contributors:**
4444

45+
* [Aaron Sadler](https://github.com/aaronsadleruk)
4546
* [Abhinav Nigam](https://github.com/abhinavnigam)
4647
* [Adam Reisinger](https://github.com/Res42)
4748
* [Albert Zakiev](https://github.com/serber)
@@ -159,6 +160,7 @@ If a provider you're looking for does not exist, consider making a PR to add one
159160
| HubSpot | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.HubSpot?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.HubSpot/ "Download AspNet.Security.OAuth.HubSpot from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.HubSpot?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.HubSpot "Download AspNet.Security.OAuth.HubSpot from MyGet.org") | [Documentation](https://developers.hubspot.com/docs "HubSpot developer documentation") |
160161
| Imgur | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Imgur?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Imgur/ "Download AspNet.Security.OAuth.Imgur from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Imgur?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Imgur "Download AspNet.Security.OAuth.Imgur from MyGet.org") | [Documentation](https://apidocs.imgur.com/?version=latest#authorization-and-oauth "Imgur developer documentation") |
161162
| Instagram | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Instagram?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Instagram/ "Download AspNet.Security.OAuth.Instagram from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Instagram?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Instagram "Download AspNet.Security.OAuth.Instagram from MyGet.org") | [Documentation](https://www.instagram.com/developer/authentication/ "Instagram developer documentation") |
163+
| JumpCloud | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.JumpCloud?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.JumpCloud/ "Download AspNet.Security.OAuth.JumpCloud from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.JumpCloud?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.JumpCloud "Download AspNet.Security.OAuth.JumpCloud from MyGet.org") | [Documentation](https://jumpcloud.com/support/sso-with-oidc "JumpCloud developer documentation") |
162164
| KakaoTalk | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.KakaoTalk?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.KakaoTalk/ "Download AspNet.Security.OAuth.KakaoTalk from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.KakaoTalk?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.KakaoTalk "Download AspNet.Security.OAuth.KakaoTalk from MyGet.org") | [Documentation](https://developers.kakao.com/docs/latest/en/kakaologin/common "KakaoTalk developer documentation") |
163165
| Keycloak | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Keycloak?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Keycloak/ "Download AspNet.Security.OAuth.Keycloak from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Keycloak?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Keycloak "Download AspNet.Security.OAuth.Keycloak from MyGet.org") | [Documentation](https://www.keycloak.org/docs/latest/authorization_services/#_service_overview "Keycloak developer documentation") |
164166
| Kloudless | [![NuGet](https://buildstats.info/nuget/AspNet.Security.OAuth.Kloudless?includePreReleases=false)](https://www.nuget.org/packages/AspNet.Security.OAuth.Kloudless/ "Download AspNet.Security.OAuth.Kloudless from NuGet.org") | [![MyGet](https://buildstats.info/myget/aspnet-contrib/AspNet.Security.OAuth.Kloudless?includePreReleases=true)](https://www.myget.org/feed/aspnet-contrib/package/nuget/AspNet.Security.OAuth.Kloudless "Download AspNet.Security.OAuth.Kloudless from MyGet.org") | [Documentation](https://developers.kloudless.com/docs/v1/authentication "Kloudless developer documentation") |

docs/jumpcloud.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Integrating the JumpCloud Provider
2+
3+
## Example
4+
5+
```csharp
6+
services.AddAuthentication(options => /* Auth configuration */)
7+
.AddJumpCloud(options =>
8+
{
9+
options.ClientId = "my-client-id";
10+
options.ClientSecret = "my-client-secret";
11+
options.Domain = "https://oauth.id.jumpcloud.com";
12+
});
13+
```
14+
15+
## Required Additional Settings
16+
17+
| Property Name | Property Type | Description | Default Value |
18+
|:--|:--|:--|:--|
19+
| `Domain` | `string?` | The JumpCloud domain to use for authentication. | `null` |
20+
21+
## Optional Settings
22+
23+
_None._
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<PackageValidationBaselineVersion>7.0.4</PackageValidationBaselineVersion>
5+
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
6+
</PropertyGroup>
7+
8+
<!-- This property group is required until the new provider is first published to NuGet.org -->
9+
<PropertyGroup>
10+
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation>
11+
</PropertyGroup>
12+
13+
<PropertyGroup>
14+
<Description>ASP.NET Core security middleware enabling JumpCloud authentication.</Description>
15+
<Authors>AaronSadlerUK</Authors>
16+
<PackageTags>jumpcloud;aspnetcore;authentication;oauth;security</PackageTags>
17+
</PropertyGroup>
18+
19+
<ItemGroup>
20+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
21+
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.Globalization;
8+
9+
namespace AspNet.Security.OAuth.JumpCloud;
10+
11+
/// <summary>
12+
/// Default values used by the JumpCloud authentication provider.
13+
/// </summary>
14+
public static class JumpCloudAuthenticationDefaults
15+
{
16+
/// <summary>
17+
/// Default value for <see cref="AuthenticationScheme.Name"/>.
18+
/// </summary>
19+
public const string AuthenticationScheme = "JumpCloud";
20+
21+
/// <summary>
22+
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>.
23+
/// </summary>
24+
public static readonly string DisplayName = "JumpCloud";
25+
26+
/// <summary>
27+
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>.
28+
/// </summary>
29+
public static readonly string Issuer = "JumpCloud";
30+
31+
/// <summary>
32+
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>.
33+
/// </summary>
34+
public static readonly string CallbackPath = "/signin-jumpcloud";
35+
36+
/// <summary>
37+
/// Default path format to use for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
38+
/// </summary>
39+
public static readonly string AuthorizationEndpointPathFormat = "/oauth2/auth";
40+
41+
/// <summary>
42+
/// Default path format to use for <see cref="OAuthOptions.TokenEndpoint"/>.
43+
/// </summary>
44+
public static readonly string TokenEndpointPathFormat = "/oauth2/token";
45+
46+
/// <summary>
47+
/// Default path format to use for <see cref="OAuthOptions.UserInformationEndpoint"/>.
48+
/// </summary>
49+
public static readonly string UserInformationEndpointPathFormat = "/userinfo";
50+
51+
/// <summary>
52+
/// Default path to use for <see cref="OAuthOptions.AuthorizationEndpoint"/>.
53+
/// </summary>
54+
public static readonly string AuthorizationEndpointPath = string.Format(CultureInfo.InvariantCulture, AuthorizationEndpointPathFormat);
55+
56+
/// <summary>
57+
/// Default path to use for <see cref="OAuthOptions.TokenEndpoint"/>.
58+
/// </summary>
59+
public static readonly string TokenEndpointPath = string.Format(CultureInfo.InvariantCulture, TokenEndpointPathFormat);
60+
61+
/// <summary>
62+
/// Default path to use for <see cref="OAuthOptions.UserInformationEndpoint"/>.
63+
/// </summary>
64+
public static readonly string UserInformationEndpointPath = string.Format(CultureInfo.InvariantCulture, UserInformationEndpointPathFormat);
65+
}
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 Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.DependencyInjection.Extensions;
9+
using Microsoft.Extensions.Options;
10+
11+
namespace AspNet.Security.OAuth.JumpCloud;
12+
13+
/// <summary>
14+
/// Extension methods to add JumpCloud authentication capabilities to an HTTP application pipeline.
15+
/// </summary>
16+
public static class JumpCloudAuthenticationExtensions
17+
{
18+
/// <summary>
19+
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified
20+
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud authentication capabilities.
21+
/// </summary>
22+
/// <param name="builder">The authentication builder.</param>
23+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
24+
public static AuthenticationBuilder AddJumpCloud([NotNull] this AuthenticationBuilder builder)
25+
{
26+
return builder.AddJumpCloud(JumpCloudAuthenticationDefaults.AuthenticationScheme, options => { });
27+
}
28+
29+
/// <summary>
30+
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified
31+
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud authentication capabilities.
32+
/// </summary>
33+
/// <param name="builder">The authentication builder.</param>
34+
/// <param name="configuration">The delegate used to configure the JumpCloud options.</param>
35+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
36+
public static AuthenticationBuilder AddJumpCloud(
37+
[NotNull] this AuthenticationBuilder builder,
38+
[NotNull] Action<JumpCloudAuthenticationOptions> configuration)
39+
{
40+
return builder.AddJumpCloud(JumpCloudAuthenticationDefaults.AuthenticationScheme, configuration);
41+
}
42+
43+
/// <summary>
44+
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified
45+
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud 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 JumpCloud options.</param>
50+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
51+
public static AuthenticationBuilder AddJumpCloud(
52+
[NotNull] this AuthenticationBuilder builder,
53+
[NotNull] string scheme,
54+
[NotNull] Action<JumpCloudAuthenticationOptions> configuration)
55+
{
56+
return builder.AddJumpCloud(scheme, JumpCloudAuthenticationDefaults.DisplayName, configuration);
57+
}
58+
59+
/// <summary>
60+
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified
61+
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud 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 JumpCloud options.</param>
67+
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
68+
public static AuthenticationBuilder AddJumpCloud(
69+
[NotNull] this AuthenticationBuilder builder,
70+
[NotNull] string scheme,
71+
[CanBeNull] string caption,
72+
[NotNull] Action<JumpCloudAuthenticationOptions> configuration)
73+
{
74+
builder.Services.TryAddSingleton<IPostConfigureOptions<JumpCloudAuthenticationOptions>, JumpCloudPostConfigureOptions>();
75+
return builder.AddOAuth<JumpCloudAuthenticationOptions, AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler>(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.JumpCloud;
15+
16+
/// <summary>
17+
/// Defines a handler for authentication using JumpCloud.
18+
/// </summary>
19+
public partial class JumpCloudAuthenticationHandler : OAuthHandler<JumpCloudAuthenticationOptions>
20+
{
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="JumpCloudAuthenticationHandler"/> 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 JumpCloudAuthenticationHandler(
29+
[NotNull] IOptionsMonitor<JumpCloudAuthenticationOptions> 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 JumpCloud.");
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)