From f670a75f1d537a63632e08483d62c153cd13b310 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Oct 2020 13:56:06 -0400 Subject: [PATCH 1/5] Convert recaptcha to a Jinja template. --- synapse/config/captcha.py | 3 ++ synapse/res/templates/recaptcha.html | 38 +++++++++++++++++ synapse/rest/client/v2_alpha/auth.py | 62 ++++++---------------------- 3 files changed, 53 insertions(+), 50 deletions(-) create mode 100644 synapse/res/templates/recaptcha.html diff --git a/synapse/config/captcha.py b/synapse/config/captcha.py index 82f04d7966e2..cb009581651b 100644 --- a/synapse/config/captcha.py +++ b/synapse/config/captcha.py @@ -28,6 +28,9 @@ def read_config(self, config, **kwargs): "recaptcha_siteverify_api", "https://www.recaptcha.net/recaptcha/api/siteverify", ) + self.recaptcha_template = self.read_templates( + ["recaptcha.html"], autoescape=True + )[0] def generate_config_section(self, **kwargs): return """\ diff --git a/synapse/res/templates/recaptcha.html b/synapse/res/templates/recaptcha.html new file mode 100644 index 000000000000..63944dc60814 --- /dev/null +++ b/synapse/res/templates/recaptcha.html @@ -0,0 +1,38 @@ + + +Authentication + + + + + + + +
+
+

+ Hello! We need to prevent computer programs and other automated + things from creating accounts on this server. +

+

+ Please verify that you're not a robot. +

+ +
+
+ +
+ +
+ + diff --git a/synapse/rest/client/v2_alpha/auth.py b/synapse/rest/client/v2_alpha/auth.py index 097538f96864..8fed32580198 100644 --- a/synapse/rest/client/v2_alpha/auth.py +++ b/synapse/rest/client/v2_alpha/auth.py @@ -25,46 +25,6 @@ logger = logging.getLogger(__name__) -RECAPTCHA_TEMPLATE = """ - - -Authentication - - - - - - - -
-
-

- Hello! We need to prevent computer programs and other automated - things from creating accounts on this server. -

-

- Please verify that you're not a robot. -

- -
-
- -
- -
- - -""" TERMS_TEMPLATE = """ @@ -145,18 +105,20 @@ def __init__(self, hs): self._cas_server_url = hs.config.cas_server_url self._cas_service_url = hs.config.cas_service_url + self.recaptcha_template = hs.config.recaptcha_template + async def on_GET(self, request, stagetype): session = parse_string(request, "session") if not session: raise SynapseError(400, "No session supplied") if stagetype == LoginType.RECAPTCHA: - html = RECAPTCHA_TEMPLATE % { - "session": session, - "myurl": "%s/r0/auth/%s/fallback/web" + html = self.recaptcha_template.render( + session=session, + myurl="%s/r0/auth/%s/fallback/web" % (CLIENT_API_PREFIX, LoginType.RECAPTCHA), - "sitekey": self.hs.config.recaptcha_public_key, - } + sitekey=self.hs.config.recaptcha_public_key, + ) elif stagetype == LoginType.TERMS: html = TERMS_TEMPLATE % { "session": session, @@ -224,12 +186,12 @@ async def on_POST(self, request, stagetype): if success: html = SUCCESS_TEMPLATE else: - html = RECAPTCHA_TEMPLATE % { - "session": session, - "myurl": "%s/r0/auth/%s/fallback/web" + html = self.recaptcha_template.render( + session=session, + myurl="%s/r0/auth/%s/fallback/web" % (CLIENT_API_PREFIX, LoginType.RECAPTCHA), - "sitekey": self.hs.config.recaptcha_public_key, - } + sitekey=self.hs.config.recaptcha_public_key, + ) elif stagetype == LoginType.TERMS: authdict = {"session": session} From d8491ad54bcf99b62ac3b3f67526c2af84d3ae4d Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Oct 2020 14:04:27 -0400 Subject: [PATCH 2/5] Convert terms to a Jinja template. --- synapse/config/consent_config.py | 2 ++ synapse/res/templates/terms.html | 20 +++++++++++++ synapse/rest/client/v2_alpha/auth.py | 44 +++++++--------------------- 3 files changed, 33 insertions(+), 33 deletions(-) create mode 100644 synapse/res/templates/terms.html diff --git a/synapse/config/consent_config.py b/synapse/config/consent_config.py index fbddebeeab2a..6efa59b110b0 100644 --- a/synapse/config/consent_config.py +++ b/synapse/config/consent_config.py @@ -89,6 +89,8 @@ def __init__(self, *args): def read_config(self, config, **kwargs): consent_config = config.get("user_consent") + self.terms_template = self.read_templates(["terms.html"], autoescape=True)[0] + if consent_config is None: return self.user_consent_version = str(consent_config["version"]) diff --git a/synapse/res/templates/terms.html b/synapse/res/templates/terms.html new file mode 100644 index 000000000000..dfef9897ee40 --- /dev/null +++ b/synapse/res/templates/terms.html @@ -0,0 +1,20 @@ + + +Authentication + + + + +
+
+

+ Please click the button below if you agree to the + privacy policy of this homeserver. +

+ + +
+
+ + diff --git a/synapse/rest/client/v2_alpha/auth.py b/synapse/rest/client/v2_alpha/auth.py index 8fed32580198..46b7e883b17e 100644 --- a/synapse/rest/client/v2_alpha/auth.py +++ b/synapse/rest/client/v2_alpha/auth.py @@ -26,29 +26,6 @@ logger = logging.getLogger(__name__) -TERMS_TEMPLATE = """ - - -Authentication - - - - -
-
-

- Please click the button below if you agree to the - privacy policy of this homeserver. -

- - -
-
- - -""" - SUCCESS_TEMPLATE = """ @@ -106,6 +83,7 @@ def __init__(self, hs): self._cas_service_url = hs.config.cas_service_url self.recaptcha_template = hs.config.recaptcha_template + self.terms_template = hs.config.terms_template async def on_GET(self, request, stagetype): session = parse_string(request, "session") @@ -120,13 +98,13 @@ async def on_GET(self, request, stagetype): sitekey=self.hs.config.recaptcha_public_key, ) elif stagetype == LoginType.TERMS: - html = TERMS_TEMPLATE % { - "session": session, - "terms_url": "%s_matrix/consent?v=%s" + html = self.terms_template.render( + 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" + myurl="%s/r0/auth/%s/fallback/web" % (CLIENT_API_PREFIX, LoginType.TERMS), - } + ) elif stagetype == LoginType.SSO: # Display a confirmation page which prompts the user to @@ -202,16 +180,16 @@ async def on_POST(self, request, stagetype): if success: html = SUCCESS_TEMPLATE else: - html = TERMS_TEMPLATE % { - "session": session, - "terms_url": "%s_matrix/consent?v=%s" + html = self.terms_template.render( + 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" + myurl="%s/r0/auth/%s/fallback/web" % (CLIENT_API_PREFIX, LoginType.TERMS), - } + ) elif stagetype == LoginType.SSO: # The SSO fallback workflow should not post here, raise SynapseError(404, "Fallback SSO auth does not support POST requests.") From f6a0266812536343b2ff04c10abea65a44ed48a3 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Oct 2020 14:09:51 -0400 Subject: [PATCH 3/5] Convert success to a Jinja template. --- synapse/config/registration.py | 5 +++++ synapse/res/templates/auth_success.html | 21 +++++++++++++++++ synapse/rest/client/v2_alpha/auth.py | 30 +++---------------------- 3 files changed, 29 insertions(+), 27 deletions(-) create mode 100644 synapse/res/templates/auth_success.html diff --git a/synapse/config/registration.py b/synapse/config/registration.py index 5ffbb934fe2d..d7e3690a32fb 100644 --- a/synapse/config/registration.py +++ b/synapse/config/registration.py @@ -187,6 +187,11 @@ def read_config(self, config, **kwargs): session_lifetime = self.parse_duration(session_lifetime) self.session_lifetime = session_lifetime + # The success template used during fallback auth. + self.fallback_success_template = self.read_templates( + ["auth_success.html"], autoescape=True + )[0] + def generate_config_section(self, generate_secrets=False, **kwargs): if generate_secrets: registration_shared_secret = 'registration_shared_secret: "%s"' % ( diff --git a/synapse/res/templates/auth_success.html b/synapse/res/templates/auth_success.html new file mode 100644 index 000000000000..baf463314258 --- /dev/null +++ b/synapse/res/templates/auth_success.html @@ -0,0 +1,21 @@ + + +Success! + + + + + +
+

Thank you

+

You may now close this window and return to the application

+
+ + diff --git a/synapse/rest/client/v2_alpha/auth.py b/synapse/rest/client/v2_alpha/auth.py index 46b7e883b17e..5fbfae599101 100644 --- a/synapse/rest/client/v2_alpha/auth.py +++ b/synapse/rest/client/v2_alpha/auth.py @@ -26,31 +26,6 @@ logger = logging.getLogger(__name__) -SUCCESS_TEMPLATE = """ - - -Success! - - - - - -
-

Thank you

-

You may now close this window and return to the application

-
- - -""" - - class AuthRestServlet(RestServlet): """ Handles Client / Server API authentication in any situations where it @@ -84,6 +59,7 @@ def __init__(self, hs): self.recaptcha_template = hs.config.recaptcha_template self.terms_template = hs.config.terms_template + self.success_template = hs.config.fallback_success_template async def on_GET(self, request, stagetype): session = parse_string(request, "session") @@ -162,7 +138,7 @@ async def on_POST(self, request, stagetype): ) if success: - html = SUCCESS_TEMPLATE + html = self.success_template.render() else: html = self.recaptcha_template.render( session=session, @@ -178,7 +154,7 @@ async def on_POST(self, request, stagetype): ) if success: - html = SUCCESS_TEMPLATE + html = self.success_template.render() else: html = self.terms_template.render( session=session, From 472324b96bd99be223405cb7223d55cd2482b351 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Oct 2020 15:37:46 -0400 Subject: [PATCH 4/5] Don't require public_baseurl to be set. --- synapse/config/_base.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/synapse/config/_base.py b/synapse/config/_base.py index 05a66841c338..85f65da4d95f 100644 --- a/synapse/config/_base.py +++ b/synapse/config/_base.py @@ -242,12 +242,11 @@ def read_templates( env = jinja2.Environment(loader=loader, autoescape=autoescape) # Update the environment with our custom filters - env.filters.update( - { - "format_ts": _format_ts_filter, - "mxc_to_http": _create_mxc_to_http_filter(self.public_baseurl), - } - ) + env.filters.update({"format_ts": _format_ts_filter}) + if self.public_baseurl: + env.filters.update( + {"mxc_to_http": _create_mxc_to_http_filter(self.public_baseurl)} + ) for filename in filenames: # Load the template From 17255e4168fe3e51665454b0bb1de3c1d62bff60 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 1 Oct 2020 15:56:33 -0400 Subject: [PATCH 5/5] Add a changelog. --- changelog.d/8444.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/8444.bugfix diff --git a/changelog.d/8444.bugfix b/changelog.d/8444.bugfix new file mode 100644 index 000000000000..30c4328d4bd9 --- /dev/null +++ b/changelog.d/8444.bugfix @@ -0,0 +1 @@ +Convert additional templates from inline HTML to Jinja2 templates.