Skip to content

Commit 29674b6

Browse files
Add support for the reject_tokens_expiring_in_less_than option. (#370)
* Add support for the reject_tokens_expiring_in_less_than option. * Add comment explaining interacting with leeway. * Update minimum supported rust version to support dependency updates.
1 parent aa5266d commit 29674b6

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
include:
3838
- build: pinned
3939
os: ubuntu-20.04
40-
rust: 1.67.0
40+
rust: 1.73.0
4141
- build: stable
4242
os: ubuntu-20.04
4343
rust: stable

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ include = [
1717
"README.md",
1818
"CHANGELOG.md",
1919
]
20-
rust-version = "1.67.0"
20+
rust-version = "1.73.0"
2121

2222
[dependencies]
2323
serde_json = "1.0"

src/validation.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ pub struct Validation {
3939
///
4040
/// Defaults to `60`.
4141
pub leeway: u64,
42+
/// Reject a token some time (in seconds) before the `exp` to prevent
43+
/// expiration in transit over the network.
44+
///
45+
/// The value is the inverse of `leeway`, subtracting from the validation time.
46+
///
47+
/// Defaults to `0`.
48+
pub reject_tokens_expiring_in_less_than: u64,
4249
/// Whether to validate the `exp` field.
4350
///
4451
/// It will return an error if the time in the `exp` field is past.
@@ -94,6 +101,7 @@ impl Validation {
94101
required_spec_claims: required_claims,
95102
algorithms: vec![alg],
96103
leeway: 60,
104+
reject_tokens_expiring_in_less_than: 0,
97105

98106
validate_exp: true,
99107
validate_nbf: false,
@@ -246,7 +254,8 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
246254
if options.validate_exp || options.validate_nbf {
247255
let now = get_current_timestamp();
248256

249-
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway)
257+
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp
258+
&& exp - options.reject_tokens_expiring_in_less_than < now - options.leeway )
250259
{
251260
return Err(new_error(ErrorKind::ExpiredSignature));
252261
}
@@ -366,6 +375,17 @@ mod tests {
366375
assert!(res.is_ok());
367376
}
368377

378+
#[test]
379+
#[wasm_bindgen_test]
380+
fn exp_in_future_but_in_rejection_period_fails() {
381+
let claims = json!({ "exp": get_current_timestamp() + 500 });
382+
let mut validation = Validation::new(Algorithm::HS256);
383+
validation.leeway = 0;
384+
validation.reject_tokens_expiring_in_less_than = 501;
385+
let res = validate(deserialize_claims(&claims), &validation);
386+
assert!(res.is_err());
387+
}
388+
369389
#[test]
370390
#[wasm_bindgen_test]
371391
fn exp_float_in_future_ok() {
@@ -374,6 +394,17 @@ mod tests {
374394
assert!(res.is_ok());
375395
}
376396

397+
#[test]
398+
#[wasm_bindgen_test]
399+
fn exp_float_in_future_but_in_rejection_period_fails() {
400+
let claims = json!({ "exp": (get_current_timestamp() as f64) + 500.123 });
401+
let mut validation = Validation::new(Algorithm::HS256);
402+
validation.leeway = 0;
403+
validation.reject_tokens_expiring_in_less_than = 501;
404+
let res = validate(deserialize_claims(&claims), &validation);
405+
assert!(res.is_err());
406+
}
407+
377408
#[test]
378409
#[wasm_bindgen_test]
379410
fn exp_in_past_fails() {

0 commit comments

Comments
 (0)