Skip to content

Commit

Permalink
Added request object to resfresh_token signals
Browse files Browse the repository at this point in the history
  • Loading branch information
mongkok committed Jun 1, 2019
1 parent 6a76e6f commit e86f433
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions docs/refresh_token.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ Automatically revoke a refresh token after it has been used::


@receiver(refresh_token_rotated)
def revoke_refresh_token(sender, refresh_token, **kwargs):
refresh_token.revoke()
def revoke_refresh_token(sender, request, refresh_token, **kwargs):
refresh_token.revoke(request)

Clear refresh tokens
~~~~~~~~~~~~~~~~~~~~
Expand Down
9 changes: 5 additions & 4 deletions graphql_jwt/refresh_token/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class Fields:
@setup_jwt_cookie
def refresh(cls, root, info, refresh_token, **kwargs):
context = info.context
refresh_token = get_refresh_token(refresh_token, info.context)
refresh_token = get_refresh_token(refresh_token, context)

if refresh_token.is_expired(context):
raise exceptions.JSONWebTokenError(_('Refresh token is expired'))

payload = jwt_settings.JWT_PAYLOAD_HANDLER(refresh_token.user, context)
token = jwt_settings.JWT_ENCODE_HANDLER(payload, context)

refresh_token.rotate()
refresh_token.rotate(context)
refreshed_token = refresh_token_lazy(refresh_token.user)
return cls(token=token, payload=payload, refresh_token=refreshed_token)

Expand All @@ -37,6 +37,7 @@ class RevokeMixin(object):

@classmethod
def revoke(cls, root, info, refresh_token, **kwargs):
refresh_token = get_refresh_token(refresh_token, info.context)
refresh_token.revoke()
context = info.context
refresh_token = get_refresh_token(refresh_token, context)
refresh_token.revoke(context)
return cls(revoked=timegm(refresh_token.revoked.timetuple()))
10 changes: 6 additions & 4 deletions graphql_jwt/refresh_token/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,23 @@ def get_token(self):
return self._cached_token
return self.token

def is_expired(self, context=None):
def is_expired(self, request=None):
orig_iat = timegm(self.created.timetuple())
return jwt_settings.JWT_REFRESH_EXPIRED_HANDLER(orig_iat, context)
return jwt_settings.JWT_REFRESH_EXPIRED_HANDLER(orig_iat, request)

def revoke(self):
def revoke(self, request=None):
self.revoked = timezone.now()
self.save(update_fields=['revoked'])

signals.refresh_token_revoked.send(
sender=AbstractRefreshToken,
request=request,
refresh_token=self)

def rotate(self):
def rotate(self, request=None):
signals.refresh_token_rotated.send(
sender=AbstractRefreshToken,
request=request,
refresh_token=self)


Expand Down
4 changes: 2 additions & 2 deletions graphql_jwt/refresh_token/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.dispatch import Signal

refresh_token_revoked = Signal(providing_args=['refresh_token'])
refresh_token_rotated = Signal(providing_args=['refresh_token'])
refresh_token_revoked = Signal(providing_args=['request', 'refresh_token'])
refresh_token_rotated = Signal(providing_args=['request', 'refresh_token'])
2 changes: 2 additions & 0 deletions tests/refresh_token/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_revoke(self):
handler.assert_called_once_with(
sender=AbstractRefreshToken,
signal=signals.refresh_token_revoked,
request=None,
refresh_token=self.refresh_token)

def test_rotate(self):
Expand All @@ -56,4 +57,5 @@ def test_rotate(self):
handler.assert_called_once_with(
sender=AbstractRefreshToken,
signal=signals.refresh_token_rotated,
request=None,
refresh_token=self.refresh_token)

0 comments on commit e86f433

Please sign in to comment.