Skip to content

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

Fix review remarks

fowczarek0331a9327740
diff --git a/saleor/account/emails.py b/saleor/account/emails.py
index 14872df01f..ab79343310 100644
--- a/saleor/account/emails.py
+++ b/saleor/account/emails.py
@@ -1,6 +1,7 @@
from urllib.parse import urlencode
from django.conf import settings
+from django.contrib.auth.tokens import default_token_generator
from django.urls import reverse
from templated_email import send_templated_mail
@@ -10,6 +11,12 @@ from ..core.emails import get_email_base_context
from ..core.utils import build_absolute_uri
+def send_user_password_reset_email(redirect_url, user):
+ """Trigger sending a password reset email for the given user."""
+ token = default_token_generator.make_token(user)
+ send_password_reset_email_with_url.delay(redirect_url, user.email, token, user.pk)
+
+
def _send_password_reset_email(reset_url, recipient_email, user_id):
context = get_email_base_context()
context["reset_url"] = reset_url
@@ -23,14 +30,14 @@ def _send_password_reset_email(reset_url, recipient_email, user_id):
@app.task
-def send_password_reset_email(context, recipient, user_id):
+def send_password_reset_email(context, recipient_email, user_id):
reset_url = build_absolute_uri(
reverse(
"account:reset-password-confirm",
kwargs={"uidb64": context["uid"], "token": context["token"]},
)
)
- _send_password_reset_email(reset_url, recipient, user_id)
+ _send_password_reset_email(reset_url, recipient_email, user_id)
@app.task
diff --git a/saleor/core/utils/url.py b/saleor/core/utils/url.py
new file mode 100644
index 0000000000..415373e8ea
--- /dev/null
+++ b/saleor/core/utils/url.py
@@ -0,0 +1,24 @@
+from urllib.parse import urlparse
+
+from django.conf import settings
+from django.core.exceptions import ValidationError
+from django.http.request import validate_host
+
+
+def validate_storefront_url(url):
+ """Validate the storefront URL.
+
+ Raise ValidationError if URL isn't in RFC 1808 format
+ or it isn't allowed by ALLOWED_STOREFRONT_HOSTS in settings.
+ """
+ try:
+ parsed_url = urlparse(url)
+ except ValueError as error:
+ raise ValidationError({"redirectUrl": str(error)})
+ if not validate_host(parsed_url.netloc, settings.ALLOWED_STOREFRONT_HOSTS):
+ raise ValidationError(
+ {
+ "redirectUrl": "%s this is not valid storefront address."
+ % parsed_url.netloc
+ }
+ )
diff --git a/saleor/graphql/account/mutations/base.py b/saleor/graphql/account/mutations/base.py
index 7009775c5b..528c78ec54 100644
--- a/saleor/graphql/account/mutations/base.py
+++ b/saleor/graphql/account/mutations/base.py
@@ -1,15 +1,13 @@
-from urllib.parse import urlparse
-
import graphene
-from django.conf import settings
from django.contrib.auth import password_validation
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
-from django.http.request import validate_host
from graphql_jwt.exceptions import PermissionDenied
-from ....account import emails, events as account_events, models
+from ....account import events as account_events, models
+from ....account.emails import send_user_password_reset_email
+from ....core.utils.url import validate_storefront_url
from ....dashboard.emails import send_set_password_customer_email
from ...account.i18n import I18nMixin
from ...account.types import Address, AddressInput, User
@@ -28,13 +26,6 @@ SHIPPING_ADDRESS_FIELD = "default_shipping_address"
INVALID_TOKEN = "Invalid or expired token."
-def send_user_password_reset_email(redirect_url, user):
- token = default_token_generator.make_token(user)
- emails.send_password_reset_email_with_url.delay(
- redirect_url, user.email, token, user.pk
- )
-
-
def can_edit_address(user, address):
"""Determine whether the user can edit the given address.
@@ -101,8 +92,8 @@ class RequestPasswordReset(BaseMutation):
redirect_url = graphene.String(
required=True,
description=(
- "Url of storefront view which allow user reset password, "
- "sent by email. Url in RFC 1808 format.",
+ "URL of a view where users should be redirected to "
+ "reset the password. URL in RFC 1808 format.",
),
)
@@ -113,15 +104,8 @@ class RequestPasswordReset(BaseMutation):
def perform_mutation(cls, _root, info, **data):
email = data["email"]
redirect_url = data["redirect_url"]
+ validate_storefront_url(redirect_url)
- parsed_url = urlparse(redirect_url)
- if not validate_host(parsed_url.netloc, settings.ALLOWED_STOREFRONT_HOSTS):
- raise ValidationError(
- {
- "redirectUrl": "%s this is not valid storefront address."
- % parsed_url.netloc
- }
- )
try:
user = models.User.objects.get(email=email)
except ObjectDoesNotExist: