From 4fbc0bcf91e5e6fdd3add6a0c2f0c7247e01c8dd Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 5 Sep 2019 11:33:11 +0100 Subject: [PATCH 1/3] Add POST /_matrix/client/r0/account/3pid/unbind --- synapse/handlers/identity.py | 3 ++- synapse/rest/client/v2_alpha/account.py | 32 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/synapse/handlers/identity.py b/synapse/handlers/identity.py index d199521b5878..5540f9f4d5e7 100644 --- a/synapse/handlers/identity.py +++ b/synapse/handlers/identity.py @@ -137,7 +137,8 @@ def bind_threepid(self, creds, mxid): @defer.inlineCallbacks def try_unbind_threepid(self, mxid, threepid): - """Removes a binding from an identity server + """Attempt to remove a 3PID from an identity server, or if one is not provided, all + identity servers we're aware the binding is present on Args: mxid (str): Matrix user ID of binding to be removed diff --git a/synapse/rest/client/v2_alpha/account.py b/synapse/rest/client/v2_alpha/account.py index 0620a4d0cf83..6a25470ba9ab 100644 --- a/synapse/rest/client/v2_alpha/account.py +++ b/synapse/rest/client/v2_alpha/account.py @@ -571,6 +571,37 @@ def on_POST(self, request): return 200, {} +class ThreepidUnbindRestServlet(RestServlet): + PATTERNS = client_patterns("/account/3pid/unbind$") + + def __init__(self, hs): + super(ThreepidUnbindRestServlet, self).__init__() + self.hs = hs + self.identity_handler = hs.get_handlers().identity_handler + self.auth = hs.get_auth() + self.datastore = self.hs.get_datastore() + + @defer.inlineCallbacks + def on_POST(self, request): + """Unbind the given 3pid from a specific identity server, or identity servers that are + known to have this 3pid bound + """ + user_id = yield self.auth.get_user_by_req(request) + body = parse_json_object_from_request(request) + assert_params_in_dict(body, ["medium", "address"]) + + medium = body.get("medium") + address = body.get("address") + id_server = body.get("id_server") + + # Attempt to unbind the threepid from an identity server. If id_server is None, try to + # unbind from all identity servers this threepid has been added to in the past + result = yield self.identity_handler.try_unbind_threepid( + user_id, {"address": address, "medium": medium, "id_server": id_server} + ) + return {"id_server_unbind_result": "success" if result else "no-support"} + + class ThreepidDeleteRestServlet(RestServlet): PATTERNS = client_patterns("/account/3pid/delete$") @@ -629,5 +660,6 @@ def register_servlets(hs, http_server): EmailThreepidRequestTokenRestServlet(hs).register(http_server) MsisdnThreepidRequestTokenRestServlet(hs).register(http_server) ThreepidRestServlet(hs).register(http_server) + ThreepidUnbindRestServlet(hs).register(http_server) ThreepidDeleteRestServlet(hs).register(http_server) WhoamiRestServlet(hs).register(http_server) From 6d1d636796bfc1017abf0b835ac043c8adbc7622 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 5 Sep 2019 11:39:34 +0100 Subject: [PATCH 2/3] Add changelog --- changelog.d/5980.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/5980.feature diff --git a/changelog.d/5980.feature b/changelog.d/5980.feature new file mode 100644 index 000000000000..f25d8d81d9ef --- /dev/null +++ b/changelog.d/5980.feature @@ -0,0 +1 @@ +Add POST /_matrix/client/r0/account/3pid/unbind endpoint from MSC2140 for unbinding a 3PID from an identity server without removing it from the homeserver user account. \ No newline at end of file From 6c19bf58def3a4fbfe71edbdb6f947592d6ba76c Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Thu, 5 Sep 2019 12:10:55 +0100 Subject: [PATCH 3/3] Extract MXID from Requester --- synapse/rest/client/v2_alpha/account.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/synapse/rest/client/v2_alpha/account.py b/synapse/rest/client/v2_alpha/account.py index 6a25470ba9ab..a4be5180063e 100644 --- a/synapse/rest/client/v2_alpha/account.py +++ b/synapse/rest/client/v2_alpha/account.py @@ -586,7 +586,7 @@ def on_POST(self, request): """Unbind the given 3pid from a specific identity server, or identity servers that are known to have this 3pid bound """ - user_id = yield self.auth.get_user_by_req(request) + requester = yield self.auth.get_user_by_req(request) body = parse_json_object_from_request(request) assert_params_in_dict(body, ["medium", "address"]) @@ -597,9 +597,10 @@ def on_POST(self, request): # Attempt to unbind the threepid from an identity server. If id_server is None, try to # unbind from all identity servers this threepid has been added to in the past result = yield self.identity_handler.try_unbind_threepid( - user_id, {"address": address, "medium": medium, "id_server": id_server} + requester.user.to_string(), + {"address": address, "medium": medium, "id_server": id_server}, ) - return {"id_server_unbind_result": "success" if result else "no-support"} + return 200, {"id_server_unbind_result": "success" if result else "no-support"} class ThreepidDeleteRestServlet(RestServlet):