Include the panel identity in authenticator entries
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Canceled after 0s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Canceled after 0s

This commit is contained in:
parkel committed 2026-12-01 12:43:28 +01:00
1 parent 6f2d7cd375
commit 2944c6e4a1
5 files changed
+71 -7

No files matched your search

@@ -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(
@@ -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")
+21 -1
View File
@@ -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"
)