Skip to content

Commit 628ed24

Browse files
clokepphil-flex
authored andcommitted
Convert identity handler to async/await. (matrix-org#7561)
1 parent ff0edda commit 628ed24

File tree

3 files changed

+47
-63
lines changed

3 files changed

+47
-63
lines changed

changelog.d/7561.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert the identity handler to async/await.

synapse/handlers/identity.py

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from signedjson.sign import verify_signed_json
2626
from unpaddedbase64 import decode_base64
2727

28-
from twisted.internet import defer
2928
from twisted.internet.error import TimeoutError
3029

3130
from synapse.api.errors import (
@@ -60,8 +59,7 @@ def __init__(self, hs):
6059
self.federation_http_client = hs.get_http_client()
6160
self.hs = hs
6261

63-
@defer.inlineCallbacks
64-
def threepid_from_creds(self, id_server, creds):
62+
async def threepid_from_creds(self, id_server, creds):
6563
"""
6664
Retrieve and validate a threepid identifier from a "credentials" dictionary against a
6765
given identity server
@@ -97,7 +95,7 @@ def threepid_from_creds(self, id_server, creds):
9795
url = id_server + "/_matrix/identity/api/v1/3pid/getValidated3pid"
9896

9997
try:
100-
data = yield self.http_client.get_json(url, query_params)
98+
data = await self.http_client.get_json(url, query_params)
10199
except TimeoutError:
102100
raise SynapseError(500, "Timed out contacting identity server")
103101
except HttpResponseException as e:
@@ -120,8 +118,7 @@ def threepid_from_creds(self, id_server, creds):
120118
logger.info("%s reported non-validated threepid: %s", id_server, creds)
121119
return None
122120

123-
@defer.inlineCallbacks
124-
def bind_threepid(
121+
async def bind_threepid(
125122
self, client_secret, sid, mxid, id_server, id_access_token=None, use_v2=True
126123
):
127124
"""Bind a 3PID to an identity server
@@ -161,12 +158,12 @@ def bind_threepid(
161158
try:
162159
# Use the blacklisting http client as this call is only to identity servers
163160
# provided by a client
164-
data = yield self.blacklisting_http_client.post_json_get_json(
161+
data = await self.blacklisting_http_client.post_json_get_json(
165162
bind_url, bind_data, headers=headers
166163
)
167164

168165
# Remember where we bound the threepid
169-
yield self.store.add_user_bound_threepid(
166+
await self.store.add_user_bound_threepid(
170167
user_id=mxid,
171168
medium=data["medium"],
172169
address=data["address"],
@@ -185,13 +182,12 @@ def bind_threepid(
185182
return data
186183

187184
logger.info("Got 404 when POSTing JSON %s, falling back to v1 URL", bind_url)
188-
res = yield self.bind_threepid(
185+
res = await self.bind_threepid(
189186
client_secret, sid, mxid, id_server, id_access_token, use_v2=False
190187
)
191188
return res
192189

193-
@defer.inlineCallbacks
194-
def try_unbind_threepid(self, mxid, threepid):
190+
async def try_unbind_threepid(self, mxid, threepid):
195191
"""Attempt to remove a 3PID from an identity server, or if one is not provided, all
196192
identity servers we're aware the binding is present on
197193
@@ -211,7 +207,7 @@ def try_unbind_threepid(self, mxid, threepid):
211207
if threepid.get("id_server"):
212208
id_servers = [threepid["id_server"]]
213209
else:
214-
id_servers = yield self.store.get_id_servers_user_bound(
210+
id_servers = await self.store.get_id_servers_user_bound(
215211
user_id=mxid, medium=threepid["medium"], address=threepid["address"]
216212
)
217213

@@ -221,14 +217,13 @@ def try_unbind_threepid(self, mxid, threepid):
221217

222218
changed = True
223219
for id_server in id_servers:
224-
changed &= yield self.try_unbind_threepid_with_id_server(
220+
changed &= await self.try_unbind_threepid_with_id_server(
225221
mxid, threepid, id_server
226222
)
227223

228224
return changed
229225

230-
@defer.inlineCallbacks
231-
def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
226+
async def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
232227
"""Removes a binding from an identity server
233228
234229
Args:
@@ -266,7 +261,7 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
266261
try:
267262
# Use the blacklisting http client as this call is only to identity servers
268263
# provided by a client
269-
yield self.blacklisting_http_client.post_json_get_json(
264+
await self.blacklisting_http_client.post_json_get_json(
270265
url, content, headers
271266
)
272267
changed = True
@@ -281,7 +276,7 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
281276
except TimeoutError:
282277
raise SynapseError(500, "Timed out contacting identity server")
283278

284-
yield self.store.remove_user_bound_threepid(
279+
await self.store.remove_user_bound_threepid(
285280
user_id=mxid,
286281
medium=threepid["medium"],
287282
address=threepid["address"],
@@ -376,8 +371,7 @@ async def send_threepid_validation(
376371

377372
return session_id
378373

379-
@defer.inlineCallbacks
380-
def requestEmailToken(
374+
async def requestEmailToken(
381375
self, id_server, email, client_secret, send_attempt, next_link=None
382376
):
383377
"""
@@ -412,7 +406,7 @@ def requestEmailToken(
412406
)
413407

414408
try:
415-
data = yield self.http_client.post_json_get_json(
409+
data = await self.http_client.post_json_get_json(
416410
id_server + "/_matrix/identity/api/v1/validate/email/requestToken",
417411
params,
418412
)
@@ -423,8 +417,7 @@ def requestEmailToken(
423417
except TimeoutError:
424418
raise SynapseError(500, "Timed out contacting identity server")
425419

426-
@defer.inlineCallbacks
427-
def requestMsisdnToken(
420+
async def requestMsisdnToken(
428421
self,
429422
id_server,
430423
country,
@@ -466,7 +459,7 @@ def requestMsisdnToken(
466459
)
467460

468461
try:
469-
data = yield self.http_client.post_json_get_json(
462+
data = await self.http_client.post_json_get_json(
470463
id_server + "/_matrix/identity/api/v1/validate/msisdn/requestToken",
471464
params,
472465
)
@@ -487,8 +480,7 @@ def requestMsisdnToken(
487480
)
488481
return data
489482

490-
@defer.inlineCallbacks
491-
def validate_threepid_session(self, client_secret, sid):
483+
async def validate_threepid_session(self, client_secret, sid):
492484
"""Validates a threepid session with only the client secret and session ID
493485
Tries validating against any configured account_threepid_delegates as well as locally.
494486
@@ -510,12 +502,12 @@ def validate_threepid_session(self, client_secret, sid):
510502
# Try to validate as email
511503
if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
512504
# Ask our delegated email identity server
513-
validation_session = yield self.threepid_from_creds(
505+
validation_session = await self.threepid_from_creds(
514506
self.hs.config.account_threepid_delegate_email, threepid_creds
515507
)
516508
elif self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
517509
# Get a validated session matching these details
518-
validation_session = yield self.store.get_threepid_validation_session(
510+
validation_session = await self.store.get_threepid_validation_session(
519511
"email", client_secret, sid=sid, validated=True
520512
)
521513

@@ -525,14 +517,13 @@ def validate_threepid_session(self, client_secret, sid):
525517
# Try to validate as msisdn
526518
if self.hs.config.account_threepid_delegate_msisdn:
527519
# Ask our delegated msisdn identity server
528-
validation_session = yield self.threepid_from_creds(
520+
validation_session = await self.threepid_from_creds(
529521
self.hs.config.account_threepid_delegate_msisdn, threepid_creds
530522
)
531523

532524
return validation_session
533525

534-
@defer.inlineCallbacks
535-
def proxy_msisdn_submit_token(self, id_server, client_secret, sid, token):
526+
async def proxy_msisdn_submit_token(self, id_server, client_secret, sid, token):
536527
"""Proxy a POST submitToken request to an identity server for verification purposes
537528
538529
Args:
@@ -553,20 +544,17 @@ def proxy_msisdn_submit_token(self, id_server, client_secret, sid, token):
553544
body = {"client_secret": client_secret, "sid": sid, "token": token}
554545

555546
try:
556-
return (
557-
yield self.http_client.post_json_get_json(
558-
id_server + "/_matrix/identity/api/v1/validate/msisdn/submitToken",
559-
body,
560-
)
547+
return await self.http_client.post_json_get_json(
548+
id_server + "/_matrix/identity/api/v1/validate/msisdn/submitToken",
549+
body,
561550
)
562551
except TimeoutError:
563552
raise SynapseError(500, "Timed out contacting identity server")
564553
except HttpResponseException as e:
565554
logger.warning("Error contacting msisdn account_threepid_delegate: %s", e)
566555
raise SynapseError(400, "Error contacting the identity server")
567556

568-
@defer.inlineCallbacks
569-
def lookup_3pid(self, id_server, medium, address, id_access_token=None):
557+
async def lookup_3pid(self, id_server, medium, address, id_access_token=None):
570558
"""Looks up a 3pid in the passed identity server.
571559
572560
Args:
@@ -582,7 +570,7 @@ def lookup_3pid(self, id_server, medium, address, id_access_token=None):
582570
"""
583571
if id_access_token is not None:
584572
try:
585-
results = yield self._lookup_3pid_v2(
573+
results = await self._lookup_3pid_v2(
586574
id_server, id_access_token, medium, address
587575
)
588576
return results
@@ -601,10 +589,9 @@ def lookup_3pid(self, id_server, medium, address, id_access_token=None):
601589
logger.warning("Error when looking up hashing details: %s", e)
602590
return None
603591

604-
return (yield self._lookup_3pid_v1(id_server, medium, address))
592+
return await self._lookup_3pid_v1(id_server, medium, address)
605593

606-
@defer.inlineCallbacks
607-
def _lookup_3pid_v1(self, id_server, medium, address):
594+
async def _lookup_3pid_v1(self, id_server, medium, address):
608595
"""Looks up a 3pid in the passed identity server using v1 lookup.
609596
610597
Args:
@@ -617,15 +604,15 @@ def _lookup_3pid_v1(self, id_server, medium, address):
617604
str: the matrix ID of the 3pid, or None if it is not recognized.
618605
"""
619606
try:
620-
data = yield self.blacklisting_http_client.get_json(
607+
data = await self.blacklisting_http_client.get_json(
621608
"%s%s/_matrix/identity/api/v1/lookup" % (id_server_scheme, id_server),
622609
{"medium": medium, "address": address},
623610
)
624611

625612
if "mxid" in data:
626613
if "signatures" not in data:
627614
raise AuthError(401, "No signatures on 3pid binding")
628-
yield self._verify_any_signature(data, id_server)
615+
await self._verify_any_signature(data, id_server)
629616
return data["mxid"]
630617
except TimeoutError:
631618
raise SynapseError(500, "Timed out contacting identity server")
@@ -634,8 +621,7 @@ def _lookup_3pid_v1(self, id_server, medium, address):
634621

635622
return None
636623

637-
@defer.inlineCallbacks
638-
def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
624+
async def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
639625
"""Looks up a 3pid in the passed identity server using v2 lookup.
640626
641627
Args:
@@ -650,7 +636,7 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
650636
"""
651637
# Check what hashing details are supported by this identity server
652638
try:
653-
hash_details = yield self.blacklisting_http_client.get_json(
639+
hash_details = await self.blacklisting_http_client.get_json(
654640
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme, id_server),
655641
{"access_token": id_access_token},
656642
)
@@ -717,7 +703,7 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
717703
headers = {"Authorization": create_id_access_token_header(id_access_token)}
718704

719705
try:
720-
lookup_results = yield self.blacklisting_http_client.post_json_get_json(
706+
lookup_results = await self.blacklisting_http_client.post_json_get_json(
721707
"%s%s/_matrix/identity/v2/lookup" % (id_server_scheme, id_server),
722708
{
723709
"addresses": [lookup_value],
@@ -745,13 +731,12 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
745731
mxid = lookup_results["mappings"].get(lookup_value)
746732
return mxid
747733

748-
@defer.inlineCallbacks
749-
def _verify_any_signature(self, data, server_hostname):
734+
async def _verify_any_signature(self, data, server_hostname):
750735
if server_hostname not in data["signatures"]:
751736
raise AuthError(401, "No signature from server %s" % (server_hostname,))
752737
for key_name, signature in data["signatures"][server_hostname].items():
753738
try:
754-
key_data = yield self.blacklisting_http_client.get_json(
739+
key_data = await self.blacklisting_http_client.get_json(
755740
"%s%s/_matrix/identity/api/v1/pubkey/%s"
756741
% (id_server_scheme, server_hostname, key_name)
757742
)
@@ -770,8 +755,7 @@ def _verify_any_signature(self, data, server_hostname):
770755
)
771756
return
772757

773-
@defer.inlineCallbacks
774-
def ask_id_server_for_third_party_invite(
758+
async def ask_id_server_for_third_party_invite(
775759
self,
776760
requester,
777761
id_server,
@@ -844,7 +828,7 @@ def ask_id_server_for_third_party_invite(
844828
# Attempt a v2 lookup
845829
url = base_url + "/v2/store-invite"
846830
try:
847-
data = yield self.blacklisting_http_client.post_json_get_json(
831+
data = await self.blacklisting_http_client.post_json_get_json(
848832
url,
849833
invite_config,
850834
{"Authorization": create_id_access_token_header(id_access_token)},
@@ -864,7 +848,7 @@ def ask_id_server_for_third_party_invite(
864848
url = base_url + "/api/v1/store-invite"
865849

866850
try:
867-
data = yield self.blacklisting_http_client.post_json_get_json(
851+
data = await self.blacklisting_http_client.post_json_get_json(
868852
url, invite_config
869853
)
870854
except TimeoutError:
@@ -882,7 +866,7 @@ def ask_id_server_for_third_party_invite(
882866
# types. This is especially true with old instances of Sydent, see
883867
# https://github.com/matrix-org/sydent/pull/170
884868
try:
885-
data = yield self.blacklisting_http_client.post_urlencoded_get_json(
869+
data = await self.blacklisting_http_client.post_urlencoded_get_json(
886870
url, invite_config
887871
)
888872
except HttpResponseException as e:

synapse/handlers/ui_auth/checkers.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ def __init__(self, hs):
138138
self.hs = hs
139139
self.store = hs.get_datastore()
140140

141-
@defer.inlineCallbacks
142-
def _check_threepid(self, medium, authdict):
141+
async def _check_threepid(self, medium, authdict):
143142
if "threepid_creds" not in authdict:
144143
raise LoginError(400, "Missing threepid_creds", Codes.MISSING_PARAM)
145144

@@ -155,18 +154,18 @@ def _check_threepid(self, medium, authdict):
155154
raise SynapseError(
156155
400, "Phone number verification is not enabled on this homeserver"
157156
)
158-
threepid = yield identity_handler.threepid_from_creds(
157+
threepid = await identity_handler.threepid_from_creds(
159158
self.hs.config.account_threepid_delegate_msisdn, threepid_creds
160159
)
161160
elif medium == "email":
162161
if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.REMOTE:
163162
assert self.hs.config.account_threepid_delegate_email
164-
threepid = yield identity_handler.threepid_from_creds(
163+
threepid = await identity_handler.threepid_from_creds(
165164
self.hs.config.account_threepid_delegate_email, threepid_creds
166165
)
167166
elif self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
168167
threepid = None
169-
row = yield self.store.get_threepid_validation_session(
168+
row = await self.store.get_threepid_validation_session(
170169
medium,
171170
threepid_creds["client_secret"],
172171
sid=threepid_creds["sid"],
@@ -181,7 +180,7 @@ def _check_threepid(self, medium, authdict):
181180
}
182181

183182
# Valid threepid returned, delete from the db
184-
yield self.store.delete_threepid_session(threepid_creds["sid"])
183+
await self.store.delete_threepid_session(threepid_creds["sid"])
185184
else:
186185
raise SynapseError(
187186
400, "Email address verification is not enabled on this homeserver"
@@ -220,7 +219,7 @@ def is_enabled(self):
220219
)
221220

222221
def check_auth(self, authdict, clientip):
223-
return self._check_threepid("email", authdict)
222+
return defer.ensureDeferred(self._check_threepid("email", authdict))
224223

225224

226225
class MsisdnAuthChecker(UserInteractiveAuthChecker, _BaseThreepidAuthChecker):
@@ -234,7 +233,7 @@ def is_enabled(self):
234233
return bool(self.hs.config.account_threepid_delegate_msisdn)
235234

236235
def check_auth(self, authdict, clientip):
237-
return self._check_threepid("msisdn", authdict)
236+
return defer.ensureDeferred(self._check_threepid("msisdn", authdict))
238237

239238

240239
INTERACTIVE_AUTH_CHECKERS = [

0 commit comments

Comments
 (0)