Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add two-factor authentication

Roland Geider51510bdb975c
diff --git a/pyproject.toml b/pyproject.toml
index a524d25c9..92a3570c9 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -32,7 +32,7 @@ dependencies = [
"celery[redis]~=5.6.0",
"crispy-bootstrap5~=2026.3",
"django-activity-stream~=2.0.0",
- "django-allauth~=65.16.1",
+ "django-allauth[mfa]~=65.16.1",
"django-axes[ipware]~=8.3.1",
"django-bootstrap-breadcrumbs2==1.0.0",
"django-cors-headers~=4.9.0",
diff --git a/settings/main.py b/settings/main.py
index 143b097af..9ee6a5b04 100644
--- a/settings/main.py
+++ b/settings/main.py
@@ -235,6 +235,17 @@ if os.environ.get('DJANGO_CACHE_BACKEND'):
# The site's domain as used by the email verification workflow
EMAIL_PAGE_DOMAIN = SITE_URL
+#
+# Two-factor authentication (allauth.mfa)
+#
+# Lets self-hosted instances drop a factor, most notably 'webauthn', which
+# needs a secure context (HTTPS or localhost) and therefore does not work on
+# plain-HTTP deployments.
+MFA_SUPPORTED_TYPES = env.list(
+ 'MFA_SUPPORTED_TYPES',
+ default=['totp', 'recovery_codes', 'webauthn'],
+)
+
#
# Django Axes
#
diff --git a/settings/settings_global.py b/settings/settings_global.py
index 90188aad0..3f49c51a5 100644
--- a/settings/settings_global.py
+++ b/settings/settings_global.py
@@ -52,6 +52,7 @@ WSGI_APPLICATION = 'wger.wsgi.application'
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
+ 'django.contrib.humanize', # used by allauth's mfa/webauthn templates
'django.contrib.messages',
'django.contrib.sessions',
'django.contrib.sites',
@@ -123,6 +124,7 @@ INSTALLED_APPS = [
# Django-allauth
'allauth',
'allauth.account',
+ 'allauth.mfa',
]
MIDDLEWARE = [
@@ -251,6 +253,16 @@ ACCOUNT_FORMS = {
# the non-login limits (signup, password reset, ...) stay at allauth's defaults.
ACCOUNT_RATE_LIMITS = {'login_failed': None, 'login': None}
+#
+# Two-factor authentication (allauth.mfa)
+#
+MFA_SUPPORTED_TYPES = ['totp', 'recovery_codes', 'webauthn']
+MFA_TOTP_ISSUER = 'wger'
+# Passkeys may also be used for passwordless login (a "Sign in with a passkey"
+# button), but not for passwordless signup.
+MFA_PASSKEY_LOGIN_ENABLED = True
+MFA_PASSKEY_SIGNUP_ENABLED = False
+
#
# Login
#
diff --git a/uv.lock b/uv.lock
index 8c9560e92..ab0ed519b 100644
--- a/uv.lock
+++ b/uv.lock
@@ -502,6 +502,12 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/ad/58/d95b6c3088d83697bfd93782ee57bc6a6462e41eb19121a947b8a015396a/django_allauth-65.16.1-py3-none-any.whl", hash = "sha256:e49df24056bf37c44e56aaad1e51f78994b7d175bc3476d65e8f8f58390a8ce8", size = 2051868, upload-time = "2026-04-17T18:54:12.032Z" },
]
+[package.optional-dependencies]
+mfa = [
+ { name = "fido2" },
+ { name = "qrcode" },
+]
+
[[package]]
name = "django-axes"
version = "8.3.1"
@@ -801,6 +807,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/84/0b/5c0b2d3a4b7a715f1835dd3f963bfbe841a02ae5cad1df8ee0325dfad235/faker-40.18.0-py3-none-any.whl", hash = "sha256:61a6b94b74605ddb090a065deb197a1c585ae7a874c094cf6693671d271e6083", size = 2006355, upload-time = "2026-05-14T16:43:02.489Z" },
]
+[[package]]
+name = "fido2"
+version = "2.2.0"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "cryptography" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/09/34/4837e2f5640baf61d8abd6125ccb6cc60b4b2933088528356ad6e781496f/fido2-2.2.0.tar.gz", hash = "sha256:0d8122e690096ad82afde42ac9d6433a4eeffda64084f36341ea02546b181dd1", size = 294167, upload-time = "2026-04-15T06:42:50.264Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/01/82/f3c5dd87b0977f5547cc132b7969e6f5075a8c2f5881cf4b6df6378505f9/fido2-2.2.0-py3-none-any.whl", hash = "sha256:3587ccf0af7b71b5dd73f17e1dbec9f0fd157292f9163f02e7778f46d0d25fe5", size = 234025, upload-time = "2026-04-15T06:42:51.813Z" },
+]
+
[[package]]
name = "flower"
version = "2.0.1"
@@ -1547,6 +1565,18 @@ wheels = [
{ url = "https://files.pythonhosted.org/packages/f1/12/de94a39c2ef588c7e6455cfbe7343d3b2dc9d6b6b2f40c4c6565744c873d/pyyaml-6.0.3-cp314-cp314t-win_arm64.whl", hash = "sha256:ebc55a14a21cb14062aa4162f906cd962b28e2e9ea38f9b4391244cd8de4ae0b", size = 149341, upload-time = "2025-09-25T21:32:56.828Z" },
]
+[[package]]
+name = "qrcode"
+version = "8.2"
+source = { registry = "https://pypi.org/simple" }
+dependencies = [
+ { name = "colorama", marker = "sys_platform == 'win32'" },
+]
+sdist = { url = "https://files.pythonhosted.org/packages/8f/b2/7fc2931bfae0af02d5f53b174e9cf701adbb35f39d69c2af63d4a39f81a9/qrcode-8.2.tar.gz", hash = "sha256:35c3f2a4172b33136ab9f6b3ef1c00260dd2f66f858f24d88418a015f446506c", size = 43317, upload-time = "2025-05-01T15:44:24.726Z" }
+wheels = [
+ { url = "https://files.pythonhosted.org/packages/dd/b8/d2d6d731733f51684bbf76bf34dab3b70a9148e8f2cef2bb544fccec681a/qrcode-8.2-py3-none-any.whl", hash = "sha256:16e64e0716c14960108e85d853062c9e8bba5ca8252c0b4d0231b9df4060ff4f", size = 45986, upload-time = "2025-05-01T15:44:22.781Z" },
+]
+
[[package]]
name = "redis"
version = "6.4.0"
@@ -1889,7 +1919,7 @@ dependencies = [
{ name = "crispy-bootstrap5" },
{ name = "django" },
{ name = "django-activity-stream" },
- { name = "django-allauth" },
+ { name = "django-allauth", extra = ["mfa"] },
{ name = "django-axes", extra = ["ipware"] },
{ name = "django-bootstrap-breadcrumbs2" },
{ name = "django-cors-headers" },
@@ -1949,7 +1979,7 @@ requires-dist = [
{ name = "crispy-bootstrap5", specifier = "~=2026.3" },
{ name = "django", specifier = "~=6.0.5" },
{ name = "django-activity-stream", specifier = "~=2.0.0" },
- { name = "django-allauth", specifier = "~=65.16.1" },
+ { name = "django-allauth", extras = ["mfa"], specifier = "~=65.16.1" },
{ name = "django-axes", extras = ["ipware"], specifier = "~=8.3.1" },
{ name = "django-bootstrap-breadcrumbs2", specifier = "==1.0.0" },
{ name = "django-cors-headers", specifier = "~=4.9.0" },
diff --git a/wger/core/templates/account/login.html b/wger/core/templates/account/login.html
index b3ef92497..79bdb8b59 100644
--- a/wger/core/templates/account/login.html
+++ b/wger/core/templates/account/login.html
@@ -54,6 +54,23 @@
{% translate "Login" %}
</button>
</form>
+
+ {% if PASSKEY_LOGIN_ENABLED %}
+ <hr>
+ <button type="submit"
+ form="mfa_login"
+ id="passkey_login"
+ class="btn btn-outline-primary btn-block">
+ {% translate "Sign in with a passkey" %}
+ </button>
+ {% endif %}
+{% endblock %}
+
+
+{% block extra_body %}
+ {% if PASSKEY_LOGIN_ENABLED %}
+ {% include "mfa/webauthn/snippets/login_script.html" %}
+ {% endif %}
{% endblock %}
diff --git a/wger/core/templates/allauth/elements/alert.html b/wger/core/templates/allauth/elements/alert.html
new file mode 100644
index 000000000..589855baa
--- /dev/null
+++ b/wger/core/templates/allauth/elements/alert.html
@@ -0,0 +1,4 @@
+{% load allauth %}
+<div class="alert {% if 'danger' in attrs.tags %}alert-danger{% elif 'warning' in attrs.tags %}alert-warning{% elif 'success' in attrs.tags %}alert-success{% else %}alert-info{% endif %}">
+ {% slot message %}{% endslot %}
+</div>
diff --git a/wger/core/templates/allauth/elements/button.html b/wger/core/templates/allauth/elements/button.html
new file mode 100644
index 000000000..a7478674e
--- /dev/null
+++ b/wger/core/templates/allauth/elements/button.html
@@ -0,0 +1,10 @@
+{% comment %} djlint:off {% endcomment %}{% load allauth %}
+<{% if attrs.href %}a href="{{ attrs.href }}"{% else %}button{% endif %}
+ class="btn {% if 'danger' in attrs.tags %}btn-danger{% elif 'warning' in attrs.tags %}btn-warning{% elif 'prominent' in attrs.tags %}btn-success{% else %}btn-primary{% endif %}{% if 'minor' in attrs.tags %} btn-sm{% endif %} me-1 mb-1"
+ {% if attrs.form %}form="{{ attrs.form }}"{% endif %}
+ {% if attrs.id %}id="{{ attrs.id }}"{% endif %}
+ {% if attrs.name %}name="{{ attrs.name }}"{% endif %}
+ {% if attrs.value %}value="{{ attrs.value }}"{% endif %}
+ {% if attrs.type %}type="{{ attrs.type }}"{% endif %}>
+{% slot %}{% endslot %}
+</{% if attrs.href %}a{% else %}button{% endif %}>
diff --git a/wger/core/templates/allauth/elements/h1.html b/wger/core/templates/allauth/elements/h1.html
new file mode 100644
index 000000000..357140e52
--- /dev/null
+++ b/wger/core/templates/allauth/elements/h1.html
@@ -0,0 +1,4 @@
+{% comment %}
+Suppressed: wger's base template already renders the page heading from the
+head_title block, so allauth's own <h1> would just be a duplicate.
+{% endcomment %}
diff --git a/wger/core/templates/allauth/elements/panel.html b/wger/core/templates/allauth/elements/panel.html
new file mode 100644
index 000000000..27e2897a5
--- /dev/null
+++ b/wger/core/templates/allauth/elements/panel.html
@@ -0,0 +1,12 @@
+{% load allauth %}
+<section class="card mb-3">
+ <div class="card-body">
+ <h2 class="card-title h5">{% slot title %}{% endslot %}</h2>
+ {% slot body %}{% endslot %}
+ {% if slots.actions %}
+ <div class="mt-2">
+ {% for action in slots.actions %}{{ action }}{% endfor %}
+ </div>
+ {% endif %}
+ </div>
+</section>
diff --git a/wger/core/templates/allauth/elements/table.html b/wger/core/templates/allauth/elements/table.html
new file mode 100644
index 000000000..065f5362d
--- /dev/null
+++ b/wger/core/templates/allauth/elements/table.html
@@ -0,0 +1,4 @@
+{% load allauth %}
+<table class="table align-middle">
+ {% slot %}{% endslot %}
+</table>
diff --git a/wger/core/templates/allauth/layouts/base.html b/wger/core/templates/allauth/layouts/base.html
new file mode 100644
index 000000000..8f5b03500
--- /dev/null
+++ b/wger/core/templates/allauth/layouts/base.html
@@ -0,0 +1,7 @@
+{% extends "base.html" %}
+{% comment %}
+ Routes every allauth page (mfa, email confirmation, ...) that isn't
+ overridden with a native wger template through wger's own chrome.
+{% endcomment %}
+
+{% block title %}{% block head_title %}{% endblock head_title %}{% endblock title %}
diff --git a/wger/core/templates/template.html b/wger/core/templates/template.html
index 73b8cf9ab..4fe2757b0 100644
--- a/wger/core/templates/template.html
+++ b/wger/core/templates/template.html
@@ -212,5 +212,7 @@
</div>
</footer>
+{% block extra_body %}{% endblock %}
+
</body>
</html>
diff --git a/wger/core/templates/user/preferences.html b/wger/core/templates/user/preferences.html
index 47ab6d095..525d96e11 100644
--- a/wger/core/templates/user/preferences.html
+++ b/wger/core/templates/user/preferences.html
@@ -59,6 +59,10 @@
<span class="{% fa_class 'key' %}"></span>
{% translate "Change password" %}
</a>
+ <a href="{% url 'mfa_index' %}" class="dropdown-item">
+ <span class="{% fa_class 'shield-halved' %}"></span>
+ {% translate "Two-factor authentication" %}
+ </a>
<a href="{% url 'core:user:api-key' %}" class="dropdown-item">
<span class="{% fa_class 'globe' %}"></span>
{% translate "API key" %}
diff --git a/wger/core/tests/test_preferences.py b/wger/core/tests/test_preferences.py
index cc6c41619..eaae7bf25 100644
--- a/wger/core/tests/test_preferences.py
+++ b/wger/core/tests/test_preferences.py
@@ -121,6 +121,35 @@ class PreferencesTestCase(WgerTestCase):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'account/email_change.html')
+ def test_preferences_links_to_2fa(self):
+ """The preferences page links to allauth's MFA management page."""
+
+ self.user_login('test')
+ response = self.client.get(reverse('core:user:preferences'))
+ self.assertContains(response, reverse('mfa_index'))
+
+ def test_mfa_page_renders_inside_wger_chrome(self):
+ """
+ allauth's MFA pages render through wger's base template (the
+ allauth/layouts/base.html override), not allauth's bare layout.
+ """
+
+ self.user_login('test')
+ response = self.client.get(reverse('mfa_index'))
+ self.assertEqual(response.status_code, 200)
+ self.assertTemplateUsed(response, 'mfa/index.html')
+ self.assertTemplateUsed(response, 'base.html')
+
+ def test_mfa_webauthn_page_renders(self):
+ """
+ The WebAuthn authenticator list uses ``{% load humanize %}``, so
+ django.contrib.humanize must be in INSTALLED_APPS.
+ """
+
+ self.user_login('test')
+ response = self.client.get(reverse('mfa_list_webauthn'))
+ self.assertEqual(response.status_code, 200)
+
def test_address(self):
"""
Test that the address property works correctly
diff --git a/wger/core/tests/test_user_login.py b/wger/core/tests/test_user_login.py
index cc8978ff4..45eac4d66 100644
--- a/wger/core/tests/test_user_login.py
+++ b/wger/core/tests/test_user_login.py
@@ -214,3 +214,11 @@ class WebLoginViewTestCase(WgerTestCase):
response = self.client.get(reverse('core:user:login'))
self.assertEqual(response.status_code, 200)
+
+ def test_login_page_shows_passkey_button(self):
+ """
+ With MFA_PASSKEY_LOGIN_ENABLED the login page offers passwordless
+ sign-in via a passkey.
+ """
+ response = self.client.get(reverse('core:user:login'))
+ self.assertContains(response, 'id="passkey_login"')
diff --git a/wger/urls.py b/wger/urls.py
index 1764f4214..0bedb3ae2 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -290,6 +290,9 @@ urlpatterns = i18n_patterns(
{'sitemaps': sitemaps},
name='django.contrib.sitemaps.views.sitemap',
),
+
+ path('account/', include('allauth.account.urls')),
+ path('account/2fa/', include('allauth.mfa.urls')),
)
#
@@ -370,7 +373,6 @@ urlpatterns += [
SpectacularRedocView.as_view(url_name='schema'),
name='api-redoc',
),
- path('account/', include('allauth.account.urls')),
]
#