@@ -338,17 +338,18 @@ def is_authorized(self, attributes, attribute_mapping, idp_entityid: str, assert
338
338
return attributes .get ('is_staff' , (None , ))[0 ] == True and assertion_info .get ('assertion_id' , None ) != None
339
339
340
340
def clean_attributes (self , attributes : dict , idp_entityid : str , ** kwargs ) -> dict :
341
- ''' Keep only age attribute '''
341
+ ''' Keep only certain attribute '''
342
342
return {
343
343
'age' : attributes .get ('age' , (None , )),
344
+ 'mail' : attributes .get ('mail' , (None , )),
344
345
'is_staff' : attributes .get ('is_staff' , (None , )),
345
346
'uid' : attributes .get ('uid' , (None , )),
346
347
}
347
348
348
349
def clean_user_main_attribute (self , main_attribute ):
349
- ''' Replace all spaces an dashes by underscores '''
350
+ ''' Partition string on @ and return the first part '''
350
351
if main_attribute :
351
- return main_attribute .replace ( '-' , '_' ). replace ( ' ' , '_' )
352
+ return main_attribute .partition ( '@' )[ 0 ]
352
353
return main_attribute
353
354
354
355
@@ -380,10 +381,13 @@ def test_is_authorized(self):
380
381
381
382
def test_clean_attributes (self ):
382
383
attributes = {'random' : 'dummy' , 'value' : 123 , 'age' : '28' }
383
- self .assertEqual (self .backend .clean_attributes (attributes , '' ), {'age' : '28' , 'is_staff' : (None ,), 'uid' : (None ,)})
384
-
384
+ self .assertEqual (
385
+ self .backend .clean_attributes (attributes , '' ),
386
+ {'age' : '28' , 'mail' : (None ,), 'is_staff' : (None ,), 'uid' : (None ,)}
387
+ )
388
+
385
389
def test_clean_user_main_attribute (self ):
386
- self .assertEqual (self .backend .clean_user_main_attribute ('va--l__ u -e ' ), 'va__l___u__e ' )
390
+ self .assertEqual (self .backend .clean_user_main_attribute ('john@example.com ' ), 'john ' )
387
391
388
392
def test_authenticate (self ):
389
393
attribute_mapping = {
@@ -454,3 +458,35 @@ def test_authenticate(self):
454
458
self .user .refresh_from_db ()
455
459
self .assertEqual (self .user .age , '28' )
456
460
self .assertEqual (self .user .is_staff , True )
461
+
462
+ def test_user_cleaned_main_attribute (self ):
463
+ """
464
+ In this test the username is taken from the `mail` attribute,
465
+ but cleaned to remove the @domain part. After fetching and
466
+ updating the user, the username remains the same.
467
+ """
468
+ attribute_mapping = {
469
+ 'mail' : ('username' ,),
470
+ 'cn' : ('first_name' ,),
471
+ 'sn' : ('last_name' ,),
472
+ 'is_staff' : ('is_staff' , ),
473
+ }
474
+ attributes = {
475
+ 'mail' : ('john@example.com' ,),
476
+ 'cn' : ('John' ,),
477
+ 'sn' : ('Doe' ,),
478
+ 'is_staff' : (True , ),
479
+ }
480
+ assertion_info = {
481
+ 'assertion_id' : 'abcdefg12345' ,
482
+ }
483
+ user = self .backend .authenticate (
484
+ None ,
485
+ session_info = {'ava' : attributes , 'issuer' : 'dummy_entity_id' },
486
+ attribute_mapping = attribute_mapping ,
487
+ assertion_info = assertion_info ,
488
+ )
489
+ self .assertEqual (user , self .user )
490
+
491
+ self .user .refresh_from_db ()
492
+ self .assertEqual (user .username , 'john' )
0 commit comments