Skip to content

Commit 4b449e4

Browse files
author
Giuseppe De Marco
authored
Merge pull request #380 from plojyon/logout-relaystate
Logout relaystate
2 parents b58e471 + 5351bd9 commit 4b449e4

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

djangosaml2/views.py

+22-17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import base64
1717
import logging
18+
from typing import Optional
1819
from urllib.parse import quote
1920

2021
from django.conf import settings
@@ -89,6 +90,18 @@ def _get_subject_id(session):
8990
return None
9091

9192

93+
def _get_next_path(request: HttpRequest) -> Optional[str]:
94+
if "next" in request.GET:
95+
next_path = request.GET["next"]
96+
elif "RelayState" in request.GET:
97+
next_path = request.GET["RelayState"]
98+
else:
99+
return None
100+
101+
next_path = validate_referral_url(request, next_path)
102+
return next_path
103+
104+
92105
class SPConfigMixin:
93106
"""Mixin for some of the SAML views with re-usable methods."""
94107

@@ -138,20 +151,6 @@ class LoginView(SPConfigMixin, View):
138151
"djangosaml2/post_binding_form.html",
139152
)
140153

141-
def get_next_path(self, request: HttpRequest) -> str:
142-
"""Returns the path to put in the RelayState to redirect the user to after having logged in.
143-
If the user is already logged in (and if allowed), he will redirect to there immediately.
144-
"""
145-
146-
next_path = get_fallback_login_redirect_url()
147-
if "next" in request.GET:
148-
next_path = request.GET["next"]
149-
elif "RelayState" in request.GET:
150-
next_path = request.GET["RelayState"]
151-
152-
next_path = validate_referral_url(request, next_path)
153-
return next_path
154-
155154
def unknown_idp(self, request, idp):
156155
msg = f"Error: IdP EntityID {escape(idp)} was not found in metadata"
157156
logger.error(msg)
@@ -185,7 +184,9 @@ def should_prevent_auth(self, request) -> bool:
185184

186185
def get(self, request, *args, **kwargs):
187186
logger.debug("Login process started")
188-
next_path = self.get_next_path(request)
187+
next_path = _get_next_path(request)
188+
if next_path is None:
189+
next_path = get_fallback_login_redirect_url()
189190

190191
if self.should_prevent_auth(request):
191192
# If the SAML_IGNORE_AUTHENTICATED_USERS_ON_LOGIN setting is True
@@ -822,8 +823,12 @@ def finish_logout(request, response):
822823

823824
auth.logout(request)
824825

825-
if settings.LOGOUT_REDIRECT_URL is not None:
826-
return HttpResponseRedirect(resolve_url(settings.LOGOUT_REDIRECT_URL))
826+
next_path = _get_next_path(request)
827+
if next_path is not None:
828+
return HttpResponseRedirect(next_path)
829+
elif settings.LOGOUT_REDIRECT_URL is not None:
830+
fallback_url = resolve_url(settings.LOGOUT_REDIRECT_URL)
831+
return HttpResponseRedirect(fallback_url)
827832
else:
828833
current_site = get_current_site(request)
829834
return render(

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def read(*rnames):
2727

2828
setup(
2929
name="djangosaml2",
30-
version="1.6.0",
30+
version="1.7.0",
3131
description="pysaml2 integration for Django",
3232
long_description=read("README.md"),
3333
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)