Add support for negative leeway values
I use jsonwebtoken as part of a client application. During the course of development, I have encountered two issues with how token expiration is handled:
- The Validation::get_current_timestamp() method truncates the current time to the previous second. This behaves differently from a library in a different language used by a server application that the client interacts with. This library considers a token to be expired if the current time is 0.001 seconds past the expiration in the token, rather than 1.000 seconds past.
- The Validation::validate() method does not provide a way to judge if a token is about to expire shortly. This becomes a problem if a token will expire in 0.001 seconds and the code is preparing to send it over a network socket. It may expire in-transit.
In order to allow library users to work around both these problems, I propose the following solution.
- Split the
leewayoption into separate settings for validating tokenexpandnbfclaims. These settings can be namedexp_leewayandnbf_leeway. - Allow
exp_leewayvalues to be negative numbers. This allows library users to specify that a token needs to be replaced at some time interval before expiration to account for inconsistent handling of fractional time values by other software or delays imposed by network latency. - Allow
nbf_leewayvalues to be negative numbers for consistency.
Update: Here is a PR implementing the proposed solution.
- I don't see why we would go to ms resolution when we only have seconds in the api/sec
- Not an issue with a decent leeway, that's what it's for
@Keats thanks for the quick response!
-
I'm not proposing that we go to ms resolution. My proposed fix is in the attached PR.
-
I have not been able to identify a leeway value that fixes the following issue. Here is a drawing representing a token which is about to expire, but has not expired yet.
I would like to make the current code fail for this case since the token may expire in transit over the network.
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway) { return Err(new_error(ErrorKind::ExpiredSignature)); }It looks like it will succeed for all unsigned integers.
Can you suggest a leeway value that will allow me to return
ErrorKind::ExpiredSignaturewhen the token is about to expire? As a specific example, maybe you could suggest a value that would work when timenow=1and timeexp=2.
It seems that what you want, rather than negative leeway which is imo a bit confusing is another option to reject tokens that are x seconds or less from expiration?
It seems that what you want, rather than negative leeway which is imo a bit confusing is another option to reject tokens that are
xseconds or less from expiration?
@Keats yes, rejecting tokens that are x seconds or less prior to expiration is our goal.
Configuring this as a separate setting rather than as a negative leeway value should work fine.
Something like Validation::reject_tokens_expiring_in_less_than(seconds: int)?
Something like
Validation::reject_tokens_expiring_in_less_than(seconds: int)?
Yes, that is a clear name in my opinion.
Would you like me to update the existing PR to reflect?
@Keats Here is a new PR implementing Validation::reject_tokens_expiring_in_less_than. Can you review and approve?
Fix released as part of version 9.3.0; closing this issue