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

Commit e0eef47

Browse files
committed
Fix existing v2 identity server calls (MSC2140) (#6013)
Two things I missed while implementing [MSC2140](https://github.com/matrix-org/matrix-doc/pull/2140/files#diff-c03a26de5ac40fb532de19cb7fc2aaf7R80). 1. Access tokens should be provided to the identity server as `access_token`, not `id_access_token`, even though the homeserver may accept the tokens as `id_access_token`. 2. Access tokens must be sent to the identity server in a query parameter, the JSON body is not allowed. We now send the access token as part of an `Authorization: ...` header, which fixes both things. The breaking code was added in #5892 Sytest PR: matrix-org/sytest#697
1 parent 44d2ca2 commit e0eef47

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

changelog.d/6013.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Compatibility with v2 Identity Service APIs other than /lookup.

synapse/handlers/identity.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,25 @@ def _extract_items_from_creds_dict(self, creds):
7474
id_access_token = creds.get("id_access_token")
7575
return client_secret, id_server, id_access_token
7676

77+
def create_id_access_token_header(self, id_access_token):
78+
"""Create an Authorization header for passing to SimpleHttpClient as the header value
79+
of an HTTP request.
80+
81+
Args:
82+
id_access_token (str): An identity server access token.
83+
84+
Returns:
85+
list[str]: The ascii-encoded bearer token encased in a list.
86+
"""
87+
# Prefix with Bearer
88+
bearer_token = "Bearer %s" % id_access_token
89+
90+
# Encode headers to standard ascii
91+
bearer_token.encode("ascii")
92+
93+
# Return as a list as that's how SimpleHttpClient takes header values
94+
return [bearer_token]
95+
7796
@defer.inlineCallbacks
7897
def threepid_from_creds(self, id_server, creds):
7998
"""
@@ -149,15 +168,20 @@ def bind_threepid(self, creds, mxid, use_v2=True):
149168
use_v2 = False
150169

151170
# Decide which API endpoint URLs to use
171+
headers = {}
152172
bind_data = {"sid": creds["sid"], "client_secret": client_secret, "mxid": mxid}
153173
if use_v2:
154174
bind_url = "https://%s/_matrix/identity/v2/3pid/bind" % (id_server,)
155-
bind_data["id_access_token"] = id_access_token
175+
headers["Authorization"] = self.create_id_access_token_header(
176+
id_access_token
177+
)
156178
else:
157179
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server,)
158180

159181
try:
160-
data = yield self.http_client.post_json_get_json(bind_url, bind_data)
182+
data = yield self.http_client.post_json_get_json(
183+
bind_url, bind_data, headers=headers
184+
)
161185
logger.debug("bound threepid %r to %s", creds, mxid)
162186

163187
# Remember where we bound the threepid

0 commit comments

Comments
 (0)