Skip to main content

TOTP-based two-factor authentication for Django admin

Project description

django-admin-2fa

TOTP-based two-factor authentication for Django admin. Works with Google Authenticator, Authy, 1Password, and any TOTP-compatible app.

Features

  • TOTP authentication with QR code setup
  • Backup codes (10 per user, single-use)
  • Brute force protection (account lockout after failed attempts)
  • Middleware-based — no changes to Django's auth system
  • Works with any admin theme (Unfold, Grappelli, Jazzmin, etc.)
  • Encrypted secret storage using django.core.signing
  • Hashed backup codes using Django's password hashing

Installation

pip install django-admin-totp

Quick Start

1. Add to INSTALLED_APPS (before django.contrib.admin):

INSTALLED_APPS = [
    'django_admin_2fa',
    'django.contrib.admin',
    ...
]

2. Add middleware:

MIDDLEWARE = [
    ...
    'django_admin_2fa.middleware.TwoFactorMiddleware',
]

3. Add URLs (before admin URLs):

from django.urls import include, path

urlpatterns = [
    path('admin/2fa/', include('django_admin_2fa.urls')),
    path('admin/', admin.site.urls),
]

4. Run migrations:

python manage.py migrate django_admin_2fa

That's it. Staff users will be redirected to 2FA setup on their next login.

Configuration

All settings are optional. Add to settings.py:

ADMIN_2FA = {
    'TOTP_ISSUER': 'My App',        # Name shown in authenticator app
    'BACKUP_CODE_COUNT': 10,         # Number of backup codes per user
    'MAX_ATTEMPTS': 5,               # Failed attempts before lockout
    'LOCKOUT_DURATION': 900,         # Lockout duration in seconds (default: 15 min)
    'REQUIRE_2FA_FOR_STAFF': True,   # Require 2FA for all staff users
}

User Flow

  1. Staff user logs in with username + password
  2. Redirected to 2FA setup (first time) or verification screen
  3. Scans QR code with authenticator app
  4. Enters 6-digit code to verify
  5. Receives 10 single-use backup codes
  6. On subsequent logins, enters TOTP code to access admin

Admin Panel

The following models are available in the Django admin:

  • TOTP Devices — View and manage user devices
  • Backup Codes — View backup code usage
  • 2FA Login Attempts — Monitor login attempts, unlock users

Pages

URL Description
/admin/2fa/verify/ Enter TOTP or backup code
/admin/2fa/setup/ QR code setup for new devices
/admin/2fa/backup-codes/ View and regenerate backup codes
/admin/2fa/manage/ 2FA status, reset device

Requirements

  • Python >= 3.10
  • Django >= 4.2
  • pyotp >= 2.9.0
  • qrcode >= 7.4

License

MIT

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

django_admin_totp-0.1.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_admin_totp-0.1.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file django_admin_totp-0.1.0.tar.gz.

File metadata

  • Download URL: django_admin_totp-0.1.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_admin_totp-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2d605ff36423459a2a7d2ff62bc286c15f808e6228fdf3b4535b6685b70318e4
MD5 ad7443b2541a26969f55d54eba76fd1e
BLAKE2b-256 16db35e83fa738f5243662165605775649601d3ad457c29b83f7d160be8c1c12

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_totp-0.1.0.tar.gz:

Publisher: publish.yml on Reyretee/django2fa

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_admin_totp-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_totp-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2380c1255444b81ed725ee760573fe6201a01093b2d0fab5cc4b92c605753956
MD5 1b30ff89cd281e216671d194b2e27baa
BLAKE2b-256 ac8a0f388c0abfac82594ff643a28227712a0b8fe2b9139314482675b8b15eab

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_admin_totp-0.1.0-py3-none-any.whl:

Publisher: publish.yml on Reyretee/django2fa

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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