@@ -301,12 +301,14 @@ class CustomizedBackend(Saml2Backend):
301
301
"""
302
302
def is_authorized (self , attributes , attribute_mapping ):
303
303
''' Allow only staff users from the IDP '''
304
- return attributes .get ('is_staff' , (None , ))[0 ] == 'true'
304
+ return attributes .get ('is_staff' , (None , ))[0 ] == True
305
305
306
- def clean_attributes (self , attributes : dict ):
306
+ def clean_attributes (self , attributes : dict ) -> dict :
307
307
''' Keep only age attribute '''
308
308
return {
309
- 'age' : attributes .get ('age' , ()),
309
+ 'age' : attributes .get ('age' , (None , )),
310
+ 'is_staff' : attributes .get ('is_staff' , (None , )),
311
+ 'uid' : attributes .get ('uid' , (None , )),
310
312
}
311
313
312
314
def clean_user_main_attribute (self , main_attribute ):
@@ -334,16 +336,48 @@ def test_is_authorized(self):
334
336
'sn' : ('Doe' , ),
335
337
}
336
338
self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping ))
337
- attributes ['is_staff' ] = ('true' , )
339
+ attributes ['is_staff' ] = (True , )
338
340
self .assertTrue (self .backend .is_authorized (attributes , attribute_mapping ))
339
341
340
342
def test_clean_attributes (self ):
341
343
attributes = {'random' : 'dummy' , 'value' : 123 , 'age' : '28' }
342
- self .assertEqual (self .backend .clean_attributes (attributes ), {'age' : '28' })
344
+ self .assertEqual (self .backend .clean_attributes (attributes ), {'age' : '28' , 'is_staff' : ( None ,), 'uid' : ( None ,) })
343
345
344
346
def test_clean_user_main_attribute (self ):
345
347
self .assertEqual (self .backend .clean_user_main_attribute ('va--l__ u -e' ), 'va__l___u__e' )
346
348
349
+ def test_authenticate (self ):
350
+ attribute_mapping = {
351
+ 'uid' : ('username' , ),
352
+ 'mail' : ('email' , ),
353
+ 'cn' : ('first_name' , ),
354
+ 'sn' : ('last_name' , ),
355
+ 'age' : ('age' , ),
356
+ 'is_staff' : ('is_staff' , ),
357
+ }
358
+ attributes = {
359
+ 'uid' : ('john' , ),
360
+
361
+ 'cn' : ('John' , ),
362
+ 'sn' : ('Doe' , ),
363
+ 'age' : ('28' , ),
364
+ 'is_staff' : (True , ),
365
+ }
366
+
367
+ self .assertEqual (self .user .age , '' )
368
+ self .assertEqual (self .user .is_staff , False )
369
+
370
+ user = self .backend .authenticate (
371
+ None ,
372
+ session_info = {'ava' : attributes },
373
+ attribute_mapping = attribute_mapping ,
374
+ )
375
+
376
+ self .assertEqual (user , self .user )
377
+
378
+ self .user .refresh_from_db ()
379
+ self .assertEqual (self .user .age , '28' )
380
+ self .assertEqual (self .user .is_staff , True )
347
381
348
382
349
383
class LowerCaseSaml2Backend (Saml2Backend ):
0 commit comments