2015-04-01 10:05:30 -04:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
2024-01-23 06:26:48 -05:00
|
|
|
# Copyright 2015, 2016 OpenMarket Ltd
|
2023-11-21 15:29:58 -05:00
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2015-04-01 10:05:30 -04:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2018-07-09 02:09:20 -04:00
|
|
|
import logging
|
2024-08-30 10:04:08 -04:00
|
|
|
from typing import TYPE_CHECKING, cast
|
2018-07-09 02:09:20 -04:00
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
from twisted.web.server import Request
|
|
|
|
|
2015-04-01 10:05:30 -04:00
|
|
|
from synapse.api.constants import LoginType
|
2021-08-18 08:13:35 -04:00
|
|
|
from synapse.api.errors import LoginError, SynapseError
|
2019-05-15 12:37:46 -04:00
|
|
|
from synapse.api.urls import CLIENT_API_PREFIX
|
2024-08-30 08:52:57 -04:00
|
|
|
from synapse.http.server import HttpServer, respond_with_html, respond_with_redirect
|
2018-11-19 13:27:33 -05:00
|
|
|
from synapse.http.servlet import RestServlet, parse_string
|
2021-08-26 07:53:52 -04:00
|
|
|
from synapse.http.site import SynapseRequest
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
from ._base import client_patterns
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2021-01-04 13:13:49 -05:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from synapse.server import HomeServer
|
|
|
|
|
2015-04-01 10:05:30 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-04-02 12:06:17 -04:00
|
|
|
|
2015-04-01 10:05:30 -04: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 05:32:02 -04:00
|
|
|
|
2019-06-03 07:28:59 -04:00
|
|
|
PATTERNS = client_patterns(r"/auth/(?P<stagetype>[\w\.]*)/fallback/web")
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2021-01-04 13:13:49 -05:00
|
|
|
def __init__(self, hs: "HomeServer"):
|
2020-09-18 09:56:44 -04:00
|
|
|
super().__init__()
|
2015-04-01 10:05:30 -04:00
|
|
|
self.hs = hs
|
|
|
|
self.auth = hs.get_auth()
|
2016-06-02 08:31:45 -04:00
|
|
|
self.auth_handler = hs.get_auth_handler()
|
2019-02-20 02:47:31 -05:00
|
|
|
self.registration_handler = hs.get_registration_handler()
|
2021-09-23 07:13:34 -04:00
|
|
|
self.recaptcha_template = hs.config.captcha.recaptcha_template
|
2021-10-06 10:47:41 -04:00
|
|
|
self.terms_template = hs.config.consent.terms_template
|
2021-10-04 07:18:54 -04:00
|
|
|
self.registration_token_template = (
|
|
|
|
hs.config.registration.registration_token_template
|
|
|
|
)
|
|
|
|
self.success_template = hs.config.registration.fallback_success_template
|
2020-10-02 06:15:53 -04:00
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
async def on_GET(self, request: SynapseRequest, stagetype: str) -> None:
|
2018-11-19 13:27:33 -05:00
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2024-08-30 08:52:57 -04:00
|
|
|
if (
|
|
|
|
self.hs.config.experimental.msc3861.enabled
|
|
|
|
and stagetype == "org.matrix.cross_signing_reset"
|
|
|
|
):
|
2024-08-30 10:04:08 -04:00
|
|
|
# If MSC3861 is enabled, we can assume self._auth is an instance of MSC3861DelegatedAuth
|
|
|
|
# We import lazily here because of the authlib requirement
|
|
|
|
from synapse.api.auth.msc3861_delegated import MSC3861DelegatedAuth
|
|
|
|
|
|
|
|
auth = cast(MSC3861DelegatedAuth, self.auth)
|
|
|
|
|
|
|
|
url = await auth.account_management_url()
|
|
|
|
if url is not None:
|
|
|
|
url = f"{url}?action=org.matrix.cross_signing_reset"
|
2024-08-30 08:52:57 -04:00
|
|
|
else:
|
2024-08-30 10:04:08 -04:00
|
|
|
url = await auth.issuer()
|
2024-08-30 08:52:57 -04:00
|
|
|
respond_with_redirect(request, str.encode(url))
|
|
|
|
|
2018-11-19 13:27:33 -05:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2020-10-02 06:15:53 -04:00
|
|
|
html = self.recaptcha_template.render(
|
|
|
|
session=session,
|
2022-02-22 06:20:45 -05:00
|
|
|
myurl="%s/v3/auth/%s/fallback/web"
|
2019-05-15 12:37:46 -04:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
2021-09-23 07:13:34 -04:00
|
|
|
sitekey=self.hs.config.captcha.recaptcha_public_key,
|
2020-10-02 06:15:53 -04:00
|
|
|
)
|
2018-09-27 16:53:58 -04:00
|
|
|
elif stagetype == LoginType.TERMS:
|
2020-10-02 06:15:53 -04:00
|
|
|
html = self.terms_template.render(
|
|
|
|
session=session,
|
|
|
|
terms_url="%s_matrix/consent?v=%s"
|
2021-09-13 13:07:12 -04:00
|
|
|
% (
|
|
|
|
self.hs.config.server.public_baseurl,
|
2021-09-23 07:13:34 -04:00
|
|
|
self.hs.config.consent.user_consent_version,
|
2021-09-13 13:07:12 -04:00
|
|
|
),
|
2022-02-22 06:20:45 -05:00
|
|
|
myurl="%s/v3/auth/%s/fallback/web"
|
2019-05-15 12:37:46 -04:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2020-10-02 06:15:53 -04:00
|
|
|
)
|
2020-04-01 08:48:00 -04:00
|
|
|
|
2020-04-03 15:35:05 -04:00
|
|
|
elif stagetype == LoginType.SSO:
|
2020-04-01 08:48:00 -04:00
|
|
|
# Display a confirmation page which prompts the user to
|
|
|
|
# re-authenticate with their SSO provider.
|
2021-01-12 12:38:03 -05:00
|
|
|
html = await self.auth_handler.start_sso_ui_auth(request, session)
|
2020-04-03 15:35:05 -04:00
|
|
|
|
2021-08-21 17:14:43 -04:00
|
|
|
elif stagetype == LoginType.REGISTRATION_TOKEN:
|
|
|
|
html = self.registration_token_template.render(
|
|
|
|
session=session,
|
|
|
|
myurl=f"{CLIENT_API_PREFIX}/r0/auth/{LoginType.REGISTRATION_TOKEN}/fallback/web",
|
|
|
|
)
|
|
|
|
|
2015-04-01 10:05:30 -04:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
2020-03-20 16:22:47 -04:00
|
|
|
# Render the HTML and return.
|
2020-07-01 09:10:23 -04:00
|
|
|
respond_with_html(request, 200, html)
|
2020-03-20 16:22:47 -04:00
|
|
|
return None
|
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
async def on_POST(self, request: Request, stagetype: str) -> None:
|
2018-11-19 13:27:33 -05:00
|
|
|
session = parse_string(request, "session")
|
|
|
|
if not session:
|
|
|
|
raise SynapseError(400, "No session supplied")
|
|
|
|
|
2018-10-03 17:54:19 -04:00
|
|
|
if stagetype == LoginType.RECAPTCHA:
|
2018-11-19 13:27:33 -05:00
|
|
|
response = parse_string(request, "g-recaptcha-response")
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2018-11-19 13:27:33 -05:00
|
|
|
if not response:
|
|
|
|
raise SynapseError(400, "No captcha response supplied")
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2018-11-19 13:27:33 -05:00
|
|
|
authdict = {"response": response, "session": session}
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2021-08-18 08:13:35 -04:00
|
|
|
try:
|
|
|
|
await self.auth_handler.add_oob_auth(
|
2022-05-04 14:11:21 -04:00
|
|
|
LoginType.RECAPTCHA, authdict, request.getClientAddress().host
|
2021-08-18 08:13:35 -04:00
|
|
|
)
|
|
|
|
except LoginError as e:
|
|
|
|
# Authentication failed, let user try again
|
2020-10-02 06:15:53 -04:00
|
|
|
html = self.recaptcha_template.render(
|
|
|
|
session=session,
|
2022-02-22 06:20:45 -05:00
|
|
|
myurl="%s/v3/auth/%s/fallback/web"
|
2019-05-15 12:37:46 -04:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.RECAPTCHA),
|
2021-09-23 07:13:34 -04:00
|
|
|
sitekey=self.hs.config.captcha.recaptcha_public_key,
|
2021-08-18 08:13:35 -04:00
|
|
|
error=e.msg,
|
2020-10-02 06:15:53 -04:00
|
|
|
)
|
2021-08-18 08:13:35 -04:00
|
|
|
else:
|
|
|
|
# No LoginError was raised, so authentication was successful
|
|
|
|
html = self.success_template.render()
|
|
|
|
|
2018-10-03 17:54:19 -04:00
|
|
|
elif stagetype == LoginType.TERMS:
|
|
|
|
authdict = {"session": session}
|
|
|
|
|
2021-08-18 08:13:35 -04:00
|
|
|
try:
|
|
|
|
await self.auth_handler.add_oob_auth(
|
2022-05-04 14:11:21 -04:00
|
|
|
LoginType.TERMS, authdict, request.getClientAddress().host
|
2021-08-18 08:13:35 -04:00
|
|
|
)
|
|
|
|
except LoginError as e:
|
|
|
|
# Authentication failed, let user try again
|
2020-10-02 06:15:53 -04:00
|
|
|
html = self.terms_template.render(
|
|
|
|
session=session,
|
|
|
|
terms_url="%s_matrix/consent?v=%s"
|
2018-11-15 22:41:53 -05:00
|
|
|
% (
|
2021-09-13 13:07:12 -04:00
|
|
|
self.hs.config.server.public_baseurl,
|
2021-09-23 07:13:34 -04:00
|
|
|
self.hs.config.consent.user_consent_version,
|
2018-10-03 17:54:19 -04:00
|
|
|
),
|
2022-02-22 06:20:45 -05:00
|
|
|
myurl="%s/v3/auth/%s/fallback/web"
|
2019-05-15 12:37:46 -04:00
|
|
|
% (CLIENT_API_PREFIX, LoginType.TERMS),
|
2021-08-18 08:13:35 -04:00
|
|
|
error=e.msg,
|
2020-10-02 06:15:53 -04:00
|
|
|
)
|
2021-08-18 08:13:35 -04:00
|
|
|
else:
|
|
|
|
# No LoginError was raised, so authentication was successful
|
|
|
|
html = self.success_template.render()
|
|
|
|
|
2020-04-01 08:48:00 -04:00
|
|
|
elif stagetype == LoginType.SSO:
|
|
|
|
# The SSO fallback workflow should not post here,
|
|
|
|
raise SynapseError(404, "Fallback SSO auth does not support POST requests.")
|
2021-08-18 08:13:35 -04:00
|
|
|
|
2021-08-21 17:14:43 -04:00
|
|
|
elif stagetype == LoginType.REGISTRATION_TOKEN:
|
|
|
|
token = parse_string(request, "token", required=True)
|
|
|
|
authdict = {"session": session, "token": token}
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self.auth_handler.add_oob_auth(
|
2022-05-04 14:11:21 -04:00
|
|
|
LoginType.REGISTRATION_TOKEN,
|
|
|
|
authdict,
|
|
|
|
request.getClientAddress().host,
|
2021-08-21 17:14:43 -04:00
|
|
|
)
|
|
|
|
except LoginError as e:
|
|
|
|
html = self.registration_token_template.render(
|
|
|
|
session=session,
|
|
|
|
myurl=f"{CLIENT_API_PREFIX}/r0/auth/{LoginType.REGISTRATION_TOKEN}/fallback/web",
|
|
|
|
error=e.msg,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
html = self.success_template.render()
|
|
|
|
|
2015-04-01 10:05:30 -04:00
|
|
|
else:
|
|
|
|
raise SynapseError(404, "Unknown auth stage type")
|
|
|
|
|
2020-03-20 16:22:47 -04:00
|
|
|
# Render the HTML and return.
|
2020-07-01 09:10:23 -04:00
|
|
|
respond_with_html(request, 200, html)
|
2020-03-20 16:22:47 -04:00
|
|
|
return None
|
|
|
|
|
2015-04-01 10:05:30 -04:00
|
|
|
|
2021-08-26 07:53:52 -04:00
|
|
|
def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
|
2015-04-01 10:05:30 -04:00
|
|
|
AuthRestServlet(hs).register(http_server)
|