-
Notifications
You must be signed in to change notification settings - Fork 442
Add CaseSensitiveClaimsIdentity type. #2700
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
891f671
Add CaseSensitiveClaimsIdentity. Update JsonWebTokenHandler.
pmaytak 63bfa21
Move switch to a separate class. Update claims identity creation code.
pmaytak b8b2fbc
Add test.
pmaytak 98fc5f4
Update AppContextSwitches
pmaytak f15f4c4
Update test/Microsoft.IdentityModel.Tokens.Tests/CaseSensitiveClaimsI…
pmaytak a76cc11
Update comments.
pmaytak c0aa6bf
Merge remote-tracking branch 'origin/dev' into pmaytak/cs-claims
pmaytak b18236d
Update ClaimsIdentity code creation in src.
pmaytak dd1566e
Add tests.
pmaytak 42eb9a0
Update tests to use correct types.
pmaytak c4bbcf6
Add SecurityToken property to CsClaimsIdentity.
pmaytak b19d572
Update tests to use CsClaimsIdentity.
pmaytak 30b17fe
Refactor code into ClaimsIdentityFactory.
pmaytak 2cfd756
Merge remote-tracking branch 'origin/dev' into pmaytak/cs-claims
pmaytak 807790e
Update tests.
pmaytak 73d4164
Update ClaimsIdentityFactory.
pmaytak 25eed7f
Fix tests.
pmaytak c631785
Update tests for CaseSensitiveClaimsIdentity
bafae8b
ignore SecurityToken in IdentityComparer
36cdd6b
Set security token in ClaimsIdentityFactory. Add tests.
pmaytak 0f93969
Apply suggestions from code review
pmaytak 9532084
Update test.
pmaytak aef682c
Merge branch 'pmaytak/cs-claims' of https://github.com/AzureAD/azure-…
pmaytak 11faf0b
Merge remote-tracking branch 'origin/dev' into pmaytak/cs-claims
pmaytak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Security.Claims; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| /// <summary> | ||
| /// AppContext switches for Microsoft.IdentityModel.Tokens and referencing packages. | ||
| /// </summary> | ||
| internal static class AppContextSwitches | ||
| { | ||
| /// <summary> | ||
| /// Enables a fallback to the previous behavior of using <see cref="ClaimsIdentity"/> instead of <see cref="CaseSensitiveClaimsIdentity"/> globally. | ||
| /// </summary> | ||
| internal const string UseClaimsIdentityTypeSwitch = "Microsoft.IdentityModel.Tokens.UseClaimsIdentityType"; | ||
|
|
||
| internal static bool UseClaimsIdentityType() => (AppContext.TryGetSwitch(UseClaimsIdentityTypeSwitch, out bool useClaimsIdentityType) && useClaimsIdentityType); | ||
| } | ||
| } |
122 changes: 122 additions & 0 deletions
122
src/Microsoft.IdentityModel.Tokens/CaseSensitiveClaimsIdentity.cs
This file contains hidden or 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,122 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Security.Claims; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| /// <summary> | ||
| /// A derived <see cref="ClaimsIdentity"/> where claim retrieval is case-sensitive. The current <see cref="ClaimsIdentity"/> retrieves claims in a case-insensitive manner which is different than querying the underlying <see cref="SecurityToken"/>. The <see cref="CaseSensitiveClaimsIdentity"/> provides consistent retrieval logic between the <see cref="SecurityToken"/> and <see cref="ClaimsIdentity"/>. | ||
| /// </summary> | ||
| public class CaseSensitiveClaimsIdentity : ClaimsIdentity | ||
| { | ||
| /// <summary> | ||
| /// Gets the <see cref="SecurityToken"/> that was used to create this claims identity. | ||
pmaytak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| public SecurityToken SecurityToken { get; internal set; } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| public CaseSensitiveClaimsIdentity() : base() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="authenticationType">The authentication method used to establish this identity.</param> | ||
| public CaseSensitiveClaimsIdentity(string authenticationType) : base(authenticationType) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="claimsIdentity"><see cref="ClaimsIdentity"/> to copy.</param> | ||
| public CaseSensitiveClaimsIdentity(ClaimsIdentity claimsIdentity) : base(claimsIdentity) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param> | ||
| public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims) : base(claims) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param> | ||
| /// <param name="authenticationType">The authentication method used to establish this identity.</param> | ||
| public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims, string authenticationType) : base(claims, authenticationType) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="claims"><see cref="IEnumerable{Claim}"/> associated with this instance.</param> | ||
| /// <param name="authenticationType">The authentication method used to establish this identity.</param> | ||
| /// <param name="nameType">The <see cref="Claim.Type"/> used when obtaining the value of <see cref="ClaimsIdentity.Name"/>.</param> | ||
| /// <param name="roleType">The <see cref="Claim.Type"/> used when performing logic for <see cref="ClaimsPrincipal.IsInRole"/>.</param> | ||
| public CaseSensitiveClaimsIdentity(IEnumerable<Claim> claims, string authenticationType, string nameType, string roleType) : | ||
| base(claims, authenticationType, nameType, roleType) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes an instance of <see cref="CaseSensitiveClaimsIdentity"/>. | ||
| /// </summary> | ||
| /// <param name="authenticationType">The authentication method used to establish this identity.</param> | ||
| /// <param name="nameType">The <see cref="Claim.Type"/> used when obtaining the value of <see cref="ClaimsIdentity.Name"/>.</param> | ||
| /// <param name="roleType">The <see cref="Claim.Type"/> used when performing logic for <see cref="ClaimsPrincipal.IsInRole"/>.</param> | ||
| public CaseSensitiveClaimsIdentity(string authenticationType, string nameType, string roleType) : | ||
| base(authenticationType, nameType, roleType) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves a <see cref="IEnumerable{Claim}"/> where each <see cref="Claim.Type"/> equals <paramref name="type"/>. | ||
| /// </summary> | ||
| /// <param name="type">The type of the claim to match.</param> | ||
| /// <returns>A <see cref="IEnumerable{Claim}"/> of matched claims.</returns> | ||
| /// <remarks>Comparison is <see cref="StringComparison.Ordinal"/>.</remarks> | ||
| /// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception> | ||
| public override IEnumerable<Claim> FindAll(string type) | ||
| { | ||
| return base.FindAll(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true); | ||
msbw2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Retrieves the first <see cref="Claim"/> where <see cref="Claim.Type"/> equals <paramref name="type"/>. | ||
| /// </summary> | ||
| /// <param name="type">The type of the claim to match.</param> | ||
| /// <returns>A <see cref="Claim"/>, <see langword="null"/> if nothing matches.</returns> | ||
| /// <remarks>Comparison is <see cref="StringComparison.Ordinal"/>.</remarks> | ||
| /// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception> | ||
| public override Claim FindFirst(string type) | ||
| { | ||
| return base.FindFirst(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Determines if a claim with type AND value is contained within this claims identity. | ||
| /// </summary> | ||
| /// <param name="type">The type of the claim to match.</param> | ||
| /// <param name="value">The value of the claim to match.</param> | ||
| /// <returns><c>true</c> if a claim is matched, <c>false</c> otherwise.</returns> | ||
| /// <remarks>Comparison is <see cref="StringComparison.Ordinal"/> for <see cref="Claim.Type"/> and <see cref="Claim.Value"/>.</remarks> | ||
| /// <exception cref="ArgumentNullException">if <paramref name="type"/> is null.</exception> | ||
| /// <exception cref="ArgumentNullException">if <paramref name="value"/> is null.</exception> | ||
| public override bool HasClaim(string type, string value) | ||
| { | ||
| return base.HasClaim(claim => claim?.Type.Equals(type, StringComparison.Ordinal) == true | ||
| && claim?.Value.Equals(value, StringComparison.Ordinal) == true); | ||
| } | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
src/Microsoft.IdentityModel.Tokens/ClaimsIdentityFactory.cs
This file contains hidden or 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,81 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Security.Claims; | ||
|
|
||
| namespace Microsoft.IdentityModel.Tokens | ||
| { | ||
| /// <summary> | ||
| /// Facilitates the creation of <see cref="ClaimsIdentity"/> and <see cref="CaseSensitiveClaimsIdentity"/> instances based on the <see cref="AppContextSwitches.UseClaimsIdentityTypeSwitch"/>. | ||
| /// </summary> | ||
| internal static class ClaimsIdentityFactory | ||
| { | ||
| internal static ClaimsIdentity Create(IEnumerable<Claim> claims) | ||
| { | ||
| if (AppContextSwitches.UseClaimsIdentityType()) | ||
| return new ClaimsIdentity(claims); | ||
|
|
||
| return new CaseSensitiveClaimsIdentity(claims); | ||
| } | ||
|
|
||
| internal static ClaimsIdentity Create(IEnumerable<Claim> claims, string authenticationType) | ||
| { | ||
| if (AppContextSwitches.UseClaimsIdentityType()) | ||
| return new ClaimsIdentity(claims, authenticationType); | ||
|
|
||
| return new CaseSensitiveClaimsIdentity(claims, authenticationType); | ||
| } | ||
|
|
||
| internal static ClaimsIdentity Create(string authenticationType, string nameType, string roleType, SecurityToken securityToken) | ||
| { | ||
| if (AppContextSwitches.UseClaimsIdentityType()) | ||
| return new ClaimsIdentity(authenticationType: authenticationType, nameType: nameType, roleType: roleType); | ||
|
|
||
| return new CaseSensitiveClaimsIdentity(authenticationType: authenticationType, nameType: nameType, roleType: roleType) | ||
| { | ||
| SecurityToken = securityToken, | ||
| }; | ||
| } | ||
|
|
||
| internal static ClaimsIdentity Create(SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer) | ||
| { | ||
| ClaimsIdentity claimsIdentity = validationParameters.CreateClaimsIdentity(securityToken, issuer); | ||
|
|
||
| // Set the SecurityToken in cases where derived TokenValidationParameters created a CaseSensitiveClaimsIdentity. | ||
| if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null) | ||
| { | ||
| caseSensitiveClaimsIdentity.SecurityToken = securityToken; | ||
| } | ||
| else if (claimsIdentity is not CaseSensitiveClaimsIdentity && !AppContextSwitches.UseClaimsIdentityType()) | ||
| { | ||
| claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity) | ||
| { | ||
| SecurityToken = securityToken, | ||
| }; | ||
| } | ||
|
|
||
| return claimsIdentity; | ||
| } | ||
|
|
||
| internal static ClaimsIdentity Create(TokenHandler tokenHandler, SecurityToken securityToken, TokenValidationParameters validationParameters, string issuer) | ||
| { | ||
| ClaimsIdentity claimsIdentity = tokenHandler.CreateClaimsIdentityInternal(securityToken, validationParameters, issuer); | ||
|
|
||
| // Set the SecurityToken in cases where derived TokenHandler created a CaseSensitiveClaimsIdentity. | ||
| if (claimsIdentity is CaseSensitiveClaimsIdentity caseSensitiveClaimsIdentity && caseSensitiveClaimsIdentity.SecurityToken == null) | ||
| { | ||
| caseSensitiveClaimsIdentity.SecurityToken = securityToken; | ||
| } | ||
| else if (claimsIdentity is not CaseSensitiveClaimsIdentity && !AppContextSwitches.UseClaimsIdentityType()) | ||
| { | ||
| claimsIdentity = new CaseSensitiveClaimsIdentity(claimsIdentity) | ||
| { | ||
| SecurityToken = securityToken, | ||
| }; | ||
| } | ||
|
|
||
| return claimsIdentity; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.