40 - Logout & Session Expiry

Overview

OpenAlgo implements automatic session expiry at a configurable time daily (default 3:00 AM IST) to ensure security and force re-authentication. When a session expires or user logs out, multiple caches are cleared and tokens are revoked.

Session Expiry Flow

┌──────────────────────────────────────────────────────────────────────────────┐
│                        Session Expiry Architecture                           │
└──────────────────────────────────────────────────────────────────────────────┘

                         Every Request


┌─────────────────────────────────────────────────────────────────────────────┐
│                      @app.before_request                                     │
│                      check_session_expiry()                                  │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  Skip for:                                                           │   │
│  │  - Static files (/static/)                                           │   │
│  │  - API endpoints (/api/)                                             │   │
│  │  - Public routes (/, /auth/login, /setup, etc.)                      │   │
│  │  - OAuth callbacks (/auth/broker/)                                   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                              │                                               │
│                              ▼                                               │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  is_session_valid()?                                                 │   │
│  │                                                                      │   │
│  │  1. Check session['logged_in'] exists                                │   │
│  │  2. Check session['login_time'] exists                               │   │
│  │  3. Compare current time with SESSION_EXPIRY_TIME                    │   │
│  │     - If now > expiry_time AND login_time < expiry_time → EXPIRED   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                              │                                               │
│                    ┌─────────┴─────────┐                                    │
│                    │                   │                                    │
│                 Valid              Expired                                   │
│                    │                   │                                    │
│                    ▼                   ▼                                    │
│              Continue            revoke_user_tokens()                       │
│              Request             session.clear()                            │
│                                  Redirect to login                          │
└─────────────────────────────────────────────────────────────────────────────┘

Session Expiry Logic

Location: utils/session.py

Configuration

Expiry Check

Visual Timeline

Token Revocation Process

When session expires or user logs out, these cleanup actions occur:

Implementation

revoke_user_tokens Function

Session Decorator

Manual Logout

When user clicks logout:

What Gets Cleared

Cache/Data
Location
Purpose
Cleared On

Auth Token Cache

auth_cache (TTLCache)

Broker auth tokens

Logout/Expiry

Feed Token Cache

feed_token_cache (TTLCache)

WebSocket tokens

Logout/Expiry

Symbol Cache

BrokerSymbolCache

100K+ symbols

Logout/Expiry

Settings Cache

settings_cache

User preferences

Logout/Expiry

Strategy Cache

strategy_cache

Strategy configs

Logout/Expiry

Telegram Cache

telegram_cache

Bot settings

Logout/Expiry

Database Token

auth table

is_revoked=True

Logout/Expiry

Flask Session

Server-side

All session data

Logout/Expiry

Why 3:00 AM IST?

The default expiry time is set to 3:00 AM IST for several reasons:

  1. Market Closed: Indian markets are closed (NSE: 9:15 AM - 3:30 PM)

  2. Low Activity: Minimal user activity during this time

  3. Daily Reset: Forces fresh authentication each trading day

  4. Security: Limits exposure if credentials are compromised

  5. Token Refresh: Ensures broker tokens are refreshed daily

Configuration Options

Session Lifetime Calculation

Key Files Reference

File
Purpose

utils/session.py

Session validation and token revocation

blueprints/auth.py

Login/logout endpoints

app.py

check_session_expiry before_request hook

database/auth_db.py

Auth token storage

database/master_contract_cache_hook.py

Symbol cache clearing

database/settings_db.py

Settings cache

database/strategy_db.py

Strategy cache

database/telegram_db.py

Telegram cache

Last updated