Skip to main content

DRF-TOTP

TOTP (Time-based One-Time Password) authentication for Django REST Framework.

📊 Complete Workflow

User Registration Flow

┌─────────────────┐
│    Sign Up      │
└────────┬────────┘
         │
         ▼
┌─────────────────┐      ┌──────────────────┐
│  POST /generate/│─────▶│  Scan QR Code    │
└────────┬────────┘      └──────────────────┘
         │                        │
         │                        │
         ▼                        ▼
┌─────────────────┐      ┌─────────────────────┐
│  Enter 6-digit  │      │ Open Authenticator  │
│     Token       │      │   App (Google/      │
└────────┬────────┘      │    Authy/etc)       │
         │               └─────────────────────┘
         ▼
┌─────────────────┐      ┌──────────────────────────┐
│  POST /verify/  │─────▶│ DB: is_confirmed = True  │
└────────┬────────┘      └──────────────────────────┘
         │
         ▼
   ✅ 2FA Enabled

Login Flow (with 2FA)

┌─────────────────┐
│   Username &    │
│    Password     │
└────────┬────────┘
         │
         ▼
┌─────────────────┐      ┌──────────────────┐
│  GET /status/   │─────▶│  TOTP Enabled?   │
└────────┬────────┘      └────────┬─────────┘
         │                        │
         │                   Yes  │
         ▼                        ▼
┌─────────────────┐      ┌──────────────────┐
│ POST /validate/ │◀─────│  Enter 6-digit   │
│  {token:123456} │      │   TOTP Code      │
└────────┬────────┘      └──────────────────┘
         │
         ▼
  ✅ Grant Access

Disable 2FA Flow

┌─────────────────┐
│  User Settings  │
└────────┬────────┘
         │
         ▼
┌─────────────────┐      ┌──────────────────┐
│ POST /disable/  │◀─────│  Enter Current   │
│  {token:123456} │      │   TOTP Token     │
└────────┬────────┘      └──────────────────┘
         │
         ▼
┌─────────────────────────┐
│ DB: Delete TOTPDevice   │
└─────────────────────────┘
         │
         ▼
   ❌ 2FA Disabled

Features

  • Generate TOTP secrets for users
  • Verify TOTP tokens
  • Enable/disable TOTP authentication
  • Check TOTP status
  • Validate TOTP tokens

Installation

pip install drf-totp

Quick Start

  1. Add "drf_totp" to your INSTALLED_APPS setting:
INSTALLED_APPS = [
    ...
    'rest_framework',
    'drf_totp',
]
  1. Include the TOTP URLconf in your project urls.py:
path('auth/', include('drf_totp.urls')),
  1. Run migrations:
python manage.py migrate

Settings

Add these to your Django settings:

# Optional: Set your TOTP issuer name (defaults to "drftotp")
TOTP_ISSUER_NAME = "Your App Name"

API Endpoints

  • POST /auth/otp/generate/: Generate new TOTP secret
  • POST /auth/otp/verify/: Verify and enable TOTP
  • GET /auth/otp/status/: Get TOTP status
  • POST /auth/otp/disable/: Disable TOTP
  • POST /auth/otp/validate/: Validate TOTP token

Usage Example

import axios from "axios";

// Generate TOTP
export async function generateTotp() {
  try {
    const response = await axios.post("/auth/otp/generate/");
    const { secret, otpauth_url } = response.data;
    return { secret, otpauth_url };
  } catch (error) {
    console.error("Error generating TOTP:", error);
    throw error;
  }
}

// Verify TOTP
export async function verifyTotp(token) {
  try {
    const response = await axios.post("/auth/otp/verify/", { token });
    return response.data;
  } catch (error) {
    console.error("Error verifying TOTP:", error);
    throw error;
  }
}

// Check Status
export async function checkStatus() {
  try {
    const response = await axios.get("/auth/otp/status/");
    return response.data;
  } catch (error) {
    console.error("Error checking status:", error);
    throw error;
  }
}

// Validate TOTP
export async function validateTotp(token) {
  try {
    const response = await axios.post("/auth/otp/validate/", { token });
    return response.data;
  } catch (error) {
    console.error("Error validating TOTP:", error);
    throw error;
  }
}

License

MIT License - see LICENSE file for details.

Release files for drf-totp 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-totp 0.1.5
File Size Uploaded
drf_totp-0.1.5.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-totp 0.1.5
File Interpreter ABI Platform
drf_totp-0.1.5-py3-none-any.whl Python 3 none any Details

Total release size: 15.5 kB

Release files / drf_totp-0.1.5.tar.gz

Download URL drf_totp-0.1.5.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f1551f251de7e6929ed234c7b3806ecfeee02b7de129d0e783d294e870d3001b
BLAKE2b-256 checksum
How to use checksums
4adf1a4910cf40b0003ada6fdeacaac3a873158860736d9a3a990a1fa8056198
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release files / drf_totp-0.1.5-py3-none-any.whl

Download URL drf_totp-0.1.5-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
09a82fd08e55369628fb1140c345595a62e64b9a0010b42a6d0a62911200b4c7
BLAKE2b-256 checksum
How to use checksums
74d4733fcfb55377574e15a4c877c58133e4456038bc48948c594645b3800fce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page