Identify the panel in authenticator app entries #227
No files matched your search
@@ -3,6 +3,8 @@ COULOBRE_ALLOWED_HOSTS=panel.example.net
|
|||||||
COULOBRE_HTTPS_PROXY=1
|
COULOBRE_HTTPS_PROXY=1
|
||||||
COULOBRE_REQUIRE_PRIVILEGED_MFA=1
|
COULOBRE_REQUIRE_PRIVILEGED_MFA=1
|
||||||
COULOBRE_TRUST_APACHE_CLIENT_IP=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.
|
# Set an SMTP relay for email verification and customer password resets.
|
||||||
# COULOBRE_SMTP_HOST=smtp.example.net
|
# COULOBRE_SMTP_HOST=smtp.example.net
|
||||||
# COULOBRE_SMTP_PORT=587
|
# COULOBRE_SMTP_PORT=587
|
||||||
|
|||||||
@@ -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
|
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.
|
||||||
|
|
||||||
|
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
|
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
|
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
|
the address assigned to the account at enrollment. These codes expire in
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from secrets import token_urlsafe
|
from secrets import token_urlsafe
|
||||||
|
from urllib.parse import parse_qs, unquote, urlsplit
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.core import mail
|
from django.core import mail
|
||||||
@@ -175,6 +176,7 @@ class AccountSecurityTests(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertNotIn("_auth_user_id", fresh.session)
|
self.assertNotIn("_auth_user_id", fresh.session)
|
||||||
|
|
||||||
|
@override_settings(AUTHENTICATOR_NAME="Hosting North")
|
||||||
def test_last_recovery_code_can_authorize_authenticator_replacement(self):
|
def test_last_recovery_code_can_authorize_authenticator_replacement(self):
|
||||||
original = TOTPDevice.objects.create(user=self.user, name=DEVICE_NAME)
|
original = TOTPDevice.objects.create(user=self.user, name=DEVICE_NAME)
|
||||||
code = "b" * 20
|
code = "b" * 20
|
||||||
@@ -197,6 +199,11 @@ class AccountSecurityTests(TestCase):
|
|||||||
self.client.post(start, {"password": PASSWORD}), confirm
|
self.client.post(start, {"password": PASSWORD}), confirm
|
||||||
)
|
)
|
||||||
self.assertTrue(TOTPDevice.objects.filter(pk=original.pk).exists())
|
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)
|
pending = TOTPDevice.objects.get(user=self.user, confirmed=False)
|
||||||
token = f"{totp(pending.bin_key):06d}"
|
token = f"{totp(pending.bin_key):06d}"
|
||||||
self.assertContains(
|
self.assertContains(
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from datetime import datetime, timedelta
|
|||||||
from io import StringIO
|
from io import StringIO
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
from unittest.mock import patch
|
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 import SESSION_KEY, get_user_model
|
||||||
from django.contrib.auth.tokens import default_token_generator
|
from django.contrib.auth.tokens import default_token_generator
|
||||||
@@ -94,7 +94,11 @@ class PanelAuthenticationTests(TestCase):
|
|||||||
self.assertContains(response, "Copy recovery codes")
|
self.assertContains(response, "Copy recovery codes")
|
||||||
return response.context["recovery_codes"].splitlines()
|
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):
|
def test_authenticator_only_opens_qr_setup_directly(self):
|
||||||
confirm = reverse("panel:factor_confirm")
|
confirm = reverse("panel:factor_confirm")
|
||||||
self.assertRedirects(self.login(self.operator), confirm)
|
self.assertRedirects(self.login(self.operator), confirm)
|
||||||
@@ -104,6 +108,10 @@ class PanelAuthenticationTests(TestCase):
|
|||||||
self.assertContains(response, "Enter the setup key manually")
|
self.assertContains(response, "Enter the setup key manually")
|
||||||
self.assertIn("no-store", response.headers["Cache-Control"])
|
self.assertIn("no-store", response.headers["Cache-Control"])
|
||||||
device = TOTPDevice.objects.get(user=self.operator)
|
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.assertRedirects(
|
||||||
self.client.get(reverse("panel:factor_enroll")), confirm
|
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.assertEqual(TOTPDevice.objects.get(user=self.operator), device)
|
||||||
self.assertRedirects(Client().get(confirm), reverse("panel:login"))
|
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):
|
def test_reissued_welcome_link_is_single_use(self):
|
||||||
user = get_user_model().objects.create_user(
|
user = get_user_model().objects.create_user(
|
||||||
"invitee", email="invitee@example.test"
|
"invitee", email="invitee@example.test"
|
||||||
@@ -335,7 +358,8 @@ class PanelAuthenticationTests(TestCase):
|
|||||||
)
|
)
|
||||||
self.assertNotIn(SESSION_KEY, fresh_browser.session)
|
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)
|
self.enroll_authenticator(self.operator)
|
||||||
device = TOTPDevice.objects.get(user=self.operator)
|
device = TOTPDevice.objects.get(user=self.operator)
|
||||||
code_generator = TOTP(device.bin_key)
|
code_generator = TOTP(device.bin_key)
|
||||||
@@ -344,9 +368,12 @@ class PanelAuthenticationTests(TestCase):
|
|||||||
verify = reverse("panel:factor_verify")
|
verify = reverse("panel:factor_verify")
|
||||||
self.assertRedirects(self.login(self.operator, browser), verify)
|
self.assertRedirects(self.login(self.operator, browser), verify)
|
||||||
data = {"code": f"{code_generator.token():06d}"}
|
data = {"code": f"{code_generator.token():06d}"}
|
||||||
with patch(
|
with (
|
||||||
|
self.settings(AUTHENTICATOR_NAME="Renamed panel"),
|
||||||
|
patch(
|
||||||
"django_otp.plugins.otp_totp.models.time",
|
"django_otp.plugins.otp_totp.models.time",
|
||||||
SimpleNamespace(time=lambda: code_generator.time),
|
SimpleNamespace(time=lambda: code_generator.time),
|
||||||
|
),
|
||||||
):
|
):
|
||||||
self.assertRedirects(
|
self.assertRedirects(
|
||||||
browser.post(verify, data), reverse("panel:accounts")
|
browser.post(verify, data), reverse("panel:accounts")
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ from ipaddress import ip_address
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from urllib.parse import urlsplit
|
from urllib.parse import urlsplit
|
||||||
|
|
||||||
|
from django.conf import settings as django_settings
|
||||||
from django.core.exceptions import ImproperlyConfigured
|
from django.core.exceptions import ImproperlyConfigured
|
||||||
|
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
@@ -42,6 +43,24 @@ def ip_addresses_env(name, limit):
|
|||||||
return addresses
|
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")
|
SECRET_KEY = required_env("COULOBRE_SECRET_KEY")
|
||||||
HTTPS_PROXY = os.environ.get("COULOBRE_HTTPS_PROXY") == "1"
|
HTTPS_PROXY = os.environ.get("COULOBRE_HTTPS_PROXY") == "1"
|
||||||
REQUIRE_PRIVILEGED_MFA = (
|
REQUIRE_PRIVILEGED_MFA = (
|
||||||
@@ -50,7 +69,8 @@ REQUIRE_PRIVILEGED_MFA = (
|
|||||||
)
|
)
|
||||||
== "1"
|
== "1"
|
||||||
)
|
)
|
||||||
OTP_TOTP_ISSUER = "Coulobre Panel"
|
AUTHENTICATOR_NAME = os.environ.get("COULOBRE_AUTHENTICATOR_NAME", "").strip()
|
||||||
|
OTP_TOTP_ISSUER = authenticator_issuer
|
||||||
TRUST_APACHE_CLIENT_IP = (
|
TRUST_APACHE_CLIENT_IP = (
|
||||||
os.environ.get("COULOBRE_TRUST_APACHE_CLIENT_IP") == "1"
|
os.environ.get("COULOBRE_TRUST_APACHE_CLIENT_IP") == "1"
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in new issue
Block a user