File tree Expand file tree Collapse file tree 4 files changed +15
-8
lines changed
Expand file tree Collapse file tree 4 files changed +15
-8
lines changed Original file line number Diff line number Diff line change @@ -315,15 +315,16 @@ authentication. This assertion contains attributes about the user that
315315was authenticated. It depends on the IdP configuration what exact
316316attributes are sent to each SP it can talk to.
317317
318- When such assertion is received on the Django side it is used to find
319- a Django user and create a session for it. By default djangosaml2 will
320- do a query on the User model with the 'username' attribute but you can
321- change it to any other attribute of the User model. For example,
322- you can do this lookup using the 'email' attribute. In order to do so
323- you should set the following setting::
318+ When such assertion is received on the Django side it is used to find a Django
319+ user and create a session for it. By default djangosaml2 will do a query on the
320+ User model with the USERNAME_FIELD _ attribute but you can change it to any
321+ other attribute of the User model. For example, you can do this lookup using
322+ the 'email' attribute. In order to do so you should set the following setting::
324323
325324 SAML_DJANGO_USER_MAIN_ATTRIBUTE = 'email'
326325
326+ .. _USERNAME_FIELD : https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD
327+
327328Please, use an unique attribute when setting this option. Otherwise
328329the authentication process may fail because djangosaml2 will not know
329330which Django user it should pick.
Original file line number Diff line number Diff line change @@ -136,7 +136,9 @@ def clean_user_main_attribute(self, main_attribute):
136136
137137 def get_django_user_main_attribute (self ):
138138 return getattr (
139- settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' , 'username' )
139+ settings ,
140+ 'SAML_DJANGO_USER_MAIN_ATTRIBUTE' ,
141+ getattr (get_saml_user_model (), 'USERNAME_FIELD' , 'username' ))
140142
141143 def get_django_user_main_attribute_lookup (self ):
142144 return getattr (settings , 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP' , '' )
Original file line number Diff line number Diff line change @@ -27,7 +27,6 @@ def process_first_name(self, first_name):
2727 from django .contrib .auth .models import AbstractUser
2828 class TestUser (AbstractUser ):
2929 age = models .CharField (max_length = 100 , blank = True )
30-
3130 def process_first_name (self , first_name ):
3231 self .first_name = first_name [0 ]
3332
Original file line number Diff line number Diff line change @@ -117,6 +117,11 @@ def test_update_user_empty_attribute(self):
117117 def test_django_user_main_attribute (self ):
118118 backend = Saml2Backend ()
119119
120+ old_username_field = User .USERNAME_FIELD
121+ User .USERNAME_FIELD = 'slug'
122+ self .assertEquals (backend .get_django_user_main_attribute (), 'slug' )
123+ User .USERNAME_FIELD = old_username_field
124+
120125 with override_settings (AUTH_USER_MODEL = 'auth.User' ):
121126 self .assertEquals (
122127 DjangoUserModel .USERNAME_FIELD ,
You can’t perform that action at this time.
0 commit comments