summary refs log tree commit diff
path: root/synapse/handlers/identity.py
diff options
context:
space:
mode:
Diffstat (limited to 'synapse/handlers/identity.py')
-rw-r--r--synapse/handlers/identity.py74
1 files changed, 50 insertions, 24 deletions
diff --git a/synapse/handlers/identity.py b/synapse/handlers/identity.py

index 97daca5fee..4cea75723b 100644 --- a/synapse/handlers/identity.py +++ b/synapse/handlers/identity.py
@@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright 2015, 2016 OpenMarket Ltd # Copyright 2017 Vector Creations Ltd -# Copyright 2018 New Vector Ltd +# Copyright 2018, 2019 New Vector Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -43,23 +43,8 @@ class IdentityHandler(BaseHandler): self.federation_http_client = hs.get_http_client() self.trusted_id_servers = set(hs.config.trusted_third_party_id_servers) - self.trust_any_id_server_just_for_testing_do_not_use = ( - hs.config.use_insecure_ssl_client_just_for_testing_do_not_use - ) - - def _should_trust_id_server(self, id_server): - if id_server not in self.trusted_id_servers: - if self.trust_any_id_server_just_for_testing_do_not_use: - logger.warn( - "Trusting untrustworthy ID server %r even though it isn't" - " in the trusted id list for testing because" - " 'use_insecure_ssl_client_just_for_testing_do_not_use'" - " is set in the config", - id_server, - ) - else: - return False - return True + self.rewrite_identity_server_urls = hs.config.rewrite_identity_server_urls + self._enable_lookup = hs.config.enable_3pid_lookup @defer.inlineCallbacks def threepid_from_creds(self, creds): @@ -77,13 +62,17 @@ class IdentityHandler(BaseHandler): else: raise SynapseError(400, "No client_secret in creds") - if not self._should_trust_id_server(id_server): + if not should_trust_id_server(self.hs, id_server): logger.warn( "%s is not a trusted ID server: rejecting 3pid " + "credentials", id_server, ) return None + # if we have a rewrite rule set for the identity server, + # apply it now. + id_server = self.rewrite_identity_server_urls.get(id_server, id_server) + try: data = yield self.http_client.get_json( "https://%s%s" @@ -117,9 +106,14 @@ class IdentityHandler(BaseHandler): else: raise SynapseError(400, "No client_secret in creds") + # if we have a rewrite rule set for the identity server, + # apply it now, but only for sending the request (not + # storing in the database). + id_server_host = self.rewrite_identity_server_urls.get(id_server, id_server) + try: data = yield self.http_client.post_json_get_json( - "https://%s%s" % (id_server, "/_matrix/identity/api/v1/3pid/bind"), + "https://%s%s" % (id_server_host, "/_matrix/identity/api/v1/3pid/bind"), {"sid": creds["sid"], "client_secret": client_secret, "mxid": mxid}, ) logger.debug("bound threepid %r to %s", creds, mxid) @@ -187,7 +181,6 @@ class IdentityHandler(BaseHandler): Deferred[bool]: True on success, otherwise False if the identity server doesn't support unbinding """ - url = "https://%s/_matrix/identity/api/v1/3pid/unbind" % (id_server,) content = { "mxid": mxid, "threepid": {"medium": threepid["medium"], "address": threepid["address"]}, @@ -205,6 +198,15 @@ class IdentityHandler(BaseHandler): ) headers = {b"Authorization": auth_headers} + # if we have a rewrite rule set for the identity server, + # apply it now. + # + # Note that destination_is has to be the real id_server, not + # the server we connect to. + id_server = self.rewrite_identity_server_urls.get(id_server, id_server) + + url = "https://%s/_matrix/identity/api/v1/3pid/unbind" % (id_server,) + try: yield self.http_client.post_json_get_json(url, content, headers) changed = True @@ -230,7 +232,7 @@ class IdentityHandler(BaseHandler): def requestEmailToken( self, id_server, email, client_secret, send_attempt, next_link=None ): - if not self._should_trust_id_server(id_server): + if not should_trust_id_server(self.hs, id_server): raise SynapseError( 400, "Untrusted ID server '%s'" % id_server, Codes.SERVER_NOT_TRUSTED ) @@ -241,6 +243,9 @@ class IdentityHandler(BaseHandler): "send_attempt": send_attempt, } + # Rewrite id_server URL if necessary + id_server = self.rewrite_identity_server_urls.get(id_server, id_server) + if next_link: params.update({"next_link": next_link}) @@ -259,11 +264,14 @@ class IdentityHandler(BaseHandler): def requestMsisdnToken( self, id_server, country, phone_number, client_secret, send_attempt, **kwargs ): - if not self._should_trust_id_server(id_server): + if not should_trust_id_server(self.hs, id_server): raise SynapseError( 400, "Untrusted ID server '%s'" % id_server, Codes.SERVER_NOT_TRUSTED ) + # Rewrite id_server URL if necessary + id_server = self.rewrite_identity_server_urls.get(id_server, id_server) + params = { "country": country, "phone_number": phone_number, @@ -271,7 +279,10 @@ class IdentityHandler(BaseHandler): "send_attempt": send_attempt, } params.update(kwargs) - + # if we have a rewrite rule set for the identity server, + # apply it now. + if id_server in self.rewrite_identity_server_urls: + id_server = self.rewrite_identity_server_urls[id_server] try: data = yield self.http_client.post_json_get_json( "https://%s%s" @@ -284,6 +295,21 @@ class IdentityHandler(BaseHandler): raise e.to_synapse_error() +def should_trust_id_server(hs, id_server): + if id_server not in hs.config.trusted_third_party_id_servers: + if hs.trust_any_id_server_just_for_testing_do_not_use: + logger.warn( + "Trusting untrustworthy ID server %r even though it isn't" + " in the trusted id list for testing because" + " 'use_insecure_ssl_client_just_for_testing_do_not_use'" + " is set in the config", + id_server, + ) + else: + return False + return True + + class LookupAlgorithm: """ Supported hashing algorithms when performing a 3PID lookup.