An implementation of basic multi-factor authentication (MFA) for django projects
An implementation of basic Multi-Factor Authentication (MFA) functionality for Django applications (phone call, text message, and email).
Now available on PyPI (
pip install django-simplemfa)
The intent of this project is to:
- Provide basic yet effective MFA functionality for new and existing Django applications.
- Limit external (outside Django) dependencies wherever feasible.
- Simplify the setup and integration of MFA functionality in Django applications.
- Limit the amount of alteration required in existing codebases to integrate basic MFA functionality.
- Provide options to integrate more advanced or customized MFA functionality if desired.
At this time only email, phone call, and text message MFA options are supported. Future integrations (such as MS/Google Authenticator apps, OTP keys, WebAuthn, etc.) are possible.
Why It Exists
Upon evaluating various other Django MFA applications, most appeared to be one or more of the following:
- Their codebases were deprecated or unsupported.
- They often required a substantial amount of very specific dependencies in order to achieve even basic functionality.
- They often required substantial changes to existing applications to function and integrate properly.
- Django >= version 3.0
- Django-Twilio (if using phone and/or text message MFA)
Twilio is the current integration for phone and text message MFA, but more are planned. Email MFA leverages the built-in Django email utilities.
Setup and Use
Download or clone the
simplemfa package here or install from PyPi with
pip install django-simplemfa.
templates directory, create a new directory called 'simplemfa' and in it place or create the following templates:
simplemfa/mfa_email.html(the MFA email message template)
simplemfa/mfa_text.html(the MFA text message template)
simplemfa/mfa_voice.html(plain text file with the message you want to send via phone call)
simplemfa/auth.html(the MFA login screen template)
Examples are provided in the package's
templates directory (
path('mfa/', include('simplemfa.urls', namespace="simplemfa"))
Make sure to include the namespace as above.
REQUIRE_MFA = True(global setting which activates MFA for all users)
DEFAULT_FROM_EMAIL = "firstname.lastname@example.org"(the email address you want MFA messages to come from)
INSTALLED_APPS = [ ... 'simplemfa' ]
MIDDLEWARE = [ ... 'simplemfa.middleware.ValidateMFAMiddleware' ]
If using Twilio (text and voice):
TWILIO_AUTH_TOKEN(your Twilio Auth Token)
TWILIO_ACCOUNT_SID(your Twilio account SID)
TWILIO_NUMBER(your Twilio phone number you want MFA codes sent from)
MFA_USER_PHONE_ATTRIBUTE(the attribute of
request.userthat has the phone number for the user in the format
MFA_USER_MODE_ATTRIBUTE(the attribute of
request.userthat has the user's default way of receiving the MFA code, e.g.
request.user.profile.mfa_modewhich must be one of the choices from
simplemfa.models.AUTH_CODE_DELIVERY_CHOICES- currently "EMAIL", "TEXT", and "PHONE")
APP_NAME = "My App Name"(application name which is provided in the messages to the user)
LOGIN_REDIRECT_URL = 'my_view_name'(the default view users are sent to after they authenticate)
MFA_CODE_LENGTH(default is 6)
MFA_CODE_EXPIRATION(default is 900 seconds (15 minutes))
MFA_CODE_DELIVERY_DEFAULT(default is "EMAIL")
If using Twilio (for phone call or text message), you will need to install and set up djang-twilio according to the instructions for that package.
For email, ensure that your email is configured properly in your Django settings.
Once those items are complete, run
migrate for your project.
Run your project. It should allow you to access all public (login exempt) pages. After you log in, however, it will automatically redirect you to the MFA verification page where you will request and then enter an MFA code. If the code passes, you will be allowed to proceed as any normal authenticated user would in your application.
A project example is coming shortly.
As of right now, MFA is applied globablly in the
settings.py file. We are working on changing that to track in a User's settings as part of an
MFAProfile model attached to the User object.
MFA codes sent to users are stored as one-way hashed objects using Django's built-in hashers. It is treated as a password field in the application. The hashes are created and verified using Django's own
check_password() functions, respectively. The ONLY time a plain-text MFA code is created in the application is during the sending of the user message to the Twilio API or via email. At no other time are MFA codes rendered or stored as plain text. All MFA codes are destroyed immediately after use or upon expiration (
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for django_simplemfa-0.3.1-py3-none-any.whl