Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Call user events in OIDC (#15535)

Iga Karbowiakefed5ac4b1dc
diff --git a/saleor/account/tests/test_utils.py b/saleor/account/tests/test_utils.py
index 7855a703a1..065379400f 100644
--- a/saleor/account/tests/test_utils.py
+++ b/saleor/account/tests/test_utils.py
@@ -5,6 +5,7 @@ from django.test import override_settings
from ...checkout import AddressType
from ...plugins.manager import get_plugins_manager
+from ...tests.utils import flush_post_commit_hooks
from ..models import Address, User
from ..utils import (
get_user_groups_permissions,
@@ -13,6 +14,7 @@ from ..utils import (
remove_the_oldest_user_address,
remove_the_oldest_user_address_if_address_limit_is_reached,
retrieve_user_by_email,
+ send_user_event,
store_user_address,
)
@@ -237,3 +239,113 @@ def get_user_groups_permissions_user(
# then
assert permissions.count() == 2
+
+
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.staff_created")
+def test_send_user_event_no_webhook_sent(
+ mock_staff_created_webhook,
+ mock_staff_updated_webhook,
+ mock_customer_created_webhook,
+ mock_customer_updated_webhook,
+ customer_user,
+):
+ # when
+ send_user_event(customer_user, False, False)
+
+ # then
+ flush_post_commit_hooks()
+ mock_staff_created_webhook.assert_not_called()
+ mock_staff_updated_webhook.assert_not_called()
+ mock_customer_created_webhook.assert_not_called()
+ mock_customer_updated_webhook.assert_not_called()
+
+
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.staff_created")
+def test_send_user_event_customer_created_event(
+ mock_staff_created_webhook,
+ mock_staff_updated_webhook,
+ mock_customer_created_webhook,
+ mock_customer_updated_webhook,
+ customer_user,
+):
+ # when
+ send_user_event(customer_user, True, True)
+
+ # then
+ flush_post_commit_hooks()
+ mock_customer_created_webhook.assert_called_once_with(customer_user)
+ mock_customer_updated_webhook.assert_not_called()
+ mock_staff_created_webhook.assert_not_called()
+ mock_staff_updated_webhook.assert_not_called()
+
+
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.staff_created")
+def test_send_user_event_customer_updated_event(
+ mock_staff_created_webhook,
+ mock_staff_updated_webhook,
+ mock_customer_created_webhook,
+ mock_customer_updated_webhook,
+ customer_user,
+):
+ # when
+ send_user_event(customer_user, False, True)
+
+ # then
+ flush_post_commit_hooks()
+ mock_customer_updated_webhook.assert_called_once_with(customer_user)
+ mock_customer_created_webhook.assert_not_called()
+ mock_staff_created_webhook.assert_not_called()
+ mock_staff_updated_webhook.assert_not_called()
+
+
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.staff_created")
+def test_send_user_event_staff_created_event(
+ mock_staff_created_webhook,
+ mock_staff_updated_webhook,
+ mock_customer_created_webhook,
+ mock_customer_updated_webhook,
+ staff_user,
+):
+ # when
+ send_user_event(staff_user, True, True)
+
+ # then
+ flush_post_commit_hooks()
+ mock_staff_created_webhook.assert_called_once_with(staff_user)
+ mock_staff_updated_webhook.assert_not_called()
+ mock_customer_created_webhook.assert_not_called()
+ mock_customer_updated_webhook.assert_not_called()
+
+
+@patch("saleor.plugins.manager.PluginsManager.customer_updated")
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+@patch("saleor.plugins.manager.PluginsManager.staff_updated")
+@patch("saleor.plugins.manager.PluginsManager.staff_created")
+def test_send_user_event_staff_updated_event(
+ mock_staff_created_webhook,
+ mock_staff_updated_webhook,
+ mock_customer_created_webhook,
+ mock_customer_updated_webhook,
+ staff_user,
+):
+ # when
+ send_user_event(staff_user, False, True)
+
+ # then
+ flush_post_commit_hooks()
+ mock_staff_updated_webhook.assert_called_once_with(staff_user)
+ mock_staff_created_webhook.assert_not_called()
+ mock_customer_created_webhook.assert_not_called()
+ mock_customer_updated_webhook.assert_not_called()
diff --git a/saleor/account/utils.py b/saleor/account/utils.py
index 0c9eae6681..1a268be936 100644
--- a/saleor/account/utils.py
+++ b/saleor/account/utils.py
@@ -4,7 +4,9 @@ from django.conf import settings
from django.db.models import Exists, OuterRef
from ..checkout import AddressType
+from ..core.utils.events import call_event
from ..permission.models import Permission
+from ..plugins.manager import get_plugins_manager
from .models import Group, User
if TYPE_CHECKING:
@@ -143,3 +145,15 @@ def get_user_groups_permissions(user: User):
return Permission.objects.filter(
Exists(group_permissions.filter(permission_id=OuterRef("id")))
)
+
+
+def send_user_event(user: User, created: bool, updated: bool):
+ """Send created or updated event for user."""
+ manager = get_plugins_manager(allow_replica=False)
+ event = None
+ if created:
+ event = manager.staff_created if user.is_staff else manager.customer_created
+ elif updated:
+ event = manager.staff_updated if user.is_staff else manager.customer_updated
+ if event:
+ call_event(event, user)
diff --git a/saleor/plugins/openid_connect/plugin.py b/saleor/plugins/openid_connect/plugin.py
index bc5151ea4e..cdb4794ebc 100644
--- a/saleor/plugins/openid_connect/plugin.py
+++ b/saleor/plugins/openid_connect/plugin.py
@@ -41,6 +41,7 @@ from .utils import (
get_user_from_oauth_access_token,
get_user_from_token,
is_owner_of_token_valid,
+ send_user_event,
validate_refresh_token,
)
@@ -295,7 +296,7 @@ class OpenIDConnectPlugin(BasePlugin):
token_data, self.config.json_web_key_set_url
)
- user = get_or_create_user_from_payload(
+ user, user_created, user_updated = get_or_create_user_from_payload(
parsed_id_token,
self.config.authorization_url,
)
@@ -314,11 +315,16 @@ class OpenIDConnectPlugin(BasePlugin):
)
if not user.is_staff:
user.is_staff = True
+ user_updated = True
user.save(update_fields=["is_staff"])
elif user.is_staff and not is_staff_user:
user.is_staff = False
+ user_updated = True
user.save(update_fields=["is_staff"])
+ if user_created or user_updated:
+ send_user_event(user, user_created, user_updated)
+
tokens = create_tokens_from_oauth_payload(
token_data, user, parsed_id_token, user_permissions, owner=self.PLUGIN_ID
)
diff --git a/saleor/plugins/openid_connect/tests/test_plugin.py b/saleor/plugins/openid_connect/tests/test_plugin.py
index 38b0abfe22..870efd31d2 100644
--- a/saleor/plugins/openid_connect/tests/test_plugin.py
+++ b/saleor/plugins/openid_connect/tests/test_plugin.py
@@ -416,7 +416,7 @@ def test_external_obtain_access_tokens(
oauth_payload,
plugin.config.json_web_key_set_url,
)
- user = get_or_create_user_from_payload(
+ user, _, _ = get_or_create_user_from_payload(
claims,
oauth_url="https://saleor.io/oauth",
)
@@ -487,7 +487,7 @@ def test_external_obtain_access_tokens_with_permissions(
oauth_payload,
plugin.config.json_web_key_set_url,
)
- user = get_or_create_user_from_payload(claims, "https://saleor.io/oauth")
+ user, _, _ = get_or_create_user_from_payload(claims, "https://saleor.io/oauth")
user.is_staff = True
expected_tokens = create_tokens_from_oauth_payload(
oauth_payload,
@@ -512,12 +512,14 @@ def test_external_obtain_access_tokens_with_permissions(
@freeze_time("2019-03-18 12:00:00")
+@patch("saleor.plugins.openid_connect.plugin.send_user_event")
@patch("saleor.plugins.openid_connect.utils.cache.set")
@patch("saleor.plugins.openid_connect.utils.cache.get")
@pytest.mark.vcr
def test_external_obtain_access_tokens_with_saleor_staff(
mocked_cache_get,
mocked_cache_set,
+ mock_send_user_event,
openid_plugin,
monkeypatch,
rf,
@@ -566,7 +568,7 @@ def test_external_obtain_access_tokens_with_saleor_staff(
oauth_payload,
plugin.config.json_web_key_set_url,
)
- user = get_or_create_user_from_payload(
+ user, _, _ = get_or_create_user_from_payload(
claims,
"https://saleor.io/oauth",
)
@@ -586,15 +588,18 @@ def test_external_obtain_access_tokens_with_saleor_staff(
decoded_refresh_token = jwt_decode(tokens.refresh_token)
assert tokens.csrf_token == decoded_refresh_token["csrf_token"]
assert decoded_refresh_token["oauth_refresh_token"] == "refresh"
+ mock_send_user_event.assert_called_once_with(user, True, True)
@freeze_time("2019-03-18 12:00:00")
+@patch("saleor.plugins.openid_connect.plugin.send_user_event")
@patch("saleor.plugins.openid_connect.utils.cache.set")
@patch("saleor.plugins.openid_connect.utils.cache.get")
@pytest.mark.vcr
def test_external_obtain_access_tokens_user_which_is_no_more_staff(
mocked_cache_get,
mocked_cache_set,
+ mock_send_user_event,
openid_plugin,
monkeypatch,
rf,
@@ -648,7 +653,7 @@ def test_external_obtain_access_tokens_user_which_is_no_more_staff(
oauth_payload,
plugin.config.json_web_key_set_url,
)
- user = get_or_create_user_from_payload(claims, "https://saleor.io/oauth")
+ user, _, _ = get_or_create_user_from_payload(claims, "https://saleor.io/oauth")
staff_user.refresh_from_db()
assert staff_user == user
@@ -657,14 +662,17 @@ def test_external_obtain_access_tokens_user_which_is_no_more_staff(
decoded_access_token = jwt_decode(tokens.token)
assert decoded_access_token["permissions"] == []
assert decoded_access_token["is_staff"] is False
+ mock_send_user_event.assert_called_once_with(user, False, True)
@freeze_time("2019-03-18 12:00:00")
+@patch("saleor.plugins.openid_connect.plugin.send_user_event")
@patch("saleor.plugins.openid_connect.utils.cache.set")
@patch("saleor.plugins.openid_connect.utils.cache.get")
def test_external_obtain_access_tokens_user_created(
mocked_cache_get,
mocked_cache_set,
+ mock_send_user_event,
openid_plugin,
monkeypatch,
rf,
@@ -732,6 +740,7 @@ def test_external_obtain_access_tokens_user_created(
decoded_refresh_token = jwt_decode(tokens.refresh_token)
assert tokens.csrf_token == decoded_refresh_token["csrf_token"]
assert decoded_refresh_token["oauth_refresh_token"] == "refresh"
+ mock_send_user_event.assert_called_once_with(user, True, True)
@freeze_time("2019-03-18 12:00:00")
@@ -1118,7 +1127,9 @@ def test_authenticate_user_with_jwt_access_token(
@freeze_time("2021-03-08 12:00:00")
+@patch("saleor.plugins.openid_connect.utils.send_user_event")
def test_authenticate_user_with_jwt_access_token_which_is_no_more_staff(
+ mock_send_user_event,
openid_plugin,
decoded_access_token,
user_info_response,
@@ -1166,10 +1177,13 @@ def test_authenticate_user_with_jwt_access_token_which_is_no_more_staff(
assert user == customer_user
assert user.is_staff is False
assert list(user.effective_permissions) == []
+ mock_send_user_event.assert_called_once_with(user, False, True)
@freeze_time("2021-03-08 12:00:00")
+@patch("saleor.plugins.openid_connect.utils.send_user_event")
def test_authenticate_staff_user_with_jwt_access_token_and_staff_scope(
+ mock_send_user_event,
openid_plugin,
decoded_access_token,
user_info_response,
@@ -1222,6 +1236,7 @@ def test_authenticate_staff_user_with_jwt_access_token_and_staff_scope(
assert group.name == plugin.config.default_group_name
assert user.groups.count() == 1
assert user.groups.first() == group
+ mock_send_user_event.assert_called_once_with(user, False, True)
@freeze_time("2021-03-08 12:00:00")
diff --git a/saleor/plugins/openid_connect/tests/test_utils.py b/saleor/plugins/openid_connect/tests/test_utils.py
index 8632f75bb1..71e5f9a9e8 100644
--- a/saleor/plugins/openid_connect/tests/test_utils.py
+++ b/saleor/plugins/openid_connect/tests/test_utils.py
@@ -24,6 +24,7 @@ from ....core.jwt import (
jwt_user_payload,
)
from ....permission.models import Permission
+from ....tests.utils import flush_post_commit_hooks
from ..exceptions import AuthenticationError
from ..utils import (
JWKS_CACHE_TIME,
@@ -129,7 +130,7 @@ def test_create_tokens_from_oauth_payload(monkeypatch, id_token, id_payload):
"token_type": "Bearer",
"expires_at": 1600851112,
}
- user = get_or_create_user_from_payload(id_payload, "https://saleor.io/oauth")
+ user, _, _ = get_or_create_user_from_payload(id_payload, "https://saleor.io/oauth")
permissions = get_saleor_permissions_qs_from_scope(auth_payload.get("scope"))
perms = get_saleor_permission_names(permissions)
tokens = create_tokens_from_oauth_payload(
@@ -230,7 +231,7 @@ def test_get_or_create_user_from_payload_retrieve_user_by_sub(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
)
@@ -258,7 +259,7 @@ def test_get_or_create_user_from_payload_updates_sub(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
)
@@ -285,7 +286,7 @@ def test_get_or_create_user_from_payload_assigns_sub(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
)
@@ -314,7 +315,7 @@ def test_get_or_create_user_from_payload_creates_user_with_sub(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_email},
oauth_url=oauth_url,
)
@@ -349,7 +350,7 @@ def test_get_or_create_user_from_payload_match_orders_for_new_user(
order.save()
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_email},
oauth_url=oauth_url,
)
@@ -384,7 +385,7 @@ def test_get_or_create_user_from_payload_match_orders_when_changing_email(
order.save()
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": new_customer_email},
oauth_url=oauth_url,
)
@@ -413,7 +414,7 @@ def test_get_or_create_user_from_payload_multiple_subs(customer_user, admin_user
# when
with warnings.catch_warnings(record=True):
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
)
@@ -439,7 +440,7 @@ def test_get_or_create_user_from_payload_different_email(
customer_user.save()
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": new_customer_email},
oauth_url=oauth_url,
)
@@ -477,7 +478,7 @@ def test_get_or_create_user_from_payload_with_last_login(
customer_user.save()
# when
- user_from_payload = get_or_create_user_from_payload(
+ user_from_payload, _, _ = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
last_login=current_ts,
@@ -511,7 +512,7 @@ def test_get_or_create_user_from_payload_update_last_login(
mocked_cache_get.side_effect = lambda cache_key: None
# when
- get_or_create_user_from_payload(
+ user, created, updated = get_or_create_user_from_payload(
payload={"sub": sub_id, "email": customer_user.email},
oauth_url=oauth_url,
)
@@ -526,6 +527,7 @@ def test_get_or_create_user_from_payload_update_last_login(
assert customer_user.last_login
last_login = customer_user.last_login.strftime("%Y-%m-%d %H:%M:%S")
assert last_login == "2019-03-18 12:00:00"
+ assert updated is True
@mock.patch("saleor.plugins.openid_connect.utils.cache.set")
@@ -614,7 +616,11 @@ def test_get_or_create_user_from_payload_last_login_modifies(
assert customer_user.last_login != last_login
-def test_jwt_token_without_expiration_claim(monkeypatch, decoded_access_token):
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
+def test_jwt_token_without_expiration_claim(
+ mocked_customer_created_webhook, monkeypatch, decoded_access_token
+):
+ # given
monkeypatch.setattr(
"saleor.plugins.openid_connect.utils.get_user_info_from_cache_or_fetch",
lambda *args, **kwargs: {
@@ -628,6 +634,8 @@ def test_jwt_token_without_expiration_claim(monkeypatch, decoded_access_token):
decoded_access_token,
{},
)
+
+ # when
user = get_user_from_oauth_access_token_in_jwt_format(
token_payload,
"https://example.com",
@@ -637,14 +645,20 @@ def test_jwt_token_without_expiration_claim(monkeypatch, decoded_access_token):
staff_user_domains=[],
staff_default_group_name="",
)
+
+ # then
assert user.email == "[email protected]"
+ flush_post_commit_hooks()
+ mocked_customer_created_webhook.assert_called_once_with(user)
+@patch("saleor.plugins.manager.PluginsManager.customer_created")
@mock.patch("saleor.plugins.openid_connect.utils.cache.set")
@mock.patch("saleor.plugins.openid_connect.utils.cache.get")
def test_jwt_token_without_expiration_claim_mixed_permissions_from_group(
mocked_cache_get,
mocked_cache_set,
+ mocked_customer_created_webhook,
customer_user,
monkeypatch,
decoded_access_token,
@@ -686,6 +700,8 @@ def test_jwt_token_without_expiration_claim_mixed_permissions_from_group(
assert len(user.effective_permissions) > 1
# ensure that manage_shipping is not from openID scope permissions
assert "saleor:manage_shipping" not in token_payload["scope"]
+ flush_post_commit_hooks()
+ mocked_customer_created_webhook.assert_not_called()
@mock.patch("saleor.plugins.openid_connect.utils.cache.set")
@@ -1007,7 +1023,7 @@ def test_update_user_details_update_user_first_name(
assert customer_user.search_document != expected_search_document
# when
- _update_user_details(
+ updated = _update_user_details(
customer_user,
"test oidc_key",
customer_user.email,
@@ -1021,6 +1037,7 @@ def test_update_user_details_update_user_first_name(
customer_user.refresh_from_db()
assert customer_user.first_name == "test user_first_name"
assert customer_user.search_document == expected_search_document
+ assert updated is True
def test_update_user_details_update_user_last_name(
@@ -1032,7 +1049,7 @@ def test_update_user_details_update_user_last_name(
assert customer_user.search_document != expected_search_document
# when
- _update_user_details(
+ updated = _update_user_details(
customer_user,
"test oidc_key",
customer_user.email,
@@ -1046,3 +1063,32 @@ def test_update_user_details_update_user_last_name(
customer_user.refresh_from_db()
assert customer_user.last_name == "test user_last_name"
assert customer_user.search_document == expected_search_document
+ assert updated is True
+
+
+def test_update_user_details_nothing_changed(
+ customer_user,
+):
+ # given
+ last_login = timezone.now() - timedelta(minutes=14)
+ customer_user.last_login = last_login
+ customer_user.search_document = "abc"
+ customer_user.save(update_fields=["search_document", "last_login"])
+
+ first_name = customer_user.first_name
+
+ # when
+ updated = _update_user_details(
+ customer_user,
+ "test oidc_key",
+ customer_user.email,
+ customer_user.first_name,
+ customer_user.last_name,
+ None,
+ 5,
+ )
+
+ # then
+ customer_user.refresh_from_db()
+ assert customer_user.first_name == first_name
+ assert updated is False
diff --git a/saleor/plugins/openid_connect/utils.py b/saleor/plugins/openid_connect/utils.py
index f86a4d6b0c..1ab3a8207d 100644
--- a/saleor/plugins/openid_connect/utils.py
+++ b/saleor/plugins/openid_connect/utils.py
@@ -18,7 +18,7 @@ from jwt import PyJWTError
from ...account.models import Group, User
from ...account.search import prepare_user_search_document_value
-from ...account.utils import get_user_groups_permissions
+from ...account.utils import get_user_groups_permissions, send_user_event
from ...core.http_client import HTTPClient
from ...core.jwt import (
JWT_ACCESS_TYPE,
@@ -190,7 +190,7 @@ def get_user_from_oauth_access_token_in_jwt_format(
return None
try:
- user = get_or_create_user_from_payload(
+ user, user_created, user_updated = get_or_create_user_from_payload(
user_info,
user_info_url,
last_login=token_payload.get("iat"),
@@ -241,8 +241,12 @@ def get_user_from_oauth_access_token_in_jwt_format(
if is_staff is not None:
user.is_staff = is_staff
+ user_updated = True
user.save(update_fields=["is_staff"])
+ if user_created or user_updated:
+ send_user_event(user, user_created, user_updated)
+
return user
@@ -276,7 +280,7 @@ def get_user_from_oauth_access_token(
"Failed to fetch OIDC user info", extra={"user_info_url": user_info_url}
)
return None
- user = get_or_create_user_from_payload(
+ user, user_created, user_updated = get_or_create_user_from_payload(
user_info,
oauth_url=user_info_url,
)
@@ -284,12 +288,17 @@ def get_user_from_oauth_access_token(
email_domain = get_domain_from_email(user.email)
is_staff_email = email_domain in staff_user_domains
if not use_scope_permissions and not is_staff_email:
- user.is_staff = False
+ if user.is_staff:
+ user.is_staff = False
+ user_updated = True
elif is_staff_email:
assign_staff_to_default_group_and_update_permissions(
user, staff_default_group_name
)
+ if user_created or user_updated:
+ send_user_event(user, user_created, user_updated)
+
return user
@@ -379,7 +388,7 @@ def get_or_create_user_from_payload(
payload: dict,
oauth_url: str,
last_login: Optional[int] = None,
-) -> User:
+) -> tuple[User, bool, bool]:
oidc_metadata_key = f"oidc:{oauth_url}"
user_email = payload.get("email")
if not user_email:
@@ -404,16 +413,18 @@ def get_or_create_user_from_payload(
user_id = cache.get(cache_key)
if user_id:
get_kwargs = {"id": user_id}
+ created = False
try:
user = User.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME).get(
**get_kwargs
)
except User.DoesNotExist:
- user, _ = User.objects.get_or_create(
+ user, created = User.objects.get_or_create(
email=user_email,
defaults=defaults_create,
)
match_orders_with_new_user(user)
+
except User.MultipleObjectsReturned:
logger.warning("Multiple users returned for single OIDC sub ID")
user, _ = User.objects.get_or_create(
@@ -426,7 +437,7 @@ def get_or_create_user_from_payload(
if not user.is_active:
raise AuthenticationError("Unable to log in.")
- _update_user_details(
+ updated = _update_user_details(
user=user,
oidc_key=oidc_metadata_key,
user_email=user_email,
@@ -437,7 +448,7 @@ def get_or_create_user_from_payload(
)
cache.set(cache_key, user.id, min(JWKS_CACHE_TIME, OIDC_DEFAULT_CACHE_TIME))
- return user
+ return user, created, updated
def get_domain_from_email(email: str):
@@ -454,7 +465,7 @@ def _update_user_details(
user_last_name: str,
sub: str,
last_login: Optional[int],
-):
+) -> bool:
user_sub = user.get_value_from_private_metadata(oidc_key)
fields_to_save = set()
if user_sub != sub:
@@ -467,7 +478,7 @@ def _update_user_details(
"Unable to update user email as the new one already exists in DB",
extra={"oidc_key": oidc_key},
)
- return
+ return False
user.email = user_email
match_orders_with_new_user(user)
fields_to_save.update({"email", "search_document"})
@@ -507,6 +518,8 @@ def _update_user_details(
if fields_to_save:
user.save(update_fields=fields_to_save)
+ return bool(fields_to_save)
+
def get_staff_user_domains(
config: "OpenIDConnectConfig",