Skip to main content

django-simplemfa

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)

Overview

The intent of this project is to:

  1. Provide essential MFA functionality for new and existing Django applications.
  2. Limit external (outside Django) dependencies wherever feasible.
  3. Simplify the setup and integration of MFA functionality in Django applications.

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:

  1. Their codebases were deprecated or unsupported.
  2. They often required a substantial amount of very specific dependencies in order to achieve even basic functionality.
  3. They often required substantial changes to existing applications to function and integrate properly.

Basic Requirements

  • 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.

In your 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 (simplemfa/templates).

In your urls.py:

Add path('mfa/', include('simplemfa.urls', namespace="simplemfa")) to your routes, making sure to include the namespace as shown.

In your settings.py:

Required Settings:

  • Required: REQUIRE_MFA = True (global setting which activates MFA for all users)

  • Required: Ensure the Django email system is configured properly

  • Required: INSTALLED_APPS = [ ... 'simplemfa' ]

  • Required: MIDDLEWARE = [ ... 'simplemfa.middleware.ValidateMFAMiddleware' ]

If using Twilio (text and voice):

  • Required: Install and set up djang-twilio
  • Required: MFA_USER_PHONE_ATTRIBUTE (the attribute of request.user that has the phone number for the user in the format +12345678900, e.g. profile.phone resolves to request.user.profile.phone)
  • Required: MFA_USER_MODE_ATTRIBUTE (the attribute of request.user that has the user's default way of receiving the MFA code, e.g. profile.mfa_mode resolves to request.user.profile.mfa_mode which must be one of the choices from simplemfa.models.AUTH_CODE_DELIVERY_CHOICES - currently "EMAIL", "TEXT", and "PHONE")

Optional Settings:

  • Optional: APP_NAME = "My App Name" (application name which is provided in the messages to the user)
  • Optional: MFA_CODE_LENGTH (default is 6)
  • Optional: MFA_CODE_EXPIRATION (default is 900 seconds (15 minutes))
  • Optional: MFA_CODE_DELIVERY_DEFAULT (default is "EMAIL")

Once those items are complete, run makemigrations then migrate for your project.

Run your project. That's it!

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.

Notes

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 make_password() and 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 (MFA_CODE_EXPIRATION).

Metadata

Release files for django-simplemfa 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-simplemfa 0.4.1
File Size Uploaded
django-simplemfa-0.4.1.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-simplemfa 0.4.1
File Interpreter ABI Platform
django_simplemfa-0.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 26.6 kB

Release files / django-simplemfa-0.4.1.tar.gz

Download URL django-simplemfa-0.4.1.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
80741e98ee8b44b1b1c0e3cc3c02d0c2f2c5a6c2ffaf0722d8f73dfc65a620a7
BLAKE2b-256 checksum
How to use checksums
2d2ce2748f67eaa25d0567686f2682f835817b7aeb23ca40780190d61462c257
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release files / django_simplemfa-0.4.1-py3-none-any.whl

Download URL django_simplemfa-0.4.1-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c20c05a22440d629a655c9839c704bb26af54b257eb91523d024ce29193f9d37
BLAKE2b-256 checksum
How to use checksums
19df04e51c2061dbd54b787fcff3682c8268710e3a946e95a148b4b36ac07f3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.4.1 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.1

2 release files

0.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page