Skip to content
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

feat: Set is first login value inside token #1195

Merged
merged 3 commits into from
Mar 19, 2024
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
8 changes: 6 additions & 2 deletions terraso_backend/apps/auth/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, extra_params={}):
return self.create_token(
user,
self.JWT_ACCESS_EXP_DELTA_SECONDS,
{**extra_params, "access": True},
)

def verify_access_token(self, token):
decoded = self._verify_token(token)
Expand Down
7 changes: 4 additions & 3 deletions terraso_backend/apps/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
Expand Down Expand Up @@ -244,8 +245,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, 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")

Expand Down
16 changes: 16 additions & 0 deletions terraso_backend/tests/auth/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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_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)
)
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["isFirstLogin"] is True
Loading