Skip to content

Commit c351c9b

Browse files
committed
SAML bug fixes
* Fix error casting request_xml to bytes when request_xml is an object * Fix _sp_force_authn error when value is missing * Fix allow_create being set to true even when the value is false * Fix bug with idp selection * Ensure migrations for test models get created
1 parent 5c59f6b commit c351c9b

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

djangosaml2/views.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
from saml2.ident import code, decode
4949
from saml2.sigver import MissingKey
5050
from saml2.s_utils import UnsupportedBinding
51+
from saml2.request import AuthnRequest
5152
from saml2.response import (
5253
StatusError, StatusAuthnFailed, SignatureError, StatusRequestDenied,
5354
UnsolicitedResponse, StatusNoAuthnContext,
@@ -147,26 +148,26 @@ def login(request,
147148

148149
kwargs = {}
149150
# pysaml needs a string otherwise: "cannot serialize True (type bool)"
150-
if getattr(conf, '_sp_force_authn'):
151+
if getattr(conf, '_sp_force_authn', False):
151152
kwargs['force_authn'] = "true"
152-
if getattr(conf, '_sp_allow_create', "false"):
153-
kwargs['allow_create'] = "true"
153+
if hasattr(conf, '_sp_allow_create'):
154+
kwargs['allow_create'] = str(conf._sp_allow_create is True).lower()
154155

155156
# is a embedded wayf needed?
156157
idps = available_idps(conf)
157-
if selected_idp is None and len(idps) > 1:
158-
logger.debug('A discovery process is needed')
159-
return render(request, wayf_template, {
160-
'available_idps': idps.items(),
161-
'came_from': came_from,
162-
})
163-
else:
164-
# is the first one, otherwise next logger message will print None
165-
if not idps:
158+
if selected_idp is None:
159+
if len(idps) > 1:
160+
logger.debug('A discovery process is needed')
161+
return render(request, wayf_template, {
162+
'available_idps': idps.items(),
163+
'came_from': came_from,
164+
})
165+
elif not idps:
166166
raise IdPConfigurationMissing(('IdP configuration is missing or '
167167
'its metadata is expired.'))
168-
selected_idp = list(idps.keys())[0]
169-
168+
else:
169+
selected_idp = list(idps.keys())[0]
170+
170171
# choose a binding to try first
171172
sign_requests = getattr(conf, '_sp_authn_requests_signed', False)
172173
binding = BINDING_HTTP_POST if sign_requests else BINDING_HTTP_REDIRECT
@@ -226,6 +227,9 @@ def login(request,
226227
**kwargs)
227228
try:
228229
if PY3:
230+
if isinstance(request_xml, AuthnRequest):
231+
# request_xml will be an instance of AuthnRequest if the message is not signed
232+
request_xml = str(request_xml)
229233
saml_request = base64.b64encode(binary_type(request_xml, 'UTF-8')).decode('utf-8')
230234
else:
231235
saml_request = base64.b64encode(binary_type(request_xml))

tests/run_tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
# Load models
2727
application = get_wsgi_application()
2828

29+
management.call_command('makemigrations', 'testprofiles')
2930
management.call_command('test', 'djangosaml2.tests', 'testprofiles')

0 commit comments

Comments
 (0)