From a9eee247465cf4c303aee743f0ea4a050dec5634 Mon Sep 17 00:00:00 2001 From: ZelionD Date: Wed, 11 Sep 2024 11:12:43 -0400 Subject: [PATCH] use configurable lifetime for session token --- event-indexer/config/default.json | 2 +- event-indexer/src/auto_refresh_token.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/event-indexer/config/default.json b/event-indexer/config/default.json index 31fa5fb..d811bfb 100644 --- a/event-indexer/config/default.json +++ b/event-indexer/config/default.json @@ -6,7 +6,7 @@ "requestTimeout": 10, "session": { "secret": null, - "lifetime": 86400 + "lifetime": 3600 } }, "contracts": { diff --git a/event-indexer/src/auto_refresh_token.rs b/event-indexer/src/auto_refresh_token.rs index 09a6b12..9a8c1a0 100644 --- a/event-indexer/src/auto_refresh_token.rs +++ b/event-indexer/src/auto_refresh_token.rs @@ -4,8 +4,6 @@ use std::time::Duration; use airdao_gov_portal_db::session_manager::SessionManager; -const TOKEN_LIFETIME: Duration = Duration::from_secs(3600); // 1 hour - pub struct AutoRefreshToken { session_manager: SessionManager, token: SessionToken, @@ -14,8 +12,9 @@ pub struct AutoRefreshToken { impl AutoRefreshToken { pub fn new(session_manager: SessionManager) -> anyhow::Result { - let expires_at = Utc::now(); - let token = session_manager.acquire_internal_token_with_lifetime(TOKEN_LIFETIME)?; + let expires_at = Utc::now() + session_manager.config.lifetime; + let token = session_manager + .acquire_internal_token_with_lifetime(session_manager.config.lifetime)?; Ok(Self { session_manager, @@ -25,11 +24,12 @@ impl AutoRefreshToken { } pub fn acquire_token(&mut self) -> anyhow::Result<&SessionToken> { - if self.expires_at <= Utc::now() + Duration::from_secs(10) { - self.expires_at = Utc::now(); + let now = Utc::now(); + if self.expires_at <= now + Duration::from_secs(10) { + self.expires_at = now + self.session_manager.config.lifetime; self.token = self .session_manager - .acquire_internal_token_with_lifetime(TOKEN_LIFETIME)?; + .acquire_internal_token_with_lifetime(self.session_manager.config.lifetime)?; } Ok(&self.token)