Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 Always use requests.hostname for 2FA app title #185

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/nrc/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/nrc/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/nrc/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from mozilla_django_oidc_db.views import AdminLoginFailure
from vng_api_common.views import ViewConfigView

from nrc.accounts.views import QRGeneratorView

handler500 = "nrc.utils.views.server_error"

admin.site.enable_nav_sidebar = False
Expand All @@ -21,6 +23,11 @@
urlpatterns = [
path("admin/login/failure/", AdminLoginFailure.as_view(), name="admin-oidc-error"),
# 2fa
# 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((urlpatterns, "maykin_2fa"))),
path("admin/", include((webauthn_urlpatterns, "two_factor"))),
path("admin/", admin.site.urls),
Expand Down
Loading