account_validity.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 synapse.config._base import Config, ConfigError
  16. logger = logging.getLogger(__name__)
  17. LEGACY_TEMPLATE_DIR_WARNING = """
  18. This server's configuration file is using the deprecated 'template_dir' setting in the
  19. 'account_validity' section. Support for this setting has been deprecated and will be
  20. removed in a future version of Synapse. Server admins should instead use the new
  21. 'custom_templates_directory' setting documented here:
  22. https://matrix-org.github.io/synapse/latest/templates.html
  23. ---------------------------------------------------------------------------------------"""
  24. class AccountValidityConfig(Config):
  25. section = "account_validity"
  26. def read_config(self, config, **kwargs):
  27. """Parses the old account validity config. The config format looks like this:
  28. account_validity:
  29. enabled: true
  30. period: 6w
  31. renew_at: 1w
  32. renew_email_subject: "Renew your %(app)s account"
  33. template_dir: "res/templates"
  34. account_renewed_html_path: "account_renewed.html"
  35. invalid_token_html_path: "invalid_token.html"
  36. We expect admins to use modules for this feature (which is why it doesn't appear
  37. in the sample config file), but we want to keep support for it around for a bit
  38. for backwards compatibility.
  39. """
  40. account_validity_config = config.get("account_validity") or {}
  41. self.account_validity_enabled = account_validity_config.get("enabled", False)
  42. self.account_validity_renew_by_email_enabled = (
  43. "renew_at" in account_validity_config
  44. )
  45. if self.account_validity_enabled:
  46. if "period" in account_validity_config:
  47. self.account_validity_period = self.parse_duration(
  48. account_validity_config["period"]
  49. )
  50. else:
  51. raise ConfigError("'period' is required when using account validity")
  52. if "renew_at" in account_validity_config:
  53. self.account_validity_renew_at = self.parse_duration(
  54. account_validity_config["renew_at"]
  55. )
  56. if "renew_email_subject" in account_validity_config:
  57. self.account_validity_renew_email_subject = account_validity_config[
  58. "renew_email_subject"
  59. ]
  60. else:
  61. self.account_validity_renew_email_subject = "Renew your %(app)s account"
  62. self.account_validity_startup_job_max_delta = (
  63. self.account_validity_period * 10.0 / 100.0
  64. )
  65. if self.account_validity_renew_by_email_enabled:
  66. if not self.root.server.public_baseurl:
  67. raise ConfigError("Can't send renewal emails without 'public_baseurl'")
  68. # Load account validity templates.
  69. account_validity_template_dir = account_validity_config.get("template_dir")
  70. if account_validity_template_dir is not None:
  71. logger.warning(LEGACY_TEMPLATE_DIR_WARNING)
  72. account_renewed_template_filename = account_validity_config.get(
  73. "account_renewed_html_path", "account_renewed.html"
  74. )
  75. invalid_token_template_filename = account_validity_config.get(
  76. "invalid_token_html_path", "invalid_token.html"
  77. )
  78. # Read and store template content
  79. custom_template_directories = (
  80. self.root.server.custom_template_directory,
  81. account_validity_template_dir,
  82. )
  83. (
  84. self.account_validity_account_renewed_template,
  85. self.account_validity_account_previously_renewed_template,
  86. self.account_validity_invalid_token_template,
  87. ) = self.read_templates(
  88. [
  89. account_renewed_template_filename,
  90. "account_previously_renewed.html",
  91. invalid_token_template_filename,
  92. ],
  93. (td for td in custom_template_directories if td),
  94. )