Skip to content

Commit cd903e6

Browse files
mcalingheeMatMaul
andcommitted
Send an email if the address is already bound to an user account
Co-authored-by: Mathieu Velten <[email protected]>
1 parent 79a88b5 commit cd903e6

File tree

6 files changed

+59
-2
lines changed

6 files changed

+59
-2
lines changed

Diff for: changelog.d/16819.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Send an email if the address is already bound to an user account.

Diff for: synapse/config/emailconfig.py

+12
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"invite_from_person_to_space": "[%(app)s] %(person)s has invited you to join the %(space)s space on %(app)s...",
5151
"password_reset": "[%(server_name)s] Password reset",
5252
"email_validation": "[%(server_name)s] Validate your email",
53+
"email_already_in_use": "[%(server_name)s] Email already in use",
5354
}
5455

5556
LEGACY_TEMPLATE_DIR_WARNING = """
@@ -74,6 +75,7 @@ class EmailSubjectConfig:
7475
invite_from_person_to_space: str
7576
password_reset: str
7677
email_validation: str
78+
email_already_in_use: str
7779

7880

7981
class EmailConfig(Config):
@@ -178,6 +180,12 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
178180
registration_template_text = email_config.get(
179181
"registration_template_text", "registration.txt"
180182
)
183+
already_in_use_template_html = email_config.get(
184+
"already_in_use_template_html", "already_in_use.html"
185+
)
186+
already_in_use_template_text = email_config.get(
187+
"already_in_use_template_html", "already_in_use.txt"
188+
)
181189
add_threepid_template_html = email_config.get(
182190
"add_threepid_template_html", "add_threepid.html"
183191
)
@@ -213,6 +221,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
213221
self.email_password_reset_template_text,
214222
self.email_registration_template_html,
215223
self.email_registration_template_text,
224+
self.email_already_in_use_template_html,
225+
self.email_already_in_use_template_text,
216226
self.email_add_threepid_template_html,
217227
self.email_add_threepid_template_text,
218228
self.email_password_reset_template_confirmation_html,
@@ -228,6 +238,8 @@ def read_config(self, config: JsonDict, **kwargs: Any) -> None:
228238
password_reset_template_text,
229239
registration_template_html,
230240
registration_template_text,
241+
already_in_use_template_html,
242+
already_in_use_template_text,
231243
add_threepid_template_html,
232244
add_threepid_template_text,
233245
"password_reset_confirmation.html",

Diff for: synapse/push/mailer.py

+14
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ async def send_registration_mail(
189189
template_vars,
190190
)
191191

192+
async def send_already_in_use_mail(self, email_address: str) -> None:
193+
"""Send an email if the address is already bound to an user account
194+
195+
Args:
196+
email_address: Email address we're sending to the "already in use" mail
197+
"""
198+
199+
await self.send_email(
200+
email_address,
201+
self.email_subjects.email_already_in_use
202+
% {"server_name": self.hs.config.server.server_name, "app": self.app_name},
203+
{},
204+
)
205+
192206
async def send_add_threepid_mail(
193207
self, email_address: str, token: str, client_secret: str, sid: str
194208
) -> None:

Diff for: synapse/res/templates/already_in_use.html

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "_base.html" %}
2+
{% block title %}Email already in use{% endblock %}
3+
4+
{% block body %}
5+
<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>
6+
7+
<p></p>Please reset your password if needed.</p>
8+
9+
<p>If this was not you, you can safely disregard this email.</p>
10+
11+
<p>Thank you.</p>
12+
{% endblock %}

Diff for: synapse/res/templates/already_in_use.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Hello there,
2+
3+
You have asked us to register this email with a new Matrix account,
4+
but this email is already registered with an existing account.
5+
6+
Please reset your password if needed.
7+
8+
If this was not you, you can safely disregard this email.
9+
10+
Thank you.

Diff for: synapse/rest/client/register.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,18 @@ def __init__(self, hs: "HomeServer"):
8484
self.config = hs.config
8585

8686
if self.hs.config.email.can_verify_email:
87-
self.mailer = Mailer(
87+
self.registration_mailer = Mailer(
8888
hs=self.hs,
8989
app_name=self.config.email.email_app_name,
9090
template_html=self.config.email.email_registration_template_html,
9191
template_text=self.config.email.email_registration_template_text,
9292
)
93+
self.already_in_use_mailer = Mailer(
94+
hs=self.hs,
95+
app_name=self.config.email.email_app_name,
96+
template_html=self.config.email.email_already_in_use_template_html,
97+
template_text=self.config.email.email_already_in_use_template_text,
98+
)
9399

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

@@ -149,7 +157,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
149157
email,
150158
client_secret,
151159
send_attempt,
152-
self.mailer.send_registration_mail,
160+
self.registration_mailer.send_registration_mail,
153161
next_link,
154162
)
155163

0 commit comments

Comments
 (0)