Streamline authenticator enrollment and recovery code copying

This commit is contained in:
parkel committed 2026-11-22 21:21:39 +01:00
1 parent d7c8c66220
commit e68e54288f
7 files changed
+84 -12

No files matched your search

+4 -3
View File
@@ -281,8 +281,9 @@ restarting the panel, worker, and scheduler.
A privileged user signs in with a password to start first-time setup. The 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 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 authenticator app, scan the QR code, and confirm a code from the app. The
from the app. Use the authenticator's code at future sign-ins. Keep 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. panel host and authenticator device clocks synchronized.
If an SMTP relay is configured and the account already has a valid email 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 five minutes and can be requested once a minute. An authenticator is the
preferred option for privileged accounts. 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 can be entered after the account password. If the user loses the factor
and all recovery codes, verify their identity and issue a replacement and all recovery codes, verify their identity and issue a replacement
enrollment code on the panel host. enrollment code on the panel host.
+1
View File
@@ -16,6 +16,7 @@ dependencies = [
"cryptography>=45", "cryptography>=45",
"dnspython>=2.7,<3", "dnspython>=2.7,<3",
"idna>=3,<4", "idna>=3,<4",
"qrcode>=8.2,<9",
] ]
[project.scripts] [project.scripts]
+20 -3
View File
@@ -1,5 +1,6 @@
import base64 import base64
import qrcode
from django import forms from django import forms
from django.contrib.auth import logout from django.contrib.auth import logout
from django.contrib.auth.views import LoginView, redirect_to_login 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.views.generic import FormView
from django_otp.plugins.otp_email.models import EmailDevice from django_otp.plugins.otp_email.models import EmailDevice
from qrcode.image.svg import SvgPathFillImage
from .panel_factors import ( from .panel_factors import (
SETUP_SESSION, SETUP_SESSION,
@@ -94,6 +96,11 @@ class PanelLoginView(LoginView):
return redirect("panel:factor_verify") return redirect("panel:factor_verify")
super().form_valid(form) super().form_valid(form)
start_initial_setup(self.request, destination) 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") return redirect("panel:factor_enroll")
@@ -244,6 +251,10 @@ class EnrollFactorView(FactorView):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
if confirmed_device(self.user): if confirmed_device(self.user):
return redirect("panel:factor_verify") 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) return super().get(request, *args, **kwargs)
def get_form_kwargs(self): def get_form_kwargs(self):
@@ -277,7 +288,7 @@ class EnrollFactorView(FactorView):
class ConfirmFactorView(FactorView): class ConfirmFactorView(FactorView):
heading = "Confirm your second factor" heading = "Confirm your second factor"
instructions = "Enter a code to complete setup." instructions = "Enter a verification code to complete setup."
submit_label = "Confirm" submit_label = "Confirm"
form_class = ConfirmFactorForm form_class = ConfirmFactorForm
@@ -291,15 +302,21 @@ class ConfirmFactorView(FactorView):
return redirect("panel:factor_enroll") return redirect("panel:factor_enroll")
return super().get(request, *args, **kwargs) return super().get(request, *args, **kwargs)
@sensitive_variables("context", "configuration_url")
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
device = self.current_device() device = self.current_device()
context = {} context = {}
if isinstance(device, EmailDevice): if isinstance(device, EmailDevice):
context["email_factor"] = True context["email_factor"] = True
elif device: elif device:
configuration_url = device.config_url
qr = qrcode.make(
configuration_url, image_factory=SvgPathFillImage
).to_string(encoding="utf-8")
context = { context = {
"setup_key": base64.b32encode(device.bin_key).decode(), "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) return super().get_context_data(**context, **kwargs)
@@ -318,5 +335,5 @@ class ConfirmFactorView(FactorView):
return render( return render(
self.request, self.request,
"hosting/factor_recovery_codes.html", "hosting/factor_recovery_codes.html",
{"codes": codes, "destination": destination}, {"recovery_codes": "\n".join(codes), "destination": destination},
) )
@@ -701,6 +701,24 @@ dd {
font-size: 2rem; 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) { @media (max-width: 780px) {
.panel { .panel {
display: block; display: block;
@@ -1,9 +1,22 @@
{% load static %}
<h1>{{ heading }}</h1> <h1>{{ heading }}</h1>
{% if instructions %}<p>{{ instructions }}</p>{% endif %} {% if instructions %}<p>{{ instructions }}</p>{% endif %}
{% if code_notice %}<p role="status">{{ code_notice }}</p>{% endif %} {% if code_notice %}<p role="status">{{ code_notice }}</p>{% endif %}
{% if configuration_url %} {% if configuration_url %}
<p><a href="{{ configuration_url }}" rel="noreferrer">Open in an authenticator app</a> or add a time-based account using this key.</p> <div class="authenticator-setup">
<p><code>{{ setup_key }}</code></p> <p>Scan this QR code with your authenticator app.</p>
<img class="authenticator-qr" src="data:image/svg+xml;base64,{{ qr_image }}" width="192" height="192" alt="Authenticator setup QR code">
<p><a href="{{ configuration_url }}" rel="noreferrer">Open in an authenticator app</a></p>
<details>
<summary>Enter the setup key manually</summary>
<div class="form-actions">
<input id="setup-key" type="text" readonly autocomplete="off" spellcheck="false" aria-label="Setup key" value="{{ setup_key }}" data-secret>
<button class="button secondary" type="button" hidden data-copy-field="setup-key">Copy setup key</button>
</div>
<p id="copy-field-status" role="status"></p>
</details>
</div>
<script src="{% static 'hosting/copy-field.js' %}" defer></script>
{% endif %} {% endif %}
{% if form %} {% if form %}
<form method="post" class="editor"> <form method="post" class="editor">
@@ -1,12 +1,16 @@
{% extends "hosting/base.html" %} {% extends "hosting/base.html" %}
{% load static %}
{% block title %}Save your recovery codes · Coulobre Panel{% endblock %} {% block title %}Save your recovery codes · Coulobre Panel{% endblock %}
{% block content %} {% block content %}
<header class="page-heading"><h1>Save your recovery codes</h1></header> <header class="page-heading"><h1>Save your recovery codes</h1></header>
<section class="surface editor-surface"> <section class="surface editor-surface">
<p>Keep these codes somewhere safe. Each code lets you sign in once if you lose access to your second factor.</p> <p>Keep these codes somewhere safe. Each code lets you sign in once if you lose access to your second factor.</p>
<ul> <textarea class="recovery-codes" id="recovery-codes" rows="10" readonly spellcheck="false" aria-label="Recovery codes" data-secret>{{ recovery_codes }}</textarea>
{% for code in codes %}<li><code>{{ code }}</code></li>{% endfor %} <div class="form-actions">
</ul> <button class="button secondary" type="button" hidden data-copy-field="recovery-codes">Copy recovery codes</button>
<a class="button primary" href="{{ destination }}">Continue to the panel</a> <a class="button primary" href="{{ destination }}">Continue to the panel</a>
</div>
<p id="copy-field-status" role="status"></p>
<script src="{% static 'hosting/copy-field.js' %}" defer></script>
</section> </section>
{% endblock %} {% endblock %}
@@ -77,7 +77,25 @@ class PanelAuthenticationTests(TestCase):
) )
self.assertContains(response, "Save your recovery codes") self.assertContains(response, "Save your recovery codes")
self.assertIn("no-store", response.headers["Cache-Control"]) 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( def test_first_password_login_enrolls_and_later_logins_require_a_factor(
self, self,