Skip to content

Commit

Permalink
fix: error when field locale was not available on Google API response.
Browse files Browse the repository at this point in the history
If you need to define a default value for this field, please use the `GOOGLE_SSO_DEFAULT_LOCALE` option.

Also make these fields optional: `given_name`, `given_name` and `picture`

Resolves #31
  • Loading branch information
chrismaille committed Mar 14, 2024
1 parent e4f457e commit 646d986
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions django_google_sso/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
GOOGLE_SSO_ALWAYS_UPDATE_USER_DATA = getattr(
settings, "GOOGLE_SSO_ALWAYS_UPDATE_USER_DATA", False
)
GOOGLE_SSO_DEFAULT_LOCALE = getattr(settings, "GOOGLE_SSO_DEFAULT_LOCALE", "en")
SSO_USE_ALTERNATE_W003 = getattr(settings, "SSO_USE_ALTERNATE_W003", False)

if SSO_USE_ALTERNATE_W003:
Expand Down
9 changes: 5 additions & 4 deletions django_google_sso/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,18 @@ def get_or_create_user(self):
user=user,
defaults={
"google_id": self.user_info["id"],
"picture_url": self.user_info["picture"],
"locale": self.user_info["locale"],
"picture_url": self.user_info.get("picture"),
"locale": self.user_info.get("locale")
or conf.GOOGLE_SSO_DEFAULT_LOCALE,
},
)
return user

def check_for_update(self, created, user):
if created or conf.GOOGLE_SSO_ALWAYS_UPDATE_USER_DATA:
self.check_for_permissions(user)
user.first_name = self.user_info["given_name"]
user.last_name = self.user_info["family_name"]
user.first_name = self.user_info.get("given_name")
user.last_name = self.user_info.get("family_name")
user.username = self.user_email
user.set_unusable_password()
self.user_changed = True
Expand Down
23 changes: 23 additions & 0 deletions django_google_sso/tests/test_user_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
from copy import deepcopy

import pytest

Expand Down Expand Up @@ -117,3 +118,25 @@ def test_create_super_user_from_list(google_response, callback_request, settings
assert user.is_active is True
assert user.is_staff is True
assert user.is_superuser is True


def test_different_null_values(google_response, callback_request, monkeypatch):
# Arrange
monkeypatch.setattr(conf, "GOOGLE_SSO_DEFAULT_LOCALE", "pt_BR")
google_response_no_key = deepcopy(google_response)
del google_response_no_key["locale"]
google_response_key_none = deepcopy(google_response)
google_response_key_none["locale"] = None

# Act
no_key_helper = UserHelper(google_response_no_key, callback_request)
no_key_helper.get_or_create_user()
user_one = no_key_helper.find_user()

none_key_helper = UserHelper(google_response_key_none, callback_request)
none_key_helper.get_or_create_user()
user_two = none_key_helper.find_user()

# Assert
assert user_one.googlessouser.locale == "pt_BR"
assert user_two.googlessouser.locale == "pt_BR"
1 change: 1 addition & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| `GOOGLE_SSO_CALLBACK_DOMAIN` | The netloc to be used on Callback URI. Default: `None` |
| `GOOGLE_SSO_CLIENT_ID` | The Google OAuth 2.0 Web Application Client ID. Default: `None` |
| `GOOGLE_SSO_CLIENT_SECRET` | The Google OAuth 2.0 Web Application Client Secret. Default: `None` |
| `GOOGLE_SSO_DEFAULT_LOCALE` | Default code for Google locale. Default: `en` |
| `GOOGLE_SSO_ENABLED` | Enable or disable the plugin. Default: `True` |
| `GOOGLE_SSO_LOGIN_FAILED_URL` | The named url path that the user will be redirected to if an authentication error is encountered. Default: `admin:index` |
| `GOOGLE_SSO_LOGO_URL` | The URL of the logo to be used on the login button. Default: `https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/512px-Google_%22G%22_Logo.svg.png` |
Expand Down

0 comments on commit 646d986

Please sign in to comment.