Skip to content

Commit c13e998

Browse files
committed
Fix failOnAuthnContextMismatch code
1 parent 1694935 commit c13e998

File tree

5 files changed

+6
-7
lines changed

5 files changed

+6
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ In addition to the required settings data (idp, sp), extra settings can be defin
410410
"requestedAuthnContext": true,
411411
// Allows the authn comparison parameter to be set, defaults to 'exact' if the setting is not present.
412412
"requestedAuthnContextComparison": "exact",
413-
// Set to true to check that the AuthnContext received matches the one requested.
413+
// Set to true to check that the AuthnContext(s) received match(es) the requested.
414414
"failOnAuthnContextMismatch": false,
415415

416416
// In some environment you will need to set how long the published metadata of the Service Provider gonna be valid.

src/onelogin/saml2/authn_request.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ def __init__(self, settings, force_authn=False, is_passive=False, set_nameid_pol
9595

9696
requested_authn_context_str = ''
9797
if security['requestedAuthnContext'] is not False:
98-
authn_comparison = 'exact'
99-
if 'requestedAuthnContextComparison' in security.keys():
100-
authn_comparison = security['requestedAuthnContextComparison']
98+
authn_comparison = security['requestedAuthnContextComparison']
10199

102100
if security['requestedAuthnContext'] is True:
103101
requested_authn_context_str = """ <samlp:RequestedAuthnContext Comparison="%s">

src/onelogin/saml2/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ def is_valid(self, request_data, request_id=None, raise_exceptions=False):
166166
requested_authn_contexts = security['requestedAuthnContext']
167167
if security['failOnAuthnContextMismatch'] and requested_authn_contexts and requested_authn_contexts is not True:
168168
authn_contexts = self.get_authn_contexts()
169-
unmatched_contexts = set(requested_authn_contexts).difference(authn_contexts)
169+
unmatched_contexts = set(authn_contexts).difference(requested_authn_contexts)
170170
if unmatched_contexts:
171171
raise OneLogin_Saml2_ValidationError(
172-
'The AuthnContext "%s" didn\'t include requested context "%s"' % (', '.join(authn_contexts), ', '.join(unmatched_contexts)),
172+
'The AuthnContext "%s" was not a requested context "%s"' % (', '.join(unmatched_contexts), ', '.join(requested_authn_contexts)),
173173
OneLogin_Saml2_ValidationError.AUTHN_CONTEXT_MISMATCH
174174
)
175175

src/onelogin/saml2/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ def __add_default_values(self):
310310
self.__sp.setdefault('privateKey', '')
311311

312312
self.__security.setdefault('requestedAuthnContext', True)
313+
self.__security.setdefault('requestedAuthnContextComparison', 'exact')
313314
self.__security.setdefault('failOnAuthnContextMismatch', False)
314315

315316
def check_settings(self, settings):

tests/src/OneLogin/saml2_tests/response_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def testIsInValidAuthenticationContext(self):
10561056
# check that we catch when the contexts don't match
10571057
response = OneLogin_Saml2_Response(settings, message)
10581058
self.assertFalse(response.is_valid(request_data))
1059-
self.assertIn('The AuthnContext "%s" didn\'t include requested context "%s"' % (password_context, two_factor_context), response.get_error())
1059+
self.assertIn('The AuthnContext "%s" was not a requested context "%s"' % (password_context, two_factor_context), response.get_error())
10601060

10611061
# now drop in the expected AuthnContextClassRef and see that it passes
10621062
original_message = compat.to_string(OneLogin_Saml2_Utils.b64decode(message))

0 commit comments

Comments
 (0)