Skip to content
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

Add support for the reject_tokens_expiring_in_less_than option. #370

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
include:
- build: pinned
os: ubuntu-20.04
rust: 1.67.0
rust: 1.73.0
- build: stable
os: ubuntu-20.04
rust: stable
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ include = [
"README.md",
"CHANGELOG.md",
]
rust-version = "1.67.0"
rust-version = "1.73.0"

[dependencies]
serde_json = "1.0"
Expand Down
33 changes: 32 additions & 1 deletion src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ pub struct Validation {
///
/// Defaults to `60`.
pub leeway: u64,
/// Reject a token some time (in seconds) before the `exp` to prevent
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the docs mention how it interacts with leeway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have added some explanation.

/// expiration in transit over the network.
///
/// The value is the inverse of `leeway`, subtracting from the validation time.
///
/// Defaults to `0`.
pub reject_tokens_expiring_in_less_than: u64,
/// Whether to validate the `exp` field.
///
/// It will return an error if the time in the `exp` field is past.
Expand Down Expand Up @@ -94,6 +101,7 @@ impl Validation {
required_spec_claims: required_claims,
algorithms: vec![alg],
leeway: 60,
reject_tokens_expiring_in_less_than: 0,

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

if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway)
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp
&& exp - options.reject_tokens_expiring_in_less_than < now - options.leeway )
{
return Err(new_error(ErrorKind::ExpiredSignature));
}
Expand Down Expand Up @@ -366,6 +375,17 @@ mod tests {
assert!(res.is_ok());
}

#[test]
#[wasm_bindgen_test]
fn exp_in_future_but_in_rejection_period_fails() {
let claims = json!({ "exp": get_current_timestamp() + 500 });
let mut validation = Validation::new(Algorithm::HS256);
validation.leeway = 0;
validation.reject_tokens_expiring_in_less_than = 501;
let res = validate(deserialize_claims(&claims), &validation);
assert!(res.is_err());
}

#[test]
#[wasm_bindgen_test]
fn exp_float_in_future_ok() {
Expand All @@ -374,6 +394,17 @@ mod tests {
assert!(res.is_ok());
}

#[test]
#[wasm_bindgen_test]
fn exp_float_in_future_but_in_rejection_period_fails() {
let claims = json!({ "exp": (get_current_timestamp() as f64) + 500.123 });
let mut validation = Validation::new(Algorithm::HS256);
validation.leeway = 0;
validation.reject_tokens_expiring_in_less_than = 501;
let res = validate(deserialize_claims(&claims), &validation);
assert!(res.is_err());
}

#[test]
#[wasm_bindgen_test]
fn exp_in_past_fails() {
Expand Down