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

JWT tokens can now be created from both SecurityTokenDescriptor.Subject and SecurityTokenDescriptor.Claims #1235

Merged
merged 1 commit into from
Sep 5, 2019

Conversation

mafurman
Copy link
Member

@mafurman mafurman commented Aug 9, 2019

Fixes #1193.

If both SecurityTokenDescriptor.Subject and SecurityTokenDescriptor.Claims are set, both are used when creating a JWT token. However, if duplicate values are present, the ones found in SecurityTokenDescriptor.Claims will override those found in SecurityTokenDescriptor.Subject.

@GeoK
Copy link
Member

GeoK commented Aug 9, 2019

Also fixes #1086

// If a key is present in both tokenDescriptor.Subject.Claims and tokenDescriptor.Claims, but the key is not the same case in both (e.g. "key" and "KEY"),
// the key present in tokenDescriptor.Subject.Claims is the one that will remain after the merge. However, the corresponding value will be overriden with the value
// that was present in tokenDescriptor.Claims.
if (tokenDescriptor.Claims != null)
Copy link
Member

@brentschmaltz brentschmaltz Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add logic ( tokenDescriptor.Claims != null && tokenDescriptor.Claims.Count is > 0 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@@ -54,6 +55,45 @@ public class JwtTokenUtilities
/// </summary>
public static Regex RegexJwe = new Regex(JwtConstants.JweCompactSerializationRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant, TimeSpan.FromMilliseconds(100));

internal static Dictionary<string, object> CreateDictionaryFromClaims(IEnumerable<Claim> claims)
{
if (claims == null)
Copy link
Member

@brentschmaltz brentschmaltz Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is an internal method, how about in both cases (claims == null) && foreach loop is never entered (there are no claims), just return empty dictionary.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this change.

{
if (claim.ValueType == ClaimValueTypes.Boolean)
{
bool boolValue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the pattern in ParseTimeValue, if (double.TryParse(claim.Value, out int intValue))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -778,7 +795,66 @@ public static TheoryData<CreateTokenTheoryData> CreateJWSUsingSecurityTokenDescr
ValidIssuer = "Issuer"
}
},

new CreateTokenTheoryData // Test checks that values in SecurityTokenDescriptor.Subject.Claims
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we start comments on new line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@mafurman mafurman force-pushed the mafurman/createTokenUsingSubject branch 2 times, most recently from b6ad019 to cb40a29 Compare August 23, 2019 21:22
@GeoK GeoK self-requested a review August 23, 2019 21:47
@mafurman mafurman force-pushed the mafurman/createTokenUsingSubject branch from cb40a29 to d8875f5 Compare August 27, 2019 00:25
@mafurman mafurman force-pushed the mafurman/createTokenUsingSubject branch from d8875f5 to b39bca1 Compare September 5, 2019 21:26
@mafurman mafurman merged commit 46eb0ca into dev Sep 5, 2019
@mafurman mafurman deleted the mafurman/createTokenUsingSubject branch September 5, 2019 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should JsonWebTokenHandler take SecurityTokenDescriptor.Subject into account?
3 participants