Redirect after a CAS logout

This commit is contained in:
Martin Pépin 2020-06-10 22:22:13 +02:00
parent 6a75f78541
commit 8e95a01647
No known key found for this signature in database
GPG key ID: E7520278B1774448
2 changed files with 15 additions and 4 deletions

View file

@ -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):

View file

@ -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