Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed deprecated chrono functions #54

Merged
merged 1 commit into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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