Skip to main content

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

Project description

DRF-TOTP

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

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_totp-0.1.4.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

drf_totp-0.1.4-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file drf_totp-0.1.4.tar.gz.

File metadata

  • Download URL: drf_totp-0.1.4.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for drf_totp-0.1.4.tar.gz
Algorithm Hash digest
SHA256 6d7c5223beaf86767e7383c79f60e514409440e0a5b1d8b3e7910fb9e4dc8eff
MD5 b694a4c5c2d879a37be54f847bf84ad2
BLAKE2b-256 329fbae81a0c31635d2caaa85c8cc3775be639fc01f02d23b5fcb70370f11b4c

See more details on using hashes here.

File details

Details for the file drf_totp-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: drf_totp-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for drf_totp-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 5b466ac4e269c98a0d956d035224b584f7bb8ac27d156ea21372657f43f18d4b
MD5 8c062adb7d5479373ed2f15e20374f17
BLAKE2b-256 221e9e900fd01606e8239f7cf0b472483b49f548838e626f487e83957209b7be

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page