Modify API for SimpleHttpClient.get_json and update usages.
Previously, this would only return the HTTP body as JSON, and discard other response information (e.g. the HTTP response code). This has now been changed to throw a CodeMessageException on a non-2xx response, with the response code and body, which can then be parsed as JSON. Affected modules include: - Registration/Login (when using an email for IS auth)
This commit is contained in:
parent
aa8cce58bf
commit
96d4bf9012
|
@ -16,12 +16,13 @@
|
||||||
from twisted.internet import defer
|
from twisted.internet import defer
|
||||||
|
|
||||||
from ._base import BaseHandler
|
from ._base import BaseHandler
|
||||||
from synapse.api.errors import LoginError, Codes
|
from synapse.api.errors import LoginError, Codes, CodeMessageException
|
||||||
from synapse.http.client import SimpleHttpClient
|
from synapse.http.client import SimpleHttpClient
|
||||||
from synapse.util.emailutils import EmailException
|
from synapse.util.emailutils import EmailException
|
||||||
import synapse.util.emailutils as emailutils
|
import synapse.util.emailutils as emailutils
|
||||||
|
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -96,16 +97,20 @@ class LoginHandler(BaseHandler):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _query_email(self, email):
|
def _query_email(self, email):
|
||||||
httpCli = SimpleHttpClient(self.hs)
|
http_client = SimpleHttpClient(self.hs)
|
||||||
data = yield httpCli.get_json(
|
try:
|
||||||
# TODO FIXME This should be configurable.
|
data = yield http_client.get_json(
|
||||||
# XXX: ID servers need to use HTTPS
|
# TODO FIXME This should be configurable.
|
||||||
"http://%s%s" % (
|
# XXX: ID servers need to use HTTPS
|
||||||
"matrix.org:8090", "/_matrix/identity/api/v1/lookup"
|
"http://%s%s" % (
|
||||||
),
|
"matrix.org:8090", "/_matrix/identity/api/v1/lookup"
|
||||||
{
|
),
|
||||||
'medium': 'email',
|
{
|
||||||
'address': email
|
'medium': 'email',
|
||||||
}
|
'address': email
|
||||||
)
|
}
|
||||||
defer.returnValue(data)
|
)
|
||||||
|
defer.returnValue(data)
|
||||||
|
except CodeMessageException as e:
|
||||||
|
data = json.loads(e.msg)
|
||||||
|
defer.returnValue(data)
|
||||||
|
|
|
@ -18,7 +18,7 @@ from twisted.internet import defer
|
||||||
|
|
||||||
from synapse.types import UserID
|
from synapse.types import UserID
|
||||||
from synapse.api.errors import (
|
from synapse.api.errors import (
|
||||||
SynapseError, RegistrationError, InvalidCaptchaError
|
SynapseError, RegistrationError, InvalidCaptchaError, CodeMessageException
|
||||||
)
|
)
|
||||||
from ._base import BaseHandler
|
from ._base import BaseHandler
|
||||||
import synapse.util.stringutils as stringutils
|
import synapse.util.stringutils as stringutils
|
||||||
|
@ -28,6 +28,7 @@ from synapse.http.client import CaptchaServerHttpClient
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -161,21 +162,26 @@ class RegistrationHandler(BaseHandler):
|
||||||
def _threepid_from_creds(self, creds):
|
def _threepid_from_creds(self, creds):
|
||||||
# TODO: get this from the homeserver rather than creating a new one for
|
# TODO: get this from the homeserver rather than creating a new one for
|
||||||
# each request
|
# each request
|
||||||
httpCli = SimpleHttpClient(self.hs)
|
http_client = SimpleHttpClient(self.hs)
|
||||||
# XXX: make this configurable!
|
# XXX: make this configurable!
|
||||||
trustedIdServers = ['matrix.org:8090', 'matrix.org']
|
trustedIdServers = ['matrix.org:8090', 'matrix.org']
|
||||||
if not creds['idServer'] in trustedIdServers:
|
if not creds['idServer'] in trustedIdServers:
|
||||||
logger.warn('%s is not a trusted ID server: rejecting 3pid ' +
|
logger.warn('%s is not a trusted ID server: rejecting 3pid ' +
|
||||||
'credentials', creds['idServer'])
|
'credentials', creds['idServer'])
|
||||||
defer.returnValue(None)
|
defer.returnValue(None)
|
||||||
data = yield httpCli.get_json(
|
|
||||||
# XXX: This should be HTTPS
|
data = {}
|
||||||
"http://%s%s" % (
|
try:
|
||||||
creds['idServer'],
|
data = yield http_client.get_json(
|
||||||
"/_matrix/identity/api/v1/3pid/getValidated3pid"
|
# XXX: This should be HTTPS
|
||||||
),
|
"http://%s%s" % (
|
||||||
{'sid': creds['sid'], 'clientSecret': creds['clientSecret']}
|
creds['idServer'],
|
||||||
)
|
"/_matrix/identity/api/v1/3pid/getValidated3pid"
|
||||||
|
),
|
||||||
|
{'sid': creds['sid'], 'clientSecret': creds['clientSecret']}
|
||||||
|
)
|
||||||
|
except CodeMessageException as e:
|
||||||
|
data = json.loads(e.msg)
|
||||||
|
|
||||||
if 'medium' in data:
|
if 'medium' in data:
|
||||||
defer.returnValue(data)
|
defer.returnValue(data)
|
||||||
|
@ -185,19 +191,23 @@ class RegistrationHandler(BaseHandler):
|
||||||
def _bind_threepid(self, creds, mxid):
|
def _bind_threepid(self, creds, mxid):
|
||||||
yield
|
yield
|
||||||
logger.debug("binding threepid")
|
logger.debug("binding threepid")
|
||||||
httpCli = SimpleHttpClient(self.hs)
|
http_client = SimpleHttpClient(self.hs)
|
||||||
data = yield httpCli.post_urlencoded_get_json(
|
data = None
|
||||||
# XXX: Change when ID servers are all HTTPS
|
try:
|
||||||
"http://%s%s" % (
|
data = yield http_client.post_urlencoded_get_json(
|
||||||
creds['idServer'], "/_matrix/identity/api/v1/3pid/bind"
|
# XXX: Change when ID servers are all HTTPS
|
||||||
),
|
"http://%s%s" % (
|
||||||
{
|
creds['idServer'], "/_matrix/identity/api/v1/3pid/bind"
|
||||||
'sid': creds['sid'],
|
),
|
||||||
'clientSecret': creds['clientSecret'],
|
{
|
||||||
'mxid': mxid,
|
'sid': creds['sid'],
|
||||||
}
|
'clientSecret': creds['clientSecret'],
|
||||||
)
|
'mxid': mxid,
|
||||||
logger.debug("bound threepid")
|
}
|
||||||
|
)
|
||||||
|
logger.debug("bound threepid")
|
||||||
|
except CodeMessageException as e:
|
||||||
|
data = json.loads(e.msg)
|
||||||
defer.returnValue(data)
|
defer.returnValue(data)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
from synapse.api.errors import CodeMessageException
|
||||||
from synapse.http.agent_name import AGENT_NAME
|
from synapse.http.agent_name import AGENT_NAME
|
||||||
from twisted.internet import defer, reactor
|
from twisted.internet import defer, reactor
|
||||||
from twisted.web.client import (
|
from twisted.web.client import (
|
||||||
|
@ -83,7 +83,7 @@ class SimpleHttpClient(object):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def get_json(self, uri, args={}):
|
def get_json(self, uri, args={}):
|
||||||
""" Get's some json from the given host and path
|
""" Gets some json from the given host and path
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
uri (str): The URI to request, not including query parameters
|
uri (str): The URI to request, not including query parameters
|
||||||
|
@ -91,15 +91,11 @@ class SimpleHttpClient(object):
|
||||||
None.
|
None.
|
||||||
**Note**: The value of each key is assumed to be an iterable
|
**Note**: The value of each key is assumed to be an iterable
|
||||||
and *not* a string.
|
and *not* a string.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Deferred: Succeeds when we get *any* HTTP response.
|
Deferred: Succeeds when we get *any* 2xx HTTP response.
|
||||||
|
Raises:
|
||||||
The result of the deferred is a tuple of `(code, response)`,
|
On a non-2xx HTTP response.
|
||||||
where `response` is a dict representing the decoded JSON body.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
yield
|
|
||||||
if len(args):
|
if len(args):
|
||||||
query_bytes = urllib.urlencode(args, True)
|
query_bytes = urllib.urlencode(args, True)
|
||||||
uri = "%s?%s" % (uri, query_bytes)
|
uri = "%s?%s" % (uri, query_bytes)
|
||||||
|
@ -114,7 +110,10 @@ class SimpleHttpClient(object):
|
||||||
|
|
||||||
body = yield readBody(response)
|
body = yield readBody(response)
|
||||||
|
|
||||||
defer.returnValue(json.loads(body))
|
if 200 <= response.code < 300:
|
||||||
|
defer.returnValue(json.loads(body))
|
||||||
|
else:
|
||||||
|
raise CodeMessageException(response.code, body)
|
||||||
|
|
||||||
|
|
||||||
class CaptchaServerHttpClient(SimpleHttpClient):
|
class CaptchaServerHttpClient(SimpleHttpClient):
|
||||||
|
|
Loading…
Reference in New Issue