diff options
Diffstat (limited to 'synapse/rest')
-rw-r--r-- | synapse/rest/profile.py | 6 | ||||
-rw-r--r-- | synapse/rest/register.py | 56 | ||||
-rw-r--r-- | synapse/rest/room.py | 56 | ||||
-rw-r--r-- | synapse/rest/transactions.py | 14 | ||||
-rw-r--r-- | synapse/rest/voip.py | 14 |
5 files changed, 85 insertions, 61 deletions
diff --git a/synapse/rest/profile.py b/synapse/rest/profile.py index dad5a208c7..72e02d8dd8 100644 --- a/synapse/rest/profile.py +++ b/synapse/rest/profile.py @@ -108,9 +108,9 @@ class ProfileRestServlet(RestServlet): ) defer.returnValue((200, { - "displayname": displayname, - "avatar_url": avatar_url - })) + "displayname": displayname, + "avatar_url": avatar_url + })) def register_servlets(hs, http_server): diff --git a/synapse/rest/register.py b/synapse/rest/register.py index 804117ee09..5c15614ea9 100644 --- a/synapse/rest/register.py +++ b/synapse/rest/register.py @@ -60,40 +60,45 @@ class RegisterRestServlet(RestServlet): def on_GET(self, request): if self.hs.config.enable_registration_captcha: - return (200, { - "flows": [ + return ( + 200, + {"flows": [ { "type": LoginType.RECAPTCHA, - "stages": ([LoginType.RECAPTCHA, - LoginType.EMAIL_IDENTITY, - LoginType.PASSWORD]) + "stages": [ + LoginType.RECAPTCHA, + LoginType.EMAIL_IDENTITY, + LoginType.PASSWORD + ] }, { "type": LoginType.RECAPTCHA, "stages": [LoginType.RECAPTCHA, LoginType.PASSWORD] } - ] - }) + ]} + ) else: - return (200, { - "flows": [ + return ( + 200, + {"flows": [ { "type": LoginType.EMAIL_IDENTITY, - "stages": ([LoginType.EMAIL_IDENTITY, - LoginType.PASSWORD]) + "stages": [ + LoginType.EMAIL_IDENTITY, LoginType.PASSWORD + ] }, { "type": LoginType.PASSWORD } - ] - }) + ]} + ) @defer.inlineCallbacks def on_POST(self, request): register_json = _parse_json(request) - session = (register_json["session"] if "session" in register_json - else None) + session = (register_json["session"] + if "session" in register_json else None) login_type = None if "type" not in register_json: raise SynapseError(400, "Missing 'type' key.") @@ -122,7 +127,9 @@ class RegisterRestServlet(RestServlet): defer.returnValue((200, response)) except KeyError as e: logger.exception(e) - raise SynapseError(400, "Missing JSON keys for login type %s." % login_type) + raise SynapseError(400, "Missing JSON keys for login type %s." % ( + login_type, + )) def on_OPTIONS(self, request): return (200, {}) @@ -183,8 +190,10 @@ class RegisterRestServlet(RestServlet): session["user"] = register_json["user"] defer.returnValue(None) else: - raise SynapseError(400, "Captcha bypass HMAC incorrect", - errcode=Codes.CAPTCHA_NEEDED) + raise SynapseError( + 400, "Captcha bypass HMAC incorrect", + errcode=Codes.CAPTCHA_NEEDED + ) challenge = None user_response = None @@ -230,12 +239,15 @@ class RegisterRestServlet(RestServlet): if ("user" in session and "user" in register_json and session["user"] != register_json["user"]): - raise SynapseError(400, "Cannot change user ID during registration") + raise SynapseError( + 400, "Cannot change user ID during registration" + ) password = register_json["password"].encode("utf-8") - desired_user_id = (register_json["user"].encode("utf-8") if "user" - in register_json else None) - if desired_user_id and urllib.quote(desired_user_id) != desired_user_id: + desired_user_id = (register_json["user"].encode("utf-8") + if "user" in register_json else None) + if (desired_user_id + and urllib.quote(desired_user_id) != desired_user_id): raise SynapseError( 400, "User ID must only contain characters which do not " + diff --git a/synapse/rest/room.py b/synapse/rest/room.py index a01dab1b8e..ec0ce78fda 100644 --- a/synapse/rest/room.py +++ b/synapse/rest/room.py @@ -48,7 +48,9 @@ class RoomCreateRestServlet(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, txn_id): try: - defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + defer.returnValue( + self.txns.get_client_transaction(request, txn_id) + ) except KeyError: pass @@ -98,8 +100,8 @@ class RoomStateEventRestServlet(RestServlet): no_state_key = "/rooms/(?P<room_id>[^/]*)/state/(?P<event_type>[^/]*)$" # /room/$roomid/state/$eventtype/$statekey - state_key = ("/rooms/(?P<room_id>[^/]*)/state/" + - "(?P<event_type>[^/]*)/(?P<state_key>[^/]*)$") + state_key = ("/rooms/(?P<room_id>[^/]*)/state/" + "(?P<event_type>[^/]*)/(?P<state_key>[^/]*)$") http_server.register_path("GET", client_path_pattern(state_key), @@ -133,7 +135,9 @@ class RoomStateEventRestServlet(RestServlet): ) if not data: - raise SynapseError(404, "Event not found.", errcode=Codes.NOT_FOUND) + raise SynapseError( + 404, "Event not found.", errcode=Codes.NOT_FOUND + ) defer.returnValue((200, data[0].get_dict()["content"])) @defer.inlineCallbacks @@ -195,7 +199,9 @@ class RoomSendEventRestServlet(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, room_id, event_type, txn_id): try: - defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + defer.returnValue( + self.txns.get_client_transaction(request, txn_id) + ) except KeyError: pass @@ -254,7 +260,9 @@ class JoinRoomAliasServlet(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, room_identifier, txn_id): try: - defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + defer.returnValue( + self.txns.get_client_transaction(request, txn_id) + ) except KeyError: pass @@ -293,7 +301,8 @@ class RoomMemberListRestServlet(RestServlet): target_user = self.hs.parse_userid(event["user_id"]) # Presence is an optional cache; don't fail if we can't fetch it try: - presence_state = yield self.handlers.presence_handler.get_state( + presence_handler = self.handlers.presence_handler + presence_state = yield presence_handler.get_state( target_user=target_user, auth_user=user ) event["content"].update(presence_state) @@ -344,7 +353,7 @@ class RoomInitialSyncRestServlet(RestServlet): @defer.inlineCallbacks def on_GET(self, request, room_id): - user = yield self.auth.get_user_by_req(request) + yield self.auth.get_user_by_req(request) # TODO: Get all the initial sync data for this room and return in the # same format as initial sync, that is: # { @@ -359,11 +368,11 @@ class RoomInitialSyncRestServlet(RestServlet): # { state event } , { state event } # ] # } - # Probably worth keeping the keys room_id and membership for parity with - # /initialSync even though they must be joined to sync this and know the - # room ID, so clients can reuse the same code (room_id and membership - # are MANDATORY for /initialSync, so the code will expect it to be - # there) + # Probably worth keeping the keys room_id and membership for parity + # with /initialSync even though they must be joined to sync this and + # know the room ID, so clients can reuse the same code (room_id and + # membership are MANDATORY for /initialSync, so the code will expect + # it to be there) defer.returnValue((200, {})) @@ -388,8 +397,8 @@ class RoomMembershipRestServlet(RestServlet): def register(self, http_server): # /rooms/$roomid/[invite|join|leave] - PATTERN = ("/rooms/(?P<room_id>[^/]*)/" + - "(?P<membership_action>join|invite|leave|ban|kick)") + PATTERN = ("/rooms/(?P<room_id>[^/]*)/" + "(?P<membership_action>join|invite|leave|ban|kick)") register_txn_path(self, PATTERN, http_server) @defer.inlineCallbacks @@ -422,7 +431,9 @@ class RoomMembershipRestServlet(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, room_id, membership_action, txn_id): try: - defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + defer.returnValue( + self.txns.get_client_transaction(request, txn_id) + ) except KeyError: pass @@ -431,6 +442,7 @@ class RoomMembershipRestServlet(RestServlet): self.txns.store_client_transaction(request, txn_id, response) defer.returnValue(response) + class RoomRedactEventRestServlet(RestServlet): def register(self, http_server): PATTERN = ("/rooms/(?P<room_id>[^/]*)/redact/(?P<event_id>[^/]*)") @@ -457,7 +469,9 @@ class RoomRedactEventRestServlet(RestServlet): @defer.inlineCallbacks def on_PUT(self, request, room_id, event_id, txn_id): try: - defer.returnValue(self.txns.get_client_transaction(request, txn_id)) + defer.returnValue( + self.txns.get_client_transaction(request, txn_id) + ) except KeyError: pass @@ -503,10 +517,10 @@ def register_txn_path(servlet, regex_string, http_server, with_get=False): ) if with_get: http_server.register_path( - "GET", - client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"), - servlet.on_GET - ) + "GET", + client_path_pattern(regex_string + "/(?P<txn_id>[^/]*)$"), + servlet.on_GET + ) def register_servlets(hs, http_server): diff --git a/synapse/rest/transactions.py b/synapse/rest/transactions.py index e06dcc8c57..93c0122f30 100644 --- a/synapse/rest/transactions.py +++ b/synapse/rest/transactions.py @@ -30,9 +30,9 @@ class HttpTransactionStore(object): """Retrieve a response for this request. Args: - key (str): A transaction-independent key for this request. Typically - this is a combination of the path (without the transaction id) and - the user's access token. + key (str): A transaction-independent key for this request. Usually + this is a combination of the path (without the transaction id) + and the user's access token. txn_id (str): The transaction ID for this request Returns: A tuple of (HTTP response code, response content) or None. @@ -51,9 +51,9 @@ class HttpTransactionStore(object): """Stores an HTTP response tuple. Args: - key (str): A transaction-independent key for this request. Typically - this is a combination of the path (without the transaction id) and - the user's access token. + key (str): A transaction-independent key for this request. Usually + this is a combination of the path (without the transaction id) + and the user's access token. txn_id (str): The transaction ID for this request. response (tuple): A tuple of (HTTP response code, response content) """ @@ -92,5 +92,3 @@ class HttpTransactionStore(object): token = request.args["access_token"][0] path_without_txn_id = request.path.rsplit("/", 1)[0] return path_without_txn_id + "/" + token - - diff --git a/synapse/rest/voip.py b/synapse/rest/voip.py index 0d0243a249..432c2475f8 100644 --- a/synapse/rest/voip.py +++ b/synapse/rest/voip.py @@ -34,23 +34,23 @@ class VoipRestServlet(RestServlet): turnSecret = self.hs.config.turn_shared_secret userLifetime = self.hs.config.turn_user_lifetime if not turnUris or not turnSecret or not userLifetime: - defer.returnValue( (200, {}) ) + defer.returnValue((200, {})) expiry = (self.hs.get_clock().time_msec() + userLifetime) / 1000 username = "%d:%s" % (expiry, auth_user.to_string()) - + mac = hmac.new(turnSecret, msg=username, digestmod=hashlib.sha1) - # We need to use standard base64 encoding here, *not* syutil's encode_base64 - # because we need to add the standard padding to get the same result as the - # TURN server. + # We need to use standard base64 encoding here, *not* syutil's + # encode_base64 because we need to add the standard padding to get the + # same result as the TURN server. password = base64.b64encode(mac.digest()) - defer.returnValue( (200, { + defer.returnValue((200, { 'username': username, 'password': password, 'ttl': userLifetime / 1000, 'uris': turnUris, - }) ) + })) def on_OPTIONS(self, request): return (200, {}) |