2015-04-01 14:05:30 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-07 04:26:29 +00:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2015-04-01 14:05:30 +00: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 06:09:20 +00:00
|
|
|
import logging
|
2021-01-04 18:13:49 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2018-07-09 06:09:20 +00:00
|
|
|
|
2015-04-01 14:05:30 +00:00
|
|
|
from synapse.api.constants import LoginType
|
|
|
|
from synapse.api.errors import SynapseError
|
2019-05-15 16:37:46 +00:00
|
|
|
from synapse.api.urls import CLIENT_API_PREFIX
|
2021-01-04 18:13:49 +00:00
|
|
|
from synapse.handlers.sso import SsoIdentityProvider
|
2020-07-01 13:10:23 +00:00
|
|
|
from synapse.http.server import respond_with_html
|
2018-11-19 18:27:33 +00:00
|
|
|
from synapse.http.servlet import RestServlet, parse_string
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2019-06-03 11:28:59 +00:00
|
|
|
from ._base import client_patterns
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2021-01-04 18:13:49 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2015-04-01 14:05:30 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-04-02 16:06:17 +00:00
|
|
|
|
2015-04-01 14:05:30 +00: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 09:32:02 +00:00
|
|
|
|
2019-06-03 11:28:59 +00:00
|
|
|
PATTERNS = client_patterns(r"/auth/(?P<stagetype>[\w\.]*)/fallback/web")
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2021-01-04 18:13:49 +00:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 13:56:44 +00:00
|
|
|
super().__init__()
|
2015-04-01 14:05:30 +00:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-06-02 12:31:45 +00:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
2019-02-20 07:47:31 +00:00
|
|
|
self.registration_handler = hs.get_registration_handler()
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2020-04-01 12:48:00 +00:00
|
|
|
# SSO configuration.
|
2020-04-03 19:35:05 +00:00
|
|
|
self._cas_enabled = hs.config.cas_enabled
|
|
|
|
if self._cas_enabled:
|
|
|
|
self._cas_handler = hs.get_cas_handler()
|
|
|
|
self._cas_server_url = hs.config.cas_server_url
|
|
|
|
self._cas_service_url = hs.config.cas_service_url
|
2020-05-15 16:26:02 +00:00
|
|
|
self._saml_enabled = hs.config.saml2_enabled
|
|
|
|
if self._saml_enabled:
|
|
|
|
self._saml_handler = hs.get_saml_handler()
|
|
|
|
self._oidc_enabled = hs.config.oidc_enabled
|
|
|
|
if self._oidc_enabled:
|
|
|
|
self._oidc_handler = hs.get_oidc_handler()
|
|
|
|
self._cas_server_url = hs.config.cas_server_url
|
|
|
|
self._cas_service_url = hs.config.cas_service_url
|
2020-04-01 12:48:00 +00:00
|
|
|
|
2020-10-02 10:15:53 +00:00
|
|
|
self.recaptcha_template = hs.config.recaptcha_template
|
|
|
|
self.terms_template = hs.config.terms_template
|
|
|
|
self.success_template = hs.config.fallback_success_template
|
|
|
|
|
2020-04-30 17:47:49 +00:00
|
|
|
async def on_GET(self, request, stagetype):
|
2018-11-19 18:27:33 +00:00
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2018-11-19 18:27:33 +00:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.recaptcha_template.render(
|
|
|
|
session=session,
|
|
|
|
myurl="%s/r0/auth/%s/fallback/web"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
2020-10-02 10:15:53 +00:00
|
|
|
sitekey=self.hs.config.recaptcha_public_key,
|
|
|
|
)
|
2018-09-27 20:53:58 +00:00
|
|
|
elif stagetype == LoginType.TERMS:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.terms_template.render(
|
|
|
|
session=session,
|
|
|
|
terms_url="%s_matrix/consent?v=%s"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (self.hs.config.public_baseurl, self.hs.config.user_consent_version),
|
2020-10-02 10:15:53 +00:00
|
|
|
myurl="%s/r0/auth/%s/fallback/web"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2020-10-02 10:15:53 +00:00
|
|
|
)
|
2020-04-01 12:48:00 +00:00
|
|
|
|
2020-04-03 19:35:05 +00:00
|
|
|
elif stagetype == LoginType.SSO:
|
2020-04-01 12:48:00 +00:00
|
|
|
# Display a confirmation page which prompts the user to
|
|
|
|
# re-authenticate with their SSO provider.
|
2020-04-03 19:35:05 +00:00
|
|
|
|
2021-01-04 18:13:49 +00:00
|
|
|
if self._cas_enabled:
|
|
|
|
sso_auth_provider = self._cas_handler # type: SsoIdentityProvider
|
2020-04-03 19:35:05 +00:00
|
|
|
elif self._saml_enabled:
|
2021-01-04 18:13:49 +00:00
|
|
|
sso_auth_provider = self._saml_handler
|
2020-05-15 16:26:02 +00:00
|
|
|
elif self._oidc_enabled:
|
2021-01-04 18:13:49 +00:00
|
|
|
sso_auth_provider = self._oidc_handler
|
2020-04-03 19:35:05 +00:00
|
|
|
else:
|
|
|
|
raise SynapseError(400, "Homeserver not configured for SSO.")
|
|
|
|
|
2021-01-04 18:13:49 +00:00
|
|
|
sso_redirect_url = await sso_auth_provider.handle_redirect_request(
|
|
|
|
request, None, session
|
|
|
|
)
|
|
|
|
|
2020-04-30 17:47:49 +00:00
|
|
|
html = await self.auth_handler.start_sso_ui_auth(sso_redirect_url, session)
|
2020-04-03 19:35:05 +00:00
|
|
|
|
2015-04-01 14:05:30 +00:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
2020-03-20 20:22:47 +00:00
|
|
|
# Render the HTML and return.
|
2020-07-01 13:10:23 +00:00
|
|
|
respond_with_html(request, 200, html)
|
2020-03-20 20:22:47 +00:00
|
|
|
return None
|
|
|
|
|
2019-12-05 16:46:37 +00:00
|
|
|
async def on_POST(self, request, stagetype):
|
2018-11-19 18:27:33 +00:00
|
|
|
|
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
|
|
|
|
2018-10-03 21:54:19 +00:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2018-11-19 18:27:33 +00:00
|
|
|
response = parse_string(request, "g-recaptcha-response")
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2018-11-19 18:27:33 +00:00
|
|
|
if not response:
|
|
|
|
raise SynapseError(400, "No captcha response supplied")
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2019-06-20 09:32:02 +00:00
|
|
|
authdict = {"response": response, "session": session}
|
2015-04-01 14:05:30 +00:00
|
|
|
|
2019-12-05 16:46:37 +00:00
|
|
|
success = await self.auth_handler.add_oob_auth(
|
2021-01-12 12:48:12 +00:00
|
|
|
LoginType.RECAPTCHA, authdict, request.getClientIP()
|
2015-04-01 14:05:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if success:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.success_template.render()
|
2015-04-01 14:05:30 +00:00
|
|
|
else:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.recaptcha_template.render(
|
|
|
|
session=session,
|
|
|
|
myurl="%s/r0/auth/%s/fallback/web"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
2020-10-02 10:15:53 +00:00
|
|
|
sitekey=self.hs.config.recaptcha_public_key,
|
|
|
|
)
|
2018-10-03 21:54:19 +00:00
|
|
|
elif stagetype == LoginType.TERMS:
|
2019-06-20 09:32:02 +00:00
|
|
|
authdict = {"session": session}
|
2018-10-03 21:54:19 +00:00
|
|
|
|
2019-12-05 16:46:37 +00:00
|
|
|
success = await self.auth_handler.add_oob_auth(
|
2021-01-12 12:48:12 +00:00
|
|
|
LoginType.TERMS, authdict, request.getClientIP()
|
2018-10-03 21:54:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if success:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.success_template.render()
|
2018-10-03 21:54:19 +00:00
|
|
|
else:
|
2020-10-02 10:15:53 +00:00
|
|
|
html = self.terms_template.render(
|
|
|
|
session=session,
|
|
|
|
terms_url="%s_matrix/consent?v=%s"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (
|
2018-10-03 21:54:19 +00:00
|
|
|
self.hs.config.public_baseurl,
|
2018-10-31 19:19:28 +00:00
|
|
|
self.hs.config.user_consent_version,
|
2018-10-03 21:54:19 +00:00
|
|
|
),
|
2020-10-02 10:15:53 +00:00
|
|
|
myurl="%s/r0/auth/%s/fallback/web"
|
2019-06-20 09:32:02 +00:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2020-10-02 10:15:53 +00:00
|
|
|
)
|
2020-04-01 12:48:00 +00:00
|
|
|
elif stagetype == LoginType.SSO:
|
|
|
|
# The SSO fallback workflow should not post here,
|
|
|
|
raise SynapseError(404, "Fallback SSO auth does not support POST requests.")
|
2015-04-01 14:05:30 +00:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
2020-03-20 20:22:47 +00:00
|
|
|
# Render the HTML and return.
|
2020-07-01 13:10:23 +00:00
|
|
|
respond_with_html(request, 200, html)
|
2020-03-20 20:22:47 +00:00
|
|
|
return None
|
|
|
|
|
2015-04-01 14:05:30 +00:00
|
|
|
|
|
|
|
def register_servlets(hs, http_server):
|
|
|
|
AuthRestServlet(hs).register(http_server)
|