Skip to content

Commit 59ed742

Browse files
Inspect User model to determine Django user main attribute
Instead of defaulting SAML_DJANGO_USER_MAIN_ATTRIBUTE to 'username', inspect the current user model and use USERNAME_FIELD. This value should be provided for any custom User model inheriting from AbstractBaseUser. (https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#django.contrib.auth.models.CustomUser.USERNAME_FIELD)
1 parent cd463e8 commit 59ed742

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

README.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,15 +315,16 @@ authentication. This assertion contains attributes about the user that
315315
was authenticated. It depends on the IdP configuration what exact
316316
attributes 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+
327328
Please, use an unique attribute when setting this option. Otherwise
328329
the authentication process may fail because djangosaml2 will not know
329330
which Django user it should pick.

djangosaml2/backends.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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', '')

tests/testprofiles/models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff 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

tests/testprofiles/tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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,

0 commit comments

Comments
 (0)