@@ -156,34 +156,44 @@ def login(request,
156
156
# http://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf
157
157
binding = BINDING_HTTP_POST if getattr (conf , '_sp_authn_requests_signed' , False ) else BINDING_HTTP_REDIRECT
158
158
159
- client = Saml2Client (conf )
160
- try :
161
- (session_id , result ) = client .prepare_for_authenticate (
162
- entityid = selected_idp , relay_state = came_from ,
163
- binding = binding ,
164
- )
165
- except TypeError as e :
166
- logger .error ('Unable to know which IdP to use' )
167
- return HttpResponse (text_type (e ))
168
-
169
- logger .debug ('Saving the session_id in the OutstandingQueries cache' )
170
- oq_cache = OutstandingQueriesCache (request .session )
171
- oq_cache .set (session_id , came_from )
172
-
173
- logger .debug ('Redirecting user to the IdP via %s binding.' , binding .split (':' )[- 1 ])
159
+ http_response = None
160
+ logger .debug ('Redirecting user to the IdP via %s binding.' , binding )
174
161
if binding == BINDING_HTTP_REDIRECT :
175
- return HttpResponseRedirect (get_location (result ))
162
+ try :
163
+ # we use sign kwarg to override in case of redirect binding
164
+ # otherwise pysaml2 may sign the xml for redirect which is incorrect
165
+ session_id , result = client .prepare_for_authenticate (
166
+ entityid = selected_idp , relay_state = came_from ,
167
+ binding = binding , sign = False )
168
+ except TypeError as e :
169
+ logger .error ('Unable to know which IdP to use' )
170
+ return HttpResponse (text_type (e ))
171
+ else :
172
+ http_response = HttpResponseRedirect (get_location (result ))
176
173
elif binding == BINDING_HTTP_POST :
174
+ # use the html provided by pysaml2 if no template specified
177
175
if not post_binding_form_template :
178
- # use the html provided by pysaml2
179
- return HttpResponse (result ['data' ])
176
+ try :
177
+ session_id , result = client .prepare_for_authenticate (
178
+ entityid = selected_idp , relay_state = came_from ,
179
+ binding = binding )
180
+ except TypeError as e :
181
+ logger .error ('Unable to know which IdP to use' )
182
+ return HttpResponse (text_type (e ))
183
+ else :
184
+ http_response = HttpResponse (result ['data' ])
185
+ # get request XML to build our own html based on the template
180
186
else :
181
- # manually get request XML to build our own template
182
- request_id , request_xml = client .create_authn_request (
183
- client .sso_location (selected_idp , binding ),
187
+ try :
188
+ location = client .sso_location (selected_idp , binding )
189
+ except TypeError as e :
190
+ logger .error ('Unable to know which IdP to use' )
191
+ return HttpResponse (text_type (e ))
192
+ session_id , request_xml = client .create_authn_request (
193
+ location ,
184
194
binding = binding )
185
- return render (request , post_binding_form_template , {
186
- 'target_url' : result [ 'url' ] ,
195
+ http_response = render (request , post_binding_form_template , {
196
+ 'target_url' : location ,
187
197
'params' : {
188
198
'SAMLRequest' : base64 .b64encode (request_xml ),
189
199
'RelayState' : came_from ,
@@ -192,6 +202,12 @@ def login(request,
192
202
else :
193
203
raise NotImplementedError ('Unsupported binding: %s' , binding )
194
204
205
+ # success, so save the session ID and return our response
206
+ logger .debug ('Saving the session_id in the OutstandingQueries cache' )
207
+ oq_cache = OutstandingQueriesCache (request .session )
208
+ oq_cache .set (session_id , came_from )
209
+ return http_response
210
+
195
211
196
212
@require_POST
197
213
@csrf_exempt
0 commit comments