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
- Staff user logs in with username + password
- Redirected to 2FA setup (first time) or verification screen
- Scans QR code with authenticator app
- Enters 6-digit code to verify
- Receives 10 single-use backup codes
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d605ff36423459a2a7d2ff62bc286c15f808e6228fdf3b4535b6685b70318e4
|
|
| MD5 |
ad7443b2541a26969f55d54eba76fd1e
|
|
| BLAKE2b-256 |
16db35e83fa738f5243662165605775649601d3ad457c29b83f7d160be8c1c12
|
Provenance
The following attestation bundles were made for django_admin_totp-0.1.0.tar.gz:
Publisher:
publish.yml on Reyretee/django2fa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_admin_totp-0.1.0.tar.gz -
Subject digest:
2d605ff36423459a2a7d2ff62bc286c15f808e6228fdf3b4535b6685b70318e4 - Sigstore transparency entry: 1108222439
- Sigstore integration time:
-
Permalink:
Reyretee/django2fa@09aea4bf56a84af6a15d1688bab089d976b98e65 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Reyretee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@09aea4bf56a84af6a15d1688bab089d976b98e65 -
Trigger Event:
release
-
Statement type:
File details
Details for the file django_admin_totp-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_admin_totp-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2380c1255444b81ed725ee760573fe6201a01093b2d0fab5cc4b92c605753956
|
|
| MD5 |
1b30ff89cd281e216671d194b2e27baa
|
|
| BLAKE2b-256 |
ac8a0f388c0abfac82594ff643a28227712a0b8fe2b9139314482675b8b15eab
|
Provenance
The following attestation bundles were made for django_admin_totp-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Reyretee/django2fa
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_admin_totp-0.1.0-py3-none-any.whl -
Subject digest:
2380c1255444b81ed725ee760573fe6201a01093b2d0fab5cc4b92c605753956 - Sigstore transparency entry: 1108222501
- Sigstore integration time:
-
Permalink:
Reyretee/django2fa@09aea4bf56a84af6a15d1688bab089d976b98e65 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Reyretee
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@09aea4bf56a84af6a15d1688bab089d976b98e65 -
Trigger Event:
release
-
Statement type: