Skip to content

Commit ef1a7b6

Browse files
committed
🔧 Always use requests.hostname for 2FA app title
issue: maykinmedia/open-api-framework#40
1 parent ac4c0aa commit ef1a7b6

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/nrc/accounts/tests/test_2fa.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from django.contrib.sites.models import Site
2+
from django.test import RequestFactory, TestCase, override_settings
3+
from django.urls import resolve
4+
5+
6+
@override_settings(ALLOWED_HOSTS=["some-domain.local"], DISABLE_2FA=False)
7+
class TwoFactorQRGeneratorTestCase(TestCase):
8+
def test_qr_code_generator_does_not_use_sites_framework(self):
9+
"""
10+
Regression test for https://github.com/maykinmedia/open-api-framework/issues/40
11+
Testing the actual QR code output is too much of a hassle, so instead retrieve
12+
the view class based on the URL and check if `get_issuer` behaves as expected
13+
"""
14+
site = Site.objects.get_current()
15+
site.domain = "testserver"
16+
site.save()
17+
18+
qr_generator_view_class = resolve("/admin/mfa/qrcode/").func.view_class
19+
issuer = qr_generator_view_class(
20+
request=RequestFactory().get("/", headers={"Host": "some-domain.local"})
21+
).get_issuer()
22+
23+
self.assertEqual(issuer, "some-domain.local")

src/nrc/accounts/views.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.contrib.sites.requests import RequestSite
2+
3+
from maykin_2fa.views import QRGeneratorView as _QRGeneratorView
4+
5+
6+
class QRGeneratorView(_QRGeneratorView):
7+
def get_issuer(self):
8+
return RequestSite(self.request).name

src/nrc/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from mozilla_django_oidc_db.views import AdminLoginFailure
1212
from vng_api_common.views import ViewConfigView
1313

14+
from nrc.accounts.views import QRGeneratorView
15+
1416
handler500 = "nrc.utils.views.server_error"
1517

1618
admin.site.enable_nav_sidebar = False
@@ -21,6 +23,11 @@
2123
urlpatterns = [
2224
path("admin/login/failure/", AdminLoginFailure.as_view(), name="admin-oidc-error"),
2325
# 2fa
26+
# See https://github.com/maykinmedia/open-api-framework/issues/40
27+
# and https://github.com/maykinmedia/open-api-framework/issues/59
28+
# Temporary workaround to remove the dependency on `django.contrib.sites` when
29+
# generating the app label for 2FA. This should be removed once `sites` are removed
30+
path("admin/mfa/qrcode/", QRGeneratorView.as_view(), name="qr"),
2431
path("admin/", include((urlpatterns, "maykin_2fa"))),
2532
path("admin/", include((webauthn_urlpatterns, "two_factor"))),
2633
path("admin/", admin.site.urls),

0 commit comments

Comments
 (0)