2015-04-01 08:05:30 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-04-01 08:05:30 -06:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
|
|
|
|
2015-04-01 08:05:30 -06:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.api.constants import LoginType
|
|
|
|
from synapse.api.errors import SynapseError
|
2019-05-15 10:37:46 -06:00
|
|
|
from synapse.api.urls import CLIENT_API_PREFIX
|
2016-02-12 06:46:59 -07:00
|
|
|
from synapse.http.server import finish_request
|
2018-11-19 11:27:33 -07:00
|
|
|
from synapse.http.servlet import RestServlet, parse_string
|
2015-04-01 08:05:30 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
from ._base import client_patterns
|
2015-04-01 08:05:30 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
RECAPTCHA_TEMPLATE = """
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Authentication</title>
|
|
|
|
<meta name='viewport' content='width=device-width, initial-scale=1,
|
|
|
|
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
|
2019-02-25 12:15:36 -07:00
|
|
|
<script src="https://www.recaptcha.net/recaptcha/api.js"
|
2015-04-01 08:05:30 -06:00
|
|
|
async defer></script>
|
|
|
|
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
|
|
|
|
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
|
|
|
|
<script>
|
|
|
|
function captchaDone() {
|
|
|
|
$('#registrationForm').submit();
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form id="registrationForm" method="post" action="%(myurl)s">
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
Hello! We need to prevent computer programs and other automated
|
|
|
|
things from creating accounts on this server.
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
Please verify that you're not a robot.
|
|
|
|
</p>
|
|
|
|
<input type="hidden" name="session" value="%(session)s" />
|
|
|
|
<div class="g-recaptcha"
|
|
|
|
data-sitekey="%(sitekey)s"
|
|
|
|
data-callback="captchaDone">
|
|
|
|
</div>
|
|
|
|
<noscript>
|
|
|
|
<input type="submit" value="All Done" />
|
|
|
|
</noscript>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2018-10-03 15:54:19 -06:00
|
|
|
TERMS_TEMPLATE = """
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Authentication</title>
|
|
|
|
<meta name='viewport' content='width=device-width, initial-scale=1,
|
|
|
|
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
|
|
|
|
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<form id="registrationForm" method="post" action="%(myurl)s">
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
Please click the button below if you agree to the
|
|
|
|
<a href="%(terms_url)s">privacy policy of this homeserver.</a>
|
|
|
|
</p>
|
|
|
|
<input type="hidden" name="session" value="%(session)s" />
|
|
|
|
<input type="submit" value="Agree" />
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2015-04-01 08:05:30 -06:00
|
|
|
SUCCESS_TEMPLATE = """
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Success!</title>
|
|
|
|
<meta name='viewport' content='width=device-width, initial-scale=1,
|
|
|
|
user-scalable=no, minimum-scale=1.0, maximum-scale=1.0'>
|
|
|
|
<link rel="stylesheet" href="/_matrix/static/client/register/style.css">
|
|
|
|
<script>
|
2016-10-06 11:16:59 -06:00
|
|
|
if (window.onAuthDone) {
|
2015-04-01 08:05:30 -06:00
|
|
|
window.onAuthDone();
|
2016-10-06 11:16:59 -06:00
|
|
|
} else if (window.opener && window.opener.postMessage) {
|
|
|
|
window.opener.postMessage("authDone", "*");
|
2015-04-01 08:05:30 -06:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div>
|
|
|
|
<p>Thank you</p>
|
|
|
|
<p>You may now close this window and return to the application</p>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2015-04-02 10:06:17 -06:00
|
|
|
|
2015-04-01 08:05:30 -06:00
|
|
|
class AuthRestServlet(RestServlet):
|
|
|
|
"""
|
|
|
|
Handles Client / Server API authentication in any situations where it
|
|
|
|
cannot be handled in the normal flow (with requests to the same endpoint).
|
|
|
|
Current use is for web fallback auth.
|
|
|
|
"""
|
2019-06-20 03:32:02 -06:00
|
|
|
|
2019-06-03 05:28:59 -06:00
|
|
|
PATTERNS = client_patterns(r"/auth/(?P<stagetype>[\w\.]*)/fallback/web")
|
2015-04-01 08:05:30 -06:00
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(AuthRestServlet, self).__init__()
|
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-06-02 06:31:45 -06:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
2019-02-20 00:47:31 -07:00
|
|
|
self.registration_handler = hs.get_registration_handler()
|
2015-04-01 08:05:30 -06:00
|
|
|
|
|
|
|
def on_GET(self, request, stagetype):
|
2018-11-19 11:27:33 -07:00
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
2015-04-01 08:05:30 -06:00
|
|
|
|
2018-11-19 11:27:33 -07:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2015-04-01 08:05:30 -06:00
|
|
|
html = RECAPTCHA_TEMPLATE % {
|
2019-06-20 03:32:02 -06:00
|
|
|
"session": session,
|
|
|
|
"myurl": "%s/r0/auth/%s/fallback/web"
|
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
|
|
|
"sitekey": self.hs.config.recaptcha_public_key,
|
2015-04-01 08:05:30 -06:00
|
|
|
}
|
|
|
|
html_bytes = html.encode("utf8")
|
|
|
|
request.setResponseCode(200)
|
|
|
|
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
|
|
|
|
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
|
|
|
|
|
|
|
|
request.write(html_bytes)
|
2018-09-27 14:53:58 -06:00
|
|
|
finish_request(request)
|
2018-11-19 11:27:33 -07:00
|
|
|
return None
|
2018-09-27 14:53:58 -06:00
|
|
|
elif stagetype == LoginType.TERMS:
|
2018-10-03 15:54:19 -06:00
|
|
|
html = TERMS_TEMPLATE % {
|
2019-06-20 03:32:02 -06:00
|
|
|
"session": session,
|
|
|
|
"terms_url": "%s_matrix/consent?v=%s"
|
|
|
|
% (self.hs.config.public_baseurl, self.hs.config.user_consent_version),
|
|
|
|
"myurl": "%s/r0/auth/%s/fallback/web"
|
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2018-10-03 15:54:19 -06:00
|
|
|
}
|
2018-09-27 14:53:58 -06:00
|
|
|
html_bytes = html.encode("utf8")
|
|
|
|
request.setResponseCode(200)
|
|
|
|
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
|
|
|
|
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
|
|
|
|
|
|
|
|
request.write(html_bytes)
|
2016-02-12 06:46:59 -07:00
|
|
|
finish_request(request)
|
2018-11-19 11:27:33 -07:00
|
|
|
return None
|
2015-04-01 08:05:30 -06:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_POST(self, request, stagetype):
|
2018-11-19 11:27:33 -07:00
|
|
|
|
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
|
|
|
|
2018-10-03 15:54:19 -06:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2018-11-19 11:27:33 -07:00
|
|
|
response = parse_string(request, "g-recaptcha-response")
|
2015-04-01 08:05:30 -06:00
|
|
|
|
2018-11-19 11:27:33 -07:00
|
|
|
if not response:
|
|
|
|
raise SynapseError(400, "No captcha response supplied")
|
2015-04-01 08:05:30 -06:00
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
authdict = {"response": response, "session": session}
|
2015-04-01 08:05:30 -06:00
|
|
|
|
|
|
|
success = yield self.auth_handler.add_oob_auth(
|
2019-06-20 03:32:02 -06:00
|
|
|
LoginType.RECAPTCHA, authdict, self.hs.get_ip_from_request(request)
|
2015-04-01 08:05:30 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if success:
|
|
|
|
html = SUCCESS_TEMPLATE
|
|
|
|
else:
|
|
|
|
html = RECAPTCHA_TEMPLATE % {
|
2019-06-20 03:32:02 -06:00
|
|
|
"session": session,
|
|
|
|
"myurl": "%s/r0/auth/%s/fallback/web"
|
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
|
|
|
"sitekey": self.hs.config.recaptcha_public_key,
|
2015-04-01 08:05:30 -06:00
|
|
|
}
|
|
|
|
html_bytes = html.encode("utf8")
|
|
|
|
request.setResponseCode(200)
|
|
|
|
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
|
|
|
|
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
|
|
|
|
|
|
|
|
request.write(html_bytes)
|
2016-02-12 06:46:59 -07:00
|
|
|
finish_request(request)
|
2015-04-01 08:05:30 -06:00
|
|
|
|
2019-07-23 07:00:55 -06:00
|
|
|
return None
|
2018-10-03 15:54:19 -06:00
|
|
|
elif stagetype == LoginType.TERMS:
|
2019-06-20 03:32:02 -06:00
|
|
|
if ("session" not in request.args or len(request.args["session"])) == 0:
|
2018-10-03 15:54:19 -06:00
|
|
|
raise SynapseError(400, "No session supplied")
|
|
|
|
|
2019-06-20 03:32:02 -06:00
|
|
|
session = request.args["session"][0]
|
|
|
|
authdict = {"session": session}
|
2018-10-03 15:54:19 -06:00
|
|
|
|
|
|
|
success = yield self.auth_handler.add_oob_auth(
|
2019-06-20 03:32:02 -06:00
|
|
|
LoginType.TERMS, authdict, self.hs.get_ip_from_request(request)
|
2018-10-03 15:54:19 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
if success:
|
|
|
|
html = SUCCESS_TEMPLATE
|
|
|
|
else:
|
|
|
|
html = TERMS_TEMPLATE % {
|
2019-06-20 03:32:02 -06:00
|
|
|
"session": session,
|
|
|
|
"terms_url": "%s_matrix/consent?v=%s"
|
|
|
|
% (
|
2018-10-03 15:54:19 -06:00
|
|
|
self.hs.config.public_baseurl,
|
2018-10-31 13:19:28 -06:00
|
|
|
self.hs.config.user_consent_version,
|
2018-10-03 15:54:19 -06:00
|
|
|
),
|
2019-06-20 03:32:02 -06:00
|
|
|
"myurl": "%s/r0/auth/%s/fallback/web"
|
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2018-10-03 15:54:19 -06:00
|
|
|
}
|
|
|
|
html_bytes = html.encode("utf8")
|
|
|
|
request.setResponseCode(200)
|
|
|
|
request.setHeader(b"Content-Type", b"text/html; charset=utf-8")
|
|
|
|
request.setHeader(b"Content-Length", b"%d" % (len(html_bytes),))
|
|
|
|
|
|
|
|
request.write(html_bytes)
|
|
|
|
finish_request(request)
|
2019-07-23 07:00:55 -06:00
|
|
|
return None
|
2015-04-01 08:05:30 -06:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
|
|
|
def on_OPTIONS(self, _):
|
|
|
|
return 200, {}
|
|
|
|
|
|
|
|
|
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
AuthRestServlet(hs).register(http_server)
|