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

Commit

Permalink
Fix existing v2 identity server calls (MSC2140) (#6013)
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Feb 25, 2020
2 parents a2e8eed + 3505ffc commit 136caf4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.d/6013.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Compatibility with v2 Identity Service APIs other than /lookup.
28 changes: 26 additions & 2 deletions synapse/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ def _extract_items_from_creds_dict(self, creds):
id_access_token = creds.get("id_access_token")
return client_secret, id_server, id_access_token

def create_id_access_token_header(self, id_access_token):
"""Create an Authorization header for passing to SimpleHttpClient as the header value
of an HTTP request.
Args:
id_access_token (str): An identity server access token.
Returns:
list[str]: The ascii-encoded bearer token encased in a list.
"""
# Prefix with Bearer
bearer_token = "Bearer %s" % id_access_token

# Encode headers to standard ascii
bearer_token.encode("ascii")

# Return as a list as that's how SimpleHttpClient takes header values
return [bearer_token]

@defer.inlineCallbacks
def threepid_from_creds(self, id_server, creds):
"""
Expand Down Expand Up @@ -180,15 +199,20 @@ def bind_threepid(self, creds, mxid, use_v2=True):
id_server_host = id_server

# Decide which API endpoint URLs to use
headers = {}
bind_data = {"sid": sid, "client_secret": client_secret, "mxid": mxid}
if use_v2:
bind_url = "https://%s/_matrix/identity/v2/3pid/bind" % (id_server_host,)
bind_data["id_access_token"] = id_access_token
headers["Authorization"] = self.create_id_access_token_header(
id_access_token
)
else:
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server_host,)

try:
data = yield self.http_client.post_json_get_json(bind_url, bind_data)
data = yield self.http_client.post_json_get_json(
bind_url, bind_data, headers=headers
)
logger.debug("bound threepid %r to %s", creds, mxid)

# Remember where we bound the threepid
Expand Down
1 change: 0 additions & 1 deletion tests/handlers/test_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def test_exchange_revoked_invite(self):
)

d = self.handler.on_exchange_third_party_invite_request(
origin="example.com",
room_id=room_id,
event_dict={
"type": EventTypes.Member,
Expand Down
1 change: 1 addition & 0 deletions tests/handlers/test_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def test_rewritten_id_server(self):
"client_secret": creds["client_secret"],
"mxid": self.user_id,
},
headers={},
)

# Check that the original server name is saved in the database instead of the
Expand Down

0 comments on commit 136caf4

Please sign in to comment.