Skip to content

Commit 8b24150

Browse files
authored
Merge pull request #194 from KolibriSolutions/master2
Fix for #117 post_authenticated_signal
2 parents ef59c7c + a9cb229 commit 8b24150

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

djangosaml2/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
import django.dispatch
1616

1717
pre_user_save = django.dispatch.Signal(providing_args=['attributes', 'user_modified'])
18-
post_authenticated = django.dispatch.Signal(providing_args=['session_info'])
18+
post_authenticated = django.dispatch.Signal(providing_args=['session_info', 'request'])

djangosaml2/tests/__init__.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
from django.contrib.auth import SESSION_KEY, get_user_model
2626
from django.contrib.auth.models import AnonymousUser
2727
from django.contrib.sessions.middleware import SessionMiddleware
28+
from django.http.request import HttpRequest
2829
from django.template import Context, Template
2930
from django.test import TestCase
3031
from django.test.client import RequestFactory
3132
from djangosaml2 import views
3233
from djangosaml2.cache import OutstandingQueriesCache
3334
from djangosaml2.conf import get_config
34-
from djangosaml2.signals import post_authenticated
35+
from djangosaml2.signals import post_authenticated, pre_user_save
3536
from djangosaml2.tests import conf
3637
from djangosaml2.tests.utils import SAMLPostFormParser
3738
from djangosaml2.tests.auth_response import auth_response
@@ -496,16 +497,51 @@ def _test_metadata(self):
496497
self.assertEqual(response.content, expected_metadata)
497498

498499
def test_post_authenticated_signal(self):
500+
self.called = []
499501

500-
def signal_handler(signal, user, session_info):
501-
self.assertEqual(isinstance(user, User), True)
502+
def signal_handler(sender, instance, session_info, request, **kwargs):
503+
self.called.append({'sender': sender, 'instance': instance, 'request': request, 'session_info': session_info})
502504

503505
post_authenticated.connect(signal_handler, dispatch_uid='test_signal')
504506

505507
self.do_login()
506508

509+
# make sure the handler is only called once
510+
self.assertEqual(len(self.called), 1)
511+
# test 'sender', this should be User.__class__
512+
self.assertEqual(self.called[0]['sender'], get_user_model(), 'post_authenticated signal sender is not a User')
513+
# test 'instance', this should be User
514+
self.assertIsInstance(self.called[0]['instance'], get_user_model(), 'post_authenticated signal did not send a User instance')
515+
# test the request
516+
self.assertIsInstance(self.called[0]['request'], HttpRequest, 'post_authenticated signal did not send a request')
517+
# test the session_info
518+
self.assertIsInstance(self.called[0]['session_info'], dict, 'post_authenticated signal did not send a session_info dict')
519+
507520
post_authenticated.disconnect(dispatch_uid='test_signal')
508521

522+
def test_pre_user_save_signal(self):
523+
self.called = []
524+
525+
def signal_handler(sender, instance, attributes, user_modified, **kwargs):
526+
self.called.append({'sender': sender, 'instance': instance, 'attributes': attributes, 'user_modified': user_modified})
527+
528+
pre_user_save.connect(signal_handler, dispatch_uid='test_signal')
529+
530+
self.do_login()
531+
532+
# make sure the handler is only called once
533+
self.assertEqual(len(self.called), 1)
534+
# test 'sender', this should be User.__class__
535+
self.assertEqual(self.called[0]['sender'], get_user_model(), 'pre_user_save signal sender is not a User')
536+
# test 'instance', this should be User
537+
self.assertIsInstance(self.called[0]['instance'], get_user_model(), 'pre_user_save signal did not send a User instance')
538+
# test the attributes
539+
self.assertIsInstance(self.called[0]['attributes'], dict, 'pre_user_save signal did not send attributes')
540+
# test the user_modified
541+
self.assertIsInstance(self.called[0]['user_modified'], bool, 'pre_user_save signal did not send a user_modified bool')
542+
543+
pre_user_save.disconnect(dispatch_uid='test_signal')
544+
509545
def test_idplist_templatetag(self):
510546
settings.SAML_CONFIG = conf.create_conf(
511547
sp_host='sp.example.com',

djangosaml2/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def login(request,
157157
'its metadata is expired.'))
158158
if selected_idp is None:
159159
selected_idp = list(idps.keys())[0]
160-
160+
161161
# choose a binding to try first
162162
sign_requests = getattr(conf, '_sp_authn_requests_signed', False)
163163
binding = BINDING_HTTP_POST if sign_requests else BINDING_HTTP_REDIRECT
@@ -348,7 +348,12 @@ def post(self,
348348
logger.debug("User %s authenticated via SSO.", user)
349349
logger.debug('Sending the post_authenticated signal')
350350

351-
post_authenticated.send_robust(sender=user, session_info=session_info)
351+
# post_authenticated.send_robust(sender=user, session_info=session_info)
352+
# https://github.com/knaperek/djangosaml2/issues/117
353+
post_authenticated.send_robust(sender=user.__class__,
354+
instance=user,
355+
session_info=session_info,
356+
request=request)
352357
self.customize_session(user, session_info)
353358

354359
relay_state = self.build_relay_state()

0 commit comments

Comments
 (0)