-
Notifications
You must be signed in to change notification settings - Fork 413
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
Conversation
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) |
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: add logic ( tokenDescriptor.Claims != null && tokenDescriptor.Claims.Count is > 0 )
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.
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) |
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.
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.
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.
Made this change.
{ | ||
if (claim.ValueType == ClaimValueTypes.Boolean) | ||
{ | ||
bool boolValue; |
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.
use the pattern in ParseTimeValue, if (double.TryParse(claim.Value, out int intValue))
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.
Fixed.
@@ -778,7 +795,66 @@ public static TheoryData<CreateTokenTheoryData> CreateJWSUsingSecurityTokenDescr | |||
ValidIssuer = "Issuer" | |||
} | |||
}, | |||
|
|||
new CreateTokenTheoryData // Test checks that values in SecurityTokenDescriptor.Subject.Claims |
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.
can we start comments on new line?
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.
Fixed.
src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs
Outdated
Show resolved
Hide resolved
b6ad019
to
cb40a29
Compare
cb40a29
to
d8875f5
Compare
…ct and SecurityTokenDescriptor.Claims
d8875f5
to
b39bca1
Compare
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.