Skip to content

Commit 6532a0b

Browse files
authored
Fixed issue jazzband#363
This commit is rewrite from jazzband#488 ref: Upon refreshing the token a new Outstanding token is created in the serializers.py where the user from the blacklisted token is added to the new refresh token. This insures that there is always an Outstanding token for each refresh token in use. Therefore, this will solve the issue of logging out from all devices by blacklisting all the Outstanding tokens linked to that specific user.
1 parent 8e8a08a commit 6532a0b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

rest_framework_simplejwt/serializers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
from rest_framework import exceptions, serializers
66
from rest_framework.exceptions import ValidationError
77

8+
from .authentication import JWTAuthentication
89
from .settings import api_settings
910
from .tokens import RefreshToken, SlidingToken, UntypedToken
11+
from .utils import datetime_from_epoch
12+
13+
if api_settings.ROTATE_REFRESH_TOKENS:
14+
from .token_blacklist.models import OutstandingToken
1015

1116
if api_settings.BLACKLIST_AFTER_ROTATION:
1217
from .token_blacklist.models import BlacklistedToken
@@ -117,6 +122,17 @@ def validate(self, attrs):
117122
refresh.set_jti()
118123
refresh.set_exp()
119124
refresh.set_iat()
125+
126+
# Create OutstandingToken when rotate refresh token
127+
auth = JWTAuthentication()
128+
user = auth.get_user(validated_token=refresh)
129+
OutstandingToken.objects.create(
130+
user=user,
131+
jti=refresh[api_settings.JTI_CLAIM],
132+
token=str(refresh),
133+
created_at=refresh.current_time,
134+
expires_at=datetime_from_epoch(refresh['exp'])
135+
)
120136

121137
data["refresh"] = str(refresh)
122138

0 commit comments

Comments
 (0)