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

Commit 27fa0fa

Browse files
authored
Send the appservice access token as a header. (#13996)
Implements MSC2832 by sending application service access tokens in the Authorization header. The access token is also still sent as a query parameter until the application service ecosystem has fully migrated to using headers. In the future this could be made opt-in, or removed completely.
1 parent 1613857 commit 27fa0fa

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

changelog.d/13996.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Send application service access tokens as a header (and query parameter). Implement [MSC2832](https://github.com/matrix-org/matrix-spec-proposals/pull/2832).

synapse/appservice/api.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,11 @@ async def query_user(self, service: "ApplicationService", user_id: str) -> bool:
120120

121121
uri = service.url + ("/users/%s" % urllib.parse.quote(user_id))
122122
try:
123-
response = await self.get_json(uri, {"access_token": service.hs_token})
123+
response = await self.get_json(
124+
uri,
125+
{"access_token": service.hs_token},
126+
headers={"Authorization": f"Bearer {service.hs_token}"},
127+
)
124128
if response is not None: # just an empty json object
125129
return True
126130
except CodeMessageException as e:
@@ -140,7 +144,11 @@ async def query_alias(self, service: "ApplicationService", alias: str) -> bool:
140144

141145
uri = service.url + ("/rooms/%s" % urllib.parse.quote(alias))
142146
try:
143-
response = await self.get_json(uri, {"access_token": service.hs_token})
147+
response = await self.get_json(
148+
uri,
149+
{"access_token": service.hs_token},
150+
headers={"Authorization": f"Bearer {service.hs_token}"},
151+
)
144152
if response is not None: # just an empty json object
145153
return True
146154
except CodeMessageException as e:
@@ -181,7 +189,9 @@ async def query_3pe(
181189
**fields,
182190
b"access_token": service.hs_token,
183191
}
184-
response = await self.get_json(uri, args=args)
192+
response = await self.get_json(
193+
uri, args=args, headers={"Authorization": f"Bearer {service.hs_token}"}
194+
)
185195
if not isinstance(response, list):
186196
logger.warning(
187197
"query_3pe to %s returned an invalid response %r", uri, response
@@ -217,7 +227,11 @@ async def _get() -> Optional[JsonDict]:
217227
urllib.parse.quote(protocol),
218228
)
219229
try:
220-
info = await self.get_json(uri, {"access_token": service.hs_token})
230+
info = await self.get_json(
231+
uri,
232+
{"access_token": service.hs_token},
233+
headers={"Authorization": f"Bearer {service.hs_token}"},
234+
)
221235

222236
if not _is_valid_3pe_metadata(info):
223237
logger.warning(
@@ -313,6 +327,7 @@ async def push_bulk(
313327
uri=uri,
314328
json_body=body,
315329
args={"access_token": service.hs_token},
330+
headers={"Authorization": f"Bearer {service.hs_token}"},
316331
)
317332
if logger.isEnabledFor(logging.DEBUG):
318333
logger.debug(

tests/appservice/test_api.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ def test_query_3pe_authenticates_token(self):
6969

7070
self.request_url = None
7171

72-
async def get_json(url: str, args: Mapping[Any, Any]) -> List[JsonDict]:
73-
if not args.get(b"access_token"):
72+
async def get_json(
73+
url: str, args: Mapping[Any, Any], headers: Mapping[Any, Any]
74+
) -> List[JsonDict]:
75+
# Ensure the access token is passed as both a header and query arg.
76+
if not headers.get("Authorization") or not args.get(b"access_token"):
7477
raise RuntimeError("Access token not provided")
7578

79+
self.assertEqual(headers.get("Authorization"), f"Bearer {TOKEN}")
7680
self.assertEqual(args.get(b"access_token"), TOKEN)
7781
self.request_url = url
7882
if url == URL_USER:

0 commit comments

Comments
 (0)