Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Revert JWT expiration error code (#12254)
Marcin Gębala0541bb8afa25
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.pyindex f2e9022a18..8b41a9037e 100644--- a/saleor/graphql/account/tests/test_account.py+++ b/saleor/graphql/account/tests/test_account.py@@ -5517,10 +5517,12 @@ def test_account_reset_password_user_is_inactive("channel": channel_USD.slug,}response = user_api_client.post_graphql(REQUEST_PASSWORD_RESET_MUTATION, variables)- content = get_graphql_content(response)- data = content["data"]["requestPasswordReset"]- assert len(data["errors"]) == 1- assert data["errors"][0]["code"] == AccountErrorCode.JWT_INVALID_TOKEN.name+ results = response.json()+ assert "errors" in results+ assert (+ results["errors"][0]["message"]+ == "Invalid token. User does not exist or is inactive."+ )assert not mocked_notify.calleddiff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.pyindex 889c8736c8..5d73aaefce 100644--- a/saleor/graphql/core/mutations.py+++ b/saleor/graphql/core/mutations.py@@ -19,7 +19,6 @@ from typing import (from uuid import UUIDimport graphene-import jwtfrom django.core.exceptions import (NON_FIELD_ERRORS,ImproperlyConfigured,@@ -33,7 +32,6 @@ from graphene import ObjectTypefrom graphene.types.mutation import MutationOptionsfrom graphql.error import GraphQLError-from ...account.error_codes import AccountErrorCodefrom ...core.error_codes import MetadataErrorCodefrom ...core.exceptions import PermissionDeniedfrom ...core.utils.events import call_event@@ -504,14 +502,7 @@ class BaseMutation(graphene.Mutation):@classmethoddef mutate(cls, root, info: ResolveInfo, **data):disallow_replica_in_context(info.context)- try:- setup_context_user(info.context)- except jwt.InvalidTokenError:- return cls.handle_errors(- ValidationError(- "Invalid token", code=AccountErrorCode.JWT_INVALID_TOKEN.value- )- )+ setup_context_user(info.context)if not cls.check_permissions(info.context, data=data):raise PermissionDenied(permissions=cls._meta.permissions)@@ -947,14 +938,7 @@ class BaseBulkMutation(BaseMutation):@classmethoddef mutate(cls, root, info: ResolveInfo, **data):disallow_replica_in_context(info.context)- try:- setup_context_user(info.context)- except jwt.InvalidTokenError:- return cls.handle_errors(- ValidationError(- "Invalid token", code=AccountErrorCode.JWT_INVALID_TOKEN.value- )- )+ setup_context_user(info.context)if not cls.check_permissions(info.context):raise PermissionDenied(permissions=cls._meta.permissions)diff --git a/saleor/graphql/core/tests/test_base_mutation.py b/saleor/graphql/core/tests/test_base_mutation.pyindex 0aa594fbb8..7cce01152e 100644--- a/saleor/graphql/core/tests/test_base_mutation.py+++ b/saleor/graphql/core/tests/test_base_mutation.py@@ -4,9 +4,12 @@ import grapheneimport pytestfrom django.core.exceptions import ImproperlyConfiguredfrom django.utils.functional import SimpleLazyObject+from freezegun import freeze_timefrom graphql import GraphQLErrorfrom graphql.execution import ExecutionResult+from ....core.jwt import create_access_token+from ....graphql.tests.utils import get_graphql_contentfrom ....order.models import Orderfrom ....permission.enums import ProductPermissionsfrom ....plugins.tests.sample_plugins import PluginSample@@ -419,3 +422,36 @@ def test_base_mutation_get_node_by_pk_with_qs_for_product(product):# thenassert node.id == product.id+++def test_expired_token_error(user_api_client, channel_USD):+ # given+ user = user_api_client.user+ with freeze_time("2023-01-01 12:00:00"):+ expired_access_token = create_access_token(user)+ user_api_client.token = expired_access_token++ mutation = """+ mutation createCheckout($checkoutInput: CheckoutCreateInput!) {+ checkoutCreate(input: $checkoutInput) {+ checkout {+ id+ }+ errors {+ field+ message+ code+ }+ }+ }+ """++ # when+ variables = {"checkoutInput": {"channel": channel_USD.slug, "lines": []}}+ response = user_api_client.post_graphql(mutation, variables)+ content = get_graphql_content(response, ignore_errors=True)++ # then+ error = content["errors"][0]+ assert error["message"] == "Signature has expired"+ assert error["extensions"]["exception"]["code"] == "ExpiredSignatureError"diff --git a/saleor/graphql/utils/__init__.py b/saleor/graphql/utils/__init__.pyindex a434a17299..aa8b02ad61 100644--- a/saleor/graphql/utils/__init__.py+++ b/saleor/graphql/utils/__init__.py@@ -12,6 +12,7 @@ from django.db.models.functions import Concatfrom graphql import GraphQLDocumentfrom graphql.error import GraphQLErrorfrom graphql.error import format_error as format_graphql_error+from jwt import InvalidTokenErrorfrom ...account.models import Userfrom ...app.models import App@@ -44,6 +45,7 @@ REVERSED_DIRECTION = {ALLOWED_ERRORS = [CircularSubscriptionSyncEvent,GraphQLError,+ InvalidTokenError,PermissionDenied,ReadOnlyException,ValidationError,@@ -290,7 +292,10 @@ def format_error(error, handled_exceptions):# If DEBUG mode is disabled we allow only certain error messages to be returned in# the API. This prevents from leaking internals that might be included in Python# exceptions' error messages.- if type(exc) not in ALLOWED_ERRORS and not settings.DEBUG:+ is_allowed_err = type(exc) in ALLOWED_ERRORS or any(+ [isinstance(exc, allowed_err) for allowed_err in ALLOWED_ERRORS]+ )+ if not is_allowed_err and not settings.DEBUG:result["message"] = INTERNAL_ERROR_MESSAGEresult["extensions"]["exception"] = {"code": type(exc).__name__}