To set up the package, follow these configuration steps in your Django project:
1. Update INSTALLED_APPS
Add the required apps. Note that specific plugins (email, phonenumber, yubikey) are optional depending on the features you want to support.
2. Configure MIDDLEWARE
Add django_otp.middleware.OTPMiddleware. It must be placed after django.contrib.auth.middleware.AuthenticationMiddleware.
3. Set Login URLs
Update your settings.py to point to the new two-factor login pages.
4. Update URL Configuration
Include the two_factor.urls.urlpatterns in your project's root URL configuration.
Warning: Remove any other existing login routes to prevent users from circumventing two-factor authentication. The Django admin interface is automatically patched to use the new method.
INSTALLED_APPS = [
...
'django_otp',
'django_otp.plugins.otp_static',
'django_otp.plugins.otp_totp',
'django_otp.plugins.otp_email', # <- for email capability.
'otp_yubikey', # <- for yubikey capability.
'two_factor',
'two_factor.plugins.phonenumber', # <- for phone number capability.
'two_factor.plugins.email', # <- for email capability.
'two_factor.plugins.yubikey', # <- for yubikey capability.
]
MIDDLEWARE = (
...
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django_otp.middleware.OTPMiddleware',
...
)
LOGIN_URL = 'two_factor:login'
# this one is optional
LOGIN_REDIRECT_URL = 'two_factor:profile'
# In urls.py
from two_factor.urls import urlpatterns as tf_urls
urlpatterns = [
path('', include(tf_urls)),
...
]