Skip to content

Commit 5f4dd3c

Browse files
authored
feat(logging): track expiration in error message (#37)
1 parent 9ab939f commit 5f4dd3c

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

relay_rpc/src/jwt.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ pub enum JwtError {
2222
#[error("Invalid JWT signing algorithm")]
2323
Header,
2424

25-
#[error("JWT Token is expired")]
26-
Expired,
27-
28-
#[error("JWT Token is not yet valid")]
29-
NotYetValid,
25+
#[error("JWT Token is expired: {:?}", expiration)]
26+
Expired { expiration: Option<i64> },
27+
28+
#[error(
29+
"JWT Token is not yet valid: basic.iat: {}, now + time_leeway: {}, time_leeway: {}",
30+
basic_iat,
31+
now_time_leeway,
32+
time_leeway
33+
)]
34+
NotYetValid {
35+
basic_iat: i64,
36+
now_time_leeway: i64,
37+
time_leeway: i64,
38+
},
3039

3140
#[error("Invalid audience")]
3241
InvalidAudience,
@@ -202,11 +211,17 @@ pub trait VerifyableClaims: Serialize + DeserializeOwned {
202211
let now = Utc::now().timestamp();
203212

204213
if matches!(basic.exp, Some(exp) if now - time_leeway > exp) {
205-
return Err(JwtError::Expired);
214+
return Err(JwtError::Expired {
215+
expiration: basic.exp,
216+
});
206217
}
207218

208219
if now + time_leeway < basic.iat {
209-
return Err(JwtError::NotYetValid);
220+
return Err(JwtError::NotYetValid {
221+
basic_iat: basic.iat,
222+
now_time_leeway: now + time_leeway,
223+
time_leeway,
224+
});
210225
}
211226

212227
if !aud.contains(&basic.aud) {
@@ -278,7 +293,7 @@ mod test {
278293
.unwrap();
279294
assert!(matches!(
280295
Jwt(jwt.into()).decode(&aud),
281-
Err(JwtError::NotYetValid)
296+
Err(JwtError::NotYetValid { .. })
282297
));
283298

284299
// IAT leeway, valid.
@@ -297,7 +312,7 @@ mod test {
297312
.unwrap();
298313
assert!(matches!(
299314
Jwt(jwt.into()).decode(&aud),
300-
Err(JwtError::NotYetValid)
315+
Err(JwtError::NotYetValid { .. })
301316
));
302317

303318
// Past expiration.
@@ -308,7 +323,7 @@ mod test {
308323
.unwrap();
309324
assert!(matches!(
310325
Jwt(jwt.into()).decode(&aud),
311-
Err(JwtError::Expired)
326+
Err(JwtError::Expired { .. })
312327
));
313328

314329
// Expiration leeway, valid.
@@ -333,7 +348,7 @@ mod test {
333348
.unwrap();
334349
assert!(matches!(
335350
Jwt(jwt.into()).decode(&aud),
336-
Err(JwtError::Expired)
351+
Err(JwtError::Expired { .. })
337352
));
338353

339354
// Invalid aud.

0 commit comments

Comments
 (0)