Skip to content

Commit

Permalink
fixed deprecated chrono functions (kilork#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilork authored Apr 21, 2024
1 parent 1ed9ad4 commit 640ff88
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub enum Missing {
#[derive(Debug, Error)]
pub enum Expiry {
#[error("Token expired at: {0}")]
Expires(::chrono::naive::NaiveDateTime),
Expires(::chrono::DateTime<::chrono::Utc>),
#[error("Token is too old: {0}")]
MaxAge(::chrono::Duration),
#[error("Token exp is not valid UNIX timestamp: {0}")]
Expand Down
10 changes: 3 additions & 7 deletions src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
Claims, Config,
};
use biscuit::SingleOrMultiple;
use chrono::{Duration, Utc};
use chrono::{DateTime, Duration, Utc};

pub fn validate_token_issuer<C: Claims>(claims: &C, config: &Config) -> Result<(), Error> {
if claims.iss() != &config.issuer {
Expand Down Expand Up @@ -64,14 +64,10 @@ pub fn validate_token_exp<'max_age, C: Claims>(
max_age: impl Into<Option<&'max_age Duration>>,
) -> Result<(), Error> {
let now = Utc::now();
// Now should never be less than the time this code was written!
if now.timestamp() < 1504758600 {
panic!("chrono::Utc::now() can never be before this was written!")
}
let exp = claims.exp();
if exp <= now.timestamp() {
return Err(Validation::Expired(
chrono::naive::NaiveDateTime::from_timestamp_opt(exp, 0)
DateTime::from_timestamp(exp, 0)
.map(Expiry::Expires)
.unwrap_or_else(|| Expiry::NotUnix(exp)),
)
Expand All @@ -81,7 +77,7 @@ pub fn validate_token_exp<'max_age, C: Claims>(
if let Some(max) = max_age.into() {
match claims.auth_time() {
Some(time) => {
let age = chrono::Duration::seconds(now.timestamp() - time);
let age = Duration::seconds(now.timestamp() - time);
if age >= *max {
return Err(Validation::Expired(Expiry::MaxAge(age)).into());
}
Expand Down

0 comments on commit 640ff88

Please sign in to comment.