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
315
315
was authenticated. It depends on the IdP configuration what exact
316
316
attributes are sent to each SP it can talk to.
317
317
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::
324
323
325
324
SAML_DJANGO_USER_MAIN_ATTRIBUTE = 'email'
326
325
326
+ .. _USERNAME_FIELD : https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD
327
+
327
328
Please, use an unique attribute when setting this option. Otherwise
328
329
the authentication process may fail because djangosaml2 will not know
329
330
which 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):
136
136
137
137
def get_django_user_main_attribute (self ):
138
138
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' ))
140
142
141
143
def get_django_user_main_attribute_lookup (self ):
142
144
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):
27
27
from django .contrib .auth .models import AbstractUser
28
28
class TestUser (AbstractUser ):
29
29
age = models .CharField (max_length = 100 , blank = True )
30
-
31
30
def process_first_name (self , first_name ):
32
31
self .first_name = first_name [0 ]
33
32
Original file line number Diff line number Diff line change @@ -117,6 +117,11 @@ def test_update_user_empty_attribute(self):
117
117
def test_django_user_main_attribute (self ):
118
118
backend = Saml2Backend ()
119
119
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
+
120
125
with override_settings (AUTH_USER_MODEL = 'auth.User' ):
121
126
self .assertEquals (
122
127
DjangoUserModel .USERNAME_FIELD ,
You can’t perform that action at this time.
0 commit comments