Django Two-Factor Authentication

1.18.1 · active · verified Sat Apr 11

Complete Two-Factor Authentication for Django. Built on top of the django-otp framework and Django's built-in authentication system, it offers easy integration into most Django projects. Inspired by Google's Two-Step Authentication, it supports various methods including token generator apps (like Google Authenticator), SMS, call, and YubiKey. The library is actively maintained with frequent releases, typically every few months.

Warnings

Install

Imports

Quickstart

Install the library, add `django_otp` and `two_factor` (and any desired plugins) to `INSTALLED_APPS`, and ensure `OTPMiddleware` is in `MIDDLEWARE`. Then, configure `LOGIN_URL` and `LOGIN_REDIRECT_URL` to point to the library's views, and include its URL patterns in your project's `urls.py`. Remember to run `python manage.py migrate` after setup.

import os

# settings.py
# Add required apps, ensuring 'two_factor' is listed after django_otp plugins
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'django_otp',
    'django_otp.plugins.otp_static',
    'django_otp.plugins.otp_totp',
    # Optional plugins:
    # 'django_otp.plugins.otp_email', # For email tokens
    # 'two_factor.plugins.phonenumber', # For SMS/call tokens
    # 'two_factor.plugins.email', # For email tokens (alternative)
    # 'two_factor.plugins.yubikey', # For YubiKey support
    # 'webauthn', # For WebAuthn support

    'two_factor',
]

# Add OTP middleware after AuthenticationMiddleware
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django_otp.middleware.OTPMiddleware', # Must be after AuthenticationMiddleware
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

# Point to the two-factor authentication login/profile URLs
LOGIN_URL = 'two_factor:login'
LOGIN_REDIRECT_URL = 'two_factor:profile'

# Optional: Configure email backend if using email tokens
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = os.environ.get('DEFAULT_FROM_EMAIL', 'webmaster@localhost')

# urls.py
from django.contrib import admin
from django.urls import path, include
from two_factor.urls import urlpatterns as tf_urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include(tf_urls)), # Include two-factor URLs at the root or desired path
    # path('account/', include(tf_urls)), # Alternative: include at a specific path
]

# To integrate with Django Admin (optional, and usually patched automatically)
# from two_factor.admin import AdminSiteOTPRequiredMixin
# class OTPAdminSite(AdminSiteOTPRequiredMixin, admin.AdminSite):
#     pass
# admin.site = OTPAdminSite() # Replace default admin site if needed, often not required.

view raw JSON →