2021-04-19 14:16:34 -04:00
|
|
|
# Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# 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.
|
2021-09-06 10:23:50 -04:00
|
|
|
import logging
|
2022-04-11 12:07:23 -04:00
|
|
|
from typing import Any
|
2021-09-06 10:23:50 -04:00
|
|
|
|
2021-04-19 14:16:34 -04:00
|
|
|
from synapse.config._base import Config, ConfigError
|
2022-04-11 12:07:23 -04:00
|
|
|
from synapse.types import JsonDict
|
2021-04-19 14:16:34 -04:00
|
|
|
|
2021-09-06 10:23:50 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
LEGACY_TEMPLATE_DIR_WARNING = """
|
|
|
|
This server's configuration file is using the deprecated 'template_dir' setting in the
|
|
|
|
'account_validity' section. Support for this setting has been deprecated and will be
|
|
|
|
removed in a future version of Synapse. Server admins should instead use the new
|
2022-08-17 05:59:05 -04:00
|
|
|
'custom_template_directory' setting documented here:
|
2021-09-06 10:23:50 -04:00
|
|
|
https://matrix-org.github.io/synapse/latest/templates.html
|
|
|
|
---------------------------------------------------------------------------------------"""
|
|
|
|
|
2021-04-19 14:16:34 -04:00
|
|
|
|
|
|
|
class AccountValidityConfig(Config):
|
|
|
|
section = "account_validity"
|
|
|
|
|
2022-04-11 12:07:23 -04:00
|
|
|
def read_config(self, config: JsonDict, **kwargs: Any) -> None:
|
2021-07-16 12:11:53 -04:00
|
|
|
"""Parses the old account validity config. The config format looks like this:
|
|
|
|
|
|
|
|
account_validity:
|
|
|
|
enabled: true
|
|
|
|
period: 6w
|
|
|
|
renew_at: 1w
|
|
|
|
renew_email_subject: "Renew your %(app)s account"
|
|
|
|
template_dir: "res/templates"
|
|
|
|
account_renewed_html_path: "account_renewed.html"
|
|
|
|
invalid_token_html_path: "invalid_token.html"
|
|
|
|
|
|
|
|
We expect admins to use modules for this feature (which is why it doesn't appear
|
|
|
|
in the sample config file), but we want to keep support for it around for a bit
|
|
|
|
for backwards compatibility.
|
|
|
|
"""
|
2021-04-19 14:16:34 -04:00
|
|
|
account_validity_config = config.get("account_validity") or {}
|
|
|
|
self.account_validity_enabled = account_validity_config.get("enabled", False)
|
|
|
|
self.account_validity_renew_by_email_enabled = (
|
|
|
|
"renew_at" in account_validity_config
|
|
|
|
)
|
|
|
|
|
|
|
|
if self.account_validity_enabled:
|
|
|
|
if "period" in account_validity_config:
|
|
|
|
self.account_validity_period = self.parse_duration(
|
|
|
|
account_validity_config["period"]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
raise ConfigError("'period' is required when using account validity")
|
|
|
|
|
|
|
|
if "renew_at" in account_validity_config:
|
|
|
|
self.account_validity_renew_at = self.parse_duration(
|
|
|
|
account_validity_config["renew_at"]
|
|
|
|
)
|
|
|
|
|
|
|
|
if "renew_email_subject" in account_validity_config:
|
|
|
|
self.account_validity_renew_email_subject = account_validity_config[
|
|
|
|
"renew_email_subject"
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
self.account_validity_renew_email_subject = "Renew your %(app)s account"
|
|
|
|
|
|
|
|
self.account_validity_startup_job_max_delta = (
|
|
|
|
self.account_validity_period * 10.0 / 100.0
|
|
|
|
)
|
|
|
|
|
|
|
|
# Load account validity templates.
|
|
|
|
account_validity_template_dir = account_validity_config.get("template_dir")
|
2021-09-06 10:23:50 -04:00
|
|
|
if account_validity_template_dir is not None:
|
|
|
|
logger.warning(LEGACY_TEMPLATE_DIR_WARNING)
|
2021-04-19 14:16:34 -04:00
|
|
|
|
|
|
|
account_renewed_template_filename = account_validity_config.get(
|
|
|
|
"account_renewed_html_path", "account_renewed.html"
|
|
|
|
)
|
|
|
|
invalid_token_template_filename = account_validity_config.get(
|
|
|
|
"invalid_token_html_path", "invalid_token.html"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Read and store template content
|
2021-08-17 08:45:24 -04:00
|
|
|
custom_template_directories = (
|
|
|
|
self.root.server.custom_template_directory,
|
|
|
|
account_validity_template_dir,
|
|
|
|
)
|
|
|
|
|
2021-04-19 14:16:34 -04:00
|
|
|
(
|
|
|
|
self.account_validity_account_renewed_template,
|
|
|
|
self.account_validity_account_previously_renewed_template,
|
|
|
|
self.account_validity_invalid_token_template,
|
|
|
|
) = self.read_templates(
|
|
|
|
[
|
|
|
|
account_renewed_template_filename,
|
|
|
|
"account_previously_renewed.html",
|
|
|
|
invalid_token_template_filename,
|
|
|
|
],
|
2021-08-17 08:45:24 -04:00
|
|
|
(td for td in custom_template_directories if td),
|
2021-04-19 14:16:34 -04:00
|
|
|
)
|