Skip to content
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

Reduce Allocations in ValidateSignature #2586

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1311,7 +1311,7 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JsonWebToken jwtToken
}

if (key != null)
keys = new List<SecurityKey> { key };
keys = [key];
}

// on decryption for ECDH-ES, we get the public key from the EPK value see: https://datatracker.ietf.org/doc/html/rfc7518#appendix-C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
if (key != null)
{
kidMatched = true;
keys = new List<SecurityKey> { key };
keys = [key];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ private SamlSecurityToken ValidateSignature(SamlSecurityToken samlToken, string
{
// remember that key was matched for throwing exception SecurityTokenSignatureKeyNotFoundException
keyMatched = true;
keys = new List<SecurityKey> { securityKey };
keys = [securityKey];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,6 @@ internal static SecurityKey ResolveTokenSigningKey(KeyInfo tokenKeyInfo, TokenVa
return null;
}

/// <summary>
/// Returns all <see cref="SecurityKey"/> to use when validating the signature of a token.
/// </summary>
/// <param name="token">The <see cref="string"/> representation of the token that is being validated.</param>
/// <param name="samlToken">The <see cref="SecurityToken"/> that is being validated.</param>
/// <param name="tokenKeyInfo">The <see cref="KeyInfo"/> field of the token being validated</param>
/// <param name="validationParameters">A <see cref="TokenValidationParameters"/> required for validation.</param>
/// <param name="keyMatched">A <see cref="bool"/> to represent if a a issuer signing key matched with token kid or x5t</param>
/// <returns>Returns all <see cref="SecurityKey"/> to use for signature validation.</returns>
internal static IEnumerable<SecurityKey> GetKeysForTokenSignatureValidation(string token, SecurityToken samlToken, KeyInfo tokenKeyInfo, TokenValidationParameters validationParameters, out bool keyMatched)
{
keyMatched = false;

if (validationParameters.IssuerSigningKeyResolver != null)
{
return validationParameters.IssuerSigningKeyResolver(token, samlToken, tokenKeyInfo?.Id, validationParameters);
}
else
{
SecurityKey key = ResolveTokenSigningKey(tokenKeyInfo, validationParameters);

if (key != null)
{
keyMatched = true;
return new List<SecurityKey> { key };
}
else
{
keyMatched = false;
if (validationParameters.TryAllIssuerSigningKeys)
{
return TokenUtilities.GetAllSigningKeys(validationParameters: validationParameters);
}
}
}
return null;
}

/// <summary>
/// Creates <see cref="Claim"/>'s from <paramref name="claimsCollection"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ private Saml2SecurityToken ValidateSignature(Saml2SecurityToken samlToken, strin
{
// remember that key was matched for throwing exception SecurityTokenSignatureKeyNotFoundException
keyMatched = true;
keys = new List<SecurityKey> { key };
keys = [key];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ private JwtSecurityToken ValidateSignature(string token, JwtSecurityToken jwtTok
if (key != null)
{
kidMatched = true;
keys = new List<SecurityKey> { key };
keys = [key];
}
}

Expand Down Expand Up @@ -1812,7 +1812,7 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JwtSecurityToken jwtT
{
var key = ResolveTokenDecryptionKey(jwtToken.RawData, jwtToken, validationParameters);
if (key != null)
keys = new List<SecurityKey> { key };
keys = [key];
}

// control gets here if:
Expand Down
Loading