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

Commit 136caf4

Browse files
committed
Fix existing v2 identity server calls (MSC2140) (#6013)
2 parents a2e8eed + 3505ffc commit 136caf4

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
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
@@ -86,6 +86,25 @@ def _extract_items_from_creds_dict(self, creds):
8686
id_access_token = creds.get("id_access_token")
8787
return client_secret, id_server, id_access_token
8888

89+
def create_id_access_token_header(self, id_access_token):
90+
"""Create an Authorization header for passing to SimpleHttpClient as the header value
91+
of an HTTP request.
92+
93+
Args:
94+
id_access_token (str): An identity server access token.
95+
96+
Returns:
97+
list[str]: The ascii-encoded bearer token encased in a list.
98+
"""
99+
# Prefix with Bearer
100+
bearer_token = "Bearer %s" % id_access_token
101+
102+
# Encode headers to standard ascii
103+
bearer_token.encode("ascii")
104+
105+
# Return as a list as that's how SimpleHttpClient takes header values
106+
return [bearer_token]
107+
89108
@defer.inlineCallbacks
90109
def threepid_from_creds(self, id_server, creds):
91110
"""
@@ -180,15 +199,20 @@ def bind_threepid(self, creds, mxid, use_v2=True):
180199
id_server_host = id_server
181200

182201
# Decide which API endpoint URLs to use
202+
headers = {}
183203
bind_data = {"sid": sid, "client_secret": client_secret, "mxid": mxid}
184204
if use_v2:
185205
bind_url = "https://%s/_matrix/identity/v2/3pid/bind" % (id_server_host,)
186-
bind_data["id_access_token"] = id_access_token
206+
headers["Authorization"] = self.create_id_access_token_header(
207+
id_access_token
208+
)
187209
else:
188210
bind_url = "https://%s/_matrix/identity/api/v1/3pid/bind" % (id_server_host,)
189211

190212
try:
191-
data = yield self.http_client.post_json_get_json(bind_url, bind_data)
213+
data = yield self.http_client.post_json_get_json(
214+
bind_url, bind_data, headers=headers
215+
)
192216
logger.debug("bound threepid %r to %s", creds, mxid)
193217

194218
# Remember where we bound the threepid

tests/handlers/test_federation.py

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def test_exchange_revoked_invite(self):
5050
)
5151

5252
d = self.handler.on_exchange_third_party_invite_request(
53-
origin="example.com",
5453
room_id=room_id,
5554
event_dict={
5655
"type": EventTypes.Member,

tests/handlers/test_identity.py

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def test_rewritten_id_server(self):
9191
"client_secret": creds["client_secret"],
9292
"mxid": self.user_id,
9393
},
94+
headers={},
9495
)
9596

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

0 commit comments

Comments
 (0)