From 2c3cc9731717c200a892bdac328eda608e5d6e28 Mon Sep 17 00:00:00 2001 From: alemi Date: Fri, 10 Mar 2023 00:30:58 +0100 Subject: [PATCH] feat: distinction between duration and lifetime --- src/main.rs | 8 ++++++-- src/routes/auth.rs | 2 +- src/routes/session.rs | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3a6926a..71b2152 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,8 +31,12 @@ struct ConfigArgs { #[arg(short, long, default_value = "127.0.0.1:26656")] bind_addr: String, - /// Access tokens lifetime, in seconds + /// How long an access token stays valid, in seconds #[arg(long, default_value_t = 3600)] + token_duration: u32, + + /// How long an access token is refreshable, in hours + #[arg(long, default_value_t = 168)] token_lifetime: u32, /// Valid time for join requests, in seconds @@ -76,7 +80,7 @@ async fn main() -> Result<(), Box> { let db = Database::connect(cfg.database.clone()).await?; - purge_expired_tokens(&db, Duration::seconds(cfg.token_lifetime.into())).await?; + purge_expired_tokens(&db, Duration::hours(cfg.token_lifetime.into())).await?; let secret = load_secret(&db).await?; diff --git a/src/routes/auth.rs b/src/routes/auth.rs index 27f4aea..04354fc 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -14,7 +14,7 @@ pub async fn validate(State(state): State, Json(payload): Json Duration::seconds(state.cfg.token_lifetime as i64) { + if Utc::now() - t.created_at > Duration::seconds(state.cfg.token_duration as i64) { warn!(target: "AUTH", "[VALIDATE] expired token!"); return Err(StatusCode::UNAUTHORIZED); } diff --git a/src/routes/session.rs b/src/routes/session.rs index 02a3464..355af00 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -24,7 +24,7 @@ pub async fn join(State(state): State, Json(payload): Json