-
Notifications
You must be signed in to change notification settings - Fork 48
feat: modernize JWT auth (SimpleJWT) + fix CI + docs #209
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
Open
kamrankhan78694
wants to merge
5
commits into
Coders-HQ:main
Choose a base branch
from
kamrankhan78694:modernize-jwt-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5693818
feat(api): modernize JWT auth
kamrankhan78694 5bc7ba4
chore: drop accidental package-lock change
kamrankhan78694 0879ae7
Update codershq/api/auth_views.py
kamrankhan78694 51cc200
chore: keep PR scoped (no hiredis bump)
kamrankhan78694 edfb4bd
feat: modernize JWT auth + fix CI linter + update docs
kamrankhan78694 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from rest_framework.permissions import AllowAny | ||
| from rest_framework.response import Response | ||
| from rest_framework.views import APIView | ||
| from rest_framework_simplejwt.serializers import TokenObtainPairSerializer | ||
|
|
||
|
|
||
| class LegacyTokenObtainView(APIView): | ||
| """Backwards-compatible JWT endpoint. | ||
|
|
||
| Historically this project exposed `/api-token-auth/` (drf-jwt) which returned | ||
| `{"token": "..."}`. SimpleJWT returns `{access, refresh}`. | ||
|
|
||
| This view preserves the legacy response shape while also returning the | ||
| modern fields. | ||
| """ | ||
|
|
||
| permission_classes = [AllowAny] | ||
|
|
||
| def post(self, request, *args, **kwargs): | ||
| serializer = TokenObtainPairSerializer(data=request.data) | ||
| serializer.is_valid(raise_exception=True) | ||
| tokens = serializer.validated_data | ||
| access = tokens.get("access") | ||
| refresh = tokens.get("refresh") | ||
| return Response({"token": access, "access": access, "refresh": refresh}) | ||
|
kamrankhan78694 marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,62 +1,68 @@ | ||
| from django.contrib.auth import get_user_model | ||
| from django.contrib.auth.models import Group, User | ||
| from django.contrib.auth.password_validation import validate_password | ||
| from djangosaml2idp.models import ServiceProvider | ||
| from rest_framework import serializers | ||
| from rest_framework.validators import UniqueValidator | ||
|
|
||
| from codershq.users.models import User | ||
| from codershq.portfolio.models import Portfolio | ||
|
|
||
| User=get_user_model() # to point to the custom user model | ||
| User = get_user_model() | ||
|
|
||
|
|
||
| class UserSerializer(serializers.HyperlinkedModelSerializer): | ||
| class Meta: | ||
| model = User | ||
| fields = ['url', 'username', 'email', 'groups','id'] | ||
| fields = ["url", "username", "email", "groups", "id"] | ||
|
|
||
|
|
||
| class PortfolioSerializer(serializers.ModelSerializer): | ||
| class Meta: | ||
| model = Portfolio | ||
| fields = '__all__' | ||
| fields = "__all__" | ||
|
|
||
|
|
||
| class RegisterSerializer(serializers.ModelSerializer): | ||
|
|
||
| email = serializers.EmailField( | ||
| required=True, | ||
| validators=[UniqueValidator(queryset=User.objects.all())] | ||
| ) | ||
| required=True, validators=[UniqueValidator(queryset=User.objects.all())] | ||
| ) | ||
|
|
||
| password = serializers.CharField(write_only=True, required=True, validators=[validate_password]) | ||
| password = serializers.CharField( | ||
| write_only=True, required=True, validators=[validate_password] | ||
| ) | ||
| password2 = serializers.CharField(write_only=True, required=True) | ||
|
|
||
|
|
||
|
|
||
| class Meta: | ||
| model = User | ||
| fields = ('username', 'password', 'password2', 'email', 'first_name', 'last_name') | ||
| fields = ( | ||
| "username", | ||
| "password", | ||
| "password2", | ||
| "email", | ||
| "first_name", | ||
| "last_name", | ||
| ) | ||
| extra_kwargs = { | ||
| 'first_name': {'required': True}, | ||
| 'last_name': {'required': True} | ||
| "first_name": {"required": True}, | ||
| "last_name": {"required": True}, | ||
| } | ||
|
|
||
| def validate(self, attrs): | ||
| if attrs['password'] != attrs['password2']: | ||
| raise serializers.ValidationError({"password": "Password fields didn't match."}) | ||
| if attrs["password"] != attrs["password2"]: | ||
| raise serializers.ValidationError( | ||
| {"password": "Password fields didn't match."} | ||
| ) | ||
|
|
||
| return attrs | ||
|
|
||
| def create(self, validated_data): | ||
| user = User.objects.create( | ||
| username=validated_data['username'], | ||
| email=validated_data['email'], | ||
| first_name=validated_data['first_name'], | ||
| last_name=validated_data['last_name'] | ||
| username=validated_data["username"], | ||
| email=validated_data["email"], | ||
| first_name=validated_data["first_name"], | ||
| last_name=validated_data["last_name"], | ||
| ) | ||
|
|
||
|
|
||
| user.set_password(validated_data['password']) | ||
| user.set_password(validated_data["password"]) | ||
| user.save() | ||
|
|
||
| return user |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The flake8 version has been upgraded from 4.0.1 to 7.1.1, which is a major version jump (3 major versions). This is a significant upgrade that may introduce new linting rules or behavior changes. Ensure that all team members are aware of this upgrade and that any new linting errors are addressed. Consider documenting this change in the PR description or migration notes.