2121from django .core .exceptions import ImproperlyConfigured
2222from django .test import TestCase , override_settings
2323from djangosaml2 .backends import Saml2Backend , set_attribute
24+ from saml2 .saml import Assertion
2425
2526from testprofiles .models import TestUser
2627
@@ -104,7 +105,7 @@ def test_extract_user_identifier_params_use_nameid_missing(self):
104105 self .assertEqual (lookup_value , None )
105106
106107 def test_is_authorized (self ):
107- self .assertTrue (self .backend .is_authorized ({}, {}, '' ))
108+ self .assertTrue (self .backend .is_authorized ({}, {}, '' , None ))
108109
109110 def test_clean_attributes (self ):
110111 attributes = {'random' : 'dummy' , 'value' : 123 }
@@ -333,9 +334,9 @@ def test_deprecations(self):
333334class CustomizedBackend (Saml2Backend ):
334335 """ Override the available methods with some customized implementation to test customization
335336 """
336- def is_authorized (self , attributes , attribute_mapping , idp_entityid : str , ** kwargs ):
337+ def is_authorized (self , attributes , attribute_mapping , idp_entityid : str , assertion , ** kwargs ):
337338 ''' Allow only staff users from the IDP '''
338- return attributes .get ('is_staff' , (None , ))[0 ] == True
339+ return attributes .get ('is_staff' , (None , ))[0 ] == True and getattr ( assertion , 'id' , None ) != None
339340
340341 def clean_attributes (self , attributes : dict , idp_entityid : str , ** kwargs ) -> dict :
341342 ''' Keep only age attribute '''
@@ -368,9 +369,12 @@ def test_is_authorized(self):
368369 'cn' : ('John' , ),
369370 'sn' : ('Doe' , ),
370371 }
371- self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping , '' ))
372+ assertion = Assertion ()
373+ self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping , '' , assertion ))
372374 attributes ['is_staff' ] = (True , )
373- self .assertTrue (self .backend .is_authorized (attributes , attribute_mapping , '' ))
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 ))
374378
375379 def test_clean_attributes (self ):
376380 attributes = {'random' : 'dummy' , 'value' : 123 , 'age' : '28' }
@@ -396,6 +400,7 @@ def test_authenticate(self):
396400 'age' : ('28' , ),
397401 'is_staff' : (True , ),
398402 }
403+ assertion = Assertion (id = 'abcdefg12345' )
399404
400405 self .assertEqual (self .user .age , '' )
401406 self .assertEqual (self .user .is_staff , False )
@@ -409,6 +414,7 @@ def test_authenticate(self):
409414 None ,
410415 session_info = {'random' : 'content' },
411416 attribute_mapping = attribute_mapping ,
417+ assertion = assertion ,
412418 )
413419 self .assertIsNone (user )
414420
@@ -417,6 +423,7 @@ def test_authenticate(self):
417423 None ,
418424 session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
419425 attribute_mapping = attribute_mapping ,
426+ assertion = assertion ,
420427 )
421428 self .assertIsNone (user )
422429
@@ -425,6 +432,7 @@ def test_authenticate(self):
425432 None ,
426433 session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
427434 attribute_mapping = attribute_mapping ,
435+ assertion = assertion ,
428436 )
429437 self .assertIsNone (user )
430438
@@ -433,6 +441,7 @@ def test_authenticate(self):
433441 None ,
434442 session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
435443 attribute_mapping = attribute_mapping ,
444+ assertion = assertion ,
436445 )
437446
438447 self .assertEqual (user , self .user )
0 commit comments