Multi-factor authentication is a must have feature in any modern web application. Specially providing support for both TOTP (think applications on phone) and FIDO2 (say Yubikeys) usage. I created a small Django demo mfaforgood which shows how to enable both.
I am using django-mfa3 for all the hard work, but specially from a PR branch from my friend Giuseppe De Marco.
I also fetched the cbor-js package in the repository so that hardware tokens for FIDO2 to work. I hope this example will help you add the MFA support to your Django application.
Major points of the code
- Adding example templates from MFA project, with
admin
theme and addingcbor-js
to the required templates. - Adding
mfa
toINSTALLED_APPS
. - Adding
mfa.middleware.MfaSessionMiddleware
toMIDDLEWARE
. - Adding
MFA_DOMAIN
andMFA_SITE_TITLE
tosettings.py
. - Also adding
STATICFILES_DIRS
. - Adding
mfa.views.MFAListView
as the Index view of the application. - Also adding
mfa
URLs.
After login for the first time one can enable MFA in the following screen.