Skip to content

Commit 774e85d

Browse files
pass assertion_info dic instead of assertion object
1 parent fedbd28 commit 774e85d

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

djangosaml2/backends.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _get_attribute_value(self, django_field: str, attributes: dict, attribute_ma
106106
'value is missing. Probably the user '
107107
'session is expired.')
108108

109-
def authenticate(self, request, session_info=None, attribute_mapping=None, create_unknown_user=True, assertion=None, **kwargs):
109+
def authenticate(self, request, session_info=None, attribute_mapping=None, create_unknown_user=True, assertion_info=None, **kwargs):
110110
if session_info is None or attribute_mapping is None:
111111
logger.info('Session info or attribute mapping are None')
112112
return None
@@ -121,7 +121,7 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
121121

122122
logger.debug(f'attributes: {attributes}')
123123

124-
if not self.is_authorized(attributes, attribute_mapping, idp_entityid, assertion):
124+
if not self.is_authorized(attributes, attribute_mapping, idp_entityid, assertion_info):
125125
logger.error('Request not authorized')
126126
return None
127127

@@ -194,7 +194,7 @@ def clean_attributes(self, attributes: dict, idp_entityid: str, **kwargs) -> dic
194194
""" Hook to clean or filter attributes from the SAML response. No-op by default. """
195195
return attributes
196196

197-
def is_authorized(self, attributes: dict, attribute_mapping: dict, idp_entityid: str, assertion: object, **kwargs) -> bool:
197+
def is_authorized(self, attributes: dict, attribute_mapping: dict, idp_entityid: str, assertion_info: dict, **kwargs) -> bool:
198198
""" Hook to allow custom authorization policies based on SAML attributes. True by default. """
199199
return True
200200

djangosaml2/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ def post(self, request, attribute_mapping=None, create_unknown_user=None):
419419

420420
# authenticate the remote user
421421
session_info = response.session_info()
422+
assertion = response.assertion
423+
assertion_info = {'assertion_id': assertion.id, 'not_on_or_after': assertion.conditions.not_on_or_after if assertion.conditions else None}
422424

423425
if callable(attribute_mapping):
424426
attribute_mapping = attribute_mapping()
@@ -431,7 +433,7 @@ def post(self, request, attribute_mapping=None, create_unknown_user=None):
431433
session_info=session_info,
432434
attribute_mapping=attribute_mapping,
433435
create_unknown_user=create_unknown_user,
434-
assertion=response.assertion)
436+
assertion_info=assertion_info)
435437
if user is None:
436438
logger.warning(
437439
"Could not authenticate user received in SAML Assertion. Session info: %s", session_info)

tests/testprofiles/tests.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from django.core.exceptions import ImproperlyConfigured
2222
from django.test import TestCase, override_settings
2323
from djangosaml2.backends import Saml2Backend, set_attribute
24-
from saml2.saml import Assertion
2524

2625
from testprofiles.models import TestUser
2726

@@ -105,7 +104,7 @@ def test_extract_user_identifier_params_use_nameid_missing(self):
105104
self.assertEqual(lookup_value, None)
106105

107106
def test_is_authorized(self):
108-
self.assertTrue(self.backend.is_authorized({}, {}, '', None))
107+
self.assertTrue(self.backend.is_authorized({}, {}, '', {}))
109108

110109
def test_clean_attributes(self):
111110
attributes = {'random': 'dummy', 'value': 123}
@@ -334,9 +333,9 @@ def test_deprecations(self):
334333
class CustomizedBackend(Saml2Backend):
335334
""" Override the available methods with some customized implementation to test customization
336335
"""
337-
def is_authorized(self, attributes, attribute_mapping, idp_entityid: str, assertion, **kwargs):
336+
def is_authorized(self, attributes, attribute_mapping, idp_entityid: str, assertion_info, **kwargs):
338337
''' Allow only staff users from the IDP '''
339-
return attributes.get('is_staff', (None, ))[0] == True and getattr(assertion, 'id', None) != None
338+
return attributes.get('is_staff', (None, ))[0] == True and assertion_info.get('assertion_id', None) != None
340339

341340
def clean_attributes(self, attributes: dict, idp_entityid: str, **kwargs) -> dict:
342341
''' Keep only age attribute '''
@@ -369,12 +368,15 @@ def test_is_authorized(self):
369368
'cn': ('John', ),
370369
'sn': ('Doe', ),
371370
}
372-
assertion = Assertion()
373-
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping, '', assertion))
371+
assertion_info = {
372+
'assertion_id': None,
373+
'not_on_or_after': None,
374+
}
375+
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping, '', assertion_info))
374376
attributes['is_staff'] = (True, )
375-
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping, '', assertion))
376-
assertion.id = 'abcdefg12345'
377-
self.assertTrue(self.backend.is_authorized(attributes, attribute_mapping, '', assertion))
377+
self.assertFalse(self.backend.is_authorized(attributes, attribute_mapping, '', assertion_info))
378+
assertion_info['assertion_id'] = 'abcdefg12345'
379+
self.assertTrue(self.backend.is_authorized(attributes, attribute_mapping, '', assertion_info))
378380

379381
def test_clean_attributes(self):
380382
attributes = {'random': 'dummy', 'value': 123, 'age': '28'}
@@ -400,7 +402,10 @@ def test_authenticate(self):
400402
'age': ('28', ),
401403
'is_staff': (True, ),
402404
}
403-
assertion = Assertion(id='abcdefg12345')
405+
assertion_info = {
406+
'assertion_id': 'abcdefg12345',
407+
'not_on_or_after': '',
408+
}
404409

405410
self.assertEqual(self.user.age, '')
406411
self.assertEqual(self.user.is_staff, False)
@@ -414,7 +419,7 @@ def test_authenticate(self):
414419
None,
415420
session_info={'random': 'content'},
416421
attribute_mapping=attribute_mapping,
417-
assertion=assertion,
422+
assertion_info=assertion_info,
418423
)
419424
self.assertIsNone(user)
420425

@@ -423,7 +428,7 @@ def test_authenticate(self):
423428
None,
424429
session_info={'ava': attributes, 'issuer': 'dummy_entity_id'},
425430
attribute_mapping=attribute_mapping,
426-
assertion=assertion,
431+
assertion_info=assertion_info,
427432
)
428433
self.assertIsNone(user)
429434

@@ -432,7 +437,7 @@ def test_authenticate(self):
432437
None,
433438
session_info={'ava': attributes, 'issuer': 'dummy_entity_id'},
434439
attribute_mapping=attribute_mapping,
435-
assertion=assertion,
440+
assertion_info=assertion_info,
436441
)
437442
self.assertIsNone(user)
438443

@@ -441,7 +446,7 @@ def test_authenticate(self):
441446
None,
442447
session_info={'ava': attributes, 'issuer': 'dummy_entity_id'},
443448
attribute_mapping=attribute_mapping,
444-
assertion=assertion,
449+
assertion_info=assertion_info,
445450
)
446451

447452
self.assertEqual(user, self.user)

0 commit comments

Comments
 (0)