Skip to content

Commit

Permalink
token: Show a better error message when token is expired
Browse files Browse the repository at this point in the history
  • Loading branch information
bbhtt authored and barthalion committed Jan 4, 2025
1 parent a822622 commit bbee209
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::fmt::Display;
use std::rc::Rc;
use std::time::{SystemTime, UNIX_EPOCH};

use crate::config::Config;
use crate::db::Db;
Expand Down Expand Up @@ -246,7 +247,9 @@ fn parse_authorization(prefix: Option<String>, header: &HeaderValue) -> Result<S
}

fn validate_claims(secret: Vec<u8>, token: String) -> Result<Claims, ApiError> {
let validation = Validation::default();
let mut validation = Validation::default();

validation.validate_exp = false;

let token_data = match decode::<Claims>(
&token,
Expand All @@ -257,7 +260,18 @@ fn validate_claims(secret: Vec<u8>, token: String) -> Result<Claims, ApiError> {
Err(_err) => return Err(ApiError::InvalidToken("Invalid token claims".to_string())),
};

Ok(token_data.claims)
let claims = token_data.claims;

let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as i64;

if claims.exp < now {
return Err(ApiError::InvalidToken("Token is expired".to_string()));
}

Ok(claims)
}

pub struct TokenParser(Rc<Inner>);
Expand Down

0 comments on commit bbee209

Please sign in to comment.