-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow configuring claim requirements for frontend authorization (#3619)
Adds optional configuration that checks a given claim of a user authenticated via OIDC. Two new configuration values are: - `Dashboard:Frontend:OpenIdConnect:RequireClaimType` specifies the (optional) claim that be present for authorized users. Defaults to empty. - `Dashboard:Frontend:OpenIdConnect:RequireClaimValue` specifies the (optional) value of the required claim. Only used if `Dashboard:Frontend:OpenIdConnect:RequireClaimType` is also specified. Defaults to empty. Co-authored-by: Drew Noakes <git@drewnoakes.com>
- Loading branch information
1 parent
455f6cd
commit 5ca550a
Showing
4 changed files
with
59 additions
and
2 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/Aspire.Dashboard/Authentication/OpenIdConnect/AuthorizationPolicyBuilderExtensions.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,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Authorization; | ||
using OpenIdConnectOptions = Aspire.Dashboard.Configuration.OpenIdConnectOptions; | ||
|
||
namespace Aspire.Dashboard.Authentication.OpenIdConnect; | ||
|
||
internal static class AuthorizationPolicyBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Validates that the the expected claim and value are present. | ||
/// </summary> | ||
/// <remarks> | ||
/// Checks are controlled by configuration. | ||
/// | ||
/// If <see cref="OpenIdConnectOptions.RequiredClaimType"/> is non-empty, a requirement for the claim is added. | ||
/// | ||
/// If a claim is being checked and <see cref="OpenIdConnectOptions.RequiredClaimValue"/> is non-empty, then the | ||
/// requirement is extended to also validate the specified value. | ||
/// </remarks> | ||
public static AuthorizationPolicyBuilder RequireOpenIdClaims(this AuthorizationPolicyBuilder builder, OpenIdConnectOptions options) | ||
{ | ||
var claimType = options.RequiredClaimType; | ||
var claimValue = options.RequiredClaimValue; | ||
|
||
if (!string.IsNullOrWhiteSpace(claimType)) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(claimValue)) | ||
{ | ||
builder.RequireClaim(claimType, claimValue); | ||
} | ||
else | ||
{ | ||
builder.RequireClaim(claimType); | ||
} | ||
} | ||
|
||
return builder; | ||
} | ||
} |
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