25
25
from signedjson .sign import verify_signed_json
26
26
from unpaddedbase64 import decode_base64
27
27
28
- from twisted .internet import defer
29
28
from twisted .internet .error import TimeoutError
30
29
31
30
from synapse .api .errors import (
@@ -60,8 +59,7 @@ def __init__(self, hs):
60
59
self .federation_http_client = hs .get_http_client ()
61
60
self .hs = hs
62
61
63
- @defer .inlineCallbacks
64
- def threepid_from_creds (self , id_server , creds ):
62
+ async def threepid_from_creds (self , id_server , creds ):
65
63
"""
66
64
Retrieve and validate a threepid identifier from a "credentials" dictionary against a
67
65
given identity server
@@ -97,7 +95,7 @@ def threepid_from_creds(self, id_server, creds):
97
95
url = id_server + "/_matrix/identity/api/v1/3pid/getValidated3pid"
98
96
99
97
try :
100
- data = yield self .http_client .get_json (url , query_params )
98
+ data = await self .http_client .get_json (url , query_params )
101
99
except TimeoutError :
102
100
raise SynapseError (500 , "Timed out contacting identity server" )
103
101
except HttpResponseException as e :
@@ -120,8 +118,7 @@ def threepid_from_creds(self, id_server, creds):
120
118
logger .info ("%s reported non-validated threepid: %s" , id_server , creds )
121
119
return None
122
120
123
- @defer .inlineCallbacks
124
- def bind_threepid (
121
+ async def bind_threepid (
125
122
self , client_secret , sid , mxid , id_server , id_access_token = None , use_v2 = True
126
123
):
127
124
"""Bind a 3PID to an identity server
@@ -161,12 +158,12 @@ def bind_threepid(
161
158
try :
162
159
# Use the blacklisting http client as this call is only to identity servers
163
160
# 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 (
165
162
bind_url , bind_data , headers = headers
166
163
)
167
164
168
165
# Remember where we bound the threepid
169
- yield self .store .add_user_bound_threepid (
166
+ await self .store .add_user_bound_threepid (
170
167
user_id = mxid ,
171
168
medium = data ["medium" ],
172
169
address = data ["address" ],
@@ -185,13 +182,12 @@ def bind_threepid(
185
182
return data
186
183
187
184
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 (
189
186
client_secret , sid , mxid , id_server , id_access_token , use_v2 = False
190
187
)
191
188
return res
192
189
193
- @defer .inlineCallbacks
194
- def try_unbind_threepid (self , mxid , threepid ):
190
+ async def try_unbind_threepid (self , mxid , threepid ):
195
191
"""Attempt to remove a 3PID from an identity server, or if one is not provided, all
196
192
identity servers we're aware the binding is present on
197
193
@@ -211,7 +207,7 @@ def try_unbind_threepid(self, mxid, threepid):
211
207
if threepid .get ("id_server" ):
212
208
id_servers = [threepid ["id_server" ]]
213
209
else :
214
- id_servers = yield self .store .get_id_servers_user_bound (
210
+ id_servers = await self .store .get_id_servers_user_bound (
215
211
user_id = mxid , medium = threepid ["medium" ], address = threepid ["address" ]
216
212
)
217
213
@@ -221,14 +217,13 @@ def try_unbind_threepid(self, mxid, threepid):
221
217
222
218
changed = True
223
219
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 (
225
221
mxid , threepid , id_server
226
222
)
227
223
228
224
return changed
229
225
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 ):
232
227
"""Removes a binding from an identity server
233
228
234
229
Args:
@@ -266,7 +261,7 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
266
261
try :
267
262
# Use the blacklisting http client as this call is only to identity servers
268
263
# provided by a client
269
- yield self .blacklisting_http_client .post_json_get_json (
264
+ await self .blacklisting_http_client .post_json_get_json (
270
265
url , content , headers
271
266
)
272
267
changed = True
@@ -281,7 +276,7 @@ def try_unbind_threepid_with_id_server(self, mxid, threepid, id_server):
281
276
except TimeoutError :
282
277
raise SynapseError (500 , "Timed out contacting identity server" )
283
278
284
- yield self .store .remove_user_bound_threepid (
279
+ await self .store .remove_user_bound_threepid (
285
280
user_id = mxid ,
286
281
medium = threepid ["medium" ],
287
282
address = threepid ["address" ],
@@ -376,8 +371,7 @@ async def send_threepid_validation(
376
371
377
372
return session_id
378
373
379
- @defer .inlineCallbacks
380
- def requestEmailToken (
374
+ async def requestEmailToken (
381
375
self , id_server , email , client_secret , send_attempt , next_link = None
382
376
):
383
377
"""
@@ -412,7 +406,7 @@ def requestEmailToken(
412
406
)
413
407
414
408
try :
415
- data = yield self .http_client .post_json_get_json (
409
+ data = await self .http_client .post_json_get_json (
416
410
id_server + "/_matrix/identity/api/v1/validate/email/requestToken" ,
417
411
params ,
418
412
)
@@ -423,8 +417,7 @@ def requestEmailToken(
423
417
except TimeoutError :
424
418
raise SynapseError (500 , "Timed out contacting identity server" )
425
419
426
- @defer .inlineCallbacks
427
- def requestMsisdnToken (
420
+ async def requestMsisdnToken (
428
421
self ,
429
422
id_server ,
430
423
country ,
@@ -466,7 +459,7 @@ def requestMsisdnToken(
466
459
)
467
460
468
461
try :
469
- data = yield self .http_client .post_json_get_json (
462
+ data = await self .http_client .post_json_get_json (
470
463
id_server + "/_matrix/identity/api/v1/validate/msisdn/requestToken" ,
471
464
params ,
472
465
)
@@ -487,8 +480,7 @@ def requestMsisdnToken(
487
480
)
488
481
return data
489
482
490
- @defer .inlineCallbacks
491
- def validate_threepid_session (self , client_secret , sid ):
483
+ async def validate_threepid_session (self , client_secret , sid ):
492
484
"""Validates a threepid session with only the client secret and session ID
493
485
Tries validating against any configured account_threepid_delegates as well as locally.
494
486
@@ -510,12 +502,12 @@ def validate_threepid_session(self, client_secret, sid):
510
502
# Try to validate as email
511
503
if self .hs .config .threepid_behaviour_email == ThreepidBehaviour .REMOTE :
512
504
# Ask our delegated email identity server
513
- validation_session = yield self .threepid_from_creds (
505
+ validation_session = await self .threepid_from_creds (
514
506
self .hs .config .account_threepid_delegate_email , threepid_creds
515
507
)
516
508
elif self .hs .config .threepid_behaviour_email == ThreepidBehaviour .LOCAL :
517
509
# 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 (
519
511
"email" , client_secret , sid = sid , validated = True
520
512
)
521
513
@@ -525,14 +517,13 @@ def validate_threepid_session(self, client_secret, sid):
525
517
# Try to validate as msisdn
526
518
if self .hs .config .account_threepid_delegate_msisdn :
527
519
# Ask our delegated msisdn identity server
528
- validation_session = yield self .threepid_from_creds (
520
+ validation_session = await self .threepid_from_creds (
529
521
self .hs .config .account_threepid_delegate_msisdn , threepid_creds
530
522
)
531
523
532
524
return validation_session
533
525
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 ):
536
527
"""Proxy a POST submitToken request to an identity server for verification purposes
537
528
538
529
Args:
@@ -553,20 +544,17 @@ def proxy_msisdn_submit_token(self, id_server, client_secret, sid, token):
553
544
body = {"client_secret" : client_secret , "sid" : sid , "token" : token }
554
545
555
546
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 ,
561
550
)
562
551
except TimeoutError :
563
552
raise SynapseError (500 , "Timed out contacting identity server" )
564
553
except HttpResponseException as e :
565
554
logger .warning ("Error contacting msisdn account_threepid_delegate: %s" , e )
566
555
raise SynapseError (400 , "Error contacting the identity server" )
567
556
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 ):
570
558
"""Looks up a 3pid in the passed identity server.
571
559
572
560
Args:
@@ -582,7 +570,7 @@ def lookup_3pid(self, id_server, medium, address, id_access_token=None):
582
570
"""
583
571
if id_access_token is not None :
584
572
try :
585
- results = yield self ._lookup_3pid_v2 (
573
+ results = await self ._lookup_3pid_v2 (
586
574
id_server , id_access_token , medium , address
587
575
)
588
576
return results
@@ -601,10 +589,9 @@ def lookup_3pid(self, id_server, medium, address, id_access_token=None):
601
589
logger .warning ("Error when looking up hashing details: %s" , e )
602
590
return None
603
591
604
- return ( yield self ._lookup_3pid_v1 (id_server , medium , address ) )
592
+ return await self ._lookup_3pid_v1 (id_server , medium , address )
605
593
606
- @defer .inlineCallbacks
607
- def _lookup_3pid_v1 (self , id_server , medium , address ):
594
+ async def _lookup_3pid_v1 (self , id_server , medium , address ):
608
595
"""Looks up a 3pid in the passed identity server using v1 lookup.
609
596
610
597
Args:
@@ -617,15 +604,15 @@ def _lookup_3pid_v1(self, id_server, medium, address):
617
604
str: the matrix ID of the 3pid, or None if it is not recognized.
618
605
"""
619
606
try :
620
- data = yield self .blacklisting_http_client .get_json (
607
+ data = await self .blacklisting_http_client .get_json (
621
608
"%s%s/_matrix/identity/api/v1/lookup" % (id_server_scheme , id_server ),
622
609
{"medium" : medium , "address" : address },
623
610
)
624
611
625
612
if "mxid" in data :
626
613
if "signatures" not in data :
627
614
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 )
629
616
return data ["mxid" ]
630
617
except TimeoutError :
631
618
raise SynapseError (500 , "Timed out contacting identity server" )
@@ -634,8 +621,7 @@ def _lookup_3pid_v1(self, id_server, medium, address):
634
621
635
622
return None
636
623
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 ):
639
625
"""Looks up a 3pid in the passed identity server using v2 lookup.
640
626
641
627
Args:
@@ -650,7 +636,7 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
650
636
"""
651
637
# Check what hashing details are supported by this identity server
652
638
try :
653
- hash_details = yield self .blacklisting_http_client .get_json (
639
+ hash_details = await self .blacklisting_http_client .get_json (
654
640
"%s%s/_matrix/identity/v2/hash_details" % (id_server_scheme , id_server ),
655
641
{"access_token" : id_access_token },
656
642
)
@@ -717,7 +703,7 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
717
703
headers = {"Authorization" : create_id_access_token_header (id_access_token )}
718
704
719
705
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 (
721
707
"%s%s/_matrix/identity/v2/lookup" % (id_server_scheme , id_server ),
722
708
{
723
709
"addresses" : [lookup_value ],
@@ -745,13 +731,12 @@ def _lookup_3pid_v2(self, id_server, id_access_token, medium, address):
745
731
mxid = lookup_results ["mappings" ].get (lookup_value )
746
732
return mxid
747
733
748
- @defer .inlineCallbacks
749
- def _verify_any_signature (self , data , server_hostname ):
734
+ async def _verify_any_signature (self , data , server_hostname ):
750
735
if server_hostname not in data ["signatures" ]:
751
736
raise AuthError (401 , "No signature from server %s" % (server_hostname ,))
752
737
for key_name , signature in data ["signatures" ][server_hostname ].items ():
753
738
try :
754
- key_data = yield self .blacklisting_http_client .get_json (
739
+ key_data = await self .blacklisting_http_client .get_json (
755
740
"%s%s/_matrix/identity/api/v1/pubkey/%s"
756
741
% (id_server_scheme , server_hostname , key_name )
757
742
)
@@ -770,8 +755,7 @@ def _verify_any_signature(self, data, server_hostname):
770
755
)
771
756
return
772
757
773
- @defer .inlineCallbacks
774
- def ask_id_server_for_third_party_invite (
758
+ async def ask_id_server_for_third_party_invite (
775
759
self ,
776
760
requester ,
777
761
id_server ,
@@ -844,7 +828,7 @@ def ask_id_server_for_third_party_invite(
844
828
# Attempt a v2 lookup
845
829
url = base_url + "/v2/store-invite"
846
830
try :
847
- data = yield self .blacklisting_http_client .post_json_get_json (
831
+ data = await self .blacklisting_http_client .post_json_get_json (
848
832
url ,
849
833
invite_config ,
850
834
{"Authorization" : create_id_access_token_header (id_access_token )},
@@ -864,7 +848,7 @@ def ask_id_server_for_third_party_invite(
864
848
url = base_url + "/api/v1/store-invite"
865
849
866
850
try :
867
- data = yield self .blacklisting_http_client .post_json_get_json (
851
+ data = await self .blacklisting_http_client .post_json_get_json (
868
852
url , invite_config
869
853
)
870
854
except TimeoutError :
@@ -882,7 +866,7 @@ def ask_id_server_for_third_party_invite(
882
866
# types. This is especially true with old instances of Sydent, see
883
867
# https://github.com/matrix-org/sydent/pull/170
884
868
try :
885
- data = yield self .blacklisting_http_client .post_urlencoded_get_json (
869
+ data = await self .blacklisting_http_client .post_urlencoded_get_json (
886
870
url , invite_config
887
871
)
888
872
except HttpResponseException as e :
0 commit comments