Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 891afb5

Browse files
Change account_threepid_delegate to a dictionary (#5969)
`account_threepid_delegate` was an option added as part of this privacy sprint for the homeserver admin to declare which identity server (or any server handling third-party verification requests) they'd like to use to send email/sms messages on their behalf for the purposes of user registration and password resets. We realized however, that while admins would want to set this option to `""` (allow Synapse to handle email sending itself), some homeservers have users with bound phone numbers, and setting `account_threepid_delegate` to `""` would prevent them from having any phone number verification, since Synapse does not at this time support sending SMS messages. So, seeing as a common use case would be to have Synapse handle email verification, but an external server handle MSISDN verification, we split `account_threepid_delegate` into a dictionary, and called it `account_threepid_delegates` instead. This contains two keys as of present, `email` and `msisdn`. You can then set either to an external server of your choice, or `""` for Synapse to attempt to handle it.
1 parent 75f7a7b commit 891afb5

File tree

10 files changed

+132
-122
lines changed

10 files changed

+132
-122
lines changed

UPGRADE.rst

+17-15
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,23 @@ its own, phone-based password resets and registration will be disabled. For Syna
6666
emails, the ``email`` block of the config must be filled out. If not, then password resets and
6767
registration via email will be disabled entirely.
6868

69-
This release also deprecates the ``email.trust_identity_server_for_password_resets`` option
70-
and replaces it with ``account_threepid_delegate``. This option defines whether the homeserver
71-
should delegate an external server (typically an `identity server
72-
<https://matrix.org/docs/spec/identity_service/r0.2.1>`_) to handle sending password reset
73-
or registration messages via email or SMS.
74-
75-
If ``email.trust_identity_server_for_password_resets`` was changed from its default to
76-
``true``, and ``account_threepid_delegate`` is not set to an identity server domain, then the
77-
server handling password resets and registration via third-party addresses will be set to the
78-
first entry in the Synapse config's ``trusted_third_party_id_servers`` entry. If no domains are
79-
configured, Synapse will throw an error on startup.
80-
81-
If ``email.trust_identity_server_for_password_resets`` is not set to ``true`` and
82-
``account_threepid_delegate`` is not set to a domain, then Synapse will attempt to send
83-
password reset and registration messages itself.
69+
This release also deprecates the ``email.trust_identity_server_for_password_resets`` option and
70+
replaces it with the ``account_threepid_delegates`` dictionary. This option defines whether the
71+
homeserver should delegate an external server (typically an `identity server
72+
<https://matrix.org/docs/spec/identity_service/r0.2.1>`_) to handle sending password reset or
73+
registration messages via email and SMS.
74+
75+
Specifically for email, if ``email.trust_identity_server_for_password_resets`` was changed from
76+
its default to ``true``, and ``account_threepid_delegates.email`` is not set, then the server
77+
handling password resets and registration via third-party addresses will be set to the first
78+
entry in the Synapse config's ``trusted_third_party_id_servers`` entry. This is to ensure that
79+
people who set up an external server for handling these tasks before v1.4.0 will not have their
80+
setups mysteriously stop working. However, if no trusted identity server domains are
81+
configured, Synapse will throw an error.
82+
83+
If ``email.trust_identity_server_for_password_resets`` is not set to ``true`` and a type in
84+
``account_threepid_delegates`` is not set to a domain, then Synapse will attempt to send
85+
password reset and registration messages itself for that type.
8486

8587
Email templates
8688
---------------
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Replace `trust_identity_server_for_password_resets` config option with `account_threepid_delegate`.
1+
Replace `trust_identity_server_for_password_resets` config option with `account_threepid_delegates`.

changelog.d/5969.feature

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Replace `trust_identity_server_for_password_resets` config option with `account_threepid_delegates`.

docs/sample_config.yaml

+20-10
Original file line numberDiff line numberDiff line change
@@ -903,19 +903,29 @@ uploads_path: "DATADIR/uploads"
903903
# - matrix.org
904904
# - vector.im
905905

906-
# Handle threepid (email/phone etc) registration and password resets
907-
# through a *trusted* identity server. Note that this allows the configured
908-
# identity server to reset passwords for accounts.
906+
# Handle threepid (email/phone etc) registration and password resets through a set of
907+
# *trusted* identity servers. Note that this allows the configured identity server to
908+
# reset passwords for accounts!
909909
#
910-
# If this option is not defined and SMTP options have not been
911-
# configured, registration by email and resetting user passwords via
912-
# email will be disabled
910+
# Be aware that if `email` is not set, and SMTP options have not been
911+
# configured in the email config block, registration and user password resets via
912+
# email will be globally disabled.
913913
#
914-
# Otherwise, to enable set this option to the reachable domain name, including protocol
915-
# definition, for an identity server
916-
# (e.g "https://matrix.org", "http://localhost:8090")
914+
# Additionally, if `msisdn` is not set, registration and password resets via msisdn
915+
# will be disabled regardless. This is due to Synapse currently not supporting any
916+
# method of sending SMS messages on its own.
917917
#
918-
#account_threepid_delegate: ""
918+
# To enable using an identity server for operations regarding a particular third-party
919+
# identifier type, set the value to the URL of that identity server as shown in the
920+
# examples below.
921+
#
922+
# Servers handling the these requests must answer the `/requestToken` endpoints defined
923+
# by the Matrix Identity Service API specification:
924+
# https://matrix.org/docs/spec/identity_service/latest
925+
#
926+
account_threepid_delegates:
927+
#email: https://example.com # Delegate email sending to matrix.org
928+
#msisdn: http://localhost:8090 # Delegate SMS sending to this local process
919929

920930
# Users who register on this homeserver will automatically be joined
921931
# to these rooms

synapse/config/emailconfig.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,13 @@ def read_config(self, config, **kwargs):
7575
"renew_at"
7676
)
7777

78-
self.threepid_behaviour = (
79-
# Have Synapse handle the email sending if account_threepid_delegate
78+
self.threepid_behaviour_email = (
79+
# Have Synapse handle the email sending if account_threepid_delegates.email
8080
# is not defined
81+
# msisdn is currently always remote while Synapse does not support any method of
82+
# sending SMS messages
8183
ThreepidBehaviour.REMOTE
82-
if self.account_threepid_delegate
84+
if self.account_threepid_delegate_email
8385
else ThreepidBehaviour.LOCAL
8486
)
8587
# Prior to Synapse v1.4.0, there was another option that defined whether Synapse would
@@ -88,14 +90,16 @@ def read_config(self, config, **kwargs):
8890
# identity server in the process.
8991
self.using_identity_server_from_trusted_list = False
9092
if (
91-
not self.account_threepid_delegate
93+
not self.account_threepid_delegate_email
9294
and config.get("trust_identity_server_for_password_resets", False) is True
9395
):
9496
# Use the first entry in self.trusted_third_party_id_servers instead
9597
if self.trusted_third_party_id_servers:
96-
# XXX: It's a little confusing that account_threepid_delegate is modifed
98+
# XXX: It's a little confusing that account_threepid_delegate_email is modified
9799
# both in RegistrationConfig and here. We should factor this bit out
98-
self.account_threepid_delegate = self.trusted_third_party_id_servers[0]
100+
self.account_threepid_delegate_email = self.trusted_third_party_id_servers[
101+
0
102+
]
99103
self.using_identity_server_from_trusted_list = True
100104
else:
101105
raise ConfigError(
@@ -104,12 +108,15 @@ def read_config(self, config, **kwargs):
104108
)
105109

106110
self.local_threepid_handling_disabled_due_to_email_config = False
107-
if self.threepid_behaviour == ThreepidBehaviour.LOCAL and email_config == {}:
111+
if (
112+
self.threepid_behaviour_email == ThreepidBehaviour.LOCAL
113+
and email_config == {}
114+
):
108115
# We cannot warn the user this has happened here
109116
# Instead do so when a user attempts to reset their password
110117
self.local_threepid_handling_disabled_due_to_email_config = True
111118

112-
self.threepid_behaviour = ThreepidBehaviour.OFF
119+
self.threepid_behaviour_email = ThreepidBehaviour.OFF
113120

114121
# Get lifetime of a validation token in milliseconds
115122
self.email_validation_token_lifetime = self.parse_duration(
@@ -119,7 +126,7 @@ def read_config(self, config, **kwargs):
119126
if (
120127
self.email_enable_notifs
121128
or account_validity_renewal_enabled
122-
or self.threepid_behaviour == ThreepidBehaviour.LOCAL
129+
or self.threepid_behaviour_email == ThreepidBehaviour.LOCAL
123130
):
124131
# make sure we can import the required deps
125132
import jinja2
@@ -129,7 +136,7 @@ def read_config(self, config, **kwargs):
129136
jinja2
130137
bleach
131138

132-
if self.threepid_behaviour == ThreepidBehaviour.LOCAL:
139+
if self.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
133140
required = ["smtp_host", "smtp_port", "notif_from"]
134141

135142
missing = []

synapse/config/registration.py

+24-11
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def read_config(self, config, **kwargs):
9999
self.trusted_third_party_id_servers = config.get(
100100
"trusted_third_party_id_servers", ["matrix.org", "vector.im"]
101101
)
102-
self.account_threepid_delegate = config.get("account_threepid_delegate")
102+
account_threepid_delegates = config.get("account_threepid_delegates") or {}
103+
self.account_threepid_delegate_email = account_threepid_delegates.get("email")
104+
self.account_threepid_delegate_msisdn = account_threepid_delegates.get("msisdn")
105+
103106
self.default_identity_server = config.get("default_identity_server")
104107
self.allow_guest_access = config.get("allow_guest_access", False)
105108

@@ -270,19 +273,29 @@ def generate_config_section(self, generate_secrets=False, **kwargs):
270273
# - matrix.org
271274
# - vector.im
272275
273-
# Handle threepid (email/phone etc) registration and password resets
274-
# through a *trusted* identity server. Note that this allows the configured
275-
# identity server to reset passwords for accounts.
276+
# Handle threepid (email/phone etc) registration and password resets through a set of
277+
# *trusted* identity servers. Note that this allows the configured identity server to
278+
# reset passwords for accounts!
279+
#
280+
# Be aware that if `email` is not set, and SMTP options have not been
281+
# configured in the email config block, registration and user password resets via
282+
# email will be globally disabled.
283+
#
284+
# Additionally, if `msisdn` is not set, registration and password resets via msisdn
285+
# will be disabled regardless. This is due to Synapse currently not supporting any
286+
# method of sending SMS messages on its own.
276287
#
277-
# If this option is not defined and SMTP options have not been
278-
# configured, registration by email and resetting user passwords via
279-
# email will be disabled
288+
# To enable using an identity server for operations regarding a particular third-party
289+
# identifier type, set the value to the URL of that identity server as shown in the
290+
# examples below.
280291
#
281-
# Otherwise, to enable set this option to the reachable domain name, including protocol
282-
# definition, for an identity server
283-
# (e.g "https://matrix.org", "http://localhost:8090")
292+
# Servers handling the these requests must answer the `/requestToken` endpoints defined
293+
# by the Matrix Identity Service API specification:
294+
# https://matrix.org/docs/spec/identity_service/latest
284295
#
285-
#account_threepid_delegate: ""
296+
account_threepid_delegates:
297+
#email: https://example.com # Delegate email sending to matrix.org
298+
#msisdn: http://localhost:8090 # Delegate SMS sending to this local process
286299
287300
# Users who register on this homeserver will automatically be joined
288301
# to these rooms

synapse/handlers/auth.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,10 @@ def _check_threepid(self, medium, authdict, password_servlet=False, **kwargs):
461461
logger.info("Getting validated threepid. threepidcreds: %r", (threepid_creds,))
462462
if (
463463
not password_servlet
464-
or self.hs.config.threepid_behaviour == ThreepidBehaviour.REMOTE
464+
or self.hs.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE
465465
):
466466
threepid = yield identity_handler.threepid_from_creds(threepid_creds)
467-
elif self.hs.config.threepid_behaviour == ThreepidBehaviour.LOCAL:
467+
elif self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
468468
row = yield self.store.get_threepid_validation_session(
469469
medium,
470470
threepid_creds["client_secret"],

synapse/rest/client/v2_alpha/account.py

+25-29
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(self, hs):
5050
self.config = hs.config
5151
self.identity_handler = hs.get_handlers().identity_handler
5252

53-
if self.config.threepid_behaviour == ThreepidBehaviour.LOCAL:
53+
if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
5454
from synapse.push.mailer import Mailer, load_jinja2_templates
5555

5656
templates = load_jinja2_templates(
@@ -67,7 +67,7 @@ def __init__(self, hs):
6767

6868
@defer.inlineCallbacks
6969
def on_POST(self, request):
70-
if self.config.threepid_behaviour == ThreepidBehaviour.OFF:
70+
if self.config.threepid_behaviour_email == ThreepidBehaviour.OFF:
7171
if self.config.local_threepid_handling_disabled_due_to_email_config:
7272
logger.warn(
7373
"User password resets have been disabled due to lack of email config"
@@ -100,19 +100,19 @@ def on_POST(self, request):
100100
if existing_user_id is None:
101101
raise SynapseError(400, "Email not found", Codes.THREEPID_NOT_FOUND)
102102

103-
if self.config.threepid_behaviour == ThreepidBehaviour.REMOTE:
103+
if self.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
104104
# Have the configured identity server handle the request
105-
if not self.hs.config.account_threepid_delegate:
105+
if not self.hs.config.account_threepid_delegate_email:
106106
logger.warn(
107-
"No upstream account_threepid_delegate configured on the server to handle "
108-
"this request"
107+
"No upstream email account_threepid_delegate configured on the server to "
108+
"handle this request"
109109
)
110110
raise SynapseError(
111111
400, "Password reset by email is not supported on this homeserver"
112112
)
113113

114114
ret = yield self.identity_handler.requestEmailToken(
115-
self.hs.config.account_threepid_delegate,
115+
self.hs.config.account_threepid_delegate_email,
116116
email,
117117
client_secret,
118118
send_attempt,
@@ -172,31 +172,27 @@ def on_POST(self, request):
172172
if existing_user_id is None:
173173
raise SynapseError(400, "MSISDN not found", Codes.THREEPID_NOT_FOUND)
174174

175-
if self.config.threepid_behaviour == ThreepidBehaviour.REMOTE:
176-
if not self.hs.config.account_threepid_delegate:
177-
logger.warn(
178-
"No upstream account_threepid_delegate configured on the server to handle "
179-
"this request"
180-
)
181-
raise SynapseError(
182-
400,
183-
"Password reset by phone number is not supported on this homeserver",
184-
)
185-
186-
ret = yield self.identity_handler.requestMsisdnToken(
187-
self.config.account_threepid_delegate,
188-
country,
189-
phone_number,
190-
client_secret,
191-
send_attempt,
192-
next_link,
175+
if not self.hs.config.account_threepid_delegate_msisdn:
176+
logger.warn(
177+
"No upstream msisdn account_threepid_delegate configured on the server to "
178+
"handle this request"
179+
)
180+
raise SynapseError(
181+
400,
182+
"Password reset by phone number is not supported on this homeserver",
193183
)
194-
return (200, ret)
195184

196-
raise SynapseError(
197-
400, "Password reset by phone number is not supported on this homeserver"
185+
ret = yield self.identity_handler.requestMsisdnToken(
186+
self.config.account_threepid_delegate_msisdn,
187+
country,
188+
phone_number,
189+
client_secret,
190+
send_attempt,
191+
next_link,
198192
)
199193

194+
return 200, ret
195+
200196

201197
class PasswordResetSubmitTokenServlet(RestServlet):
202198
"""Handles 3PID validation token submission"""
@@ -223,7 +219,7 @@ def on_GET(self, request, medium):
223219
raise SynapseError(
224220
400, "This medium is currently not supported for password resets"
225221
)
226-
if self.config.threepid_behaviour == ThreepidBehaviour.OFF:
222+
if self.config.threepid_behaviour_email == ThreepidBehaviour.OFF:
227223
if self.config.local_threepid_handling_disabled_due_to_email_config:
228224
logger.warn(
229225
"Password reset emails have been disabled due to lack of an email config"

0 commit comments

Comments
 (0)