Skip to content

Update JWT sample code for subscriptions#1171

Merged
Shane32 merged 2 commits intomasterfrom
update_jwt
Nov 21, 2024
Merged

Update JWT sample code for subscriptions#1171
Shane32 merged 2 commits intomasterfrom
update_jwt

Conversation

@Shane32
Copy link
Copy Markdown
Member

@Shane32 Shane32 commented Nov 21, 2024

The previous sample handler decoded the parsed JWT token directly using JwtSecurityTokenHandler. This works fine when the TokenValidationParameters were all configured (including the security keys). However, when using OIDC, the TokenValidationParameters should be generated from the ConfigurationManager, as it needs to download the keys from the OIDC endpoint before the TokenValidationParameter instance contains the keys. The revised code now is a copy (without much of the event and error handling) of the JwtBearerHandler logic, so that validation will work similarly to however it is configured within ASP.NET Core. Events are still not implemented at this time.

Keep in mind that while this code can be used as a guide for to how to write authorization logic for subscriptions, it is the user's responsibility to ensure that their endpoint is secure. Perhaps in the future we may publish this code in a NuGet package, but for now it is still sample code.

@Shane32 Shane32 self-assigned this Nov 21, 2024
Comment on lines +92 to +96
catch
{
// no errors during authentication should throw an exception
// specifically, attempting to validate an invalid JWT token will result in an exception, which may be logged or simply ignored to not generate an inordinate amount of logs without purpose
}

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
Comment on lines +102 to +119
foreach (var validator in options.SecurityTokenValidators)
{
if (validator.CanReadToken(token))
{
try
{
var principal = validator.ValidateToken(token, tokenValidationParameters, out _);
// set the ClaimsPrincipal for the HttpContext; authentication will take place against this object
connection.HttpContext.User = principal;
return;
}
catch
{
// no errors during authentication should throw an exception
// specifically, attempting to validate an invalid JWT token will result in an exception, which may be logged or simply ignored to not generate an inordinate amount of logs without purpose
}
}
}

Check notice

Code scanning / CodeQL

Missed opportunity to use Where

This foreach loop [implicitly filters its target sequence](1) - consider filtering the sequence explicitly using '.Where(...)'.
Comment on lines +113 to +117
catch
{
// no errors during authentication should throw an exception
// specifically, attempting to validate an invalid JWT token will result in an exception, which may be logged or simply ignored to not generate an inordinate amount of logs without purpose
}

Check notice

Code scanning / CodeQL

Generic catch clause

Generic catch clause.
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.

3 participants