From 8f83b41511eacb0046401989cfa292395f1a76be Mon Sep 17 00:00:00 2001 From: Youssef AbouEgla <57115457+YoussefEgla@users.noreply.github.com> Date: Sat, 20 Apr 2024 23:24:10 +0200 Subject: [PATCH 1/4] Update setup.py (#607) From c1082d0f1aa501263de7b46d855c253d3739071a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ng=C3=B4=20Thanh=20L=E1=BB=A3i=20=28leonncinnamon=29?= Date: Mon, 29 Apr 2024 12:42:55 +0700 Subject: [PATCH 2/4] fix: typo (#621) --- docs/installation.rst | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index ebd0f297..290c5d46 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -182,12 +182,12 @@ If you are using GitHub for your social authentication, it uses code and not Acc ..., path('dj-rest-auth/github/', GitHubLogin.as_view(), name='github_login') ] - - + + Google ###### If you are using Google for your social authentication, you can choose ``Authorization Code Grant`` or ``Implicit Grant`` (deprecated). -Serializer of dj-rest-auth accepts both ``code`` and ``token`` +Serializer of dj-rest-auth accepts both ``code`` and ``token`` 1. Add ``allauth.socialaccount`` and ``allauth.socialaccount.providers.google`` apps to INSTALLED_APPS in your django settings.py: @@ -208,7 +208,7 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` 'allauth.socialaccount.providers.google', ) - + 3. Create new view as a subclass of ``dj_rest_auth.views.SocialLoginView`` with ``GoogleOAuth2Adapter`` adapter, an ``OAuth2Client`` and a callback_url as attributes: .. code-block:: python @@ -216,12 +216,12 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` from allauth.socialaccount.providers.google.views import GoogleOAuth2Adapter from allauth.socialaccount.providers.oauth2.client import OAuth2Client from dj_rest_auth.registration.views import SocialLoginView - + class GoogleLogin(SocialLoginView): # if you want to use Authorization Code Grant, use this adapter_class = GoogleOAuth2Adapter callback_url = CALLBACK_URL_YOU_SET_ON_GOOGLE client_class = OAuth2Client - + class GoogleLogin(SocialLoginView): # if you want to use Implicit Grant, use this adapter_class = GoogleOAuth2Adapter @@ -234,7 +234,7 @@ Serializer of dj-rest-auth accepts both ``code`` and ``token`` path('dj-rest-auth/google/', GoogleLogin.as_view(), name='google_login') ] -5. Retrive code (or token) +5. Retrieve code (or token) By accessing Google's endpoint, you can get ``code`` or ``token`` If you're using Authorization Code Grant, you can get code from following URL From 429a27032922a6077c3e8209ca4f1caa929ecf80 Mon Sep 17 00:00:00 2001 From: pomali Date: Mon, 8 Jul 2024 00:23:41 +0200 Subject: [PATCH 3/4] Fix extras naming `with_social` -> `with-social` (#635) to conform with Core metadata specification https://packaging.python.org/en/latest/specifications/core-metadata/#provides-extra-multiple-use --- docs/installation.rst | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/installation.rst b/docs/installation.rst index 290c5d46..c38a3ad2 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -44,7 +44,7 @@ You're good to go now! Registration (optional) ----------------------- -1. If you want to enable standard registration process you will need to install ``django-allauth`` by using ``pip install 'dj-rest-auth[with_social]'``. +1. If you want to enable standard registration process you will need to install ``django-allauth`` by using ``pip install 'dj-rest-auth[with-social]'``. 2. Add ``django.contrib.sites``, ``allauth``, ``allauth.account``, ``allauth.socialaccount`` and ``dj_rest_auth.registration`` apps to INSTALLED_APPS in your django settings.py: diff --git a/setup.py b/setup.py index 91f50c4b..63353b8c 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,7 @@ 'djangorestframework>=3.13.0', ], extras_require={ - 'with_social': ['django-allauth>=0.56.0,<0.62.0'], + 'with-social': ['django-allauth>=0.56.0,<0.62.0'], }, tests_require=[ 'coveralls>=1.11.1', From 5498963cb3b038cd0bdd95e124ed9e6e6757c196 Mon Sep 17 00:00:00 2001 From: Maria Khrustaleva Date: Sat, 13 Jul 2024 15:47:47 +0200 Subject: [PATCH 4/4] Fix creating user token during the registration process when both Token authentication and Session authentication are used simultaneously (#605) * Fix creating user token when both Token authentication and Session authentication are used simultaneously * Fix APIBasicTests::test_registration_with_session test --- dj_rest_auth/registration/views.py | 10 ++++------ dj_rest_auth/tests/test_api.py | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/dj_rest_auth/registration/views.py b/dj_rest_auth/registration/views.py index 10f1f325..8079a111 100644 --- a/dj_rest_auth/registration/views.py +++ b/dj_rest_auth/registration/views.py @@ -53,10 +53,10 @@ def get_response_data(self, user): 'refresh': self.refresh_token, } return api_settings.JWT_SERIALIZER(data, context=self.get_serializer_context()).data - elif api_settings.SESSION_LOGIN: - return None - else: + elif self.token_model: return api_settings.TOKEN_SERIALIZER(user.auth_token, context=self.get_serializer_context()).data + + return None def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) @@ -82,9 +82,7 @@ def perform_create(self, serializer): allauth_account_settings.EmailVerificationMethod.MANDATORY: if api_settings.USE_JWT: self.access_token, self.refresh_token = jwt_encode(user) - elif not api_settings.SESSION_LOGIN: - # Session authentication isn't active either, so this has to be - # token authentication + elif self.token_model: api_settings.TOKEN_CREATOR(self.token_model, user, serializer) complete_signup( diff --git a/dj_rest_auth/tests/test_api.py b/dj_rest_auth/tests/test_api.py index 571669ed..41cc7555 100644 --- a/dj_rest_auth/tests/test_api.py +++ b/dj_rest_auth/tests/test_api.py @@ -530,12 +530,28 @@ def test_registration_with_jwt(self): @override_api_settings(SESSION_LOGIN=True) @override_api_settings(TOKEN_MODEL=None) def test_registration_with_session(self): + import sys + from importlib import reload + from django.contrib.sessions.middleware import SessionMiddleware + from django.contrib.messages.middleware import MessageMiddleware + reload(sys.modules['dj_rest_auth.models']) + reload(sys.modules['dj_rest_auth.registration.views']) + from dj_rest_auth.registration.views import RegisterView + user_count = get_user_model().objects.all().count() self.post(self.register_url, data={}, status_code=400) - result = self.post(self.register_url, data=self.REGISTRATION_DATA, status_code=204) - self.assertEqual(result.data, None) + factory = APIRequestFactory() + request = factory.post(self.register_url, self.REGISTRATION_DATA) + + for middleware_class in (SessionMiddleware, MessageMiddleware): + middleware = middleware_class(lambda request: None) + middleware.process_request(request) + + response = RegisterView.as_view()(request) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(response.data, None) self.assertEqual(get_user_model().objects.all().count(), user_count + 1) self._login(status.HTTP_204_NO_CONTENT)