-
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 all 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); | ||
reporter.Output($"{Resources.JwtPrint_Scopes}: {scopesValue}"); | ||
} | ||
|
||
if (!jwt.Roles.IsNullOrEmpty() || showAll) | ||
{ | ||
var rolesValue = jwt.Roles.IsNullOrEmpty() | ||
? "none" | ||
: String.Join(", ", jwt.Roles); | ||
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}")); | ||
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.
Idk if you saw my question, should audience and name both be here? I think the issue mentioned including both.
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.
Yeah, so the issue refers to showing the name in the
print
command. Forlist
, I figured it would make sense to limit it to the properties that impact the JWT's behavior at runtime. Also, I wanted to be a little cautious about having too many columns in the table since ourConsoleTable
implementation is pretty rudimentary.