-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fix up user-jwts interactions #42125
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
Changes from 5 commits
060ba2e
d775743
16d55b6
9108364
cf90596
e5e1ba8
6977752
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Configuration.UserSecrets; | ||
using Microsoft.Extensions.Tools.Internal; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Microsoft.AspNetCore.Authentication.JwtBearer.Tools; | ||
|
||
|
@@ -145,16 +146,48 @@ public static string[] GetAudienceCandidatesFromLaunchSettings(string project) | |
return null; | ||
} | ||
|
||
public static void PrintJwt(IReporter reporter, Jwt jwt, JwtSecurityToken fullToken = null) | ||
public static void PrintJwt(IReporter reporter, Jwt jwt, bool showAll, JwtSecurityToken fullToken = null) | ||
{ | ||
reporter.Output(JsonSerializer.Serialize(jwt, new JsonSerializerOptions { WriteIndented = true })); | ||
reporter.Output($"{Resources.JwtPrint_Id}: {jwt.Id}"); | ||
reporter.Output($"{Resources.JwtPrint_Name}: {jwt.Name}"); | ||
reporter.Output($"{Resources.JwtPrint_Scheme}: {jwt.Scheme}"); | ||
reporter.Output($"{Resources.JwtPrint_Audiences}: {jwt.Audience}"); | ||
reporter.Output($"{Resources.JwtPrint_NotBefore}: {jwt.NotBefore:O}"); | ||
reporter.Output($"{Resources.JwtPrint_ExpiresOn}: {jwt.Expires:O}"); | ||
reporter.Output($"{Resources.JwtPrint_IssuedOn}: {jwt.Issued:O}"); | ||
|
||
if (!jwt.Scopes.IsNullOrEmpty() || showAll) | ||
{ | ||
var scopesValue = jwt.Scopes.IsNullOrEmpty() | ||
? "none" | ||
: string.Join(',', jwt.Scopes); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reporter.Output($"{Resources.JwtPrint_Scopes}: {scopesValue}"); | ||
} | ||
|
||
if (!jwt.Roles.IsNullOrEmpty() || showAll) | ||
{ | ||
var rolesValue = jwt.Roles.IsNullOrEmpty() | ||
? "none" | ||
: String.Join(',', jwt.Roles); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reporter.Output($"{Resources.JwtPrint_Roles}: [{rolesValue}]"); | ||
} | ||
|
||
if (fullToken is not null) | ||
if (!jwt.CustomClaims.IsNullOrEmpty() || showAll) | ||
{ | ||
reporter.Output($"Token Header: {fullToken.Header.SerializeToJson()}"); | ||
reporter.Output($"Token Payload: {fullToken.Payload.SerializeToJson()}"); | ||
var customClaimsValue = jwt.CustomClaims.IsNullOrEmpty() | ||
? "none" | ||
: string.Join(',', jwt.CustomClaims.Select(kvp => $"{kvp.Key}={kvp.Value}")); | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reporter.Output($"{Resources.JwtPrint_CustomClaims}: [{customClaimsValue}]"); | ||
} | ||
reporter.Output($"Compact Token: {jwt.Token}"); | ||
|
||
if (showAll) | ||
{ | ||
reporter.Output($"{Resources.JwtPrint_TokenHeader}: {fullToken.Header.SerializeToJson()}"); | ||
reporter.Output($"{Resources.JwtPrint_TokenPayload}: {fullToken.Payload.SerializeToJson()}"); | ||
} | ||
|
||
var tokenValueFieldName = showAll ? Resources.JwtPrint_CompactToken : Resources.JwtPrint_Token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this backward? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's intentional. When the user provides the |
||
reporter.Output($"{tokenValueFieldName}: {jwt.Token}"); | ||
} | ||
|
||
public static bool TryParseClaims(List<string> input, out Dictionary<string, string> 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.
expiresOnOption and validForOption conflict, is there a warning/error for this? Should we only print one of them?
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.
Good call! I think we probably want to throw an error and treat the input arguments as invalid in this case.
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.
Yep, I think that's what I did in the original prototype, you have to specify either/or.