Closed
Description
I'm trying to set an HTTP-only cookie on TokenRefreshView. I've extended the original TokenRefreshView class to add the logic to the post method. and it's working unless I change the permission class attribute to IsAuthenticated, now it's throwing a 403 forbidden error.
urls.py
.......
if getattr(settings, "REST_USE_JWT", False):
from rest_framework_simplejwt.views import TokenVerifyView
urlpatterns += [
path("token/verify/", TokenVerifyView.as_view(), name="token_verify"),
path(
"token/refresh/",
CustomTokenRefreshView.as_view(),
name="token_refresh",
),
]
views.py
class CustomTokenRefreshView(TokenRefreshView):
permission_classes = (IsAuthenticated,)
def post(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
try:
serializer.is_valid(raise_exception=True)
except TokenError as e:
raise InvalidToken(e.args[0])
response = Response(serializer.validated_data, status=status.HTTP_200_OK)
if getattr(settings, "REST_USE_JWT", False):
cookie_name = getattr(settings, "JWT_AUTH_COOKIE", None)
cookie_secure = getattr(settings, "JWT_AUTH_SECURE", False)
cookie_httponly = getattr(settings, "JWT_AUTH_HTTPONLY", True)
cookie_samesite = getattr(settings, "JWT_AUTH_SAMESITE", "Lax")
if cookie_name:
expiration = datetime.utcnow() + jwt_settings.ACCESS_TOKEN_LIFETIME
response.set_cookie(
cookie_name,
serializer.validated_data["access"],
expires=expiration,
secure=cookie_secure,
httponly=cookie_httponly,
samesite=cookie_samesite,
)
return response
settings.py
.......
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": [
"rest_framework.permissions.IsAuthenticated",
"rest_framework.permissions.IsAdminUser",
],
"DEFAULT_AUTHENTICATION_CLASSES": (
"dj_rest_auth.jwt_auth.JWTCookieAuthentication",
),
}
I am using this with dj-rest-auth package. afaik, they use simplejwt directly for token/verify and token/refresh paths. So posting it here. Please help. Thanks in advance.
Activity