Skip to content

Commit

Permalink
use configurable lifetime for session token
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill-K-1 committed Sep 11, 2024
1 parent 25f65a5 commit a9eee24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion event-indexer/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"requestTimeout": 10,
"session": {
"secret": null,
"lifetime": 86400
"lifetime": 3600
}
},
"contracts": {
Expand Down
14 changes: 7 additions & 7 deletions event-indexer/src/auto_refresh_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -14,8 +12,9 @@ pub struct AutoRefreshToken {

impl AutoRefreshToken {
pub fn new(session_manager: SessionManager) -> anyhow::Result<Self> {
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,
Expand All @@ -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)
Expand Down

0 comments on commit a9eee24

Please sign in to comment.