<s> from twisted . internet import defer <EOL> from synapse . api . constants import LoginType <EOL> from synapse . api . errors import LoginError , SynapseError , Codes <EOL> from synapse . http . servlet import RestServlet , parse_json_object_from_request <EOL> from synapse . util . async import run_on_reactor <EOL> from . _base import client_v2_patterns <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> class PasswordRestServlet ( RestServlet ) : <EOL> PATTERNS = client_v2_patterns ( "<STR_LIT>" ) <EOL> def __init__ ( self , hs ) : <EOL> super ( PasswordRestServlet , self ) . __init__ ( ) <EOL> self . hs = hs <EOL> self . auth = hs . get_auth ( ) <EOL> self . auth_handler = hs . get_handlers ( ) . auth_handler <EOL> @ defer . inlineCallbacks <EOL> def on_POST ( self , request ) : <EOL> yield run_on_reactor ( ) <EOL> body = parse_json_object_from_request ( request ) <EOL> authed , result , params , _ = yield self . auth_handler . check_auth ( [ <EOL> [ LoginType . PASSWORD ] , <EOL> [ LoginType . EMAIL_IDENTITY ] <EOL> ] , body , self . hs . get_ip_from_request ( request ) ) <EOL> if not authed : <EOL> defer . returnValue ( ( <NUM_LIT> , result ) ) <EOL> user_id = None <EOL> if LoginType . PASSWORD in result : <EOL> requester = yield self . auth . get_user_by_req ( request ) <EOL> user_id = requester . user . to_string ( ) <EOL> if user_id != result [ LoginType . PASSWORD ] : <EOL> raise LoginError ( <NUM_LIT> , "<STR_LIT>" , Codes . UNKNOWN ) <EOL> elif LoginType . EMAIL_IDENTITY in result : <EOL> threepid = result [ LoginType . EMAIL_IDENTITY ] <EOL> if '<STR_LIT>' not in threepid or '<STR_LIT:address>' not in threepid : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> threepid_user_id = yield self . hs . get_datastore ( ) . get_user_id_by_threepid ( <EOL> threepid [ '<STR_LIT>' ] , threepid [ '<STR_LIT:address>' ] <EOL> ) <EOL> if not threepid_user_id : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" , Codes . NOT_FOUND ) <EOL> user_id = threepid_user_id <EOL> else : <EOL> logger . error ( "<STR_LIT>" , result . keys ( ) ) <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" , Codes . UNKNOWN ) <EOL> if '<STR_LIT>' not in params : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" , Codes . MISSING_PARAM ) <EOL> new_password = params [ '<STR_LIT>' ] <EOL> yield self . auth_handler . set_password ( <EOL> user_id , new_password , requester <EOL> ) <EOL> defer . returnValue ( ( <NUM_LIT:200> , { } ) ) <EOL> def on_OPTIONS ( self , _ ) : <EOL> return <NUM_LIT:200> , { } <EOL> class ThreepidRestServlet ( RestServlet ) : <EOL> PATTERNS = client_v2_patterns ( "<STR_LIT>" ) <EOL> def __init__ ( self , hs ) : <EOL> super ( ThreepidRestServlet , self ) . __init__ ( ) <EOL> self . hs = hs <EOL> self . identity_handler = hs . get_handlers ( ) . identity_handler <EOL> self . auth = hs . get_auth ( ) <EOL> self . auth_handler = hs . get_handlers ( ) . auth_handler <EOL> @ defer . inlineCallbacks <EOL> def on_GET ( self , request ) : <EOL> yield run_on_reactor ( ) <EOL> requester = yield self . auth . get_user_by_req ( request ) <EOL> threepids = yield self . hs . get_datastore ( ) . user_get_threepids ( <EOL> requester . user . to_string ( ) <EOL> ) <EOL> defer . returnValue ( ( <NUM_LIT:200> , { '<STR_LIT>' : threepids } ) ) <EOL> @ defer . inlineCallbacks <EOL> def on_POST ( self , request ) : <EOL> yield run_on_reactor ( ) <EOL> body = parse_json_object_from_request ( request ) <EOL> threePidCreds = body . get ( '<STR_LIT>' ) <EOL> threePidCreds = body . get ( '<STR_LIT>' , threePidCreds ) <EOL> if threePidCreds is None : <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" , Codes . MISSING_PARAM ) <EOL> requester = yield self . auth . get_user_by_req ( request ) <EOL> user_id = requester . user . to_string ( ) <EOL> threepid = yield self . identity_handler . threepid_from_creds ( threePidCreds ) <EOL> if not threepid : <EOL> raise SynapseError ( <EOL> <NUM_LIT> , "<STR_LIT>" , Codes . THREEPID_AUTH_FAILED <EOL> ) <EOL> for reqd in [ '<STR_LIT>' , '<STR_LIT:address>' , '<STR_LIT>' ] : <EOL> if reqd not in threepid : <EOL> logger . warn ( "<STR_LIT>" ) <EOL> raise SynapseError ( <NUM_LIT> , "<STR_LIT>" ) <EOL> yield self . auth_handler . add_threepid ( <EOL> user_id , <EOL> threepid [ '<STR_LIT>' ] , <EOL> threepid [ '<STR_LIT:address>' ] , <EOL> threepid [ '<STR_LIT>' ] , <EOL> ) <EOL> if '<STR_LIT>' in body and body [ '<STR_LIT>' ] : <EOL> logger . debug ( <EOL> "<STR_LIT>" , <EOL> threepid , user_id <EOL> ) <EOL> yield self . identity_handler . bind_threepid ( <EOL> threePidCreds , user_id <EOL> ) <EOL> defer . returnValue ( ( <NUM_LIT:200> , { } ) ) <EOL> def register_servlets ( hs , http_server ) : <EOL> PasswordRestServlet ( hs ) . register ( http_server ) <EOL> ThreepidRestServlet ( hs ) . register ( http_server ) </s>
