Skip to content

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

Throttle password reset mutation per user (#12090)

Tomasz Szymański1710c58be387
diff --git a/saleor/account/error_codes.py b/saleor/account/error_codes.py
index 9f01cda753..001142e5ae 100644
--- a/saleor/account/error_codes.py
+++ b/saleor/account/error_codes.py
@@ -25,6 +25,7 @@ class AccountErrorCode(Enum):
PASSWORD_TOO_COMMON = "password_too_common"
PASSWORD_TOO_SHORT = "password_too_short"
PASSWORD_TOO_SIMILAR = "password_too_similar"
+ PASSWORD_RESET_ALREADY_REQUESTED = "password_reset_already_requested"
REQUIRED = "required"
UNIQUE = "unique"
JWT_SIGNATURE_EXPIRED = "signature_has_expired"
diff --git a/saleor/account/migrations/0064_user_last_password_reset_request.py b/saleor/account/migrations/0064_user_last_password_reset_request.py
new file mode 100644
index 0000000000..58aa365786
--- /dev/null
+++ b/saleor/account/migrations/0064_user_last_password_reset_request.py
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.16 on 2023-01-24 12:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0063_user_user_p_meta_jsonb_path_idx"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="user",
+ name="last_password_reset_request",
+ field=models.DateTimeField(blank=True, null=True),
+ ),
+ ]
diff --git a/saleor/account/migrations/0072_merge_20230221_0956.py b/saleor/account/migrations/0072_merge_20230221_0956.py
new file mode 100644
index 0000000000..003efefe84
--- /dev/null
+++ b/saleor/account/migrations/0072_merge_20230221_0956.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.18 on 2023-02-21 09:56
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0064_user_last_password_reset_request"),
+ ("account", "0071_merge_0066_merge_20221227_1315_0070_alter_user_uuid"),
+ ]
+
+ operations = []
diff --git a/saleor/account/migrations/0076_merge_20230221_1002.py b/saleor/account/migrations/0076_merge_20230221_1002.py
new file mode 100644
index 0000000000..15889e94c4
--- /dev/null
+++ b/saleor/account/migrations/0076_merge_20230221_1002.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.18 on 2023-02-21 10:02
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0072_merge_20230221_0956"),
+ ("account", "0075_add_address_metadata"),
+ ]
+
+ operations = []
diff --git a/saleor/account/migrations/0077_merge_20230310_1107.py b/saleor/account/migrations/0077_merge_20230310_1107.py
new file mode 100644
index 0000000000..4e4380e570
--- /dev/null
+++ b/saleor/account/migrations/0077_merge_20230310_1107.py
@@ -0,0 +1,12 @@
+# Generated by Django 3.2.18 on 2023-03-10 11:07
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account", "0076_fill_empty_passwords"),
+ ("account", "0076_merge_20230221_1002"),
+ ]
+
+ operations = []
diff --git a/saleor/account/models.py b/saleor/account/models.py
index a3ea5053fd..1fc940facf 100644
--- a/saleor/account/models.py
+++ b/saleor/account/models.py
@@ -169,6 +169,7 @@ class User(
note = models.TextField(null=True, blank=True)
date_joined = models.DateTimeField(default=timezone.now, editable=False)
updated_at = models.DateTimeField(auto_now=True, db_index=True)
+ last_password_reset_request = models.DateTimeField(null=True, blank=True)
default_shipping_address = models.ForeignKey(
Address, related_name="+", null=True, blank=True, on_delete=models.SET_NULL
)
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 4b76d13b50..e3fd53c927 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -1,10 +1,12 @@
from typing import cast
import graphene
+from django.conf import settings
from django.contrib.auth import password_validation
from django.contrib.auth.hashers import make_password
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from django.utils import timezone
from ....account import events as account_events
from ....account import models
@@ -179,6 +181,7 @@ class RequestPasswordReset(BaseMutation):
)
}
)
+
if not user.is_active:
raise ValidationError(
{
@@ -188,6 +191,19 @@ class RequestPasswordReset(BaseMutation):
)
}
)
+
+ if password_reset_time := user.last_password_reset_request:
+ delta = timezone.now() - password_reset_time
+ if delta.total_seconds() < settings.RESET_PASSWORD_LOCK_TIME:
+ raise ValidationError(
+ {
+ "email": ValidationError(
+ "Password reset already requested",
+ code=AccountErrorCode.PASSWORD_RESET_ALREADY_REQUESTED.value,
+ )
+ }
+ )
+
return user
@classmethod
@@ -213,6 +229,8 @@ class RequestPasswordReset(BaseMutation):
channel_slug=channel_slug,
staff=user.is_staff,
)
+ user.last_password_reset_request = timezone.now()
+ user.save(update_fields=["last_password_reset_request"])
return RequestPasswordReset()
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index 8b41a9037e..1c8ff566e8 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -5337,6 +5337,56 @@ def test_account_reset_password(
payload=expected_payload,
channel_slug=channel_PLN.slug,
)
+ user = user_api_client.user
+ user.refresh_from_db()
+ assert user.last_password_reset_request == timezone.now()
+
+
+@freeze_time("2018-05-31 12:00:01")
+@patch("saleor.plugins.manager.PluginsManager.notify")
+def test_account_reset_password_on_cooldown(
+ mocked_notify, user_api_client, customer_user, channel_PLN
+):
+ redirect_url = "https://www.example.com"
+ variables = {
+ "email": customer_user.email,
+ "redirectUrl": redirect_url,
+ "channel": channel_PLN.slug,
+ }
+ user = user_api_client.user
+ user.last_password_reset_request = timezone.now()
+ user.save(update_fields=["last_password_reset_request"])
+ response = user_api_client.post_graphql(REQUEST_PASSWORD_RESET_MUTATION, variables)
+ content = get_graphql_content(response)
+ errors = content["data"]["requestPasswordReset"]["errors"]
+ assert errors == [
+ {
+ "field": "email",
+ "message": "Password reset already requested",
+ "code": AccountErrorCode.PASSWORD_RESET_ALREADY_REQUESTED.name,
+ }
+ ]
+ mocked_notify.assert_not_called()
+
+
+@freeze_time("2018-05-31 12:00:01")
+def test_account_reset_password_after_cooldown(
+ user_api_client, customer_user, channel_PLN, settings
+):
+ redirect_url = "https://www.example.com"
+ variables = {
+ "email": customer_user.email,
+ "redirectUrl": redirect_url,
+ "channel": channel_PLN.slug,
+ }
+ user = user_api_client.user
+ user.last_password_reset_request = timezone.now() - datetime.timedelta(
+ seconds=settings.RESET_PASSWORD_LOCK_TIME
+ )
+ user.save(update_fields=["last_password_reset_request"])
+ response = user_api_client.post_graphql(REQUEST_PASSWORD_RESET_MUTATION, variables)
+ content = get_graphql_content(response)
+ assert content["data"]["requestPasswordReset"]["errors"] == []
@freeze_time("2018-05-31 12:00:01")
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 6de4c14874..a3a52d26f7 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -23657,6 +23657,7 @@ enum AccountErrorCode {
PASSWORD_TOO_COMMON
PASSWORD_TOO_SHORT
PASSWORD_TOO_SIMILAR
+ PASSWORD_RESET_ALREADY_REQUESTED
REQUIRED
UNIQUE
JWT_SIGNATURE_EXPIRED
diff --git a/saleor/settings.py b/saleor/settings.py
index 9fde2eb21c..677518049f 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -819,3 +819,8 @@ UPDATE_SEARCH_VECTOR_INDEX_QUEUE_NAME = os.environ.get(
)
# Queue name for "async webhook" events
WEBHOOK_CELERY_QUEUE_NAME = os.environ.get("WEBHOOK_CELERY_QUEUE_NAME", None)
+
+# Lock time for request password reset mutation per user (seconds)
+RESET_PASSWORD_LOCK_TIME = parse(
+ os.environ.get("RESET_PASSWORD_LOCK_TIME", "15 minutes")
+)