From f5d8604af5facfc8779b9458be2ca1f8d0070e43 Mon Sep 17 00:00:00 2001 From: Jose Buitron Date: Tue, 12 Mar 2024 10:05:15 -0500 Subject: [PATCH] fix: Added isFirstLogin token param --- terraso_backend/apps/auth/services.py | 4 ++-- terraso_backend/apps/auth/views.py | 7 ++++--- terraso_backend/tests/auth/test_views.py | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/terraso_backend/apps/auth/services.py b/terraso_backend/apps/auth/services.py index a41601653..c90e42e59 100644 --- a/terraso_backend/apps/auth/services.py +++ b/terraso_backend/apps/auth/services.py @@ -150,11 +150,11 @@ def create_test_access_token(self, user): raise ValueError("User is not a test user") return self.create_token(user, None, {"test": True, "access": True}) - def create_access_token(self, user, created_with_service=None): + def create_access_token(self, user, extra_params={}): return self.create_token( user, self.JWT_ACCESS_EXP_DELTA_SECONDS, - {"access": True, "createdWithService": created_with_service}, + {**extra_params, "access": True}, ) def verify_access_token(self, token): diff --git a/terraso_backend/apps/auth/views.py b/terraso_backend/apps/auth/views.py index de9bfb6ac..40bc1d5d8 100644 --- a/terraso_backend/apps/auth/views.py +++ b/terraso_backend/apps/auth/views.py @@ -108,7 +108,8 @@ def process_callback(self, req): try: user, created_with_service = self.process_signup() - access_token, refresh_token = terraso_login(self.request, user, created_with_service) + is_first_login = created_with_service is not None + access_token, refresh_token = terraso_login(self.request, user, is_first_login) except Exception as exc: logger.exception("Error attempting create access and refresh tokens") return HttpResponse(f"Error: {exc}", status=400) @@ -244,8 +245,8 @@ def post(self, request, *args, **kwargs): return HttpResponse("OK", status=200) -def terraso_login(request, user, created_with_service=None): - access_token = jwt_service.create_access_token(user, created_with_service) +def terraso_login(request, user, is_first_login=False): + access_token = jwt_service.create_access_token(user, {"isFirstLogin": is_first_login}) refresh_token = jwt_service.create_refresh_token(user) dj_login(request, user, backend="django.contrib.auth.backends.ModelBackend") diff --git a/terraso_backend/tests/auth/test_views.py b/terraso_backend/tests/auth/test_views.py index e3aa47488..7fdae042a 100644 --- a/terraso_backend/tests/auth/test_views.py +++ b/terraso_backend/tests/auth/test_views.py @@ -263,7 +263,7 @@ def test_userinfo_claims(rf, bearer_header, user): @mock_aws -def test_sign_up_created_with_service(client, access_tokens_google, respx_mock): +def test_sign_up_is_first_login(client, access_tokens_google, respx_mock): respx_mock.post(GoogleProvider.GOOGLE_TOKEN_URI).mock( return_value=Response(200, json=access_tokens_google) ) @@ -274,4 +274,4 @@ def test_sign_up_created_with_service(client, access_tokens_google, respx_mock): decoded_payload = JWTService().verify_access_token(token.value) - assert decoded_payload["createdWithService"] == "google" + assert decoded_payload["isFirstLogin"] is True