Add TryValidateSignature and SignatureValidatorWithToken delegate#3483
Closed
iNinja wants to merge 3 commits into
Closed
Add TryValidateSignature and SignatureValidatorWithToken delegate#3483iNinja wants to merge 3 commits into
iNinja wants to merge 3 commits into
Conversation
Add a public TryValidateSignature instance method on JsonWebTokenHandler that validates a JWT signature against a single specified key, using the handler's telemetry infrastructure. Add SignatureValidatorWithToken delegate to TokenValidationParameters that receives the already-parsed SecurityToken instead of a raw string, avoiding the need to re-parse the token. Takes priority over SignatureValidatorUsingConfiguration and SignatureValidator when set. These methods are intended for use within signature validation delegates, enabling the delegate to call back into the handler's signature validation logic for algorithms it does not handle directly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces new extensibility points for JWT signature validation in Microsoft.IdentityModel.JsonWebTokens / Microsoft.IdentityModel.Tokens, enabling custom signature validators to reuse the handler’s built-in signature verification without re-parsing tokens.
Changes:
- Adds
JsonWebTokenHandler.TryValidateSignature(JsonWebToken, SecurityKey, TokenValidationParameters)for verifying a JWS signature against a single resolved key (with telemetry). - Adds the
SignatureValidatorWithTokendelegate +TokenValidationParameters.SignatureValidatorWithTokenproperty to support signature validation using an already-parsedSecurityToken. - Updates
JsonWebTokenHandlerto (a) preferSignatureValidatorWithTokenwhen set and (b) pass the resolvedBaseConfigurationinto signature delegates; updates PublicAPI baselines and adds new unit tests.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Microsoft.IdentityModel.JsonWebTokens.Tests/JsonWebTokenHandler.TryValidateSignatureTests.cs | Adds coverage for TryValidateSignature (valid/invalid key, alg mismatch, null args) and basic e2e delegate usage. |
| src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs | Adds SignatureValidatorWithToken property and copies it in the TVP copy constructor. |
| src/Microsoft.IdentityModel.Tokens/Delegates.cs | Introduces the new SignatureValidatorWithToken delegate type. |
| src/Microsoft.IdentityModel.JsonWebTokens/PublicAPI.Unshipped.txt | Adds the new JsonWebTokenHandler.TryValidateSignature public API entry. |
| src/Microsoft.IdentityModel.Tokens/PublicAPI/*/PublicAPI.Unshipped.txt | Adds new public API entries for SignatureValidatorWithToken and the TVP property across TFMs. |
| src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.ValidateToken.cs | Wires SignatureValidatorWithToken, passes BaseConfiguration through to signature delegates, and adds the new public TryValidateSignature method. |
…n passthrough tests - Update XML docs to reference both SignatureValidatorWithToken and SignatureValidatorUsingConfiguration delegates - Clarify throwing vs false-return semantics in remarks - Add test verifying SignatureValidatorWithToken receives resolved BaseConfiguration from ConfigurationManager - Add test verifying SignatureValidatorUsingConfiguration receives resolved BaseConfiguration from ConfigurationManager Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The delegate code path in ValidateJWSAsync called the 3-parameter ValidateIssuerSecurityKey overload, which passes configuration as null. This silently skipped IssuerSigningKeyValidatorUsingConfiguration checks (e.g. AAD signing key issuer validation via EnableAadSigningKeyIssuerValidation). Changed to the 4-parameter overload that passes the resolved BaseConfiguration, matching the non-delegate code path behaviour. Added test verifying IssuerSigningKeyValidatorUsingConfiguration receives the resolved configuration when a signature delegate is used. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
Author
|
Abandoning in favour of a simpler approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add TryValidateSignature and SignatureValidatorWithToken delegate
Summary
This PR adds two public APIs to support scenarios where a
SignatureValidatorUsingConfigurationdelegate needs to call back into the handler's own signature validation logic for algorithms it does not handle directly.Changes
JsonWebTokenHandler.TryValidateSignatureA new public instance method that validates a JWT signature against a single specified key:
SignatureProvider, and verifies the signaturefalseif the signature does not matchSignatureValidatorWithTokendelegateA new delegate on
TokenValidationParametersthat receives the already-parsedSecurityTokeninstead of a raw string:SignatureValidatorUsingConfigurationandSignatureValidatorwhen setBaseConfiguration(OIDC metadata), enabling the delegate to accessconfiguration.JsonWebKeySet.Keysfor key resolutionConfiguration passthrough fix
The existing
ValidateSignatureUsingDelegatesmethod was not passing the resolvedBaseConfigurationto the signature validation delegates (hardcoded tonullwith aTODOcomment). This has been fixed — all three delegates now receive the configuration that was already resolved upstream.Usage example
Tests
8 tests covering:
SignatureValidatorWithToken+TryValidateSignature