Skip to content

Commit 2e4f6b8

Browse files
committed
updates
1 parent 0f2cea8 commit 2e4f6b8

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

djangosaml2/backends.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ 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-
request=request, session_info=session_info, attributes=attributes, attribute_mapping=attribute_mapping
137+
idp_entityid=session_info['issuer'], name_id=session_info['name_id'],
138+
attributes=attributes, attribute_mapping=attribute_mapping, request=request
138139
)
139140

140141
# Update user with new attributes from incoming request
@@ -198,12 +199,17 @@ def clean_user_main_attribute(self, main_attribute: Any) -> Any:
198199
""" Hook to clean the extracted user-identifying value. No-op by default. """
199200
return main_attribute
200201

201-
def get_or_create_user(self, user_lookup_key: str, user_lookup_value: Any, create_unknown_user: bool, **kwargs) -> Tuple[Optional[settings.AUTH_USER_MODEL], bool]:
202+
def get_or_create_user(self,
203+
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
205+
) -> Tuple[Optional[settings.AUTH_USER_MODEL], bool]:
202206
""" Look up the user to authenticate. If he doesn't exist, this method creates him (if so desired).
203207
The default implementation looks only at the user_identifier. Override this method in order to do more complex behaviour,
204-
e.g. customize this per IdP. The kwargs contain these additional params: session_info, attribute_mapping, attributes, request.
205-
The identity provider id can be found in kwargs['session_info']['issuer]
208+
e.g. customize this per IdP.
206209
"""
210+
print(f"idp_entityid: {idp_entityid}")
211+
print(f"name_id: {name_id}")
212+
print(f"user_lookup_value: {user_lookup_value}")
207213
UserModel = self._user_model
208214

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

djangosaml2/views.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,10 +336,12 @@ def assertion_consumer_service(request,
336336
create_unknown_user = create_unknown_user()
337337

338338
logger.debug('Trying to authenticate the user. Session info: %s', session_info)
339-
user = auth.authenticate(request=request,
340-
session_info=session_info,
341-
attribute_mapping=attribute_mapping,
342-
create_unknown_user=create_unknown_user)
339+
user = auth.authenticate(
340+
request=request,
341+
session_info=session_info,
342+
attribute_mapping=attribute_mapping,
343+
create_unknown_user=create_unknown_user,
344+
)
343345
if user is None:
344346
logger.warning("Could not authenticate user received in SAML Assertion. Session info: %s", session_info)
345347
return fail_acs_response(request, exception=PermissionDenied('No user could be authenticated.'))

tests/testprofiles/tests.py

Lines changed: 8 additions & 11 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)
184+
user, _ = self.backend.get_or_create_user(self.backend._user_lookup_attribute, 'john', True, None, None, None, None, None)
185185
self.backend._update_user(user, attributes, attribute_mapping)
186186

187187
self.assertIn(
@@ -200,11 +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(
204-
self.backend._user_lookup_attribute,
205-
206-
True
207-
)
203+
user, created = self.backend.get_or_create_user(self.backend._user_lookup_attribute, '[email protected]', True, None, None, None, None, None)
208204

209205
self.assertEquals(user.email, '[email protected]')
210206
self.assertIs(user.email_verified, None)
@@ -238,6 +234,7 @@ def test_get_or_create_user_existing(self):
238234
self.backend._user_lookup_attribute,
239235
'john',
240236
False,
237+
None, None, None, None, None
241238
)
242239

243240
self.assertTrue(isinstance(user, TestUser))
@@ -252,6 +249,7 @@ def test_get_or_create_user_duplicates(self):
252249
'age',
253250
'',
254251
False,
252+
None, None, None, None, None
255253
)
256254

257255
self.assertTrue(user is None)
@@ -268,6 +266,7 @@ def test_get_or_create_user_no_create(self):
268266
self.backend._user_lookup_attribute,
269267
'paul',
270268
False,
269+
None, None, None, None, None
271270
)
272271

273272
self.assertTrue(user is None)
@@ -284,6 +283,7 @@ def test_get_or_create_user_create(self):
284283
self.backend._user_lookup_attribute,
285284
'paul',
286285
True,
286+
None, None, None, None, None
287287
)
288288

289289
self.assertTrue(isinstance(user, TestUser))
@@ -313,9 +313,6 @@ def clean_user_main_attribute(self, main_attribute):
313313
''' Replace all spaces an dashes by underscores '''
314314
return main_attribute.replace('-', '_').replace(' ', '_')
315315

316-
def get_or_create_user(self, user_lookup_key, user_lookup_value, create_unknown_user, **kwargs):
317-
return super().get_or_create_user(user_lookup_key, user_lookup_value, create_unknown_user, **kwargs)
318-
319316

320317
class CustomizedSaml2BackendTests(Saml2BackendTests):
321318
backend_cls = CustomizedBackend
@@ -367,7 +364,7 @@ def test_authenticate(self):
367364

368365
user = self.backend.authenticate(
369366
None,
370-
session_info={'ava': attributes},
367+
session_info={'ava': attributes, 'issuer': 'dummy_entity_id', 'name_id': 'john'},
371368
attribute_mapping=attribute_mapping,
372369
)
373370

@@ -402,7 +399,7 @@ def test_update_user_clean_attributes(self):
402399
backend = LowerCaseSaml2Backend()
403400
user = backend.authenticate(
404401
None,
405-
session_info={'ava': attributes},
402+
session_info={'ava': attributes, 'issuer': 'dummy_entity_id', 'name_id': 'john'},
406403
attribute_mapping=attribute_mapping,
407404
)
408405
self.assertIsNotNone(user)

0 commit comments

Comments
 (0)