Skip to content

Commit d17051c

Browse files
authored
fix(decoding): do not get_current_timestamp if user dont validate exp/nbf (#302)
1 parent 1bdfb91 commit d17051c

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/validation.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,6 @@ fn is_subset(reference: &HashSet<String>, given: &HashSet<BorrowedCowIfPossible<
213213
}
214214

215215
pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Result<()> {
216-
let now = get_current_timestamp();
217-
218216
for required_claim in &options.required_spec_claims {
219217
let present = match required_claim.as_str() {
220218
"exp" => matches!(claims.exp, TryParse::Parsed(_)),
@@ -230,14 +228,18 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
230228
}
231229
}
232230

233-
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway)
234-
{
235-
return Err(new_error(ErrorKind::ExpiredSignature));
236-
}
231+
if (options.validate_exp || options.validate_nbf) {
232+
let now = get_current_timestamp();
237233

238-
if matches!(claims.nbf, TryParse::Parsed(nbf) if options.validate_nbf && nbf > now + options.leeway)
239-
{
240-
return Err(new_error(ErrorKind::ImmatureSignature));
234+
if matches!(claims.exp, TryParse::Parsed(exp) if options.validate_exp && exp < now - options.leeway)
235+
{
236+
return Err(new_error(ErrorKind::ExpiredSignature));
237+
}
238+
239+
if matches!(claims.nbf, TryParse::Parsed(nbf) if options.validate_nbf && nbf > now + options.leeway)
240+
{
241+
return Err(new_error(ErrorKind::ImmatureSignature));
242+
}
241243
}
242244

243245
if let (TryParse::Parsed(sub), Some(correct_sub)) = (claims.sub, options.sub.as_deref()) {

0 commit comments

Comments
 (0)