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

Bump djangorestframework-simplejwt to 5.4.0 and remove redundant casts and TokenViews #342

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
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
44 changes: 6 additions & 38 deletions canopeum_backend/canopeum_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ def post(self, request: Request):

user = cast(User, authenticate(email=email, password=password))
if user is not None:
refresh = cast(RefreshToken, RefreshToken.for_user(user))
refresh = RefreshToken.for_user(user)

refresh_serializer = TokenRefreshSerializer({
"refresh": str(refresh),
"access": str(refresh.access_token),
"refresh": refresh,
"access": refresh.access_token,
})
user_serializer = UserSerializer(user)
serializer = UserTokenSerializer(
Expand All @@ -166,11 +166,11 @@ def post(self, request: Request):
if register_user_serializer.is_valid():
user = register_user_serializer.create_user()
if user is not None:
refresh = cast(RefreshToken, RefreshToken.for_user(user))
refresh = RefreshToken.for_user(user)

token_refresh_serializer = TokenRefreshSerializer({
"refresh": str(refresh),
"access": str(refresh.access_token),
"refresh": refresh,
"access": refresh.access_token,
})
user_serializer = UserSerializer(user)
user_token_serializer = UserTokenSerializer(
Expand Down Expand Up @@ -1170,35 +1170,3 @@ def get(self, request: Request, code: str):

serializer = UserInvitationSerializer(user_invitation)
return Response(serializer.data)


class TokenRefreshAPIView(APIView):
@extend_schema(responses=RefreshToken, operation_id="token_refresh")
def post(self, request: Request):
refresh = RefreshToken(request.data.get("refresh"))
user = User.objects.get(pk=refresh["user_id"])
refresh["role"] = user.role.name
return Response(
{"refresh": str(refresh), "access": str(refresh.access_token)},
status=status.HTTP_200_OK,
)


class TokenObtainPairAPIView(APIView):
@extend_schema(responses=UserSerializer, operation_id="token_obtain_pair")
def post(self, request: Request):
user = cast(
User,
authenticate(
username=request.data.get("username"), password=request.data.get("password")
),
)
if user is not None:
refresh = cast(RefreshToken, RefreshToken.for_user(user))
if user.role is not None:
refresh["role"] = user.role.name
return Response(
{"refresh": str(refresh), "access": str(refresh.access_token)},
status=status.HTTP_200_OK,
)
return Response({"error": "Invalid credentials"}, status=status.HTTP_401_UNAUTHORIZED)
2 changes: 1 addition & 1 deletion canopeum_backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies = [
"dj-database-url",
"django-cors-headers",
"djangorestframework-camel-case",
"djangorestframework-simplejwt",
"djangorestframework-simplejwt>=5.4.0",
# TODO: Bump above 5.3.1 as soon as fixed for https://github.com/BesLogic/releaf-canopeum/security/dependabot/3
"djangorestframework>=3.15.2",
# Fix for https://github.com/BesLogic/releaf-canopeum/security/dependabot/15
Expand Down
8 changes: 4 additions & 4 deletions canopeum_backend/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading