Skip to content

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

Use one time token insted of user.token

fowczarek0bbade458ba3
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index 16ba2ef51f..c400c9912d 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -1,4 +1,5 @@
import graphene
+from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ValidationError
from ....account import emails, events as account_events, models, utils
@@ -97,8 +98,9 @@ class AccountRequestDeletion(BaseMutation):
user = info.context.user
redirect_url = data["redirect_url"]
validate_storefront_url(redirect_url)
+ token = default_token_generator.make_token(user)
emails.send_account_delete_confirmation_email_with_url.delay(
- str(user.token), user.email, redirect_url
+ token, user.email, redirect_url
)
return AccountRequestDeletion()
@@ -133,7 +135,7 @@ class AccountDelete(ModelDeleteMutation):
cls.clean_instance(info, user)
token = data.pop("token")
- if str(user.token) != token:
+ if not default_token_generator.check_token(user, token):
raise ValidationError({"token": INVALID_TOKEN})
db_id = user.id
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index f4090313df..0b8499c1e3 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -981,6 +981,7 @@ def test_account_request_deletion(
send_account_delete_confirmation_email_with_url_mock, user_api_client
):
user = user_api_client.user
+ token = default_token_generator.make_token(user)
variables = {"redirectUrl": "https://www.example.com"}
response = user_api_client.post_graphql(
ACCOUNT_REQUEST_DELETION_MUTATION, variables
@@ -989,7 +990,7 @@ def test_account_request_deletion(
data = content["data"]["accountRequestDeletion"]
assert not data["errors"]
send_account_delete_confirmation_email_with_url_mock.assert_called_once_with(
- str(user.token), user.email, ANY
+ token, user.email, ANY
)
url = send_account_delete_confirmation_email_with_url_mock.mock_calls[0][1][2]
url_validator = URLValidator()
@@ -1026,6 +1027,7 @@ def test_account_request_deletion_all_storefront_hosts_allowed(
send_account_delete_confirmation_email_with_url_mock, user_api_client, settings
):
user = user_api_client.user
+ token = default_token_generator.make_token(user)
settings.ALLOWED_STOREFRONT_HOSTS = ["*"]
variables = {"redirectUrl": "https://www.test.com"}
response = user_api_client.post_graphql(
@@ -1035,7 +1037,7 @@ def test_account_request_deletion_all_storefront_hosts_allowed(
data = content["data"]["accountRequestDeletion"]
assert not data["errors"]
send_account_delete_confirmation_email_with_url_mock.assert_called_once_with(
- str(user.token), user.email, ANY
+ token, user.email, ANY
)
url = send_account_delete_confirmation_email_with_url_mock.mock_calls[0][1][2]
url_validator = URLValidator()
@@ -1047,6 +1049,7 @@ def test_account_request_deletion_subdomain(
send_account_delete_confirmation_email_with_url_mock, user_api_client, settings
):
user = user_api_client.user
+ token = default_token_generator.make_token(user)
settings.ALLOWED_STOREFRONT_HOSTS = [".example.com"]
variables = {"redirectUrl": "https://sub.example.com"}
response = user_api_client.post_graphql(
@@ -1056,7 +1059,7 @@ def test_account_request_deletion_subdomain(
data = content["data"]["accountRequestDeletion"]
assert not data["errors"]
send_account_delete_confirmation_email_with_url_mock.assert_called_once_with(
- str(user.token), user.email, ANY
+ token, user.email, ANY
)
url = send_account_delete_confirmation_email_with_url_mock.mock_calls[0][1][2]
url_validator = URLValidator()
@@ -1077,7 +1080,7 @@ ACCOUNT_DELETE_MUTATION = """
def test_account_delete(user_api_client):
user = user_api_client.user
- token = user.token
+ token = default_token_generator.make_token(user)
variables = {"token": token}
response = user_api_client.post_graphql(ACCOUNT_DELETE_MUTATION, variables)