From 43cfba6f6a2016693ea1856f0320878ad60160f3 Mon Sep 17 00:00:00 2001 From: jamesstottmoj Date: Tue, 9 Apr 2024 09:16:37 +0100 Subject: [PATCH] ensure redirect to index if no justice id regardless of route hit --- controlpanel/frontend/urls.py | 1 + controlpanel/frontend/views/__init__.py | 2 +- controlpanel/frontend/views/auth.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/controlpanel/frontend/urls.py b/controlpanel/frontend/urls.py index 24cdc0521..1f3dbc5f2 100644 --- a/controlpanel/frontend/urls.py +++ b/controlpanel/frontend/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ path("", views.IndexView.as_view(), name="index"), path("oidc/entraid/auth/", views.EntraIdAuthView.as_view(), name="entraid-auth"), + path("oidc/callback/", views.EntraOIDCAuthenticationCallbackView.as_view(), name="entra_callback"), path("oidc/logout/", views.LogoutView.as_view(), name="oidc_logout"), path("datasources/", views.AdminBucketList.as_view(), name="list-all-datasources"), path( diff --git a/controlpanel/frontend/views/__init__.py b/controlpanel/frontend/views/__init__.py index 4876fc482..adcb2843a 100644 --- a/controlpanel/frontend/views/__init__.py +++ b/controlpanel/frontend/views/__init__.py @@ -8,7 +8,7 @@ # First-party/Local from controlpanel.frontend.views.accessibility import Accessibility -from controlpanel.frontend.views.auth import EntraIdAuthView +from controlpanel.frontend.views.auth import EntraIdAuthView, EntraOIDCAuthenticationCallbackView # isort: off from controlpanel.frontend.views.app import ( diff --git a/controlpanel/frontend/views/auth.py b/controlpanel/frontend/views/auth.py index 8313728fb..50a6345e8 100644 --- a/controlpanel/frontend/views/auth.py +++ b/controlpanel/frontend/views/auth.py @@ -9,6 +9,7 @@ from django.http import HttpResponseRedirect, Http404 from django.urls import reverse from django.views import View +from mozilla_django_oidc.views import OIDCAuthenticationCallbackView # First-party/Local from controlpanel.oidc import OIDCLoginRequiredMixin, oauth @@ -62,3 +63,17 @@ def update_user(self, token): email = token["userinfo"]["email"] self.request.user.justice_email = email self.request.user.save() + + +class EntraOIDCAuthenticationCallbackView(OIDCAuthenticationCallbackView): + """ + This view is used to redirect to the index page if the user has not + authenticated with their justice email. + """ + def get(self, request): + response = super().get(request) + + if self.user.justice_email is not None: + return response + + return HttpResponseRedirect(reverse("index"))