Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty claim value to mimic behavior of ASP.NET AuthorizationPolicyBuilder.RequireClaim method #1337

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Ocelot/Authorization/ClaimsAuthorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace Ocelot.Authorization
{
/// <summary>Authorizer which is implemented using Claims-based authorization.</summary>
/// <remarks>Microsoft Learn: <see href="https://learn.microsoft.com/en-us/aspnet/core/security/authorization/claims">Claims-based authorization in ASP.NET Core</see>.</remarks>
public class ClaimsAuthorizer : IClaimsAuthorizer
{
private readonly IClaimsParser _claimsParser;
Expand All @@ -22,8 +24,12 @@ List<PlaceholderNameAndValue> urlPathPlaceholderNameAndValues
{
foreach (var required in routeClaimsRequirement)
{
var values = _claimsParser.GetValuesByClaimType(claimsPrincipal.Claims, required.Key);
if (string.IsNullOrEmpty(required.Value) || string.IsNullOrWhiteSpace(required.Value))
{
continue; // if required value is not specified
}

var values = _claimsParser.GetValuesByClaimType(claimsPrincipal.Claims, required.Key);
if (values.IsError)
{
return new ErrorResponse<bool>(values.Errors);
Expand Down Expand Up @@ -66,6 +72,11 @@ List<PlaceholderNameAndValue> urlPathPlaceholderNameAndValues
}
else
{
//// if required value is not specified
//if (string.IsNullOrEmpty(required.Value))
//{
// continue;
//}
// static claim
var authorized = values.Data.Contains(required.Value);
if (!authorized)
Expand Down