5353from .signals import post_authenticated
5454from .utils import (available_idps , fail_acs_response , get_custom_setting ,
5555 get_idp_sso_supported_bindings , get_location ,
56- validate_referral_url )
56+ validate_referral_url , get_saml_request_session )
5757
5858try :
5959 from django .contrib .auth .views import LogoutView
@@ -246,10 +246,11 @@ def login(request,
246246 else :
247247 raise UnsupportedBinding ('Unsupported binding: %s' , binding )
248248
249+ saml_session = get_saml_request_session (request )
249250 # success, so save the session ID and return our response
250- logger .debug ('Saving the session_id in the OutstandingQueries cache' )
251- oq_cache = OutstandingQueriesCache (request .session )
251+ oq_cache = OutstandingQueriesCache (saml_session )
252252 oq_cache .set (session_id , came_from )
253+ logger .debug ('Saving the session_id "{}" in the OutstandingQueries cache' .format (oq_cache .__dict__ ))
253254 return http_response
254255
255256
@@ -286,9 +287,10 @@ def post(self,
286287 logger .warning ('Missing "SAMLResponse" parameter in POST data.' )
287288 raise SuspiciousOperation
288289
289- client = Saml2Client (conf , identity_cache = IdentityCache (self .request .session ))
290-
291- oq_cache = OutstandingQueriesCache (self .request .session )
290+ saml_session = get_saml_request_session (request )
291+ client = Saml2Client (conf , identity_cache = IdentityCache (saml_session ))
292+ oq_cache = OutstandingQueriesCache (saml_session )
293+ oq_cache .sync ()
292294 outstanding_queries = oq_cache .outstanding_queries ()
293295
294296 try :
@@ -343,7 +345,7 @@ def post(self,
343345 return fail_acs_response (request , exception = PermissionDenied ('No user could be authenticated.' ))
344346
345347 auth .login (self .request , user )
346- _set_subject_id (self . request . session , session_info ['name_id' ])
348+ _set_subject_id (saml_session , session_info ['name_id' ])
347349 logger .debug ("User %s authenticated via SSO." , user )
348350 logger .debug ('Sending the post_authenticated signal' )
349351
@@ -403,12 +405,13 @@ def echo_attributes(request,
403405 config_loader_path = None ,
404406 template = 'djangosaml2/echo_attributes.html' ):
405407 """Example view that echo the SAML attributes of an user"""
406- state = StateCache (request .session )
408+ saml_session = get_saml_request_session (request )
409+ state = StateCache (saml_session )
407410 conf = get_config (config_loader_path , request )
408411
409412 client = Saml2Client (conf , state_cache = state ,
410- identity_cache = IdentityCache (request . session ))
411- subject_id = _get_subject_id (request . session )
413+ identity_cache = IdentityCache (saml_session ))
414+ subject_id = _get_subject_id (saml_session )
412415 try :
413416 identity = client .users .get_identity (subject_id ,
414417 check_not_on_or_after = False )
@@ -425,12 +428,13 @@ def logout(request, config_loader_path=None):
425428 This view initiates the SAML2 Logout request
426429 using the pysaml2 library to create the LogoutRequest.
427430 """
428- state = StateCache (request .session )
431+ saml_session = get_saml_request_session (request )
432+ state = StateCache (saml_session )
429433 conf = get_config (config_loader_path , request )
430434
431435 client = Saml2Client (conf , state_cache = state ,
432- identity_cache = IdentityCache (request . session ))
433- subject_id = _get_subject_id (request . session )
436+ identity_cache = IdentityCache (saml_session ))
437+ subject_id = _get_subject_id (saml_session )
434438 if subject_id is None :
435439 logger .warning (
436440 'The session does not contain the subject id for user %s' ,
@@ -508,9 +512,10 @@ def do_logout_service(request, data, binding, config_loader_path=None, next_page
508512 logger .debug ('Logout service started' )
509513 conf = get_config (config_loader_path , request )
510514
511- state = StateCache (request .session )
515+ saml_session = get_saml_request_session (request )
516+ state = StateCache (saml_session )
512517 client = Saml2Client (conf , state_cache = state ,
513- identity_cache = IdentityCache (request . session ))
518+ identity_cache = IdentityCache (saml_session ))
514519
515520 if 'SAMLResponse' in data : # we started the logout
516521 logger .debug ('Receiving a logout response from the IdP' )
@@ -520,7 +525,7 @@ def do_logout_service(request, data, binding, config_loader_path=None, next_page
520525
521526 elif 'SAMLRequest' in data : # logout started by the IdP
522527 logger .debug ('Receiving a logout request from the IdP' )
523- subject_id = _get_subject_id (request . session ) if hasattr ( request , 'session' ) else None
528+ subject_id = _get_subject_id (saml_session )
524529
525530 if subject_id is None :
526531 logger .warning (
0 commit comments