From 8e95a0164722c625843fd5a7c1dea3e9477f1679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Wed, 10 Jun 2020 22:22:13 +0200 Subject: [PATCH] Redirect after a CAS logout --- authens/tests/test_views.py | 8 +++++++- authens/views.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/authens/tests/test_views.py b/authens/tests/test_views.py index 25dddb8..94d27c2 100644 --- a/authens/tests/test_views.py +++ b/authens/tests/test_views.py @@ -1,9 +1,11 @@ from unittest.mock import patch +from urllib.parse import quote as urlquote from django.conf import settings from django.contrib.auth import get_user_model from django.contrib.sessions.models import Session from django.test import Client, TestCase +from django.urls import reverse from authens.models import CASAccount from authens.tests.cas_utils import FakeCASClient @@ -44,7 +46,11 @@ class TestLogoutView(TestCase): response = client.get("/authens/logout") self.assertEqual(Session.objects.count(), 0) # User is logged out… self.assertRedirects( # … and redirected to the CAS logout page. - response, "https://cas.eleves.ens.fr/logout", fetch_redirect_response=False + response, + "https://cas.eleves.ens.fr/logout?service={}".format( + urlquote("http://testserver" + reverse("authens:login")) + ), + fetch_redirect_response=False, ) def test_regular_logout_on_cas_account(self): diff --git a/authens/views.py b/authens/views.py index da17c48..194814f 100644 --- a/authens/views.py +++ b/authens/views.py @@ -1,3 +1,5 @@ +from urllib.parse import urlunparse + from django.conf import settings from django.contrib import auth from django.contrib.auth import views as auth_views @@ -93,8 +95,11 @@ class LogoutView(auth_views.LogoutView): self.cas_connected = False def get_next_page(self): + next_page = super().get_next_page() if self.cas_connected: cas_client = get_cas_client(self.request) - return cas_client.get_logout_url() - else: - return super().get_next_page() + redirect_url = urlunparse( + (self.request.scheme, self.request.get_host(), next_page, "", "", "") + ) + next_page = cas_client.get_logout_url(redirect_url=redirect_url) + return next_page