Skip to main content

django-mfa3

An opinionated Django app that handles multi factor authentication (MFA) via FIDO2, TOTP, and recovery codes.

Features

  • Two factor authentication is required on login (if the user has registered a key)
  • Stuff just works without much configuration
  • The UI allows to add new keys and to remove keys that have been compromised
  • You can optionally enforce MFA for all users
  • You can (and should) customize the templates
  • Simple code, few dependencies

Installation

pip install django-mfa3

Usage

  1. Add 'mfa' to INSTALLED_APPS
  2. Use mfa.views.LoginView instead of the regular login view. (Be sure to remove any other login routes, otherwise the multi factor authentication can be circumvented. The admin login will automatically be patched to redirect to the regular login.)
  3. Set MFA_DOMAIN = 'example.com' and MFA_SITE_TITLE = 'My site'. See settings.py for a full list of settings.
  4. Register URLs: path('mfa/', include('mfa.urls', namespace='mfa')
  5. The included templates are just examples, so you should replace them with your own
  6. FIDO2 requires client side code. You can either implement it yourself or use the included fido2.js.
  7. Somewhere in your app, add a link to 'mfa:list'

Enforce MFA

Optionally, you can add 'mfa.middleware.MFAEnforceMiddleware' to MIDDLEWARE (after AuthenticationMiddleware!). It will force users to setup two factor authentication by redirecting all authenticated requests to 'mfa:list' as long as the user has no MFAKeys. You can use mfa.decorators.public to add exceptions.

Send email on failed login attempt

If someone failes to login on the second factor that might indicate that the first factor (password) has been compromised. django-mfa3 will automatically send a warning to affected users under the following conditions:

  • Django needs to be configured for sending email
  • There must be an email address associated with the user account
  • You need to provide some templates
    • mfa/login_failed_subject.txt: optional, a default is included
    • mfa/login_failed_email.txt: required, an example is included in the tests
    • mfa/login_failed_email.html: optional

All templates have access to the following context data: email, domain, site_name, user, method.

Status

I am not sure whether I will be able to maintain this library long-term. If you would like to help or even take ownership of this project, please contact me!

Related projects

django-mfa3 is based on pyotp and python-fido2.

It is inspired by but not otherwise affiliated with django-mfa2. A big difference between the two projects is that django-mfa2 supports many methods, while django-mfa3 only supports FIDO2 and TOTP. U2F was dropped because it is now superseded by FIDO2. Email and Trusted Devices were dropped because I felt like they have inferior security properties compared to FIDO2 and TOTP.

Another major inspiration is django-otp. It is probably the most mature library when it comes to two factor authentication in django. However, its basic structure is not compatible with FIDO2.

django-mfa3 is compatible with django-axes and django-stronghold.

Security considerations

The actual cryptography is handled by pyotp and python-fido2. This library only provides the glue code for django. Still, there could be issues in the glue.

A notable attack surface is server state: The authentication consists of three separate HTTP requests: The regular login, fetching a challenge, and a response. The server keeps some state in the session across these requests. For example, the user is temporarily stored in the session until the second factor authentication is done. The logic for handling this state is not as straight forward as I would like and there might be issues hidden in there.

Please also be careful when implementing and using this library in your project to prevent higher level security or usability issues. Please refer to other guidelines like the OWASP Cheat Sheet for more informaton on that topic.

Release files for django-mfa3 1.1.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-mfa3 1.1.1
File Size Uploaded
django_mfa3-1.1.1.tar.gz 19.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-mfa3 1.1.1
File Interpreter ABI Platform
django_mfa3-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 46.0 kB

Release files / django_mfa3-1.1.1.tar.gz

Download URL django_mfa3-1.1.1.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
1e9f80fbce3f13d2f2f0d956522f1ecf3b75f6802d6877647256dc6bf33335be
BLAKE2b-256 checksum
How to use checksums
adad2c68afa521f864bf6ab6a717664f3ef80eae37370510fd52e69ba8c1e743
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 31, 2026.

Transparency log

Release files / django_mfa3-1.1.1-py3-none-any.whl

Download URL django_mfa3-1.1.1-py3-none-any.whl
Size 26.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f756758bef8dcab51555883631a3d36ae41a71cc65dff92076e74ad785d90f37
BLAKE2b-256 checksum
How to use checksums
a44b1e22561a6630651cad99085048cda70081ccd52a048b492cc5a1db4b8338
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 31, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.15.2

2 release files

0.15.0

2 release files

0.13.0

2 release files

0.12.1

2 release files

0.11.0

1 release file

0.10.0

1 release file

0.9.0

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.0

2 release files

0.3.0

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

1 release file

0.1.0

1 release file

0.0.0

1 release file

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