19
19
from urllib .parse import urlparse
20
20
21
21
from pydantic import StrictBool , StrictStr , constr
22
+ from typing_extensions import Literal
22
23
23
24
from twisted .web .server import Request
24
25
43
44
from synapse .push .mailer import Mailer
44
45
from synapse .rest .client .models import (
45
46
AuthenticationData ,
47
+ ClientSecretStr ,
46
48
EmailRequestTokenBody ,
47
49
MsisdnRequestTokenBody ,
48
50
)
@@ -627,6 +629,11 @@ def __init__(self, hs: "HomeServer"):
627
629
self .auth = hs .get_auth ()
628
630
self .auth_handler = hs .get_auth_handler ()
629
631
632
+ class PostBody (RequestBodyModel ):
633
+ auth : Optional [AuthenticationData ] = None
634
+ client_secret : ClientSecretStr
635
+ sid : StrictStr
636
+
630
637
@interactive_auth_handler
631
638
async def on_POST (self , request : SynapseRequest ) -> Tuple [int , JsonDict ]:
632
639
if not self .hs .config .registration .enable_3pid_changes :
@@ -636,22 +643,17 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
636
643
637
644
requester = await self .auth .get_user_by_req (request )
638
645
user_id = requester .user .to_string ()
639
- body = parse_json_object_from_request (request )
640
-
641
- assert_params_in_dict (body , ["client_secret" , "sid" ])
642
- sid = body ["sid" ]
643
- client_secret = body ["client_secret" ]
644
- assert_valid_client_secret (client_secret )
646
+ body = parse_and_validate_json_object_from_request (request , self .PostBody )
645
647
646
648
await self .auth_handler .validate_user_via_ui_auth (
647
649
requester ,
648
650
request ,
649
- body ,
651
+ body . dict ( exclude_unset = True ) ,
650
652
"add a third-party identifier to your account" ,
651
653
)
652
654
653
655
validation_session = await self .identity_handler .validate_threepid_session (
654
- client_secret , sid
656
+ body . client_secret , body . sid
655
657
)
656
658
if validation_session :
657
659
await self .auth_handler .add_threepid (
@@ -676,23 +678,20 @@ def __init__(self, hs: "HomeServer"):
676
678
self .identity_handler = hs .get_identity_handler ()
677
679
self .auth = hs .get_auth ()
678
680
679
- async def on_POST (self , request : SynapseRequest ) -> Tuple [int , JsonDict ]:
680
- body = parse_json_object_from_request (request )
681
+ class PostBody (RequestBodyModel ):
682
+ client_secret : ClientSecretStr
683
+ id_access_token : StrictStr
684
+ id_server : StrictStr
685
+ sid : StrictStr
681
686
682
- assert_params_in_dict (
683
- body , ["id_server" , "sid" , "id_access_token" , "client_secret" ]
684
- )
685
- id_server = body ["id_server" ]
686
- sid = body ["sid" ]
687
- id_access_token = body ["id_access_token" ]
688
- client_secret = body ["client_secret" ]
689
- assert_valid_client_secret (client_secret )
687
+ async def on_POST (self , request : SynapseRequest ) -> Tuple [int , JsonDict ]:
688
+ body = parse_and_validate_json_object_from_request (request , self .PostBody )
690
689
691
690
requester = await self .auth .get_user_by_req (request )
692
691
user_id = requester .user .to_string ()
693
692
694
693
await self .identity_handler .bind_threepid (
695
- client_secret , sid , user_id , id_server , id_access_token
694
+ body . client_secret , body . sid , user_id , body . id_server , body . id_access_token
696
695
)
697
696
698
697
return 200 , {}
@@ -708,23 +707,27 @@ def __init__(self, hs: "HomeServer"):
708
707
self .auth = hs .get_auth ()
709
708
self .datastore = self .hs .get_datastores ().main
710
709
710
+ class PostBody (RequestBodyModel ):
711
+ address : StrictStr
712
+ id_server : Optional [StrictStr ] = None
713
+ medium : Literal ["email" , "msisdn" ]
714
+
711
715
async def on_POST (self , request : SynapseRequest ) -> Tuple [int , JsonDict ]:
712
716
"""Unbind the given 3pid from a specific identity server, or identity servers that are
713
717
known to have this 3pid bound
714
718
"""
715
719
requester = await self .auth .get_user_by_req (request )
716
- body = parse_json_object_from_request (request )
717
- assert_params_in_dict (body , ["medium" , "address" ])
718
-
719
- medium = body .get ("medium" )
720
- address = body .get ("address" )
721
- id_server = body .get ("id_server" )
720
+ body = parse_and_validate_json_object_from_request (request , self .PostBody )
722
721
723
722
# Attempt to unbind the threepid from an identity server. If id_server is None, try to
724
723
# unbind from all identity servers this threepid has been added to in the past
725
724
result = await self .identity_handler .try_unbind_threepid (
726
725
requester .user .to_string (),
727
- {"address" : address , "medium" : medium , "id_server" : id_server },
726
+ {
727
+ "address" : body .address ,
728
+ "medium" : body .medium ,
729
+ "id_server" : body .id_server ,
730
+ },
728
731
)
729
732
return 200 , {"id_server_unbind_result" : "success" if result else "no-support" }
730
733
@@ -738,21 +741,25 @@ def __init__(self, hs: "HomeServer"):
738
741
self .auth = hs .get_auth ()
739
742
self .auth_handler = hs .get_auth_handler ()
740
743
744
+ class PostBody (RequestBodyModel ):
745
+ address : StrictStr
746
+ id_server : Optional [StrictStr ] = None
747
+ medium : Literal ["email" , "msisdn" ]
748
+
741
749
async def on_POST (self , request : SynapseRequest ) -> Tuple [int , JsonDict ]:
742
750
if not self .hs .config .registration .enable_3pid_changes :
743
751
raise SynapseError (
744
752
400 , "3PID changes are disabled on this server" , Codes .FORBIDDEN
745
753
)
746
754
747
- body = parse_json_object_from_request (request )
748
- assert_params_in_dict (body , ["medium" , "address" ])
755
+ body = parse_and_validate_json_object_from_request (request , self .PostBody )
749
756
750
757
requester = await self .auth .get_user_by_req (request )
751
758
user_id = requester .user .to_string ()
752
759
753
760
try :
754
761
ret = await self .auth_handler .delete_threepid (
755
- user_id , body [ " medium" ] , body [ " address" ] , body .get ( " id_server" )
762
+ user_id , body . medium , body . address , body .id_server
756
763
)
757
764
except Exception :
758
765
# NB. This endpoint should succeed if there is nothing to
0 commit comments