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: GSGGR-262 Also update user identity when login from OIDC #92

Merged
merged 2 commits into from
Feb 13, 2025
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
12 changes: 8 additions & 4 deletions oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
from django.contrib.auth import get_user_model
from api.models import Identity

UserModel = get_user_model()

Expand All @@ -22,7 +23,13 @@ def _updateUser(user, claims):
user.email = claims.get("email")
user.first_name = claims.get("given_name")
user.last_name = claims.get("family_name")
return user
identity, _ = Identity.objects.get_or_create(user=user)
if not identity.email:
identity.email = claims.get("email")
identity.first_name = claims.get("given_name")
identity.last_name = claims.get("family_name")
identity.save()
user.save()


def _read_private_key(keyfile):
Expand Down Expand Up @@ -88,8 +95,6 @@ def post(self, request):
except UserModel.DoesNotExist:
user = UserModel.objects.create_user(username=user_data["email"])
_updateUser(user, user_data)
user.save()

token = RefreshToken.for_user(user)
return JsonResponse({"access": str(token.access_token), "refresh": str(token)})

Expand All @@ -104,7 +109,6 @@ def authenticate_header(self, request):
def create_user(self, claims):
user = self.UserModel.objects.create_user(username=claims.get("email"))
_updateUser(user, claims)
user.save()
return user

def update_user(self, user, claims):
Expand Down
Loading
Loading