diff --git a/terraso_backend/apps/auth/services.py b/terraso_backend/apps/auth/services.py index 2db415a77..a41601653 100644 --- a/terraso_backend/apps/auth/services.py +++ b/terraso_backend/apps/auth/services.py @@ -150,8 +150,12 @@ 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): - return self.create_token(user, self.JWT_ACCESS_EXP_DELTA_SECONDS, {"access": True}) + def create_access_token(self, user, created_with_service=None): + return self.create_token( + user, + self.JWT_ACCESS_EXP_DELTA_SECONDS, + {"access": True, "createdWithService": created_with_service}, + ) def verify_access_token(self, token): decoded = self._verify_token(token) diff --git a/terraso_backend/apps/auth/views.py b/terraso_backend/apps/auth/views.py index 9d61783da..de9bfb6ac 100644 --- a/terraso_backend/apps/auth/views.py +++ b/terraso_backend/apps/auth/views.py @@ -108,7 +108,7 @@ def process_callback(self, req): try: user, created_with_service = self.process_signup() - access_token, refresh_token = terraso_login(self.request, user) + access_token, refresh_token = terraso_login(self.request, user, created_with_service) except Exception as exc: logger.exception("Error attempting create access and refresh tokens") return HttpResponse(f"Error: {exc}", status=400) @@ -244,8 +244,8 @@ def post(self, request, *args, **kwargs): return HttpResponse("OK", status=200) -def terraso_login(request, user): - access_token = jwt_service.create_access_token(user) +def terraso_login(request, user, created_with_service=None): + access_token = jwt_service.create_access_token(user, created_with_service) 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 da15d185c..e3aa47488 100644 --- a/terraso_backend/tests/auth/test_views.py +++ b/terraso_backend/tests/auth/test_views.py @@ -24,6 +24,7 @@ from oauth2_provider.views import UserInfoView from apps.auth.providers import AppleProvider, GoogleProvider +from apps.auth.services import JWTService from apps.core.models import User pytestmark = pytest.mark.django_db @@ -259,3 +260,18 @@ def test_userinfo_claims(rf, bearer_header, user): assert resp.status_code == 200 resp_json = json.loads(resp.content) assert resp_json["email"] == user.email + + +@mock_aws +def test_sign_up_created_with_service(client, access_tokens_google, respx_mock): + respx_mock.post(GoogleProvider.GOOGLE_TOKEN_URI).mock( + return_value=Response(200, json=access_tokens_google) + ) + url = reverse("terraso_auth:google-callback") + response = client.get(url, {"code": "testing-code-google-auth"}) + + token = response.cookies.get("atoken") + + decoded_payload = JWTService().verify_access_token(token.value) + + assert decoded_payload["createdWithService"] == "google"