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

Commit 3505ffc

Browse files
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 cd17a20 commit 3505ffc

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

changelog.d/6013.misc

+1
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

+26-2
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
"""
@@ -155,15 +174,20 @@ def bind_threepid(self, creds, mxid, use_v2=True):
155174
use_v2 = False
156175

157176
# Decide which API endpoint URLs to use
177+
headers = {}
158178
bind_data = {"sid": sid, "client_secret": client_secret, "mxid": mxid}
159179
if use_v2:
160180
bind_url = "https://%s/_matrix/identity/v2/3pid/bind" % (id_server,)
161-
bind_data["id_access_token"] = id_access_token
181+
headers["Authorization"] = self.create_id_access_token_header(
182+
id_access_token
183+
)
162184
else:
163185
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server,)
164186

165187
try:
166-
data = yield self.http_client.post_json_get_json(bind_url, bind_data)
188+
data = yield self.http_client.post_json_get_json(
189+
bind_url, bind_data, headers=headers
190+
)
167191
logger.debug("bound threepid %r to %s", creds, mxid)
168192

169193
# Remember where we bound the threepid

0 commit comments

Comments
 (0)