diff --git a/synapse/http/client.py b/synapse/http/client.py
index 5c2fbd1f87..316ca1ccb9 100644
--- a/synapse/http/client.py
+++ b/synapse/http/client.py
@@ -26,11 +26,14 @@ from syutil.jsonutil import encode_canonical_json
from synapse.api.errors import CodeMessageException, SynapseError
+from syutil.crypto.jsonsign import sign_json
+
from StringIO import StringIO
import json
import logging
import urllib
+import urlparse
logger = logging.getLogger(__name__)
@@ -68,16 +71,20 @@ class BaseHttpClient(object):
self.hs = hs
@defer.inlineCallbacks
- def _create_request(self, destination, method, path_bytes, param_bytes=b"",
- query_bytes=b"", producer=None, headers_dict={},
- retry_on_dns_fail=True, on_send_callback=None):
+ def _create_request(self, destination, method, path_bytes,
+ body_callback, headers_dict={}, param_bytes=b"",
+ query_bytes=b"", retry_on_dns_fail=True):
""" Creates and sends a request to the given url
"""
headers_dict[b"User-Agent"] = [b"Synapse"]
headers_dict[b"Host"] = [destination]
- logger.debug("Sending request to %s: %s %s;%s?%s",
- destination, method, path_bytes, param_bytes, query_bytes)
+ url_bytes = urlparse.urlunparse(
+ ("", "", path_bytes, param_bytes, query_bytes, "",)
+ )
+
+ logger.debug("Sending request to %s: %s %s",
+ destination, method, url_bytes)
logger.debug(
"Types: %s",
@@ -93,8 +100,8 @@ class BaseHttpClient(object):
endpoint = self._getEndpoint(reactor, destination);
while True:
- if on_send_callback:
- on_send_callback(destination, method, path_bytes, producer)
+
+ producer = body_callback(method, url_bytes, headers_dict)
try:
response = yield self.agent.request(
@@ -142,7 +149,7 @@ class BaseHttpClient(object):
class MatrixHttpClient(BaseHttpClient):
- """ Wrapper around the twisted HTTP client api. Implements
+ """ Wrapper around the twisted HTTP client api. Implements
Attributes:
agent (twisted.web.client.Agent): The twisted Agent used to send the
@@ -151,8 +158,38 @@ class MatrixHttpClient(BaseHttpClient):
RETRY_DNS_LOOKUP_FAILURES = "__retry_dns"
+ def __init__(self, hs):
+ self.signing_key = hs.config.signing_key[0]
+ self.server_name = hs.hostname
+ BaseHttpClient.__init__(self, hs)
+
+ def sign_request(self, destination, method, url_bytes, headers_dict,
+ content=None):
+ request = {
+ "method": method,
+ "uri": url_bytes,
+ "origin": self.server_name,
+ "destination": destination,
+ }
+
+ if content is not None:
+ request["content"] = content
+
+ request = sign_json(request, self.server_name, self.signing_key)
+
+ auth_headers = []
+
+ for key,sig in request["signatures"][self.server_name].items():
+ auth_headers.append(bytes(
+ "X-Matrix origin=%s,key=\"%s\",sig=\"%s\"" % (
+ self.server_name, key, sig,
+ )
+ ))
+
+ headers_dict[b"Authorization"] = auth_headers
+
@defer.inlineCallbacks
- def put_json(self, destination, path, data, on_send_callback=None):
+ def put_json(self, destination, path, data={}, json_data_callback=None):
""" Sends the specifed json data using PUT
Args:
@@ -161,19 +198,33 @@ class MatrixHttpClient(BaseHttpClient):
path (str): The HTTP path.
data (dict): A dict containing the data that will be used as
the request body. This will be encoded as JSON.
+ json_data_callback (callable): A callable returning the dict to
+ use as the request body.
Returns:
Deferred: Succeeds when we get a 2xx HTTP response. The result
will be the decoded JSON body. On a 4xx or 5xx error response a
CodeMessageException is raised.
"""
+
+ if not json_data_callback:
+ def json_data_callback():
+ return data
+
+ def body_callback(method, url_bytes, headers_dict):
+ json_data = json_data_callback()
+ self.sign_request(
+ destination, method, url_bytes, headers_dict, json_data
+ )
+ producer = _JsonProducer(json_data)
+ return producer
+
response = yield self._create_request(
destination.encode("ascii"),
"PUT",
path.encode("ascii"),
- producer=_JsonProducer(data),
+ body_callback=body_callback,
headers_dict={"Content-Type": ["application/json"]},
- on_send_callback=on_send_callback,
)
logger.debug("Getting resp body")
@@ -206,11 +257,16 @@ class MatrixHttpClient(BaseHttpClient):
query_bytes = urllib.urlencode(args, True)
logger.debug("Query bytes: %s Retry DNS: %s", args, retry_on_dns_fail)
+ def body_callback(method, url_bytes, headers_dict):
+ self.sign_request(destination, method, url_bytes, headers_dict)
+ return None
+
response = yield self._create_request(
destination.encode("ascii"),
"GET",
path.encode("ascii"),
query_bytes=query_bytes,
+ body_callback=body_callback,
retry_on_dns_fail=retry_on_dns_fail
)
@@ -239,11 +295,14 @@ class IdentityServerHttpClient(BaseHttpClient):
logger.debug("post_urlencoded_get_json args: %s", args)
query_bytes = urllib.urlencode(args, True)
+ def body_callback(method, url_bytes, headers_dict):
+ return FileBodyProducer(StringIO(query_bytes))
+
response = yield self._create_request(
destination.encode("ascii"),
"POST",
path.encode("ascii"),
- producer=FileBodyProducer(StringIO(query_bytes)),
+ body_callback=body_callback,
headers_dict={
"Content-Type": ["application/x-www-form-urlencoded"]
}
@@ -265,11 +324,14 @@ class CaptchaServerHttpClient(MatrixHttpClient):
args={}):
query_bytes = urllib.urlencode(args, True)
+ def body_callback(method, url_bytes, headers_dict):
+ return FileBodyProducer(StringIO(query_bytes))
+
response = yield self._create_request(
destination.encode("ascii"),
"POST",
path.encode("ascii"),
- producer=FileBodyProducer(StringIO(query_bytes)),
+ body_callback=body_callback,
headers_dict={
"Content-Type": ["application/x-www-form-urlencoded"]
}
|