Skip to content

Support custom timestamp #340

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ pub struct Validation {
///
/// Defaults to `false`.
pub validate_nbf: bool,
/// Supply a custom timestamp for checking `exp` and `nbf` claims.
/// If `None`, will use `SystemTime::now()`.
///
/// Defaults to `None`.
pub current_timestamp: Option<u64>,
/// If it contains a value, the validation will check that the `aud` field is a member of the
/// audience provided and will error otherwise.
/// Use `set_audience` to set it
Expand Down Expand Up @@ -92,6 +97,7 @@ impl Validation {

validate_exp: true,
validate_nbf: false,
current_timestamp: None,

iss: None,
sub: None,
Expand Down Expand Up @@ -229,7 +235,10 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
}

if options.validate_exp || options.validate_nbf {
let now = get_current_timestamp();
let now = match options.current_timestamp {
Some(now) => now,
None => get_current_timestamp(),
};

if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway)
{
Expand Down