-
Notifications
You must be signed in to change notification settings - Fork 542
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
619 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Integrating the JumpCloud Provider | ||
|
||
## Example | ||
|
||
```csharp | ||
services.AddAuthentication(options => /* Auth configuration */) | ||
.AddJumpCloud(options => | ||
{ | ||
options.ClientId = "my-client-id"; | ||
options.ClientSecret = "my-client-secret"; | ||
options.Domain = "https://oauth.id.jumpcloud.com"; | ||
}); | ||
``` | ||
|
||
## Required Additional Settings | ||
|
||
| Property Name | Property Type | Description | Default Value | | ||
|:--|:--|:--|:--| | ||
| `Domain` | `string?` | The JumpCloud domain to use for authentication. | `null` | | ||
|
||
## Optional Settings | ||
|
||
_None._ |
24 changes: 24 additions & 0 deletions
24
src/AspNet.Security.OAuth.JumpCloud/AspNet.Security.OAuth.JumpCloud.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageValidationBaselineVersion>7.0.4</PackageValidationBaselineVersion> | ||
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<!-- This property group is required until the new provider is first published to NuGet.org --> | ||
<PropertyGroup> | ||
<DisablePackageBaselineValidation>true</DisablePackageBaselineValidation> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Description>ASP.NET Core security middleware enabling JumpCloud authentication.</Description> | ||
<Authors>AaronSadlerUK</Authors> | ||
<PackageTags>jumpcloud;aspnetcore;authentication;oauth;security</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
</Project> |
65 changes: 65 additions & 0 deletions
65
src/AspNet.Security.OAuth.JumpCloud/JumpCloudAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using System.Globalization; | ||
|
||
namespace AspNet.Security.OAuth.JumpCloud; | ||
|
||
/// <summary> | ||
/// Default values used by the JumpCloud authentication provider. | ||
/// </summary> | ||
public static class JumpCloudAuthenticationDefaults | ||
{ | ||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.Name"/>. | ||
/// </summary> | ||
public const string AuthenticationScheme = "JumpCloud"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationScheme.DisplayName"/>. | ||
/// </summary> | ||
public static readonly string DisplayName = "JumpCloud"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="AuthenticationSchemeOptions.ClaimsIssuer"/>. | ||
/// </summary> | ||
public static readonly string Issuer = "JumpCloud"; | ||
|
||
/// <summary> | ||
/// Default value for <see cref="RemoteAuthenticationOptions.CallbackPath"/>. | ||
/// </summary> | ||
public static readonly string CallbackPath = "/signin-jumpcloud"; | ||
|
||
/// <summary> | ||
/// Default path format to use for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string AuthorizationEndpointPathFormat = "/oauth2/auth"; | ||
|
||
/// <summary> | ||
/// Default path format to use for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
/// </summary> | ||
public static readonly string TokenEndpointPathFormat = "/oauth2/token"; | ||
|
||
/// <summary> | ||
/// Default path format to use for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string UserInformationEndpointPathFormat = "/userinfo"; | ||
|
||
/// <summary> | ||
/// Default path to use for <see cref="OAuthOptions.AuthorizationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string AuthorizationEndpointPath = string.Format(CultureInfo.InvariantCulture, AuthorizationEndpointPathFormat); | ||
|
||
/// <summary> | ||
/// Default path to use for <see cref="OAuthOptions.TokenEndpoint"/>. | ||
/// </summary> | ||
public static readonly string TokenEndpointPath = string.Format(CultureInfo.InvariantCulture, TokenEndpointPathFormat); | ||
|
||
/// <summary> | ||
/// Default path to use for <see cref="OAuthOptions.UserInformationEndpoint"/>. | ||
/// </summary> | ||
public static readonly string UserInformationEndpointPath = string.Format(CultureInfo.InvariantCulture, UserInformationEndpointPathFormat); | ||
} |
77 changes: 77 additions & 0 deletions
77
src/AspNet.Security.OAuth.JumpCloud/JumpCloudAuthenticationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace AspNet.Security.OAuth.JumpCloud; | ||
|
||
/// <summary> | ||
/// Extension methods to add JumpCloud authentication capabilities to an HTTP application pipeline. | ||
/// </summary> | ||
public static class JumpCloudAuthenticationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddJumpCloud([NotNull] this AuthenticationBuilder builder) | ||
{ | ||
return builder.AddJumpCloud(JumpCloudAuthenticationDefaults.AuthenticationScheme, options => { }); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <param name="configuration">The delegate used to configure the JumpCloud options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddJumpCloud( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] Action<JumpCloudAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddJumpCloud(JumpCloudAuthenticationDefaults.AuthenticationScheme, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud 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 JumpCloud options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddJumpCloud( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[NotNull] Action<JumpCloudAuthenticationOptions> configuration) | ||
{ | ||
return builder.AddJumpCloud(scheme, JumpCloudAuthenticationDefaults.DisplayName, configuration); | ||
} | ||
|
||
/// <summary> | ||
/// Adds <see cref="AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler"/> to the specified | ||
/// <see cref="AuthenticationBuilder"/>, which enables JumpCloud authentication capabilities. | ||
/// </summary> | ||
/// <param name="builder">The authentication builder.</param> | ||
/// <param name="scheme">The authentication scheme associated with this instance.</param> | ||
/// <param name="caption">The optional display name associated with this instance.</param> | ||
/// <param name="configuration">The delegate used to configure the JumpCloud options.</param> | ||
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns> | ||
public static AuthenticationBuilder AddJumpCloud( | ||
[NotNull] this AuthenticationBuilder builder, | ||
[NotNull] string scheme, | ||
[CanBeNull] string caption, | ||
[NotNull] Action<JumpCloudAuthenticationOptions> configuration) | ||
{ | ||
builder.Services.TryAddSingleton<IPostConfigureOptions<JumpCloudAuthenticationOptions>, JumpCloudPostConfigureOptions>(); | ||
return builder.AddOAuth<JumpCloudAuthenticationOptions, AspNet.Security.OAuth.JumpCloud.JumpCloudAuthenticationHandler>(scheme, caption, configuration); | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
src/AspNet.Security.OAuth.JumpCloud/JumpCloudAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) | ||
* See https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers | ||
* for more information concerning the license and the contributors participating to this project. | ||
*/ | ||
|
||
using System.Net.Http.Headers; | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace AspNet.Security.OAuth.JumpCloud; | ||
|
||
/// <summary> | ||
/// Defines a handler for authentication using JumpCloud. | ||
/// </summary> | ||
public partial class JumpCloudAuthenticationHandler : OAuthHandler<JumpCloudAuthenticationOptions> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="JumpCloudAuthenticationHandler"/> class. | ||
/// </summary> | ||
/// <param name="options">The authentication options.</param> | ||
/// <param name="logger">The logger to use.</param> | ||
/// <param name="encoder">The URL encoder to use.</param> | ||
/// <param name="clock">The system clock to use.</param> | ||
public JumpCloudAuthenticationHandler( | ||
[NotNull] IOptionsMonitor<JumpCloudAuthenticationOptions> options, | ||
[NotNull] ILoggerFactory logger, | ||
[NotNull] UrlEncoder encoder, | ||
[NotNull] ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task<AuthenticationTicket> CreateTicketAsync( | ||
[NotNull] ClaimsIdentity identity, | ||
[NotNull] AuthenticationProperties properties, | ||
[NotNull] OAuthTokenResponse tokens) | ||
{ | ||
string endpoint = Options.UserInformationEndpoint; | ||
|
||
using var request = new HttpRequestMessage(HttpMethod.Get, endpoint); | ||
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", tokens.AccessToken); | ||
|
||
using var response = await Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Context.RequestAborted); | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
await Log.UserProfileErrorAsync(Logger, response, Context.RequestAborted); | ||
throw new HttpRequestException("An error occurred while retrieving the user profile from JumpCloud."); | ||
} | ||
|
||
using var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync(Context.RequestAborted)); | ||
|
||
var principal = new ClaimsPrincipal(identity); | ||
var context = new OAuthCreatingTicketContext(principal, properties, Context, Scheme, Options, Backchannel, tokens, payload.RootElement); | ||
context.RunClaimActions(); | ||
|
||
await Events.CreatingTicket(context); | ||
return new AuthenticationTicket(context.Principal!, context.Properties, Scheme.Name); | ||
} | ||
|
||
private static partial class Log | ||
{ | ||
internal static async Task UserProfileErrorAsync(ILogger logger, HttpResponseMessage response, CancellationToken cancellationToken) | ||
{ | ||
UserProfileError( | ||
logger, | ||
response.StatusCode, | ||
response.Headers.ToString(), | ||
await response.Content.ReadAsStringAsync(cancellationToken)); | ||
} | ||
|
||
[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}.")] | ||
private static partial void UserProfileError( | ||
ILogger logger, | ||
System.Net.HttpStatusCode status, | ||
string headers, | ||
string body); | ||
} | ||
} |
Oops, something went wrong.