@@ -183,8 +183,29 @@ def login(request,
183183 else :
184184 http_response = HttpResponseRedirect (get_location (result ))
185185 elif binding == BINDING_HTTP_POST :
186- # use the html provided by pysaml2 if no template specified
187- if not post_binding_form_template :
186+ if post_binding_form_template :
187+ # get request XML to build our own html based on the template
188+ try :
189+ location = client .sso_location (selected_idp , binding )
190+ except TypeError as e :
191+ logger .error ('Unable to know which IdP to use' )
192+ return HttpResponse (text_type (e ))
193+ session_id , request_xml = client .create_authn_request (
194+ location ,
195+ binding = binding )
196+ try :
197+ http_response = render (request , post_binding_form_template , {
198+ 'target_url' : location ,
199+ 'params' : {
200+ 'SAMLRequest' : base64 .b64encode (binary_type (request_xml )),
201+ 'RelayState' : came_from ,
202+ },
203+ })
204+ except TemplateDoesNotExist :
205+ pass
206+
207+ if not http_response :
208+ # use the html provided by pysaml2 if no template was specified or it didn't exist
188209 try :
189210 session_id , result = client .prepare_for_authenticate (
190211 entityid = selected_idp , relay_state = came_from ,
@@ -194,23 +215,6 @@ def login(request,
194215 return HttpResponse (text_type (e ))
195216 else :
196217 http_response = HttpResponse (result ['data' ])
197- # get request XML to build our own html based on the template
198- else :
199- try :
200- location = client .sso_location (selected_idp , binding )
201- except TypeError as e :
202- logger .error ('Unable to know which IdP to use' )
203- return HttpResponse (text_type (e ))
204- session_id , request_xml = client .create_authn_request (
205- location ,
206- binding = binding )
207- http_response = render (request , post_binding_form_template , {
208- 'target_url' : location ,
209- 'params' : {
210- 'SAMLRequest' : base64 .b64encode (binary_type (request_xml )),
211- 'RelayState' : came_from ,
212- },
213- })
214218 else :
215219 raise UnsupportedBinding ('Unsupported binding: %s' , binding )
216220
0 commit comments