21
21
from django .core .exceptions import ImproperlyConfigured
22
22
from django .test import TestCase , override_settings
23
23
from djangosaml2 .backends import Saml2Backend , set_attribute
24
+ from saml2 .saml import Assertion
24
25
25
26
from testprofiles .models import TestUser
26
27
@@ -104,7 +105,7 @@ def test_extract_user_identifier_params_use_nameid_missing(self):
104
105
self .assertEqual (lookup_value , None )
105
106
106
107
def test_is_authorized (self ):
107
- self .assertTrue (self .backend .is_authorized ({}, {}, '' ))
108
+ self .assertTrue (self .backend .is_authorized ({}, {}, '' , None ))
108
109
109
110
def test_clean_attributes (self ):
110
111
attributes = {'random' : 'dummy' , 'value' : 123 }
@@ -333,9 +334,9 @@ def test_deprecations(self):
333
334
class CustomizedBackend (Saml2Backend ):
334
335
""" Override the available methods with some customized implementation to test customization
335
336
"""
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 ):
337
338
''' 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
339
340
340
341
def clean_attributes (self , attributes : dict , idp_entityid : str , ** kwargs ) -> dict :
341
342
''' Keep only age attribute '''
@@ -368,9 +369,12 @@ def test_is_authorized(self):
368
369
'cn' : ('John' , ),
369
370
'sn' : ('Doe' , ),
370
371
}
371
- self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping , '' ))
372
+ assertion = Assertion ()
373
+ self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping , '' , assertion ))
372
374
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 ))
374
378
375
379
def test_clean_attributes (self ):
376
380
attributes = {'random' : 'dummy' , 'value' : 123 , 'age' : '28' }
@@ -396,6 +400,7 @@ def test_authenticate(self):
396
400
'age' : ('28' , ),
397
401
'is_staff' : (True , ),
398
402
}
403
+ assertion = Assertion (id = 'abcdefg12345' )
399
404
400
405
self .assertEqual (self .user .age , '' )
401
406
self .assertEqual (self .user .is_staff , False )
@@ -409,6 +414,7 @@ def test_authenticate(self):
409
414
None ,
410
415
session_info = {'random' : 'content' },
411
416
attribute_mapping = attribute_mapping ,
417
+ assertion = assertion ,
412
418
)
413
419
self .assertIsNone (user )
414
420
@@ -417,6 +423,7 @@ def test_authenticate(self):
417
423
None ,
418
424
session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
419
425
attribute_mapping = attribute_mapping ,
426
+ assertion = assertion ,
420
427
)
421
428
self .assertIsNone (user )
422
429
@@ -425,6 +432,7 @@ def test_authenticate(self):
425
432
None ,
426
433
session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
427
434
attribute_mapping = attribute_mapping ,
435
+ assertion = assertion ,
428
436
)
429
437
self .assertIsNone (user )
430
438
@@ -433,6 +441,7 @@ def test_authenticate(self):
433
441
None ,
434
442
session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
435
443
attribute_mapping = attribute_mapping ,
444
+ assertion = assertion ,
436
445
)
437
446
438
447
self .assertEqual (user , self .user )
0 commit comments