Skip to content

Conversation

@joegoldman2
Copy link
Contributor

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Fixes #56331.

@ghost ghost added the area-security label Jun 20, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 20, 2024
@joegoldman2 joegoldman2 changed the title Add AuthorizationPolicyBuilder.RequireClaim overload that take a Predicate<Claim> Add AuthorizationPolicyBuilder.RequireClaim overload that take a Func<Claim, bool> Nov 4, 2024
@halter73
Copy link
Member

@joegoldman2 Did you see my last comment on the API proposal? I'm wondering what you think about leaving ClaimsAuthorizationRequirement unchanged and having RequireClaim(Func<Claim, bool> match) add an AssertionRequirement instead.

I'm hoping to go over this in API review tomorrow. I'm not saying we'd definitely approve the API with this modification, but it might make it more appealing since it'd reduce the size and impact of the change.

@halter73
Copy link
Member

Thanks for your contribution. As you may have already seen, we decided to reject the API proposal (#56331). Ultimately, we did not find the example usage super compelling.

builder.Services.AddAuthorization(options =>
{
   // Checking if there is any claim that starts with a certain prefix
   options.AddPolicy("prefix", policy => policy.RequireClaim(claim => claim.Value.StartsWith("prefix-"));
});

Aside from yours, we haven't heard many requests to define a policy where a user must have a claim with a given prefix. This goes for claim types and values. And the workaround seems easy enough:

builder.Services.AddAuthorization(options =>
{
   // Checking if there is any claim type that starts with a certain prefix
   options.AddPolicy("prefix", policy => policy.RequireAssertion(ctx => ctx.User.HasClaim(claim => claim.Type.StartsWith("prefix"))));
});

It's not quite as terse, but it doesn't seem too onerous.

@halter73 halter73 closed this Mar 23, 2025
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview4 milestone Mar 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-security community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: AuthorizationPolicyBuilder.RequireClaim overload that take a Func<Claim, bool>

2 participants