Skip to content

Commit 5903075

Browse files
committed
SLO workaround due to SameSite limitations
1 parent 2b41019 commit 5903075

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

djangosaml2/views.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -436,21 +436,24 @@ def logout(request, config_loader_path=None):
436436
'The session does not contain the subject id for user %s',
437437
request.user)
438438

439+
result = dict()
439440
try:
440441
result = client.global_logout(subject_id)
441442
except LogoutError as exp:
442443
logger.exception('Error Handled - SLO not supported by IDP: {}'.format(exp))
443-
auth.logout(request)
444+
# logout
445+
_do_local_logout(request)
444446
state.sync()
445-
return HttpResponseRedirect('/')
447+
return HttpResponseRedirect(settings.LOGOUT_REDIRECT_URL)
446448

449+
# user locally logged out for prudence, indipendently by IdP behaviour
450+
_do_local_logout(request)
447451
state.sync()
448452

449453
if not result:
450454
logger.error("Looks like the user %s is not logged in any IdP/AA", subject_id)
451455
return HttpResponseBadRequest("You are not logged in any IdP/AA")
452-
453-
if len(result) > 1:
456+
elif len(result) > 1:
454457
logger.error('Sorry, I do not know how to logout from several sources. I will logout just from the first one')
455458

456459
for entityid, logout_info in result.items():
@@ -470,7 +473,7 @@ def logout(request, config_loader_path=None):
470473
# We must have had a soap logout
471474
return finish_logout(request, logout_info)
472475

473-
logger.error('Could not logout because there only the HTTP_REDIRECT is supported')
476+
logger.error('Could not logout because Logout Binding is not supported')
474477
return HttpResponseServerError('Logout Binding not supported')
475478

476479

@@ -480,7 +483,15 @@ def logout_service(request, *args, **kwargs):
480483

481484
@csrf_exempt
482485
def logout_service_post(request, *args, **kwargs):
483-
return do_logout_service(request, request.POST, BINDING_HTTP_POST, *args, **kwargs)
486+
try:
487+
return do_logout_service(request, request.POST, BINDING_HTTP_POST, *args, **kwargs)
488+
except Exception as e:
489+
logging.error('Logout Service Post: {}'.format(e))
490+
491+
492+
def _do_local_logout(request):
493+
if request.user.is_authenticated:
494+
auth.logout(request)
484495

485496

486497
def do_logout_service(request, data, binding, config_loader_path=None, next_page=None,
@@ -509,12 +520,13 @@ def do_logout_service(request, data, binding, config_loader_path=None, next_page
509520

510521
elif 'SAMLRequest' in data: # logout started by the IdP
511522
logger.debug('Receiving a logout request from the IdP')
512-
subject_id = _get_subject_id(request.session)
523+
subject_id = _get_subject_id(request.session) if hasattr(request, 'session') else None
524+
513525
if subject_id is None:
514526
logger.warning(
515527
'The session does not contain the subject id for user %s. Performing local logout',
516528
request.user)
517-
auth.logout(request)
529+
_do_local_logout(request)
518530
return render(request, logout_error_template, status=403)
519531
else:
520532
http_info = client.handle_logout_request(
@@ -523,7 +535,10 @@ def do_logout_service(request, data, binding, config_loader_path=None, next_page
523535
binding,
524536
relay_state=data.get('RelayState', ''))
525537
state.sync()
526-
auth.logout(request)
538+
539+
# logout
540+
_do_local_logout(request)
541+
527542
if (
528543
http_info.get('method', 'GET') == 'POST' and
529544
'data' in http_info and

0 commit comments

Comments
 (0)