You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

110 rivejä
4.4 KiB

  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import Any
  16. from synapse.config._base import Config, ConfigError
  17. from synapse.types import JsonDict
  18. logger = logging.getLogger(__name__)
  19. LEGACY_TEMPLATE_DIR_WARNING = """
  20. This server's configuration file is using the deprecated 'template_dir' setting in the
  21. 'account_validity' section. Support for this setting has been deprecated and will be
  22. removed in a future version of Synapse. Server admins should instead use the new
  23. 'custom_template_directory' setting documented here:
  24. https://matrix-org.github.io/synapse/latest/templates.html
  25. ---------------------------------------------------------------------------------------"""
  26. class AccountValidityConfig(Config):
  27. section = "account_validity"
  28. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  29. """Parses the old account validity config. The config format looks like this:
  30. account_validity:
  31. enabled: true
  32. period: 6w
  33. renew_at: 1w
  34. renew_email_subject: "Renew your %(app)s account"
  35. template_dir: "res/templates"
  36. account_renewed_html_path: "account_renewed.html"
  37. invalid_token_html_path: "invalid_token.html"
  38. We expect admins to use modules for this feature (which is why it doesn't appear
  39. in the sample config file), but we want to keep support for it around for a bit
  40. for backwards compatibility.
  41. """
  42. account_validity_config = config.get("account_validity") or {}
  43. self.account_validity_enabled = account_validity_config.get("enabled", False)
  44. self.account_validity_renew_by_email_enabled = (
  45. "renew_at" in account_validity_config
  46. )
  47. if self.account_validity_enabled:
  48. if "period" in account_validity_config:
  49. self.account_validity_period = self.parse_duration(
  50. account_validity_config["period"]
  51. )
  52. else:
  53. raise ConfigError("'period' is required when using account validity")
  54. if "renew_at" in account_validity_config:
  55. self.account_validity_renew_at = self.parse_duration(
  56. account_validity_config["renew_at"]
  57. )
  58. if "renew_email_subject" in account_validity_config:
  59. self.account_validity_renew_email_subject = account_validity_config[
  60. "renew_email_subject"
  61. ]
  62. else:
  63. self.account_validity_renew_email_subject = "Renew your %(app)s account"
  64. self.account_validity_startup_job_max_delta = (
  65. self.account_validity_period * 10.0 / 100.0
  66. )
  67. # Load account validity templates.
  68. account_validity_template_dir = account_validity_config.get("template_dir")
  69. if account_validity_template_dir is not None:
  70. logger.warning(LEGACY_TEMPLATE_DIR_WARNING)
  71. account_renewed_template_filename = account_validity_config.get(
  72. "account_renewed_html_path", "account_renewed.html"
  73. )
  74. invalid_token_template_filename = account_validity_config.get(
  75. "invalid_token_html_path", "invalid_token.html"
  76. )
  77. # Read and store template content
  78. custom_template_directories = (
  79. self.root.server.custom_template_directory,
  80. account_validity_template_dir,
  81. )
  82. (
  83. self.account_validity_account_renewed_template,
  84. self.account_validity_account_previously_renewed_template,
  85. self.account_validity_invalid_token_template,
  86. ) = self.read_templates(
  87. [
  88. account_renewed_template_filename,
  89. "account_previously_renewed.html",
  90. invalid_token_template_filename,
  91. ],
  92. (td for td in custom_template_directories if td),
  93. )