From e68e54288f55d5e5853bd36c03fea2982edd506a Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Sun, 22 Nov 2026 21:21:39 +0100 Subject: [PATCH] Streamline authenticator enrollment and recovery code copying --- docs/deployment.md | 7 +++--- pyproject.toml | 1 + src/coulobre/hosting/sign_in.py | 23 ++++++++++++++++--- src/coulobre/hosting/static/hosting/panel.css | 18 +++++++++++++++ .../hosting/authentication_card.html | 17 ++++++++++++-- .../hosting/factor_recovery_codes.html | 12 ++++++---- .../hosting/test_panel_authentication.py | 20 +++++++++++++++- 7 files changed, 85 insertions(+), 13 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 09fbba0..9a069b9 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -281,8 +281,9 @@ restarting the panel, worker, and scheduler. A privileged user signs in with a password to start first-time setup. The setup session can only enroll a second factor or sign out. Choose an -authenticator app, enter the displayed time-based key, and confirm a code -from the app. Use the authenticator's code at future sign-ins. Keep the +authenticator app, scan the QR code, and confirm a code from the app. The +setup page also provides a link and a copyable setup key for manual entry. +Use the authenticator's code at future sign-ins. Keep the panel host and authenticator device clocks synchronized. If an SMTP relay is configured and the account already has a valid email @@ -291,7 +292,7 @@ the address assigned to the account at enrollment. These codes expire in five minutes and can be requested once a minute. An authenticator is the preferred option for privileged accounts. -Save the recovery codes shown after enrollment. Each code works once and +Use **Copy recovery codes** to save the codes shown after enrollment. Each code works once and can be entered after the account password. If the user loses the factor and all recovery codes, verify their identity and issue a replacement enrollment code on the panel host. diff --git a/pyproject.toml b/pyproject.toml index 554c288..b6e3b98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ dependencies = [ "cryptography>=45", "dnspython>=2.7,<3", "idna>=3,<4", + "qrcode>=8.2,<9", ] [project.scripts] diff --git a/src/coulobre/hosting/sign_in.py b/src/coulobre/hosting/sign_in.py index e7b8924..7bef1d9 100644 --- a/src/coulobre/hosting/sign_in.py +++ b/src/coulobre/hosting/sign_in.py @@ -1,5 +1,6 @@ import base64 +import qrcode from django import forms from django.contrib.auth import logout from django.contrib.auth.views import LoginView, redirect_to_login @@ -13,6 +14,7 @@ from django.views.decorators.debug import ( ) from django.views.generic import FormView from django_otp.plugins.otp_email.models import EmailDevice +from qrcode.image.svg import SvgPathFillImage from .panel_factors import ( SETUP_SESSION, @@ -94,6 +96,11 @@ class PanelLoginView(LoginView): return redirect("panel:factor_verify") super().form_valid(form) start_initial_setup(self.request, destination) + if not email_factor_available(user): + device = begin_enrollment(user, "authenticator") + if device is not None: + self.request.session[SETUP_SESSION] = device.persistent_id + return redirect("panel:factor_confirm") return redirect("panel:factor_enroll") @@ -244,6 +251,10 @@ class EnrollFactorView(FactorView): def get(self, request, *args, **kwargs): if confirmed_device(self.user): return redirect("panel:factor_verify") + if self.initial_setup and device_for_user( + self.user, request.session.get(SETUP_SESSION), confirmed=False + ): + return redirect("panel:factor_confirm") return super().get(request, *args, **kwargs) def get_form_kwargs(self): @@ -277,7 +288,7 @@ class EnrollFactorView(FactorView): class ConfirmFactorView(FactorView): heading = "Confirm your second factor" - instructions = "Enter a code to complete setup." + instructions = "Enter a verification code to complete setup." submit_label = "Confirm" form_class = ConfirmFactorForm @@ -291,15 +302,21 @@ class ConfirmFactorView(FactorView): return redirect("panel:factor_enroll") return super().get(request, *args, **kwargs) + @sensitive_variables("context", "configuration_url") def get_context_data(self, **kwargs): device = self.current_device() context = {} if isinstance(device, EmailDevice): context["email_factor"] = True elif device: + configuration_url = device.config_url + qr = qrcode.make( + configuration_url, image_factory=SvgPathFillImage + ).to_string(encoding="utf-8") context = { "setup_key": base64.b32encode(device.bin_key).decode(), - "configuration_url": device.config_url, + "configuration_url": configuration_url, + "qr_image": base64.b64encode(qr).decode("ascii"), } return super().get_context_data(**context, **kwargs) @@ -318,5 +335,5 @@ class ConfirmFactorView(FactorView): return render( self.request, "hosting/factor_recovery_codes.html", - {"codes": codes, "destination": destination}, + {"recovery_codes": "\n".join(codes), "destination": destination}, ) diff --git a/src/coulobre/hosting/static/hosting/panel.css b/src/coulobre/hosting/static/hosting/panel.css index 91767ad..c5e557b 100644 --- a/src/coulobre/hosting/static/hosting/panel.css +++ b/src/coulobre/hosting/static/hosting/panel.css @@ -701,6 +701,24 @@ dd { font-size: 2rem; } +.authenticator-setup { + display: grid; + gap: 0.55rem; + margin-block: 0.75rem 1rem; +} + +.authenticator-qr { + max-width: 100%; + height: auto; + justify-self: center; +} + +.recovery-codes { + width: 100%; + margin-block: 0.75rem; + font-family: monospace; +} + @media (max-width: 780px) { .panel { display: block; diff --git a/src/coulobre/hosting/templates/hosting/authentication_card.html b/src/coulobre/hosting/templates/hosting/authentication_card.html index ba36a51..365d15c 100644 --- a/src/coulobre/hosting/templates/hosting/authentication_card.html +++ b/src/coulobre/hosting/templates/hosting/authentication_card.html @@ -1,9 +1,22 @@ +{% load static %}

{{ heading }}

{% if instructions %}

{{ instructions }}

{% endif %} {% if code_notice %}

{{ code_notice }}

{% endif %} {% if configuration_url %} -

Open in an authenticator app or add a time-based account using this key.

-

{{ setup_key }}

+
+

Scan this QR code with your authenticator app.

+ Authenticator setup QR code +

Open in an authenticator app

+
+ Enter the setup key manually +
+ + +
+

+
+
+ {% endif %} {% if form %}
diff --git a/src/coulobre/hosting/templates/hosting/factor_recovery_codes.html b/src/coulobre/hosting/templates/hosting/factor_recovery_codes.html index 011066c..017b03e 100644 --- a/src/coulobre/hosting/templates/hosting/factor_recovery_codes.html +++ b/src/coulobre/hosting/templates/hosting/factor_recovery_codes.html @@ -1,12 +1,16 @@ {% extends "hosting/base.html" %} +{% load static %} {% block title %}Save your recovery codes ยท Coulobre Panel{% endblock %} {% block content %}

Save your recovery codes

Keep these codes somewhere safe. Each code lets you sign in once if you lose access to your second factor.

-
    - {% for code in codes %}
  • {{ code }}
  • {% endfor %} -
- Continue to the panel + +
+ + Continue to the panel +
+

+
{% endblock %} diff --git a/src/coulobre/hosting/test_panel_authentication.py b/src/coulobre/hosting/test_panel_authentication.py index 35bb6a9..20047fe 100644 --- a/src/coulobre/hosting/test_panel_authentication.py +++ b/src/coulobre/hosting/test_panel_authentication.py @@ -77,7 +77,25 @@ class PanelAuthenticationTests(TestCase): ) self.assertContains(response, "Save your recovery codes") self.assertIn("no-store", response.headers["Cache-Control"]) - return response.context["codes"] + self.assertContains(response, "Copy recovery codes") + return response.context["recovery_codes"].splitlines() + + @override_settings(EMAIL_HOST="") + def test_authenticator_only_opens_qr_setup_directly(self): + confirm = reverse("panel:factor_confirm") + self.assertRedirects(self.login(self.operator), confirm) + + response = self.client.get(confirm) + self.assertContains(response, "data:image/svg+xml;base64,") + self.assertContains(response, "Enter the setup key manually") + self.assertIn("no-store", response.headers["Cache-Control"]) + device = TOTPDevice.objects.get(user=self.operator) + + self.assertRedirects( + self.client.get(reverse("panel:factor_enroll")), confirm + ) + self.assertEqual(TOTPDevice.objects.get(user=self.operator), device) + self.assertRedirects(Client().get(confirm), reverse("panel:login")) def test_first_password_login_enrolls_and_later_logins_require_a_factor( self,