Skip to content

Migrate to simple jwt #790

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

Merged
merged 3 commits into from
Apr 26, 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
2 changes: 1 addition & 1 deletion {{ cookiecutter.name }}/mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ plugins =
[mypy.plugins.django-stubs]
django_settings_module = "app.settings"

[mypy-rest_framework_jwt.*]
[mypy-rest_framework_simplejwt.*]
ignore_missing_imports = on

[mypy-app.testing.api.*]
Expand Down
54 changes: 27 additions & 27 deletions {{ cookiecutter.name }}/poetry.lock

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

1 change: 1 addition & 0 deletions {{ cookiecutter.name }}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ django-split-settings = "^1.3.2"
django-storages = "^1.14.4"
djangorestframework = "^3.15.2"
djangorestframework-camel-case = "^1.4.2"
djangorestframework-simplejwt = {extras = ["crypto"], version = "^5.5.0"}
drf-jwt = "^1.19.2"
drf-spectacular = {extras = ["sidecar"], version = "^0.27.2"}
pillow = "^10.1.0"
Expand Down
6 changes: 6 additions & 0 deletions {{ cookiecutter.name }}/src/a12n/api/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.utils.translation import gettext_lazy as _
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer


class TokenObtainPairWithProperMessageSerializer(TokenObtainPairSerializer):
default_error_messages = {"no_active_account": _("Invalid username or password.")}
6 changes: 4 additions & 2 deletions {{ cookiecutter.name }}/src/a12n/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.urls import path
from rest_framework_simplejwt import views as jwt

from a12n.api import views


app_name = "a12n"

urlpatterns = [
path("token/", views.ObtainJSONWebTokenView.as_view()),
path("token/refresh/", views.RefreshJSONWebTokenView.as_view()),
path("token/", views.TokenObtainPairView.as_view(), name="auth_obtain_pair"),
path("token/refresh/", views.TokenRefreshView.as_view(), name="auth_refresh"),
path("logout/", jwt.TokenBlacklistView.as_view(), name="auth_logout"),
]
6 changes: 3 additions & 3 deletions {{ cookiecutter.name }}/src/a12n/api/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework_jwt import views as jwt
from rest_framework_simplejwt import views as jwt

from a12n.api.throttling import AuthAnonRateThrottle


class ObtainJSONWebTokenView(jwt.ObtainJSONWebTokenView):
class TokenObtainPairView(jwt.TokenObtainPairView):
throttle_classes = [AuthAnonRateThrottle]


class RefreshJSONWebTokenView(jwt.RefreshJSONWebTokenView):
class TokenRefreshView(jwt.TokenRefreshView):
throttle_classes = [AuthAnonRateThrottle]
17 changes: 17 additions & 0 deletions {{ cookiecutter.name }}/src/a12n/tests/jwt_views/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from rest_framework_simplejwt.tokens import RefreshToken


@pytest.fixture
def user(factory):
user = factory.user(username="jwt-tester-user")
user.set_password("sn00pd0g")
user.save()

return user


@pytest.fixture
def initial_token_pair(user):
refresh = RefreshToken.for_user(user)
return {"refresh": str(refresh), "access": str(refresh.access_token)}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest
from rest_framework_simplejwt.token_blacklist.models import BlacklistedToken


pytestmark = [
pytest.mark.django_db,
pytest.mark.usefixtures("user"),
]


@pytest.fixture
def logout(as_anon):
def _logout(token, expected_status=200):
return as_anon.post(
"/api/v1/auth/logout/",
{
"refresh": token,
},
expected_status=expected_status,
)

return _logout


def test_logout_token_saved_to_blacklist(logout, initial_token_pair):
logout(initial_token_pair["refresh"])

assert BlacklistedToken.objects.get(token__token=initial_token_pair["refresh"])


def test_logout_refresh_token_impossible_to_reuse(initial_token_pair, logout, as_anon):
logout(initial_token_pair["refresh"])

result = as_anon.post(
path="/api/v1/auth/token/refresh/",
data={"refresh": initial_token_pair["refresh"]},
expected_status=401,
)

assert "blacklisted" in result["detail"]
Loading