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

Commit 9feb5d0

Browse files
authored
sign_request -> build_auth_headers (#4408)
Just got very confused about the fact that the headers are only an output, not an input.
1 parent 3982a6e commit 9feb5d0

3 files changed

Lines changed: 18 additions & 15 deletions

File tree

changelog.d/4408.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor 'sign_request' as 'build_auth_headers'

synapse/handlers/identity.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,21 @@ def try_unbind_threepid(self, mxid, threepid):
167167
"mxid": mxid,
168168
"threepid": threepid,
169169
}
170-
headers = {}
170+
171171
# we abuse the federation http client to sign the request, but we have to send it
172172
# using the normal http client since we don't want the SRV lookup and want normal
173173
# 'browser-like' HTTPS.
174-
self.federation_http_client.sign_request(
174+
auth_headers = self.federation_http_client.build_auth_headers(
175175
destination=None,
176176
method='POST',
177177
url_bytes='/_matrix/identity/api/v1/3pid/unbind'.encode('ascii'),
178-
headers_dict=headers,
179178
content=content,
180179
destination_is=id_server,
181180
)
181+
headers = {
182+
b"Authorization": auth_headers,
183+
}
184+
182185
try:
183186
yield self.http_client.post_json_get_json(
184187
url,

synapse/http/matrixfederationclient.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ def _send_request(
298298
json = request.get_json()
299299
if json:
300300
headers_dict[b"Content-Type"] = [b"application/json"]
301-
self.sign_request(
301+
auth_headers = self.build_auth_headers(
302302
destination_bytes, method_bytes, url_to_sign_bytes,
303-
headers_dict, json,
303+
json,
304304
)
305305
data = encode_canonical_json(json)
306306
producer = FileBodyProducer(
@@ -309,11 +309,12 @@ def _send_request(
309309
)
310310
else:
311311
producer = None
312-
self.sign_request(
312+
auth_headers = self.build_auth_headers(
313313
destination_bytes, method_bytes, url_to_sign_bytes,
314-
headers_dict,
315314
)
316315

316+
headers_dict[b"Authorization"] = auth_headers
317+
317318
logger.info(
318319
"{%s} [%s] Sending request: %s %s",
319320
request.txn_id, request.destination, request.method,
@@ -440,24 +441,23 @@ def _send_request(
440441

441442
defer.returnValue(response)
442443

443-
def sign_request(self, destination, method, url_bytes, headers_dict,
444-
content=None, destination_is=None):
444+
def build_auth_headers(
445+
self, destination, method, url_bytes, content=None, destination_is=None,
446+
):
445447
"""
446-
Signs a request by adding an Authorization header to headers_dict
448+
Builds the Authorization headers for a federation request
447449
Args:
448450
destination (bytes|None): The desination home server of the request.
449451
May be None if the destination is an identity server, in which case
450452
destination_is must be non-None.
451453
method (bytes): The HTTP method of the request
452454
url_bytes (bytes): The URI path of the request
453-
headers_dict (dict[bytes, list[bytes]]): Dictionary of request headers to
454-
append to
455455
content (object): The body of the request
456456
destination_is (bytes): As 'destination', but if the destination is an
457457
identity server
458458
459459
Returns:
460-
None
460+
list[bytes]: a list of headers to be added as "Authorization:" headers
461461
"""
462462
request = {
463463
"method": method,
@@ -484,8 +484,7 @@ def sign_request(self, destination, method, url_bytes, headers_dict,
484484
self.server_name, key, sig,
485485
)).encode('ascii')
486486
)
487-
488-
headers_dict[b"Authorization"] = auth_headers
487+
return auth_headers
489488

490489
@defer.inlineCallbacks
491490
def put_json(self, destination, path, args={}, data={},

0 commit comments

Comments
 (0)