25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

272 lines
12 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2021 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. from typing import Any, Dict, Optional
  17. from synapse.api.constants import RoomCreationPreset
  18. from synapse.config._base import Config, ConfigError, read_file
  19. from synapse.types import JsonDict, RoomAlias, UserID
  20. from synapse.util.stringutils import random_string_with_symbols, strtobool
  21. NO_EMAIL_DELEGATE_ERROR = """\
  22. Delegation of email verification to an identity server is no longer supported. To
  23. continue to allow users to add email addresses to their accounts, and use them for
  24. password resets, configure Synapse with an SMTP server via the `email` setting, and
  25. remove `account_threepid_delegates.email`.
  26. """
  27. CONFLICTING_SHARED_SECRET_OPTS_ERROR = """\
  28. You have configured both `registration_shared_secret` and
  29. `registration_shared_secret_path`. These are mutually incompatible.
  30. """
  31. class RegistrationConfig(Config):
  32. section = "registration"
  33. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  34. self.enable_registration = strtobool(
  35. str(config.get("enable_registration", False))
  36. )
  37. if "disable_registration" in config:
  38. self.enable_registration = not strtobool(
  39. str(config["disable_registration"])
  40. )
  41. self.enable_registration_without_verification = strtobool(
  42. str(config.get("enable_registration_without_verification", False))
  43. )
  44. self.registrations_require_3pid = config.get("registrations_require_3pid", [])
  45. self.allowed_local_3pids = config.get("allowed_local_3pids", [])
  46. self.enable_3pid_lookup = config.get("enable_3pid_lookup", True)
  47. self.registration_requires_token = config.get(
  48. "registration_requires_token", False
  49. )
  50. self.enable_registration_token_3pid_bypass = config.get(
  51. "enable_registration_token_3pid_bypass", False
  52. )
  53. # read the shared secret, either inline or from an external file
  54. self.registration_shared_secret = config.get("registration_shared_secret")
  55. registration_shared_secret_path = config.get("registration_shared_secret_path")
  56. if registration_shared_secret_path:
  57. if self.registration_shared_secret:
  58. raise ConfigError(CONFLICTING_SHARED_SECRET_OPTS_ERROR)
  59. self.registration_shared_secret = read_file(
  60. registration_shared_secret_path, ("registration_shared_secret_path",)
  61. ).strip()
  62. self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
  63. account_threepid_delegates = config.get("account_threepid_delegates") or {}
  64. if "email" in account_threepid_delegates:
  65. raise ConfigError(NO_EMAIL_DELEGATE_ERROR)
  66. self.account_threepid_delegate_msisdn = account_threepid_delegates.get("msisdn")
  67. self.default_identity_server = config.get("default_identity_server")
  68. self.allow_guest_access = config.get("allow_guest_access", False)
  69. if config.get("invite_3pid_guest", False):
  70. raise ConfigError("invite_3pid_guest is no longer supported")
  71. self.auto_join_rooms = config.get("auto_join_rooms", [])
  72. for room_alias in self.auto_join_rooms:
  73. if not RoomAlias.is_valid(room_alias):
  74. raise ConfigError("Invalid auto_join_rooms entry %s" % (room_alias,))
  75. # Options for creating auto-join rooms if they do not exist yet.
  76. self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
  77. self.autocreate_auto_join_rooms_federated = config.get(
  78. "autocreate_auto_join_rooms_federated", True
  79. )
  80. self.autocreate_auto_join_room_preset = (
  81. config.get("autocreate_auto_join_room_preset")
  82. or RoomCreationPreset.PUBLIC_CHAT
  83. )
  84. self.auto_join_room_requires_invite = self.autocreate_auto_join_room_preset in {
  85. RoomCreationPreset.PRIVATE_CHAT,
  86. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  87. }
  88. # Pull the creator/inviter from the configuration, this gets used to
  89. # send invites for invite-only rooms.
  90. mxid_localpart = config.get("auto_join_mxid_localpart")
  91. self.auto_join_user_id = None
  92. if mxid_localpart:
  93. # Convert the localpart to a full mxid.
  94. self.auto_join_user_id = UserID(
  95. mxid_localpart, self.root.server.server_name
  96. ).to_string()
  97. if self.autocreate_auto_join_rooms:
  98. # Ensure the preset is a known value.
  99. if self.autocreate_auto_join_room_preset not in {
  100. RoomCreationPreset.PUBLIC_CHAT,
  101. RoomCreationPreset.PRIVATE_CHAT,
  102. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  103. }:
  104. raise ConfigError("Invalid value for autocreate_auto_join_room_preset")
  105. # If the preset requires invitations to be sent, ensure there's a
  106. # configured user to send them from.
  107. if self.auto_join_room_requires_invite:
  108. if not mxid_localpart:
  109. raise ConfigError(
  110. "The configuration option `auto_join_mxid_localpart` is required if "
  111. "`autocreate_auto_join_room_preset` is set to private_chat or trusted_private_chat, such that "
  112. "Synapse knows who to send invitations from. Please "
  113. "configure `auto_join_mxid_localpart`."
  114. )
  115. self.auto_join_rooms_for_guests = config.get("auto_join_rooms_for_guests", True)
  116. self.enable_set_displayname = config.get("enable_set_displayname", True)
  117. self.enable_set_avatar_url = config.get("enable_set_avatar_url", True)
  118. # The default value of enable_3pid_changes is True, unless msc3861 is enabled.
  119. msc3861_enabled = (
  120. (config.get("experimental_features") or {})
  121. .get("msc3861", {})
  122. .get("enabled", False)
  123. )
  124. self.enable_3pid_changes = config.get(
  125. "enable_3pid_changes", not msc3861_enabled
  126. )
  127. self.disable_msisdn_registration = config.get(
  128. "disable_msisdn_registration", False
  129. )
  130. session_lifetime = config.get("session_lifetime")
  131. if session_lifetime is not None:
  132. session_lifetime = self.parse_duration(session_lifetime)
  133. self.session_lifetime = session_lifetime
  134. # The `refreshable_access_token_lifetime` applies for tokens that can be renewed
  135. # using a refresh token, as per MSC2918.
  136. # If it is `None`, the refresh token mechanism is disabled.
  137. refreshable_access_token_lifetime = config.get(
  138. "refreshable_access_token_lifetime",
  139. "5m",
  140. )
  141. if refreshable_access_token_lifetime is not None:
  142. refreshable_access_token_lifetime = self.parse_duration(
  143. refreshable_access_token_lifetime
  144. )
  145. self.refreshable_access_token_lifetime: Optional[
  146. int
  147. ] = refreshable_access_token_lifetime
  148. if (
  149. self.session_lifetime is not None
  150. and "refreshable_access_token_lifetime" in config
  151. ):
  152. if self.session_lifetime < self.refreshable_access_token_lifetime:
  153. raise ConfigError(
  154. "Both `session_lifetime` and `refreshable_access_token_lifetime` "
  155. "configuration options have been set, but `refreshable_access_token_lifetime` "
  156. " exceeds `session_lifetime`!"
  157. )
  158. # The `nonrefreshable_access_token_lifetime` applies for tokens that can NOT be
  159. # refreshed using a refresh token.
  160. # If it is None, then these tokens last for the entire length of the session,
  161. # which is infinite by default.
  162. # The intention behind this configuration option is to help with requiring
  163. # all clients to use refresh tokens, if the homeserver administrator requires.
  164. nonrefreshable_access_token_lifetime = config.get(
  165. "nonrefreshable_access_token_lifetime",
  166. None,
  167. )
  168. if nonrefreshable_access_token_lifetime is not None:
  169. nonrefreshable_access_token_lifetime = self.parse_duration(
  170. nonrefreshable_access_token_lifetime
  171. )
  172. self.nonrefreshable_access_token_lifetime = nonrefreshable_access_token_lifetime
  173. if (
  174. self.session_lifetime is not None
  175. and self.nonrefreshable_access_token_lifetime is not None
  176. ):
  177. if self.session_lifetime < self.nonrefreshable_access_token_lifetime:
  178. raise ConfigError(
  179. "Both `session_lifetime` and `nonrefreshable_access_token_lifetime` "
  180. "configuration options have been set, but `nonrefreshable_access_token_lifetime` "
  181. " exceeds `session_lifetime`!"
  182. )
  183. refresh_token_lifetime = config.get("refresh_token_lifetime")
  184. if refresh_token_lifetime is not None:
  185. refresh_token_lifetime = self.parse_duration(refresh_token_lifetime)
  186. self.refresh_token_lifetime: Optional[int] = refresh_token_lifetime
  187. if (
  188. self.session_lifetime is not None
  189. and self.refresh_token_lifetime is not None
  190. ):
  191. if self.session_lifetime < self.refresh_token_lifetime:
  192. raise ConfigError(
  193. "Both `session_lifetime` and `refresh_token_lifetime` "
  194. "configuration options have been set, but `refresh_token_lifetime` "
  195. " exceeds `session_lifetime`!"
  196. )
  197. # The fallback template used for authenticating using a registration token
  198. self.registration_token_template = self.read_template("registration_token.html")
  199. # The success template used during fallback auth.
  200. self.fallback_success_template = self.read_template("auth_success.html")
  201. self.inhibit_user_in_use_error = config.get("inhibit_user_in_use_error", False)
  202. def generate_config_section(
  203. self, generate_secrets: bool = False, **kwargs: Any
  204. ) -> str:
  205. if generate_secrets:
  206. registration_shared_secret = 'registration_shared_secret: "%s"' % (
  207. random_string_with_symbols(50),
  208. )
  209. return registration_shared_secret
  210. else:
  211. return ""
  212. def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
  213. # if 'registration_shared_secret_path' is specified, and the target file
  214. # does not exist, generate it.
  215. registration_shared_secret_path = config.get("registration_shared_secret_path")
  216. if registration_shared_secret_path and not self.path_exists(
  217. registration_shared_secret_path
  218. ):
  219. print(
  220. "Generating registration shared secret file "
  221. + registration_shared_secret_path
  222. )
  223. secret = random_string_with_symbols(50)
  224. with open(registration_shared_secret_path, "w") as f:
  225. f.write(f"{secret}\n")
  226. @staticmethod
  227. def add_arguments(parser: argparse.ArgumentParser) -> None:
  228. reg_group = parser.add_argument_group("registration")
  229. reg_group.add_argument(
  230. "--enable-registration",
  231. action="store_true",
  232. default=None,
  233. help="Enable registration for new users.",
  234. )
  235. def read_arguments(self, args: argparse.Namespace) -> None:
  236. if args.enable_registration is not None:
  237. self.enable_registration = strtobool(str(args.enable_registration))