Skip to content

Commit

Permalink
Remove some Task allocations from JsonWebTokenHandler (#2167)
Browse files Browse the repository at this point in the history
These methods are all private and all of the call sites directly await them.  They can be ValueTasks to avoid allocating on the synchronously-completing path (which I see happening reasonably frequently in traces).
  • Loading branch information
stephentoub authored and brentschmaltz committed Sep 7, 2023
1 parent 57649a7 commit e929126
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1440,7 +1440,7 @@ private static TokenValidationResult ReadToken(string token, TokenValidationPara
/// <param name="jsonWebToken">The JWT token</param>
/// <param name="validationParameters">The <see cref="TokenValidationParameters"/> to be used for validation.</param>
/// <returns></returns>
private async Task<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters)
private async ValueTask<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters)
{
BaseConfiguration currentConfiguration = null;
if (validationParameters.ConfigurationManager != null)
Expand Down Expand Up @@ -1519,15 +1519,14 @@ private async Task<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWe
return tokenValidationResult;
}

private async Task<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
private ValueTask<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
if (jsonWebToken.IsEncrypted)
return await ValidateJWEAsync(jsonWebToken, validationParameters, configuration).ConfigureAwait(false);

return await ValidateJWSAsync(jsonWebToken, validationParameters, configuration).ConfigureAwait(false);
return jsonWebToken.IsEncrypted ?
ValidateJWEAsync(jsonWebToken, validationParameters, configuration) :
ValidateJWSAsync(jsonWebToken, validationParameters, configuration);
}

private async Task<TokenValidationResult> ValidateJWSAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
private async ValueTask<TokenValidationResult> ValidateJWSAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
try
{
Expand Down Expand Up @@ -1571,7 +1570,7 @@ private async Task<TokenValidationResult> ValidateJWSAsync(JsonWebToken jsonWebT
}
}

private async Task<TokenValidationResult> ValidateJWEAsync(JsonWebToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
private async ValueTask<TokenValidationResult> ValidateJWEAsync(JsonWebToken jwtToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
try
{
Expand Down Expand Up @@ -1642,7 +1641,7 @@ private static JsonWebToken ValidateSignatureAndIssuerSecurityKey(JsonWebToken j
return validatedToken;
}

private async Task<TokenValidationResult> ValidateTokenPayloadAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
private async ValueTask<TokenValidationResult> ValidateTokenPayloadAsync(JsonWebToken jsonWebToken, TokenValidationParameters validationParameters, BaseConfiguration configuration)
{
var expires = jsonWebToken.HasPayloadClaim(JwtRegisteredClaimNames.Exp) ? (DateTime?)jsonWebToken.ValidTo : null;
var notBefore = jsonWebToken.HasPayloadClaim(JwtRegisteredClaimNames.Nbf) ? (DateTime?)jsonWebToken.ValidFrom : null;
Expand Down

0 comments on commit e929126

Please sign in to comment.