Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the refresh token to the outstanding db after refreshing #696

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions rest_framework_simplejwt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@

AuthUser = TypeVar("AuthUser", AbstractBaseUser, TokenUser)

if api_settings.BLACKLIST_AFTER_ROTATION:
from .token_blacklist.models import BlacklistedToken
if api_settings.ROTATE_REFRESH_TOKENS:
from datetime import datetime

from .authentication import JWTAuthentication
from .token_blacklist.models import OutstandingToken

if api_settings.BLACKLIST_AFTER_ROTATION:
from .token_blacklist.models import BlacklistedToken


class PasswordField(serializers.CharField):
Expand Down Expand Up @@ -123,6 +129,16 @@ def validate(self, attrs: Dict[str, Any]) -> Dict[str, str]:
refresh.set_exp()
refresh.set_iat()

auth = JWTAuthentication()
user = auth.get_user(validated_token=refresh)
OutstandingToken.objects.create(
user=user,
jti=refresh[api_settings.JTI_CLAIM],
token=str(refresh),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
token=str(refresh),
token=data["refresh"],

This part where we encode refresh token is used twice in this code. One is here on line number 137 and another is on line:142

Maybe its bad idea to repeat this code, because in future refactor to change or update this value we might need to look into 2 places.

My suggestion will be move 142 on top then use token = data["refresh" ]

Hope this gets merged soon 😃

created_at=datetime.fromtimestamp(refresh["iat"]),
expires_at=datetime.fromtimestamp(refresh["exp"]),
)

data["refresh"] = str(refresh)

return data
Expand Down