Skip to content

Commit ac2764e

Browse files
committed
Cleanup
1 parent 2e4f6b8 commit ac2764e

File tree

3 files changed

+12
-72
lines changed

3 files changed

+12
-72
lines changed

README.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,9 @@ A typical configuration would look like this::
8181
'djangosaml2.backends.Saml2Backend',
8282
)
8383

84-
.. note::
85-
86-
Before djangosaml2 0.5.0 this authentication backend was
87-
automatically added by djangosaml2. This turned out to be
88-
a bad idea since some applications want to use their own
89-
custom policies for authorization and the authentication
90-
backend is a good place to define that. Starting from
91-
djangosaml2 0.5.0 it is now possible to define such
92-
backends.
84+
It is possible to subclass the provided Saml2Backend and customize the behaviour
85+
by overriding some methods. This way you can perform your custom cleaning or authorization
86+
policy, and modify the way users are looked up and created.
9387

9488
Finally we have to tell Django what the new login url we want to use is::
9589

djangosaml2/backends.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ def authenticate(self, request, session_info=None, attribute_mapping=None, creat
134134

135135
user, created = self.get_or_create_user(
136136
user_lookup_key, user_lookup_value, create_unknown_user,
137-
idp_entityid=session_info['issuer'], name_id=session_info['name_id'],
138-
attributes=attributes, attribute_mapping=attribute_mapping, request=request
137+
idp_entityid=session_info['issuer'], attributes=attributes, attribute_mapping=attribute_mapping, request=request
139138
)
140139

141140
# Update user with new attributes from incoming request
@@ -201,15 +200,12 @@ def clean_user_main_attribute(self, main_attribute: Any) -> Any:
201200

202201
def get_or_create_user(self,
203202
user_lookup_key: str, user_lookup_value: Any, create_unknown_user: bool,
204-
idp_entityid: str, name_id: str, attributes: dict, attribute_mapping: dict, request
203+
idp_entityid: str, attributes: dict, attribute_mapping: dict, request
205204
) -> Tuple[Optional[settings.AUTH_USER_MODEL], bool]:
206205
""" Look up the user to authenticate. If he doesn't exist, this method creates him (if so desired).
207206
The default implementation looks only at the user_identifier. Override this method in order to do more complex behaviour,
208207
e.g. customize this per IdP.
209208
"""
210-
print(f"idp_entityid: {idp_entityid}")
211-
print(f"name_id: {name_id}")
212-
print(f"user_lookup_value: {user_lookup_value}")
213209
UserModel = self._user_model
214210

215211
# Construct query parameters to query the userModel with. An additional lookup modifier could be specified in the settings.

tests/testprofiles/tests.py

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_invalid_model_attribute_log(self):
181181
}
182182

183183
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
184-
user, _ = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'john', True, None, None, None, None, None)
184+
user, _ = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'john', True, None, None, None, None)
185185
self.backend._update_user(user, attributes, attribute_mapping)
186186

187187
self.assertIn(
@@ -200,7 +200,7 @@ def test_create_user_with_required_fields(self):
200200
'mail_verified': [True],
201201
}
202202
# User creation does not fail if several fields are required.
203-
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, '[email protected]', True, None, None, None, None, None)
203+
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, '[email protected]', True, None, None, None, None)
204204

205205
self.assertEquals(user.email, '[email protected]')
206206
self.assertIs(user.email_verified, None)
@@ -230,12 +230,7 @@ def test_django_user_main_attribute(self):
230230

231231
def test_get_or_create_user_existing(self):
232232
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
233-
user, created = self.backend.get_or_create_user(
234-
self.backend._user_lookup_attribute,
235-
'john',
236-
False,
237-
None, None, None, None, None
238-
)
233+
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'john', False, None, None, None, None)
239234

240235
self.assertTrue(isinstance(user, TestUser))
241236
self.assertFalse(created)
@@ -245,12 +240,7 @@ def test_get_or_create_user_duplicates(self):
245240

246241
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
247242
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
248-
user, created = self.backend.get_or_create_user(
249-
'age',
250-
'',
251-
False,
252-
None, None, None, None, None
253-
)
243+
user, created = self.backend.get_or_create_user('age', '', False, None, None, None, None)
254244

255245
self.assertTrue(user is None)
256246
self.assertFalse(created)
@@ -262,12 +252,7 @@ def test_get_or_create_user_duplicates(self):
262252
def test_get_or_create_user_no_create(self):
263253
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
264254
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
265-
user, created = self.backend.get_or_create_user(
266-
self.backend._user_lookup_attribute,
267-
'paul',
268-
False,
269-
None, None, None, None, None
270-
)
255+
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'paul', False, None, None, None, None)
271256

272257
self.assertTrue(user is None)
273258
self.assertFalse(created)
@@ -279,12 +264,7 @@ def test_get_or_create_user_no_create(self):
279264
def test_get_or_create_user_create(self):
280265
with self.assertLogs('djangosaml2', level='DEBUG') as logs:
281266
with override_settings(SAML_USER_MODEL='testprofiles.TestUser'):
282-
user, created = self.backend.get_or_create_user(
283-
self.backend._user_lookup_attribute,
284-
'paul',
285-
True,
286-
None, None, None, None, None
287-
)
267+
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'paul', True, None, None, None, None)
288268

289269
self.assertTrue(isinstance(user, TestUser))
290270
self.assertTrue(created)
@@ -364,7 +344,7 @@ def test_authenticate(self):
364344

365345
user = self.backend.authenticate(
366346
None,
367-
session_info={'ava': attributes, 'issuer': 'dummy_entity_id', 'name_id': 'john'},
347+
session_info={'ava': attributes, 'issuer': 'dummy_entity_id'},
368348
attribute_mapping=attribute_mapping,
369349
)
370350

@@ -373,33 +353,3 @@ def test_authenticate(self):
373353
self.user.refresh_from_db()
374354
self.assertEqual(self.user.age, '28')
375355
self.assertEqual(self.user.is_staff, True)
376-
377-
378-
class LowerCaseSaml2Backend(Saml2Backend):
379-
def clean_attributes(self, attributes):
380-
return dict([k.lower(), v] for k, v in attributes.items())
381-
382-
383-
class LowerCaseSaml2BackendTest(TestCase):
384-
def test_update_user_clean_attributes(self):
385-
user = User.objects.create(username='john')
386-
attribute_mapping = {
387-
'uid': ('username', ),
388-
'mail': ('email', ),
389-
'cn': ('first_name', ),
390-
'sn': ('last_name', ),
391-
}
392-
attributes = {
393-
'UID': ['john'],
394-
'MAIL': ['[email protected]'],
395-
'CN': ['John'],
396-
'SN': [],
397-
}
398-
399-
backend = LowerCaseSaml2Backend()
400-
user = backend.authenticate(
401-
None,
402-
session_info={'ava': attributes, 'issuer': 'dummy_entity_id', 'name_id': 'john'},
403-
attribute_mapping=attribute_mapping,
404-
)
405-
self.assertIsNotNone(user)

0 commit comments

Comments
 (0)