Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Don't require csrf token when refreshToken comes from input
Maciej Korycinski1cd17b09f121
diff --git a/saleor/graphql/account/mutations/jwt.py b/saleor/graphql/account/mutations/jwt.pyindex 827e613985..81e05b1ea2 100644--- a/saleor/graphql/account/mutations/jwt.py+++ b/saleor/graphql/account/mutations/jwt.py@@ -113,14 +113,19 @@ class RefreshToken(BaseMutation):class Arguments:refresh_token = graphene.String(required=False, description="Refresh token.")csrf_token = graphene.String(- required=True, description="CSRF token required to refresh token."+ required=False,+ description=(+ "CSRF token required to refresh token. This argument is "+ "required when refreshToken is provided as a cookie."+ ),)class Meta:description = (- "Refresh JWT token. Mutation tries to take refreshToken from "- f"the http-only cookie {JWT_REFRESH_TOKEN_COOKIE_NAME}. If it "- "fails it will try to take refreshToken provided as an argument."+ "Refresh JWT token. Mutation tries to take refreshToken from the input."+ "If it fails it will try to take refreshToken from the http-only cookie -"+ f"{JWT_REFRESH_TOKEN_COOKIE_NAME}. csrfToken is required when refreshToken "+ "is provided as a cookie.")error_type_class = AccountErrorerror_type_field = "account_errors"@@ -189,8 +194,10 @@ class RefreshToken(BaseMutation):refresh_token = cls.get_refresh_token(info, data)payload = cls.clean_refresh_token(refresh_token)- csrf_token = data.get("csrf_token")- cls.clean_csrf_token(csrf_token, payload)+ # None when we got refresh_token from cookie.+ if not data.get("refresh_token"):+ csrf_token = data.get("csrf_token")+ cls.clean_csrf_token(csrf_token, payload)user = get_user(payload)token = create_access_token(user)diff --git a/saleor/graphql/account/tests/mutations/test_token_refresh.py b/saleor/graphql/account/tests/mutations/test_token_refresh.pyindex 3ba2ba7d52..3eb7f225a2 100644--- a/saleor/graphql/account/tests/mutations/test_token_refresh.py+++ b/saleor/graphql/account/tests/mutations/test_token_refresh.py@@ -15,7 +15,7 @@ from .....core.jwt import (from ....tests.utils import get_graphql_contentMUTATION_TOKEN_REFRESH = """- mutation tokenRefresh($token: String, $csrf_token: String!){+ mutation tokenRefresh($token: String, $csrf_token: String){tokenRefresh(refreshToken: $token, csrfToken: $csrf_token){tokenaccountErrors{@@ -57,7 +57,7 @@ def test_refresh_token_get_token_from_cookie(api_client, customer_user, settingsdef test_refresh_token_get_token_from_input(api_client, customer_user, settings):csrf_token = _get_new_csrf_token()refresh_token = create_refresh_token(customer_user, {"csrfToken": csrf_token})- variables = {"token": refresh_token, "csrf_token": csrf_token}+ variables = {"token": refresh_token, "csrf_token": None}response = api_client.post_graphql(MUTATION_TOKEN_REFRESH, variables)content = get_graphql_content(response)@@ -113,8 +113,9 @@ def test_access_token_used_as_a_refresh_token(api_client, customer_user):def test_refresh_token_get_token_incorrect_csrf_token(api_client, customer_user):csrf_token = _get_new_csrf_token()refresh_token = create_refresh_token(customer_user, {"csrfToken": csrf_token})- variables = {"token": refresh_token, "csrf_token": "csrf_token"}-+ variables = {"token": None, "csrf_token": "csrf_token"}+ api_client.cookies[JWT_REFRESH_TOKEN_COOKIE_NAME] = refresh_token+ api_client.cookies[JWT_REFRESH_TOKEN_COOKIE_NAME]["httponly"] = Trueresponse = api_client.post_graphql(MUTATION_TOKEN_REFRESH, variables)content = get_graphql_content(response)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex f29590804c..4e981c926f 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -2486,7 +2486,7 @@ type Mutation {appTokenDelete(id: ID!): AppTokenDeleteappTokenVerify(token: String!): AppTokenVerifytokenCreate(email: String!, password: String!): CreateToken- tokenRefresh(csrfToken: String!, refreshToken: String): RefreshToken+ tokenRefresh(csrfToken: String, refreshToken: String): RefreshTokentokenVerify(token: String!): VerifyTokentokensDeactivateAll: DeactivateAllUserTokensrequestPasswordReset(email: String!, redirectUrl: String!): RequestPasswordReset