@@ -213,8 +213,6 @@ fn is_subset(reference: &HashSet<String>, given: &HashSet<BorrowedCowIfPossible<
213
213
}
214
214
215
215
pub ( crate ) fn validate ( claims : ClaimsForValidation , options : & Validation ) -> Result < ( ) > {
216
- let now = get_current_timestamp ( ) ;
217
-
218
216
for required_claim in & options. required_spec_claims {
219
217
let present = match required_claim. as_str ( ) {
220
218
"exp" => matches ! ( claims. exp, TryParse :: Parsed ( _) ) ,
@@ -230,14 +228,18 @@ pub(crate) fn validate(claims: ClaimsForValidation, options: &Validation) -> Res
230
228
}
231
229
}
232
230
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 ( ) ;
237
233
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
+ }
241
243
}
242
244
243
245
if let ( TryParse :: Parsed ( sub) , Some ( correct_sub) ) = ( claims. sub , options. sub . as_deref ( ) ) {
0 commit comments