50 - TOTP Configuration

Overview

OpenAlgo supports Time-based One-Time Password (TOTP) for two-factor authentication. Users can enable 2FA through QR code scanning with authenticator apps like Google Authenticator, Authy, or Microsoft Authenticator.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                        TOTP Configuration Architecture                        │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────┐
│                         TOTP Setup Flow                                      │
│                                                                              │
│  Step 1: Generate Secret                                                     │
│  ─────────────────────────                                                   │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  User requests 2FA setup                                             │   │
│  │           │                                                          │   │
│  │           ▼                                                          │   │
│  │  Generate base32 secret (160 bits)                                  │   │
│  │  JBSWY3DPEHPK3PXP...                                                │   │
│  │           │                                                          │   │
│  │           ▼                                                          │   │
│  │  Generate provisioning URI                                          │   │
│  │  otpauth://totp/OpenAlgo:[email protected]?secret=...&issuer=OpenAlgo│   │
│  │           │                                                          │   │
│  │           ▼                                                          │   │
│  │  Generate QR code image                                             │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  Step 2: User Scans QR Code                                                 │
│  ─────────────────────────────                                               │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                                                                      │   │
│  │   ┌───────────────────┐      Scan with           ┌──────────────┐   │   │
│  │   │  ████████████████ │    ───────────────►      │  Authenticator│   │   │
│  │   │  █              █ │                          │  App          │   │   │
│  │   │  █  QR CODE     █ │                          │               │   │   │
│  │   │  █              █ │                          │   123456      │   │   │
│  │   │  ████████████████ │                          │   ──────      │   │   │
│  │   └───────────────────┘                          │   29 sec      │   │   │
│  │                                                  └──────────────┘   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                                    ▼                                         │
│  Step 3: Verify and Enable                                                  │
│  ──────────────────────────                                                  │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │  User enters code from app                                           │   │
│  │           │                                                          │   │
│  │           ▼                                                          │   │
│  │  Verify code against secret                                         │   │
│  │           │                                                          │   │
│  │           ├──► Valid: Enable TOTP, store encrypted secret           │   │
│  │           │                                                          │   │
│  │           └──► Invalid: Show error, allow retry                     │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────────────┘

Database Schema

TOTP Fields in users Table

TOTP Implementation

Secret Generation

QR Code Generation

Code Verification

Secret Encryption

Backup Codes

Generation

Usage

API Endpoints

Initialize TOTP Setup

Response:

Enable TOTP

Response:

Disable TOTP

Verify TOTP (Login)

Login Flow with TOTP

Implementation

Frontend Components

TOTP Setup Component

TOTP Login Component

Security Considerations

Aspect
Implementation

Secret storage

Fernet encrypted in database

Code validity

30 seconds (RFC 6238)

Clock drift

±1 window (±30 seconds)

Rate limiting

Max 5 attempts per pending session

Backup codes

One-time use, SHA-256 hashed

Recovery

Email reset requires TOTP or backup code

Key Files Reference

File
Purpose

database/user_db.py

User model with TOTP methods (get_totp_uri(), verify_totp())

blueprints/auth.py

TOTP endpoints (reset-password with TOTP)

frontend/src/pages/TwoFactorSettings.tsx

Setup UI

frontend/src/components/TOTPVerification.tsx

Login verification

Note: TOTP functionality is integrated directly into the User model in database/user_db.py. The model includes:

  • totp_secret field - stores the TOTP secret

  • get_totp_uri() method - generates provisioning URI for QR codes using pyotp

  • verify_totp() method - verifies TOTP tokens

There are no separate services/totp_service.py or utils/totp_utils.py files. QR code generation uses the pyotp library's provisioning_uri() method.

Last updated