Skip to content

Commit

Permalink
Send an email if the address is already bound to an user account
Browse files Browse the repository at this point in the history
Co-authored-by: Mathieu Velten <mathieu.velten@beta.gouv.fr>
  • Loading branch information
mcalinghee and MatMaul committed Jan 16, 2024
1 parent 79a88b5 commit 40a0123
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
12 changes: 12 additions & 0 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"invite_from_person_to_space": "[%(app)s] %(person)s has invited you to join the %(space)s space on %(app)s...",
"password_reset": "[%(server_name)s] Password reset",
"email_validation": "[%(server_name)s] Validate your email",
"email_already_in_use": "[%(server_name)s] Email already in use",
}

LEGACY_TEMPLATE_DIR_WARNING = """
Expand All @@ -74,6 +75,7 @@ class EmailSubjectConfig:
invite_from_person_to_space: str
password_reset: str
email_validation: str
email_already_in_use: str


class EmailConfig(Config):
Expand Down Expand Up @@ -178,6 +180,12 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
registration_template_text = email_config.get(
"registration_template_text", "registration.txt"
)
already_in_use_template_html = email_config.get(
"already_in_use_template_html", "already_in_use.html"
)
already_in_use_template_text = email_config.get(
"already_in_use_template_html", "already_in_use.txt"
)
add_threepid_template_html = email_config.get(
"add_threepid_template_html", "add_threepid.html"
)
Expand Down Expand Up @@ -213,6 +221,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
self.email_password_reset_template_text,
self.email_registration_template_html,
self.email_registration_template_text,
self.email_already_in_use_template_html,
self.email_already_in_use_template_text,
self.email_add_threepid_template_html,
self.email_add_threepid_template_text,
self.email_password_reset_template_confirmation_html,
Expand All @@ -228,6 +238,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
password_reset_template_text,
registration_template_html,
registration_template_text,
already_in_use_template_html,
already_in_use_template_text,
add_threepid_template_html,
add_threepid_template_text,
"password_reset_confirmation.html",
Expand Down
14 changes: 14 additions & 0 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,20 @@ async def send_registration_mail(
template_vars,
)

async def send_already_in_use_mail(self, email_address: str) -> None:
"""Send an email if the address is already bound to an user account
Args:
email_address: Email address we're sending to the "already in use" mail
"""

await self.send_email(
email_address,
self.email_subjects.email_already_in_use
% {"server_name": self.hs.config.server.server_name, "app": self.app_name},
{},
)

async def send_add_threepid_mail(
self, email_address: str, token: str, client_secret: str, sid: str
) -> None:
Expand Down
12 changes: 12 additions & 0 deletions synapse/res/templates/already_in_use.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% extends "_base.html" %}
{% block title %}Email already in use{% endblock %}

{% block body %}
<p>You have asked us to register this email with a new Matrix account, but this email is already registered with an existing account.</p>

<p></p>Please reset your password if needed.</p>

<p>If this was not you, you can safely disregard this email.</p>

<p>Thank you.</p>
{% endblock %}
10 changes: 10 additions & 0 deletions synapse/res/templates/already_in_use.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Hello there,

You have asked us to register this email with a new Matrix account,
but this email is already registered with an existing account.

Please reset your password if needed.

If this was not you, you can safely disregard this email.

Thank you.
12 changes: 10 additions & 2 deletions synapse/rest/client/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ def __init__(self, hs: "HomeServer"):
self.config = hs.config

if self.hs.config.email.can_verify_email:
self.mailer = Mailer(
self.registration_mailer = Mailer(
hs=self.hs,
app_name=self.config.email.email_app_name,
template_html=self.config.email.email_registration_template_html,
template_text=self.config.email.email_registration_template_text,
)
self.already_in_use_mailer = Mailer(
hs=self.hs,
app_name=self.config.email.email_app_name,
template_html=self.config.email.email_already_in_use_template_html,
template_text=self.config.email.email_already_in_use_template_text,
)

async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if not self.hs.config.email.can_verify_email:
Expand Down Expand Up @@ -137,8 +143,10 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if self.hs.config.server.request_token_inhibit_3pid_errors:
# Make the client think the operation succeeded. See the rationale in the
# comments for request_token_inhibit_3pid_errors.
# Still send an email to warn the user that an account already exists.
# Also wait for some random amount of time between 100ms and 1s to make it
# look like we did something.
await self.already_in_use_mailer.send_already_in_use_mail(email)
await self.hs.get_clock().sleep(random.randint(1, 10) / 10)
return 200, {"sid": random_string(16)}

Expand All @@ -149,7 +157,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
email,
client_secret,
send_attempt,
self.mailer.send_registration_mail,
self.registration_mailer.send_registration_mail,
next_link,
)

Expand Down

0 comments on commit 40a0123

Please sign in to comment.