-
Notifications
You must be signed in to change notification settings - Fork 464
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
Option to turn off or ignore certain token parameters during token validation #331
Comments
Hi! Thanks for proposing a change and the willingness to make it. Appreciate that! Let me understand the intend better. So you want to validate the signature but want to turn off the other aspects of the validation? Right now you can only turn it on or off completely: DoNotVerifySignature();
// or
WithVerifySignature(false); |
Yes, I would like to turn off certain parts of the validation. In my case, I only need to turn off the expiration validation but, to make it as accessible as possible for any other users of the library, I think it would be ideal to be able to turn off each aspect of the validation. |
Btw you don't necessarily need to switch from fluent to "classic" syntax to achieve the same: var builder = JwtBuilder.Create()
.WithAlgorithm(new HMACSHA256Algorithm())
.WithSecret(secret)
.MustVerifySignature();
string json = null;
try
{
json = builder.Decode<string>(token);
}
catch (TokenExpiredException)
{
// do nothing
} Just saying :) If you'd like to submit a PR to enhance the library - it will be very much appreciated! I'll get it reviewed and published asap. |
I have gone over the code a bit and, with the way the library is structured, I can see three ways to implement this feature:
public void Validate(string decodedPayload, IAsymmetricAlgorithm alg, byte[] bytesToSign,
byte[] decodedSignature, ValidationParameters validationParameters)
{
var ex = GetValidationException(alg, decodedPayload, bytesToSign, decodedSignature);
if (validationParameters.ValidateSignature || validationParameters.ValidateIssuedTime)
{
if (ex is SignatureVerificationException)
{
throw ex;
}
}
if (validationParameters.ValidateLifetime && ex is TokenExpiredException)
{
throw ex;
}
if (ex is not null)
{
if (ex is not SignatureVerificationException || ex is not TokenExpiredException)
{
throw ex;
}
}
} This would require changes in the
Not sure which implementation you would prefer. |
Hey @nathanpovo, thanks for flushing out the options and sorry for a delayed response! |
I agree that the interfaces should not be modified, that's why I wanted to see what you thought before working on it. Modifying the constructor of the I have opened a pull request to close this issue, let me know what you think. |
Resolved by #376. |
I wish to parse and validate some JWTs that have expired (to generate new tokens with a refresh token). However, when parsing a token using the following code:
an exception is thrown if the token has expired.
To workaround the issue, I have to change the code to this:
which is quite uglier than the original fluent style code. The other option was to create a new validation class that inherits from
IJwtValidator
and remove the expiration validation, but this would create a lot of duplicate code.Ideally, we would be able to pass options to the method
.MustVerifySignature()
, something like the following:This would allow us to control the execution of certain parts of the validation class to suit the individual needs of the token validation.
Let me know what you think, and I'd be happy to create a PR to address this issue.
The text was updated successfully, but these errors were encountered: