From 2944c6e4a10f94c6bff4d616f8e2f3a5f77c0169 Mon Sep 17 00:00:00 2001 From: Peter van Arkel Date: Tue, 1 Dec 2026 12:43:28 +0100 Subject: [PATCH] Include the panel identity in authenticator entries --- deploy/panel.env.example | 2 + docs/deployment.md | 8 ++++ src/coulobre/hosting/test_account_security.py | 7 ++++ .../hosting/test_panel_authentication.py | 39 ++++++++++++++++--- src/coulobre/settings.py | 22 ++++++++++- 5 files changed, 71 insertions(+), 7 deletions(-) diff --git a/deploy/panel.env.example b/deploy/panel.env.example index 1d3afc0..e5d4e21 100644 --- a/deploy/panel.env.example +++ b/deploy/panel.env.example @@ -3,6 +3,8 @@ COULOBRE_ALLOWED_HOSTS=panel.example.net COULOBRE_HTTPS_PROXY=1 COULOBRE_REQUIRE_PRIVILEGED_MFA=1 COULOBRE_TRUST_APACHE_CLIENT_IP=1 +# Override the hostname shown in new authenticator app entries. +# COULOBRE_AUTHENTICATOR_NAME="Hosting North" # Set an SMTP relay for email verification and customer password resets. # COULOBRE_SMTP_HOST=smtp.example.net # COULOBRE_SMTP_PORT=587 diff --git a/docs/deployment.md b/docs/deployment.md index 6a8d800..3e9b0b1 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -571,6 +571,14 @@ 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. +New authenticator entries show the username and the panel name, for example +**Coulobre Panel — srv1.example.net**. The name uses the public hostname in +`COULOBRE_PASSWORD_RESET_ORIGIN` or the first exact DNS hostname in +`COULOBRE_ALLOWED_HOSTS`. Set `COULOBRE_AUTHENTICATOR_NAME` in `panel.env` and +restart `coulobre-panel` to use a custom name. Choose a distinct name for each +panel when using this override. Existing verification and recovery codes +continue to work. Apps may retain the label saved during enrollment. + If an SMTP relay is configured and the account already has a valid email address, the user can instead register one-time email codes. The panel uses the address assigned to the account at enrollment. These codes expire in diff --git a/src/coulobre/hosting/test_account_security.py b/src/coulobre/hosting/test_account_security.py index 1e128b8..ffeb195 100644 --- a/src/coulobre/hosting/test_account_security.py +++ b/src/coulobre/hosting/test_account_security.py @@ -1,6 +1,7 @@ import re from datetime import timedelta from secrets import token_urlsafe +from urllib.parse import parse_qs, unquote, urlsplit from django.contrib.auth import get_user_model from django.core import mail @@ -175,6 +176,7 @@ class AccountSecurityTests(TestCase): ) self.assertNotIn("_auth_user_id", fresh.session) + @override_settings(AUTHENTICATOR_NAME="Hosting North") def test_last_recovery_code_can_authorize_authenticator_replacement(self): original = TOTPDevice.objects.create(user=self.user, name=DEVICE_NAME) code = "b" * 20 @@ -197,6 +199,11 @@ class AccountSecurityTests(TestCase): self.client.post(start, {"password": PASSWORD}), confirm ) self.assertTrue(TOTPDevice.objects.filter(pk=original.pk).exists()) + response = self.client.get(confirm) + setup = urlsplit(response.context["configuration_url"]) + issuer = "Coulobre Panel — Hosting North" + self.assertEqual(parse_qs(setup.query)["issuer"], [issuer]) + self.assertEqual(unquote(setup.path), f"/{issuer}:customer") pending = TOTPDevice.objects.get(user=self.user, confirmed=False) token = f"{totp(pending.bin_key):06d}" self.assertContains( diff --git a/src/coulobre/hosting/test_panel_authentication.py b/src/coulobre/hosting/test_panel_authentication.py index 8633f5b..5c2fdd9 100644 --- a/src/coulobre/hosting/test_panel_authentication.py +++ b/src/coulobre/hosting/test_panel_authentication.py @@ -2,7 +2,7 @@ from datetime import datetime, timedelta from io import StringIO from types import SimpleNamespace from unittest.mock import patch -from urllib.parse import urlsplit +from urllib.parse import parse_qs, unquote, urlsplit from django.contrib.auth import SESSION_KEY, get_user_model from django.contrib.auth.tokens import default_token_generator @@ -94,7 +94,11 @@ class PanelAuthenticationTests(TestCase): self.assertContains(response, "Copy recovery codes") return response.context["recovery_codes"].splitlines() - @override_settings(EMAIL_HOST="") + @override_settings( + EMAIL_HOST="", + AUTHENTICATOR_NAME="", + PASSWORD_RESET_ORIGIN="https://srv1.example.test:8443", + ) def test_authenticator_only_opens_qr_setup_directly(self): confirm = reverse("panel:factor_confirm") self.assertRedirects(self.login(self.operator), confirm) @@ -104,6 +108,10 @@ class PanelAuthenticationTests(TestCase): self.assertContains(response, "Enter the setup key manually") self.assertIn("no-store", response.headers["Cache-Control"]) device = TOTPDevice.objects.get(user=self.operator) + setup = urlsplit(response.context["configuration_url"]) + issuer = "Coulobre Panel — srv1.example.test" + self.assertEqual(parse_qs(setup.query)["issuer"], [issuer]) + self.assertEqual(unquote(setup.path), f"/{issuer}:operator") self.assertRedirects( self.client.get(reverse("panel:factor_enroll")), confirm @@ -111,6 +119,21 @@ class PanelAuthenticationTests(TestCase): self.assertEqual(TOTPDevice.objects.get(user=self.operator), device) self.assertRedirects(Client().get(confirm), reverse("panel:login")) + @override_settings( + AUTHENTICATOR_NAME="", + PASSWORD_RESET_ORIGIN="", + ALLOWED_HOSTS=[".example.test", "localhost", "srv2.example.test"], + ) + def test_authenticator_issuer_falls_back_to_an_exact_configured_host(self): + device = TOTPDevice(user=self.operator) + issuer = parse_qs(urlsplit(device.config_url).query)["issuer"] + self.assertEqual(issuer, ["Coulobre Panel — srv2.example.test"]) + with self.settings( + ALLOWED_HOSTS=[".example.test", "localhost", "127.0.0.1", "[::1]"] + ): + issuer = parse_qs(urlsplit(device.config_url).query)["issuer"] + self.assertEqual(issuer, ["Coulobre Panel"]) + def test_reissued_welcome_link_is_single_use(self): user = get_user_model().objects.create_user( "invitee", email="invitee@example.test" @@ -335,7 +358,8 @@ class PanelAuthenticationTests(TestCase): ) self.assertNotIn(SESSION_KEY, fresh_browser.session) - def test_later_sign_in_accepts_a_fresh_authenticator_code(self): + @override_settings(AUTHENTICATOR_NAME="") + def test_existing_authenticator_works_after_the_issuer_changes(self): self.enroll_authenticator(self.operator) device = TOTPDevice.objects.get(user=self.operator) code_generator = TOTP(device.bin_key) @@ -344,9 +368,12 @@ class PanelAuthenticationTests(TestCase): verify = reverse("panel:factor_verify") self.assertRedirects(self.login(self.operator, browser), verify) data = {"code": f"{code_generator.token():06d}"} - with patch( - "django_otp.plugins.otp_totp.models.time", - SimpleNamespace(time=lambda: code_generator.time), + with ( + self.settings(AUTHENTICATOR_NAME="Renamed panel"), + patch( + "django_otp.plugins.otp_totp.models.time", + SimpleNamespace(time=lambda: code_generator.time), + ), ): self.assertRedirects( browser.post(verify, data), reverse("panel:accounts") diff --git a/src/coulobre/settings.py b/src/coulobre/settings.py index 8056035..4aab8e2 100644 --- a/src/coulobre/settings.py +++ b/src/coulobre/settings.py @@ -5,6 +5,7 @@ from ipaddress import ip_address from pathlib import Path from urllib.parse import urlsplit +from django.conf import settings as django_settings from django.core.exceptions import ImproperlyConfigured from .domain_names import canonical_domain @@ -42,6 +43,24 @@ def ip_addresses_env(name, limit): return addresses +def authenticator_issuer(_device): + name = django_settings.AUTHENTICATOR_NAME + if not name: + origin = urlsplit(django_settings.PASSWORD_RESET_ORIGIN) + name = (origin.hostname or "").rstrip(".") + if not name: + for host in django_settings.ALLOWED_HOSTS: + try: + name = canonical_domain(host) + except ValueError: + continue + break + if name: + # A colon separates the issuer from the account name. + return f"Coulobre Panel — {name.replace(':', '-')}" + return "Coulobre Panel" + + SECRET_KEY = required_env("COULOBRE_SECRET_KEY") HTTPS_PROXY = os.environ.get("COULOBRE_HTTPS_PROXY") == "1" REQUIRE_PRIVILEGED_MFA = ( @@ -50,7 +69,8 @@ REQUIRE_PRIVILEGED_MFA = ( ) == "1" ) -OTP_TOTP_ISSUER = "Coulobre Panel" +AUTHENTICATOR_NAME = os.environ.get("COULOBRE_AUTHENTICATOR_NAME", "").strip() +OTP_TOTP_ISSUER = authenticator_issuer TRUST_APACHE_CLIENT_IP = ( os.environ.get("COULOBRE_TRUST_APACHE_CLIENT_IP") == "1" ) -- 2.43.0