From 741ac4cc2e0df1093da1f96d7a3c3b255613ea61 Mon Sep 17 00:00:00 2001 From: Willem Meints <1550763+wmeints@users.noreply.github.com> Date: Sun, 7 Apr 2024 12:56:37 +0200 Subject: [PATCH] Add support for stateless user authentication in SimpleJWT (#1221) * Add support for stateless user authentication in SimpleJWT * Add additional viewset to verify simpleJWT extension * Fix linting errors * Sort imports * make isort happy --------- Co-authored-by: T. Franzel --- drf_spectacular/contrib/rest_framework_simplejwt.py | 4 ++++ tests/contrib/test_simplejwt.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/drf_spectacular/contrib/rest_framework_simplejwt.py b/drf_spectacular/contrib/rest_framework_simplejwt.py index 3decfdec..460d42b9 100644 --- a/drf_spectacular/contrib/rest_framework_simplejwt.py +++ b/drf_spectacular/contrib/rest_framework_simplejwt.py @@ -81,3 +81,7 @@ def get_security_definition(self, auto_schema): class SimpleJWTTokenUserScheme(SimpleJWTScheme): target_class = 'rest_framework_simplejwt.authentication.JWTTokenUserAuthentication' + + +class SimpleJWTStatelessUserScheme(SimpleJWTScheme): + target_class = "rest_framework_simplejwt.authentication.JWTStatelessUserAuthentication" diff --git a/tests/contrib/test_simplejwt.py b/tests/contrib/test_simplejwt.py index bb7b8ad2..690414c4 100644 --- a/tests/contrib/test_simplejwt.py +++ b/tests/contrib/test_simplejwt.py @@ -8,7 +8,7 @@ try: from rest_framework_simplejwt.authentication import ( - JWTAuthentication, JWTTokenUserAuthentication, + JWTAuthentication, JWTStatelessUserAuthentication, JWTTokenUserAuthentication, ) from rest_framework_simplejwt.views import ( TokenObtainPairView, TokenObtainSlidingView, TokenRefreshView, TokenVerifyView, @@ -34,8 +34,14 @@ class X2Viewset(mixins.ListModelMixin, viewsets.GenericViewSet): required_scopes = ['x:read', 'x:write'] +class X3Viewset(mixins.ListModelMixin, viewsets.GenericViewSet): + serializer_class = XSerializer + authentication_classes = [JWTStatelessUserAuthentication] + required_scopes = ['x:read', 'x:write'] + + @pytest.mark.contrib('rest_framework_simplejwt') -@pytest.mark.parametrize('view', [XViewset, X2Viewset]) +@pytest.mark.parametrize('view', [XViewset, X2Viewset, X3Viewset]) def test_simplejwt(no_warnings, view): router = routers.SimpleRouter() router.register('x', view, basename="x")