@@ -301,12 +301,14 @@ class CustomizedBackend(Saml2Backend):
301301 """
302302 def is_authorized (self , attributes , attribute_mapping ):
303303 ''' 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
305305
306- def clean_attributes (self , attributes : dict ):
306+ def clean_attributes (self , attributes : dict ) -> dict :
307307 ''' Keep only age attribute '''
308308 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 , )),
310312 }
311313
312314 def clean_user_main_attribute (self , main_attribute ):
@@ -334,16 +336,48 @@ def test_is_authorized(self):
334336 'sn' : ('Doe' , ),
335337 }
336338 self .assertFalse (self .backend .is_authorized (attributes , attribute_mapping ))
337- attributes ['is_staff' ] = ('true' , )
339+ attributes ['is_staff' ] = (True , )
338340 self .assertTrue (self .backend .is_authorized (attributes , attribute_mapping ))
339341
340342 def test_clean_attributes (self ):
341343 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 ,) })
343345
344346 def test_clean_user_main_attribute (self ):
345347 self .assertEqual (self .backend .clean_user_main_attribute ('va--l__ u -e' ), 'va__l___u__e' )
346348
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+ 'mail' : ('john@example.com' , ),
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 )
347381
348382
349383class LowerCaseSaml2Backend (Saml2Backend ):
0 commit comments