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

fix: change jwt claims exp/iat to timestamp #1

Merged
merged 2 commits into from
Dec 27, 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
6 changes: 3 additions & 3 deletions auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ pub mod types {

#[derive(Clone, Serialize, Deserialize)]
pub struct JWTClaims {
pub exp: DateTime<Utc>,
pub iat: DateTime<Utc>,
pub user: String,
pub exp: i64,
pub iat: i64,
pub username: String,
pub groups: HashSet<String>,
}
15 changes: 9 additions & 6 deletions server/src/infra/auth_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ async fn create_jwt<Handler: TcpBackendHandler>(
user: &UserId,
groups: HashSet<GroupDetails>,
) -> SignedToken {
let exp_utc = Utc::now() + chrono::Duration::days(1);
let claims = JWTClaims {
exp: Utc::now() + chrono::Duration::days(1),
iat: Utc::now(),
user: user.to_string(),
exp: exp_utc.timestamp(),
iat: Utc::now().timestamp(),
username: user.to_string(),
groups: groups
.into_iter()
.map(|g| g.display_name.into_string())
.collect(),
};
let expiry = claims.exp.naive_utc();
let expiry = exp_utc.naive_utc();
let header = jwt::Header {
algorithm: jwt::AlgorithmType::Hs512,
..Default::default()
Expand Down Expand Up @@ -685,7 +686,9 @@ pub(crate) fn check_if_token_is_valid<Backend: BackendHandler>(
) -> Result<ValidationResults, actix_web::Error> {
let token: Token<_> = VerifyWithKey::verify_with_key(token_str, &state.jwt_key)
.map_err(|_| ErrorUnauthorized("Invalid JWT"))?;
if token.claims().exp.lt(&Utc::now()) {
let naive_datetime:NaiveDateTime = NaiveDateTime::from_timestamp_opt(token.claims().exp,0).unwrap();
let exp_utc = DateTime::<Utc>::from_utc(naive_datetime,Utc);
if exp_utc.lt(&Utc::now()) {
return Err(ErrorUnauthorized("Expired JWT"));
}
if token.header().algorithm != jwt::AlgorithmType::Hs512 {
Expand All @@ -699,7 +702,7 @@ pub(crate) fn check_if_token_is_valid<Backend: BackendHandler>(
return Err(ErrorUnauthorized("JWT was logged out"));
}
Ok(state.backend_handler.get_permissions_from_groups(
UserId::new(&token.claims().user),
UserId::new(&token.claims().username),
token
.claims()
.groups
Expand Down
7 changes: 7 additions & 0 deletions server/src/infra/sql_backend_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ impl TcpBackendHandler for SqlBackendHandler {
expiry_date,
}
.into_active_model();
let existing_hash = model::jwt_storage::Entity::find()
.filter(model::jwt_storage::Column::JwtHash.eq(jwt_hash as i64))
.one(&self.sql_pool)
.await?;
if existing_hash.is_some() {
return Ok(());
}
new_token.insert(&self.sql_pool).await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
use actix::Actor;
use actix_server::ServerBuilder;
use anyhow::{anyhow, bail, Context, Result};
use futures_util::TryFutureExt;
//use futures_util::TryFutureExt;
use sea_orm::{Database, DatabaseConnection};
//use secstr::{SecUtf8};
use tracing::*;
Expand Down