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