Skip to content

Commit

Permalink
removed panic on exp field with wrong unix timestamp (kilork#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilork authored Nov 28, 2022
1 parent b9aa277 commit 9e1c298
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ pub enum Expiry {
Expires(::chrono::naive::NaiveDateTime),
#[error("Token is too old: {0}")]
MaxAge(::chrono::Duration),
#[error("Token exp is not valid UNIX timestamp: {0}")]
NotUnix(i64),
}

#[derive(Debug, Error)]
Expand Down
1 change: 1 addition & 0 deletions src/provider/microsoft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub async fn authenticate<C: CompactJson + Claims, P: Provider + Configurable>(
/// - Validation::Mismatch::AuthorizedParty if the azp is not the client_id
/// - Validation::Expired::Expires if the current time is past the expiration time
/// - Validation::Expired::MaxAge is the token is older than the provided max_age
/// - Validation::Expired::NotUnix if the expiration time is not valid UNIX timestamp
/// - Validation::Missing::Authtime if a max_age was given and the token has no auth time
pub fn validate_token<C: CompactJson + Claims, P: Provider + Configurable>(
client: &Client<P, C>,
Expand Down
11 changes: 7 additions & 4 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,13 @@ pub fn validate_token_exp<C: Claims>(claims: &C, max_age: Option<&Duration>) ->
if now.timestamp() < 1504758600 {
panic!("chrono::Utc::now() can never be before this was written!")
}
if claims.exp() <= now.timestamp() {
return Err(Validation::Expired(Expiry::Expires(
chrono::naive::NaiveDateTime::from_timestamp(claims.exp(), 0),
))
let exp = claims.exp();
if exp <= now.timestamp() {
return Err(Validation::Expired(
chrono::naive::NaiveDateTime::from_timestamp_opt(exp, 0)
.map(Expiry::Expires)
.unwrap_or_else(|| Expiry::NotUnix(exp)),
)
.into());
}

Expand Down

0 comments on commit 9e1c298

Please sign in to comment.