Skip to content

Commit

Permalink
Ensured oidc_user_created signal is triggered by the DRF authenticati…
Browse files Browse the repository at this point in the history
…on backend
  • Loading branch information
ellmetha committed Oct 4, 2018
1 parent d7b34cc commit cb0a196
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions oidc_rp/contrib/rest_framework/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from oidc_rp.backends import create_oidc_user_from_claims, update_oidc_user_from_claims
from oidc_rp.conf import settings as oidc_rp_settings
from oidc_rp.models import OIDCUser
from oidc_rp.signals import oidc_user_created


class BearerTokenAuthentication(BaseAuthentication):
Expand Down Expand Up @@ -59,6 +60,7 @@ def authenticate(self, request):
sub=userinfo_response_data.get('sub'))
except OIDCUser.DoesNotExist:
oidc_user = create_oidc_user_from_claims(userinfo_response_data)
oidc_user_created.send(sender=self.__class__, request=request, oidc_user=oidc_user)
else:
update_oidc_user_from_claims(oidc_user, userinfo_response_data)

Expand Down
23 changes: 23 additions & 0 deletions tests/unit/contrib/rest_framework/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from oidc_rp.conf import settings as oidc_rp_settings
from oidc_rp.contrib.rest_framework.authentication import BearerTokenAuthentication
from oidc_rp.models import OIDCUser
from oidc_rp.signals import oidc_user_created


FIXTURE_ROOT = os.path.join(os.path.dirname(__file__), 'fixtures')
Expand Down Expand Up @@ -135,3 +136,25 @@ def test_cannot_authenticate_a_user_if_the_userinfo_endpoint_raises_an_error(sel
backend = BearerTokenAuthentication()
with pytest.raises(AuthenticationFailed):
backend.authenticate(request)

def test_oidc_user_created_signal_is_sent_during_new_user_authentication(self, rf):
self.signal_was_called = False

def handler(sender, request, oidc_user, **kwargs):
self.request = request
self.oidc_user = oidc_user
self.signal_was_called = True

oidc_user_created.connect(handler)

request = rf.get('/', HTTP_AUTHORIZATION='Bearer accesstoken')
SessionMiddleware().process_request(request)
request.session.save()
backend = BearerTokenAuthentication()
user, _ = backend.authenticate(request)

assert self.signal_was_called is True
assert user.email == '[email protected]'
assert user.oidc_user.sub == '1234'

oidc_user_created.disconnect(handler)

0 comments on commit cb0a196

Please sign in to comment.