-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
229 additions
and
44 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
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
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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net9.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<FrameworkReference Include="Microsoft.AspNetCore.App" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/EasyAuth.Handlers/EasyAuthAuthenticationBuilderExtensions.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,19 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace EasyAuth.Handlers; | ||
|
||
public static class EasyAuthAuthenticationBuilderExtensions | ||
{ | ||
public static AuthenticationBuilder AddAzureEasyAuthHandler(this AuthenticationBuilder builder, Action<EasyAuthAuthenticationOptions>? configure = null) | ||
{ | ||
if (configure == null) | ||
{ | ||
configure = o => { }; | ||
} | ||
|
||
return builder.AddScheme<EasyAuthAuthenticationOptions, EasyAuthAuthenticationHandler>( | ||
EasyAuthAuthenticationHandler.EASY_AUTH_SCHEME_NAME, | ||
EasyAuthAuthenticationHandler.EASY_AUTH_SCHEME_NAME, | ||
configure); | ||
} | ||
} |
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,43 @@ | ||
using System.Text.Encodings.Web; | ||
|
||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace EasyAuth.Handlers; | ||
|
||
public class EasyAuthAuthenticationHandler(IOptionsMonitor<EasyAuthAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder) | ||
: AuthenticationHandler<EasyAuthAuthenticationOptions>(options, logger, encoder) | ||
{ | ||
public const string EASY_AUTH_SCHEME_NAME = "EasyAuth"; | ||
|
||
protected override async Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
try | ||
{ | ||
var easyAuthProvider = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL-IDP"].FirstOrDefault() ?? "aad"; | ||
var encoded = Context.Request.Headers["X-MS-CLIENT-PRINCIPAL"].FirstOrDefault(); | ||
if (string.IsNullOrWhiteSpace(encoded) == true) | ||
{ | ||
return AuthenticateResult.NoResult(); | ||
} | ||
|
||
var principal = await MsClientPrincipal.ParseClaimsPrincipal(encoded!).ConfigureAwait(false); | ||
if (principal == null) | ||
{ | ||
return AuthenticateResult.NoResult(); | ||
} | ||
|
||
var ticket = new AuthenticationTicket(principal, easyAuthProvider); | ||
var success = AuthenticateResult.Success(ticket); | ||
|
||
this.Context.User = principal; | ||
|
||
return success; | ||
} | ||
catch (Exception ex) | ||
{ | ||
return AuthenticateResult.Fail(ex); | ||
} | ||
} | ||
} |
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,11 @@ | ||
using Microsoft.AspNetCore.Authentication; | ||
|
||
namespace EasyAuth.Handlers; | ||
|
||
public class EasyAuthAuthenticationOptions : AuthenticationSchemeOptions | ||
{ | ||
public EasyAuthAuthenticationOptions() | ||
{ | ||
Events = new object(); | ||
} | ||
} |
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,51 @@ | ||
using System.Security.Claims; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EasyAuth.Handlers; | ||
|
||
public class MsClientPrincipal | ||
{ | ||
private static readonly JsonSerializerOptions options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; | ||
|
||
[JsonPropertyName("auth_typ")] | ||
public string? IdentityProvider { get; set; } | ||
|
||
[JsonPropertyName("name_typ")] | ||
public string? NameClaimType { get; set; } | ||
|
||
[JsonPropertyName("role_typ")] | ||
public string? RoleClaimType { get; set; } | ||
|
||
[JsonPropertyName("claims")] | ||
public IEnumerable<MsClientPrincipalClaim>? Claims { get; set; } | ||
|
||
public static async Task<MsClientPrincipal?> ParseMsClientPrincipal(string value) | ||
{ | ||
var decoded = Convert.FromBase64String(value); | ||
using var stream = new MemoryStream(decoded); | ||
var principal = await JsonSerializer.DeserializeAsync<MsClientPrincipal>(stream, options).ConfigureAwait(false); | ||
|
||
return principal; | ||
} | ||
|
||
public static async Task<ClaimsPrincipal?> ParseClaimsPrincipal(string value) | ||
{ | ||
var clientPrincipal = await ParseMsClientPrincipal(value).ConfigureAwait(false); | ||
if (clientPrincipal == null || clientPrincipal.Claims?.Any() == false) | ||
{ | ||
return null; | ||
} | ||
|
||
var claims = clientPrincipal.Claims!.Select(claim => new Claim(claim.Type!, claim.Value!)); | ||
|
||
// remap "roles" claims from easy auth to the more standard ClaimTypes.Role: "http://schemas.microsoft.com/ws/2008/06/identity/claims/role" | ||
var easyAuthRoleClaims = claims.Where(claim => claim.Type == "roles"); | ||
var claimsAndRoles = claims.Concat(easyAuthRoleClaims.Select(role => new Claim(clientPrincipal.RoleClaimType!, role.Value))); | ||
|
||
var identity = new ClaimsIdentity(claimsAndRoles, clientPrincipal.IdentityProvider, clientPrincipal.NameClaimType, clientPrincipal.RoleClaimType); | ||
var claimsPrincipal = new ClaimsPrincipal(identity); | ||
|
||
return claimsPrincipal; | ||
} | ||
} |
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,12 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EasyAuth.Handlers; | ||
|
||
public class MsClientPrincipalClaim | ||
{ | ||
[JsonPropertyName("typ")] | ||
public string? Type { get; set; } | ||
|
||
[JsonPropertyName("val")] | ||
public string? Value { get; set; } | ||
} |
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 |
---|---|---|
@@ -1,9 +1,25 @@ | ||
@page "/" | ||
@inject IConfiguration Config | ||
|
||
<PageTitle>Home</PageTitle> | ||
|
||
<h1>Hello, world!</h1> | ||
|
||
Welcome to your new app. | ||
|
||
<AuthDetails @rendermode="RenderMode.InteractiveServer" /> | ||
@if (useAuthDetails == true) | ||
{ | ||
<AuthDetails @rendermode="RenderMode.InteractiveServer" /> | ||
} | ||
|
||
@code | ||
{ | ||
private bool useAuthDetails; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
useAuthDetails = bool.TryParse(Config["USE_AUTH_DETAILS"], out var result) && result; | ||
|
||
await Task.CompletedTask; | ||
} | ||
} |
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
Oops, something went wrong.