-
Notifications
You must be signed in to change notification settings - Fork 588
Add ClaimActions for bulk add and remove #1636
Conversation
/// Maps all values from the json user data as claims, excluding duplicates. | ||
/// </summary> | ||
/// <param name="collection"></param> | ||
public static void MapAllClaims(this ClaimActionCollection collection) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shorten to MapAll to be consistent with all the other methods that don't have Claims suffix?
/// </summary> | ||
/// <param name="collection"></param> | ||
/// <param name="claimTypes"></param> | ||
public static void DeleteClaims(this ClaimActionCollection collection, params string[] claimTypes) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
People have been very confused by Remove vs Delete. We need to avoid aggravating that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could also just change DeleteClaim to take a params string[] right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, but it would be binary breaking.
// Avoid adding a claim if there's a duplicate name and value. This often happens in OIDC when claims are | ||
// retrieved both from the id_token and from the user-info endpoint. | ||
var duplicate = false; | ||
foreach (var existingClaim in identity.FindAll(pair.Key)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: can you something like this?
var duplicate = identity.FindFirst(c => c.Key == pair.Key && string.Equals(c.Value, claimValue, StringComparison.Ordinal) != null
|
||
foreach (var claimType in claimTypes) | ||
{ | ||
collection.Add(new DeleteClaimAction(claimType)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider enhancing DeleteClaimAction type to take a list of claims so you don't have to add one action per type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance wise it's the same, you still need to loop through all of the names.
Adding them individually preserves the functionality of methods like ClaimActionsCollection.Remove(claimType) that removes the action by name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty sure that's exactly the behavior that's confusing today.
options.ClaimActions.Remove("something") you would think is the same as options.ClaimActions.DeleteClaim("something").
I think this api should be mostly focusing on making it easy to do the common things.
MapJsonKey
MapAll
Delete
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The list of delegate/actions is fine as an implementation detail, but I really don't think generally developers should have to understand/deal with that... when all they want to do is include/ignore some set of claims. Only someone who is trying to plug in a complex mapping should have to understand the whole claim action stuff
Maybe I am missing something - but this is only done in the sample. Not in the handler...I want this to be turned off by default. Your default mapping can do the conversion from standard to propriety if that is a requirement.
Can't we make that one operation like
|
o.ClaimActions.Clear(); | ||
o.ClaimActions.MapAllClaims(); | ||
// Note some of these protocol claims also show up in the user info | ||
o.ClaimActions.DeleteClaims("aud", "iss", "iat", "nbf", "exp", "aio", "c_hash", "uti", "nonce"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RemoveProtocolClaims
MapAllUserInfo
just to be really clear this
is not really an improvement over just turning of the inbound claims mapping globally via their static I would like to see no claims mapping at all. |
b6a8979
to
b6e905d
Compare
@leastprivilege @brockallen Updated. MapAllExcept combines both operations like you asked. |
Thanks. Will investigate. |
..and what about the claim type mappings - will this be turned off at the handler level by default? |
That's a breaking change, I can't do that right now. |
@leastprivilege @brockallen please review this week or we'll need to close this PR. |
I have a VM with preview-1. I assume I can just pull the source for this and test that. |
Two things you can do:
|
@brockallen the code should even work against 2.0 RTM. |
I don't follow. Can you give an example?
So the code you want to simplify is:
Down to something like |
Even easier. I'll do it first thing tomorrow. |
Well - you could have a This way the mapping behavior would be an OIDC handler thing... |
I was unable to get this branch working on either 2.0 or 2.1-preview-1. Mainly build issues on the Security repo. |
@brockallen no need to build this repo, you should be able to copy the MapAllClaimsAction class directly into your app. You could also copy the new extension methods if you like. |
|
||
public override void Run(JObject userData, ClaimsIdentity identity, string issuer) | ||
{ | ||
foreach (var pair in userData) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I use the new MapAll
I get a null reference exception on userData. Implicit flow only in my client, BTW: ResponseType="id_token".
I think all these new helpers should all call
Still doesn't allow
to get Same goes for |
And just to repeat since I don't know if you saw it: When I use the new Actually, any of these new helpers fail with the same null ref exception for "id_token" only client. |
b6e905d
to
c02d783
Compare
Updated |
Cool, thanks. I think with those changes, this now becomes a nice one-liner to allow controlling which claims are kept/removed. There's of course the claim type map issue, but that's another topic/thread I assume? |
Agreed, the type map is orthogonal. |
@HaoK please sanity check |
public static void MapAllExcept(this ClaimActionCollection collection, params string[] exclusions) | ||
{ | ||
collection.Clear(); | ||
collection.Add(new MapAllClaimsAction()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should call MapAll
@leastprivilege second guessing the mappings is problematic, you're better off clearing JWT's mappings. I'm going to merge this PR as your suggestions along that line are independent. I'll file a new issue to simplify clearing the JWT type map. |
c02d783
to
90064ce
Compare
#1609 @leastprivilege @brockallen how does this look? It covers your asks in three parts: