Skip to content
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
7 changes: 3 additions & 4 deletions apps/authentication/services.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from operator import itemgetter
from typing import Literal

import requests
Expand Down Expand Up @@ -64,9 +63,9 @@ def handle_access_token(access_token, provider: Literal["GOOGLE"]):
response = requests.post(url=info_url, headers=headers, timeout=10)
response.raise_for_status()
user_info_dict = response.json()
given_name, family_name, email = itemgetter("given_name", "family_name", "email")(
user_info_dict
)
given_name = user_info_dict.get("given_name", "")
family_name = user_info_dict.get("family_name", "")
email = user_info_dict["email"]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the removal of itemgetter prevents crashes when given_name or family_name are missing, accessing user_info_dict["email"] directly will still raise a KeyError if the "email" key is absent. Since the email is essential for user identification and the primary goal of this PR is to prevent crashes, it is better to handle its absence gracefully by raising a ValidationError.

    email = user_info_dict.get("email")
    if not email:
        raise ValidationError({"error": "Email not provided by the authentication provider."})

root_user, is_created = RootUser.objects.get_or_create(
email=email,
)
Expand Down
1 change: 0 additions & 1 deletion apps/organization/migrations/0002_organization_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class Migration(migrations.Migration):

dependencies = [
("organization", "0001_initial"),
]
Expand Down
Loading