sign_request -> build_auth_headers (#4408)

Just got very confused about the fact that the headers are only an output, not
an input.
This commit is contained in:
Richard van der Hoff 2019-01-17 12:40:09 +00:00 committed by GitHub
parent 3982a6ee07
commit 9feb5d0b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 15 deletions

1
changelog.d/4408.misc Normal file
View File

@ -0,0 +1 @@
Refactor 'sign_request' as 'build_auth_headers'

View File

@ -167,18 +167,21 @@ class IdentityHandler(BaseHandler):
"mxid": mxid, "mxid": mxid,
"threepid": threepid, "threepid": threepid,
} }
headers = {}
# we abuse the federation http client to sign the request, but we have to send it # we abuse the federation http client to sign the request, but we have to send it
# using the normal http client since we don't want the SRV lookup and want normal # using the normal http client since we don't want the SRV lookup and want normal
# 'browser-like' HTTPS. # 'browser-like' HTTPS.
self.federation_http_client.sign_request( auth_headers = self.federation_http_client.build_auth_headers(
destination=None, destination=None,
method='POST', method='POST',
url_bytes='/_matrix/identity/api/v1/3pid/unbind'.encode('ascii'), url_bytes='/_matrix/identity/api/v1/3pid/unbind'.encode('ascii'),
headers_dict=headers,
content=content, content=content,
destination_is=id_server, destination_is=id_server,
) )
headers = {
b"Authorization": auth_headers,
}
try: try:
yield self.http_client.post_json_get_json( yield self.http_client.post_json_get_json(
url, url,

View File

@ -298,9 +298,9 @@ class MatrixFederationHttpClient(object):
json = request.get_json() json = request.get_json()
if json: if json:
headers_dict[b"Content-Type"] = [b"application/json"] headers_dict[b"Content-Type"] = [b"application/json"]
self.sign_request( auth_headers = self.build_auth_headers(
destination_bytes, method_bytes, url_to_sign_bytes, destination_bytes, method_bytes, url_to_sign_bytes,
headers_dict, json, json,
) )
data = encode_canonical_json(json) data = encode_canonical_json(json)
producer = FileBodyProducer( producer = FileBodyProducer(
@ -309,11 +309,12 @@ class MatrixFederationHttpClient(object):
) )
else: else:
producer = None producer = None
self.sign_request( auth_headers = self.build_auth_headers(
destination_bytes, method_bytes, url_to_sign_bytes, destination_bytes, method_bytes, url_to_sign_bytes,
headers_dict,
) )
headers_dict[b"Authorization"] = auth_headers
logger.info( logger.info(
"{%s} [%s] Sending request: %s %s", "{%s} [%s] Sending request: %s %s",
request.txn_id, request.destination, request.method, request.txn_id, request.destination, request.method,
@ -440,24 +441,23 @@ class MatrixFederationHttpClient(object):
defer.returnValue(response) defer.returnValue(response)
def sign_request(self, destination, method, url_bytes, headers_dict, def build_auth_headers(
content=None, destination_is=None): self, destination, method, url_bytes, content=None, destination_is=None,
):
""" """
Signs a request by adding an Authorization header to headers_dict Builds the Authorization headers for a federation request
Args: Args:
destination (bytes|None): The desination home server of the request. destination (bytes|None): The desination home server of the request.
May be None if the destination is an identity server, in which case May be None if the destination is an identity server, in which case
destination_is must be non-None. destination_is must be non-None.
method (bytes): The HTTP method of the request method (bytes): The HTTP method of the request
url_bytes (bytes): The URI path of the request url_bytes (bytes): The URI path of the request
headers_dict (dict[bytes, list[bytes]]): Dictionary of request headers to
append to
content (object): The body of the request content (object): The body of the request
destination_is (bytes): As 'destination', but if the destination is an destination_is (bytes): As 'destination', but if the destination is an
identity server identity server
Returns: Returns:
None list[bytes]: a list of headers to be added as "Authorization:" headers
""" """
request = { request = {
"method": method, "method": method,
@ -484,8 +484,7 @@ class MatrixFederationHttpClient(object):
self.server_name, key, sig, self.server_name, key, sig,
)).encode('ascii') )).encode('ascii')
) )
return auth_headers
headers_dict[b"Authorization"] = auth_headers
@defer.inlineCallbacks @defer.inlineCallbacks
def put_json(self, destination, path, args={}, data={}, def put_json(self, destination, path, args={}, data={},