Skip to content

Commit 102e368

Browse files
committed
Fix #258. Fix failOnAuthnContextMismatch code
1 parent f83dc13 commit 102e368

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
@@ -440,7 +440,7 @@ In addition to the required settings data (idp, sp), extra settings can be defin
440440
// Allows the authn comparison parameter to be set, defaults to 'exact' if the setting is not present.
441441
"requestedAuthnContextComparison": "exact",
442442

443-
// Set to true to check that the AuthnContext received matches the one requested.
443+
// Set to true to check that the AuthnContext(s) received match(es) the requested.
444444
"failOnAuthnContextMismatch": false,
445445

446446
// 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
@@ -93,9 +93,7 @@ def __init__(self, settings, force_authn=False, is_passive=False, set_nameid_pol
9393

9494
requested_authn_context_str = ''
9595
if 'requestedAuthnContext' in security.keys() and security['requestedAuthnContext'] is not False:
96-
authn_comparison = 'exact'
97-
if 'requestedAuthnContextComparison' in security.keys():
98-
authn_comparison = security['requestedAuthnContextComparison']
96+
authn_comparison = security['requestedAuthnContextComparison']
9997

10098
if security['requestedAuthnContext'] is True:
10199
requested_authn_context_str = "\n" + """ <samlp:RequestedAuthnContext Comparison="%s">

src/onelogin/saml2/response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ def is_valid(self, request_data, request_id=None, raise_exceptions=False):
184184

185185
if security.get('failOnAuthnContextMismatch', False) and requested_authn_contexts and requested_authn_contexts is not True:
186186
authn_contexts = self.get_authn_contexts()
187-
unmatched_contexts = set(requested_authn_contexts).difference(authn_contexts)
187+
unmatched_contexts = set(authn_contexts).difference(requested_authn_contexts)
188188
if unmatched_contexts:
189189
raise OneLogin_Saml2_ValidationError(
190-
'The AuthnContext "%s" didn\'t include requested context "%s"' % (', '.join(authn_contexts), ', '.join(unmatched_contexts)),
190+
'The AuthnContext "%s" was not a requested context "%s"' % (', '.join(unmatched_contexts), ', '.join(requested_authn_contexts)),
191191
OneLogin_Saml2_ValidationError.AUTHN_CONTEXT_MISMATCH
192192
)
193193

src/onelogin/saml2/settings.py

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

306306
self.__security.setdefault('requestedAuthnContext', True)
307+
self.__security.setdefault('requestedAuthnContextComparison', 'exact')
307308
self.__security.setdefault('failOnAuthnContextMismatch', False)
308309

309310
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
@@ -1084,7 +1084,7 @@ def testIsInValidAuthenticationContext(self):
10841084
# check that we catch when the contexts don't match
10851085
response = OneLogin_Saml2_Response(settings, message)
10861086
self.assertFalse(response.is_valid(request_data))
1087-
self.assertIn('The AuthnContext "%s" didn\'t include requested context "%s"' % (password_context, two_factor_context), response.get_error())
1087+
self.assertIn('The AuthnContext "%s" was not a requested context "%s"' % (password_context, two_factor_context), response.get_error())
10881088

10891089
# now drop in the expected AuthnContextClassRef and see that it passes
10901090
original_message = b64decode(message)

0 commit comments

Comments
 (0)