Skip to content

Commit

Permalink
Don't panic if the current time is smaller than the time tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
jedisct1 committed Feb 20, 2024
1 parent f2ced1e commit 2f951bb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<CustomClaims> JWTClaims<CustomClaims> {
}
if let Some(expires_at) = self.expires_at {
ensure!(
now - time_tolerance <= expires_at,
now >= time_tolerance && now - time_tolerance <= expires_at,
JWTError::TokenHasExpired
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ Ai+4/5mNTNXI8f1rrYgffWS4wf9cvsEihrvEg9867B2f98L7ux9Llle7jsHCtwgV
)
.unwrap();
let jwt = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IjEifQ.eyJuYW1lIjoiQWRhIExvdmVsYWNlIiwiaXNzIjoiaHR0cHM6Ly9jaHJvbm9nZWFycy5jb20vdGVzdCIsImF1ZCI6InRlc3QiLCJhdXRoX3RpbWUiOjEwMCwidXNlcl9pZCI6InVpZDEyMyIsInN1YiI6InNidTEyMyIsImlhdCI6MjAwLCJleHAiOjUwMCwibmJmIjozMDAsImVtYWlsIjoiYWxvdmVsYWNlQGNocm9ub2dlYXJzLmNvbSJ9.eTQnwXrri_uY55fS4IygseBzzbosDM1hP153EZXzNlLH5s29kdlGt2mL_KIjYmQa8hmptt9RwKJHBtw6l4KFHvIcuif86Ix-iI2fCpqNnKyGZfgERV51NXk1THkgWj0GQB6X5cvOoFIdHa9XvgPl_rVmzXSUYDgkhd2t01FOjQeeT6OL2d9KdlQHJqAsvvKVc3wnaYYoSqv2z0IluvK93Tk1dUBU2yWXH34nX3GAVGvIoFoNRiiFfZwFlnz78G0b2fQV7B5g5F8XlNRdD1xmVZXU8X2-xh9LqRpnEakdhecciFHg0u6AyC4c00rlo_HBb69wlXajQ3R4y26Kpxn7HA";

let mut options = VerificationOptions::default();
options.artificial_time = Some(UnixTimeStamp::from_secs(400));
let res = key.verify_token::<NoCustomClaims>(jwt, Some(options.clone()));
assert!(res.is_err());

options.time_tolerance = Some(Duration::from_secs(100));
key.verify_token::<NoCustomClaims>(jwt, Some(options))
.unwrap();
Expand Down

0 comments on commit 2f951bb

Please sign in to comment.