Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Refactor arguments of try_unbind_threepid(_with_id_server) from dict to separate args #15053

Merged
merged 5 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15053.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor arguments of `try_unbind_threepid` and `_try_unbind_threepid_with_id_server` to not use dictionaries.
5 changes: 2 additions & 3 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -1593,9 +1593,8 @@ async def delete_threepid(
if medium == "email":
address = canonicalise_email(address)

identity_handler = self.hs.get_identity_handler()
result = await identity_handler.try_unbind_threepid(
user_id, {"medium": medium, "address": address, "id_server": id_server}
result = await self.hs.get_identity_handler().try_unbind_threepid(
user_id, medium, address, id_server
)

await self.store.user_delete_threepid(user_id, medium, address)
Expand Down
7 changes: 1 addition & 6 deletions synapse/handlers/deactivate_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,7 @@ async def deactivate_account(
for threepid in threepids:
try:
result = await self._identity_handler.try_unbind_threepid(
user_id,
{
"medium": threepid["medium"],
"address": threepid["address"],
"id_server": id_server,
},
user_id, threepid["medium"], threepid["address"], id_server
)
identity_server_supports_unbinding &= result
except Exception:
Expand Down
47 changes: 23 additions & 24 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,28 +219,31 @@ async def bind_threepid(
data = json_decoder.decode(e.msg) # XXX WAT?
return data

async def try_unbind_threepid(self, mxid: str, threepid: dict) -> bool:
"""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
async def try_unbind_threepid(
self, mxid: str, medium: str, address: str, id_server: Optional[str]
) -> bool:
"""Attempt to remove a 3PID from one or more identity servers.

Args:
mxid: Matrix user ID of binding to be removed
threepid: Dict with medium & address of binding to be
removed, and an optional id_server.
medium: The medium of the third-party ID.
address: The address of the third-party ID.
id_server: An identity server to attempt to unbind from. If None,
attempt to remove the association from all identity servers
known to potentially have it.

Raises:
SynapseError: If we failed to contact the identity server
SynapseError: If we failed to contact one or more identity servers.

Returns:
True on success, otherwise False if the identity
server doesn't support unbinding (or no identity server found to
contact).
True on success, otherwise False if the identity server doesn't
support unbinding (or no identity server to contact was found).
"""
if threepid.get("id_server"):
id_servers = [threepid["id_server"]]
if id_server:
id_servers = [id_server]
else:
id_servers = await self.store.get_id_servers_user_bound(
user_id=mxid, medium=threepid["medium"], address=threepid["address"]
mxid, medium, address
)

# We don't know where to unbind, so we don't have a choice but to return
Expand All @@ -249,20 +252,21 @@ async def try_unbind_threepid(self, mxid: str, threepid: dict) -> bool:

changed = True
for id_server in id_servers:
changed &= await self.try_unbind_threepid_with_id_server(
mxid, threepid, id_server
changed &= await self._try_unbind_threepid_with_id_server(
mxid, medium, address, id_server
)

return changed

async def try_unbind_threepid_with_id_server(
self, mxid: str, threepid: dict, id_server: str
async def _try_unbind_threepid_with_id_server(
self, mxid: str, medium: str, address: str, id_server: str
) -> bool:
"""Removes a binding from an identity server

Args:
mxid: Matrix user ID of binding to be removed
threepid: Dict with medium & address of binding to be removed
medium: The medium of the third-party ID
address: The address of the third-party ID
id_server: Identity server to unbind from

Raises:
Expand All @@ -286,7 +290,7 @@ async def try_unbind_threepid_with_id_server(

content = {
"mxid": mxid,
"threepid": {"medium": threepid["medium"], "address": threepid["address"]},
"threepid": {"medium": medium, "address": address},
}

# we abuse the federation http client to sign the request, but we have to send it
Expand Down Expand Up @@ -319,12 +323,7 @@ async def try_unbind_threepid_with_id_server(
except RequestTimedOutError:
raise SynapseError(500, "Timed out contacting identity server")

await self.store.remove_user_bound_threepid(
user_id=mxid,
medium=threepid["medium"],
address=threepid["address"],
id_server=id_server,
)
await self.store.remove_user_bound_threepid(mxid, medium, address, id_server)

return changed

Expand Down
7 changes: 1 addition & 6 deletions synapse/rest/client/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,7 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
# 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 = await self.identity_handler.try_unbind_threepid(
requester.user.to_string(),
{
"address": body.address,
"medium": body.medium,
"id_server": body.id_server,
},
requester.user.to_string(), body.medium, body.address, body.id_server
)
return 200, {"id_server_unbind_result": "success" if result else "no-support"}

Expand Down