Skip to content

Commit 7e9ac0c

Browse files
ReeceReece
authored andcommitted
Reset the branch to not include the other PR
1 parent 43d182e commit 7e9ac0c

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

djangosaml2/views.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,34 +156,44 @@ def login(request,
156156
# http://docs.oasis-open.org/security/saml/v2.0/saml-bindings-2.0-os.pdf
157157
binding = BINDING_HTTP_POST if getattr(conf, '_sp_authn_requests_signed', False) else BINDING_HTTP_REDIRECT
158158

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)
174161
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))
176173
elif binding == BINDING_HTTP_POST:
174+
# use the html provided by pysaml2 if no template specified
177175
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
180186
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,
184194
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,
187197
'params': {
188198
'SAMLRequest': base64.b64encode(request_xml),
189199
'RelayState': came_from,
@@ -192,6 +202,12 @@ def login(request,
192202
else:
193203
raise NotImplementedError('Unsupported binding: %s', binding)
194204

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+
195211

196212
@require_POST
197213
@csrf_exempt

0 commit comments

Comments
 (0)