Skip to content

Commit

Permalink
🔧 Always use requests.hostname for 2FA app title
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Aug 29, 2024
1 parent cecc40e commit 4a27285
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/objecttypes/accounts/tests/test_2fa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.contrib.sites.models import Site
from django.test import RequestFactory, TestCase, override_settings
from django.urls import resolve


@override_settings(ALLOWED_HOSTS=["some-domain.local"], DISABLE_2FA=False)
class TwoFactorQRGeneratorTestCase(TestCase):
def test_qr_code_generator_does_not_use_sites_framework(self):
"""
Regression test for https://github.com/maykinmedia/open-api-framework/issues/40
Testing the actual QR code output is too much of a hassle, so instead retrieve
the view class based on the URL and check if `get_issuer` behaves as expected
"""
site = Site.objects.get_current()
site.domain = "testserver"
site.save()

qr_generator_view_class = resolve("/admin/mfa/qrcode/").func.view_class
issuer = qr_generator_view_class(
request=RequestFactory().get("/", headers={"Host": "some-domain.local"})
).get_issuer()

self.assertEqual(issuer, "some-domain.local")
8 changes: 8 additions & 0 deletions src/objecttypes/accounts/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib.sites.requests import RequestSite

from maykin_2fa.views import QRGeneratorView as _QRGeneratorView


class QRGeneratorView(_QRGeneratorView):
def get_issuer(self):
return RequestSite(self.request).name
7 changes: 7 additions & 0 deletions src/objecttypes/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from mozilla_django_oidc_db.views import AdminLoginFailure
from rest_framework.settings import api_settings

from objecttypes.accounts.views import QRGeneratorView

handler500 = "objecttypes.utils.views.server_error"
admin.site.site_header = "objecttypes admin"
admin.site.site_title = "objecttypes admin"
Expand All @@ -31,6 +33,11 @@
name="password_reset_done",
),
path("admin/login/failure/", AdminLoginFailure.as_view(), name="admin-oidc-error"),
# See https://github.com/maykinmedia/open-api-framework/issues/40
# and https://github.com/maykinmedia/open-api-framework/issues/59
# Temporary workaround to remove the dependency on `django.contrib.sites` when
# generating the app label for 2FA. This should be removed once `sites` are removed
path("admin/mfa/qrcode/", QRGeneratorView.as_view(), name="qr"),
path("admin/", include((maykin_2fa_urlpatterns, "maykin_2fa"))),
path("admin/", include((webauthn_urlpatterns, "two_factor"))),
path("admin/", admin.site.urls),
Expand Down

0 comments on commit 4a27285

Please sign in to comment.