Skip to content

Commit cd463e8

Browse files
Remove duplicate definitions SAML_DJANGO_USER_MAIN_ATTRIBUTE{,_LOOKUP}
The saml_settings module is only used in backends.py and is not the only way to retrieve Django's user attribute and attribute lookup. Instead, define helpers for these on the backend class. Also, remove unused definitions of django_user_main_attribute_lookup.
1 parent b9105c6 commit cd463e8

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

djangosaml2/backends.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
from djangosaml2.signals import pre_user_save
2525

26-
from . import settings as saml_settings
27-
2826
try:
2927
from django.contrib.auth.models import SiteProfileNotAvailable
3028
except ImportError:
@@ -85,8 +83,7 @@ def authenticate(self, request, session_info=None, attribute_mapping=None,
8583
use_name_id_as_username = getattr(
8684
settings, 'SAML_USE_NAME_ID_AS_USERNAME', False)
8785

88-
django_user_main_attribute = saml_settings.SAML_DJANGO_USER_MAIN_ATTRIBUTE
89-
django_user_main_attribute_lookup = saml_settings.SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
86+
django_user_main_attribute = self.get_django_user_main_attribute()
9087

9188
logger.debug('attributes: %s', attributes)
9289
saml_user = None
@@ -137,15 +134,17 @@ def clean_user_main_attribute(self, main_attribute):
137134
"""
138135
return main_attribute
139136

140-
def get_user_query_args(self, main_attribute):
141-
django_user_main_attribute = getattr(
137+
def get_django_user_main_attribute(self):
138+
return getattr(
142139
settings, 'SAML_DJANGO_USER_MAIN_ATTRIBUTE', 'username')
143-
django_user_main_attribute_lookup = getattr(
144-
settings, 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP', '')
145140

146-
return {
147-
django_user_main_attribute + django_user_main_attribute_lookup: main_attribute
148-
}
141+
def get_django_user_main_attribute_lookup(self):
142+
return getattr(settings, 'SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP', '')
143+
144+
def get_user_query_args(self, main_attribute):
145+
lookup = (self.get_django_user_main_attribute() +
146+
self.get_django_user_main_attribute_lookup())
147+
return {lookup: main_attribute}
149148

150149
def get_saml2_user(self, create, main_attribute, attributes, attribute_mapping):
151150
if create:
@@ -156,8 +155,7 @@ def get_saml2_user(self, create, main_attribute, attributes, attribute_mapping):
156155
def _get_or_create_saml2_user(self, main_attribute, attributes, attribute_mapping):
157156
logger.debug('Check if the user "%s" exists or create otherwise',
158157
main_attribute)
159-
django_user_main_attribute = saml_settings.SAML_DJANGO_USER_MAIN_ATTRIBUTE
160-
django_user_main_attribute_lookup = saml_settings.SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP
158+
django_user_main_attribute = self.get_django_user_main_attribute()
161159
user_query_args = self.get_user_query_args(main_attribute)
162160
user_create_defaults = {django_user_main_attribute: main_attribute}
163161

@@ -180,7 +178,7 @@ def _get_or_create_saml2_user(self, main_attribute, attributes, attribute_mappin
180178

181179
def _get_saml2_user(self, main_attribute, attributes, attribute_mapping):
182180
User = get_saml_user_model()
183-
django_user_main_attribute = saml_settings.SAML_DJANGO_USER_MAIN_ATTRIBUTE
181+
django_user_main_attribute = self.get_django_user_main_attribute()
184182
user_query_args = self.get_user_query_args(main_attribute)
185183

186184
logger.debug('Retrieving existing user "%s"', main_attribute)

djangosaml2/settings.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

tests/testprofiles/models.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
import django
1717
from django.db import models
18-
from django.contrib.auth.models import User
19-
from django.conf import settings
2018

2119
if django.VERSION < (1, 7):
2220
class TestProfile(models.Model):
@@ -32,3 +30,10 @@ class TestUser(AbstractUser):
3230

3331
def process_first_name(self, first_name):
3432
self.first_name = first_name[0]
33+
34+
class StandaloneUserModel(models.Model):
35+
"""
36+
Does not inherit from Django's base abstract user and does not define a
37+
USERNAME_FIELD.
38+
"""
39+
username = models.CharField(max_length=30, unique=True)

tests/testprofiles/tests.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
else:
2424
User = get_user_model()
2525

26-
from django.test import TestCase
26+
from django.contrib.auth.models import User as DjangoUserModel
27+
28+
from django.test import TestCase, override_settings
2729

2830
from djangosaml2.backends import Saml2Backend
2931

@@ -110,4 +112,32 @@ def test_update_user_empty_attribute(self):
110112
self.assertEquals(user.email, '[email protected]')
111113
self.assertEquals(user.first_name, 'John')
112114
# empty attribute list: no update
113-
self.assertEquals(user.last_name, 'Smith')
115+
self.assertEquals(user.last_name, 'Smith')
116+
117+
def test_django_user_main_attribute(self):
118+
backend = Saml2Backend()
119+
120+
with override_settings(AUTH_USER_MODEL='auth.User'):
121+
self.assertEquals(
122+
DjangoUserModel.USERNAME_FIELD,
123+
backend.get_django_user_main_attribute())
124+
125+
with override_settings(
126+
AUTH_USER_MODEL='testprofiles.StandaloneUserModel'):
127+
self.assertEquals(
128+
backend.get_django_user_main_attribute(),
129+
'username')
130+
131+
with override_settings(SAML_DJANGO_USER_MAIN_ATTRIBUTE='foo'):
132+
self.assertEquals(backend.get_django_user_main_attribute(), 'foo')
133+
134+
def test_django_user_main_attribute_lookup(self):
135+
backend = Saml2Backend()
136+
137+
self.assertEquals(backend.get_django_user_main_attribute_lookup(), '')
138+
139+
with override_settings(
140+
SAML_DJANGO_USER_MAIN_ATTRIBUTE_LOOKUP='__iexact'):
141+
self.assertEquals(
142+
backend.get_django_user_main_attribute_lookup(),
143+
'__iexact')

0 commit comments

Comments
 (0)