Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
fix cached user (#14591) (#14642)
Artur63b1711db039
diff --git a/saleor/plugins/openid_connect/tests/test_utils.py b/saleor/plugins/openid_connect/tests/test_utils.pyindex d283fc139a..1e33c24f2e 100644--- a/saleor/plugins/openid_connect/tests/test_utils.py+++ b/saleor/plugins/openid_connect/tests/test_utils.py@@ -224,7 +224,7 @@ def test_get_or_create_user_from_payload_retrieve_user_by_sub(# givenoauth_url = "https://saleor.io/oauth"sub_id = "oauth|1234"- customer_user.private_metadata = {f"oidc-{oauth_url}": sub_id}+ customer_user.private_metadata = {f"oidc:{oauth_url}": sub_id}customer_user.save()mocked_cache_get.side_effect = lambda cache_key: None@@ -234,15 +234,14 @@ def test_get_or_create_user_from_payload_retrieve_user_by_sub(payload={"sub": sub_id, "email": customer_user.email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)-+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)assert user_from_payload.id == customer_user.id- assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert user_from_payload.private_metadata[f"oidc:{oauth_url}"] == sub_id@mock.patch("saleor.plugins.openid_connect.utils.cache.set")@@ -253,7 +252,7 @@ def test_get_or_create_user_from_payload_updates_sub(# givenoauth_url = "https://saleor.io/oauth"sub_id = "oauth|1234"- customer_user.private_metadata = {f"oidc-{oauth_url}": "old-sub"}+ customer_user.private_metadata = {f"oidc:{oauth_url}": "old-sub"}customer_user.save()mocked_cache_get.side_effect = lambda cache_key: None@@ -263,15 +262,15 @@ def test_get_or_create_user_from_payload_updates_sub(payload={"sub": sub_id, "email": customer_user.email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)assert user_from_payload.id == customer_user.id- assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert user_from_payload.private_metadata[f"oidc:{oauth_url}"] == sub_id@mock.patch("saleor.plugins.openid_connect.utils.cache.set")@@ -290,15 +289,15 @@ def test_get_or_create_user_from_payload_assigns_sub(payload={"sub": sub_id, "email": customer_user.email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)assert user_from_payload.id == customer_user.id- assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert user_from_payload.private_metadata[f"oidc:{oauth_url}"] == sub_idassert customer_user.is_staff is False@@ -319,15 +318,15 @@ def test_get_or_create_user_from_payload_creates_user_with_sub(payload={"sub": sub_id, "email": customer_email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, user_from_payload.id, OIDC_CACHE_TIMEOUT)assert user_from_payload.email == customer_email- assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert user_from_payload.private_metadata[f"oidc:{oauth_url}"] == sub_idassert not user_from_payload.has_usable_password()@@ -352,12 +351,12 @@ def test_get_or_create_user_from_payload_match_orders_for_new_user(payload={"sub": sub_id, "email": customer_email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, user_from_payload.id, OIDC_CACHE_TIMEOUT)order.refresh_from_db()assert order.user == user_from_payload@@ -375,7 +374,7 @@ def test_get_or_create_user_from_payload_match_orders_when_changing_email(mocked_cache_get.side_effect = lambda cache_key: None- customer_user.private_metadata = {f"oidc-{oauth_url}": sub_id}+ customer_user.private_metadata = {f"oidc:{oauth_url}": sub_id}customer_user.save()order.user_email = new_customer_email@@ -387,12 +386,12 @@ def test_get_or_create_user_from_payload_match_orders_when_changing_email(payload={"sub": sub_id, "email": new_customer_email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)customer_user.refresh_from_db()order.refresh_from_db()@@ -434,7 +433,7 @@ def test_get_or_create_user_from_payload_different_email(mocked_cache_get.side_effect = lambda cache_key: None- customer_user.private_metadata = {f"oidc-{oauth_url}": sub_id}+ customer_user.private_metadata = {f"oidc:{oauth_url}": sub_id}customer_user.save()# when@@ -442,17 +441,17 @@ def test_get_or_create_user_from_payload_different_email(payload={"sub": sub_id, "email": new_customer_email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)customer_user.refresh_from_db()assert user_from_payload.id == customer_user.idassert customer_user.email == new_customer_email- assert customer_user.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert customer_user.private_metadata[f"oidc:{oauth_url}"] == sub_id@freeze_time("2019-03-18 12:00:00")@@ -481,19 +480,19 @@ def test_get_or_create_user_from_payload_with_last_login(oauth_url=oauth_url,last_login=current_ts,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thenmocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, user_from_payload, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)customer_user.refresh_from_db()assert customer_user.last_login == timezone.make_aware(datetime.fromtimestamp(current_ts), timezone=pytz.timezone("UTC"))assert user_from_payload.email == customer_user.email- assert user_from_payload.private_metadata[f"oidc-{oauth_url}"] == sub_id+ assert user_from_payload.private_metadata[f"oidc:{oauth_url}"] == sub_id@freeze_time("2019-03-18 12:00:00")@@ -514,13 +513,13 @@ def test_get_or_create_user_from_payload_update_last_login(payload={"sub": sub_id, "email": customer_user.email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thencustomer_user.refresh_from_db()mocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, customer_user, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)assert customer_user.last_loginlast_login = customer_user.last_login.strftime("%Y-%m-%d %H:%M:%S")@@ -571,13 +570,13 @@ def test_get_or_create_user_from_payload_last_login_modifies(payload={"sub": sub_id, "email": customer_user.email},oauth_url=oauth_url,)- cache_key = f"oidc-{oauth_url}" + "-" + str(sub_id)+ cache_key = f"oidc:{oauth_url}" + ":" + str(sub_id)# thencustomer_user.refresh_from_db()mocked_cache_get.assert_called_once_with(cache_key)mocked_cache_set.assert_called_once_with(- cache_key, customer_user, OIDC_CACHE_TIMEOUT+ cache_key, customer_user.id, OIDC_CACHE_TIMEOUT)assert customer_user.last_loginassert customer_user.last_login != last_logindiff --git a/saleor/plugins/openid_connect/utils.py b/saleor/plugins/openid_connect/utils.pyindex 496170983d..7d80ab0a67 100644--- a/saleor/plugins/openid_connect/utils.py+++ b/saleor/plugins/openid_connect/utils.py@@ -380,7 +380,7 @@ def get_or_create_user_from_payload(oauth_url: str,last_login: Optional[int] = None,) -> User:- oidc_metadata_key = f"oidc-{oauth_url}"+ oidc_metadata_key = f"oidc:{oauth_url}"user_email = payload.get("email")if not user_email:raise AuthenticationError("Missing user's email.")@@ -400,23 +400,26 @@ def get_or_create_user_from_payload("private_metadata": {oidc_metadata_key: sub},"password": make_password(None),}- cache_key = oidc_metadata_key + "-" + str(sub)- user = cache.get(cache_key)- if not user:- try:- user = User.objects.get(**get_kwargs)- except User.DoesNotExist:- user, _ = 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(- email=user_email,- defaults=defaults_create,- )+ cache_key = oidc_metadata_key + ":" + str(sub)+ user_id = cache.get(cache_key)+ if user_id:+ get_kwargs = {"id": user_id}+ try:+ user = User.objects.using(settings.DATABASE_CONNECTION_REPLICA_NAME).get(+ **get_kwargs+ )+ except User.DoesNotExist:+ user, _ = 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(+ email=user_email,+ defaults=defaults_create,+ )site_settings = Site.objects.get_current().settingsif not user.can_login(site_settings): # it is true only if we fetch disabled user.@@ -432,7 +435,7 @@ def get_or_create_user_from_payload(last_login=last_login,)- cache.set(cache_key, user, min(JWKS_CACHE_TIME, OIDC_DEFAULT_CACHE_TIME))+ cache.set(cache_key, user.id, min(JWKS_CACHE_TIME, OIDC_DEFAULT_CACHE_TIME))return user