Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

226 righe
10 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, Optional
  17. from synapse.api.constants import RoomCreationPreset
  18. from synapse.config._base import Config, ConfigError
  19. from synapse.types import JsonDict, RoomAlias, UserID
  20. from synapse.util.stringutils import random_string_with_symbols, strtobool
  21. class RegistrationConfig(Config):
  22. section = "registration"
  23. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  24. self.enable_registration = strtobool(
  25. str(config.get("enable_registration", False))
  26. )
  27. if "disable_registration" in config:
  28. self.enable_registration = not strtobool(
  29. str(config["disable_registration"])
  30. )
  31. self.enable_registration_without_verification = strtobool(
  32. str(config.get("enable_registration_without_verification", False))
  33. )
  34. self.registrations_require_3pid = config.get("registrations_require_3pid", [])
  35. self.allowed_local_3pids = config.get("allowed_local_3pids", [])
  36. self.enable_3pid_lookup = config.get("enable_3pid_lookup", True)
  37. self.registration_requires_token = config.get(
  38. "registration_requires_token", False
  39. )
  40. self.enable_registration_token_3pid_bypass = config.get(
  41. "enable_registration_token_3pid_bypass", False
  42. )
  43. self.registration_shared_secret = config.get("registration_shared_secret")
  44. self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
  45. account_threepid_delegates = config.get("account_threepid_delegates") or {}
  46. self.account_threepid_delegate_email = account_threepid_delegates.get("email")
  47. self.account_threepid_delegate_msisdn = account_threepid_delegates.get("msisdn")
  48. self.default_identity_server = config.get("default_identity_server")
  49. self.allow_guest_access = config.get("allow_guest_access", False)
  50. if config.get("invite_3pid_guest", False):
  51. raise ConfigError("invite_3pid_guest is no longer supported")
  52. self.auto_join_rooms = config.get("auto_join_rooms", [])
  53. for room_alias in self.auto_join_rooms:
  54. if not RoomAlias.is_valid(room_alias):
  55. raise ConfigError("Invalid auto_join_rooms entry %s" % (room_alias,))
  56. # Options for creating auto-join rooms if they do not exist yet.
  57. self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
  58. self.autocreate_auto_join_rooms_federated = config.get(
  59. "autocreate_auto_join_rooms_federated", True
  60. )
  61. self.autocreate_auto_join_room_preset = (
  62. config.get("autocreate_auto_join_room_preset")
  63. or RoomCreationPreset.PUBLIC_CHAT
  64. )
  65. self.auto_join_room_requires_invite = self.autocreate_auto_join_room_preset in {
  66. RoomCreationPreset.PRIVATE_CHAT,
  67. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  68. }
  69. # Pull the creator/inviter from the configuration, this gets used to
  70. # send invites for invite-only rooms.
  71. mxid_localpart = config.get("auto_join_mxid_localpart")
  72. self.auto_join_user_id = None
  73. if mxid_localpart:
  74. # Convert the localpart to a full mxid.
  75. self.auto_join_user_id = UserID(
  76. mxid_localpart, self.root.server.server_name
  77. ).to_string()
  78. if self.autocreate_auto_join_rooms:
  79. # Ensure the preset is a known value.
  80. if self.autocreate_auto_join_room_preset not in {
  81. RoomCreationPreset.PUBLIC_CHAT,
  82. RoomCreationPreset.PRIVATE_CHAT,
  83. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  84. }:
  85. raise ConfigError("Invalid value for autocreate_auto_join_room_preset")
  86. # If the preset requires invitations to be sent, ensure there's a
  87. # configured user to send them from.
  88. if self.auto_join_room_requires_invite:
  89. if not mxid_localpart:
  90. raise ConfigError(
  91. "The configuration option `auto_join_mxid_localpart` is required if "
  92. "`autocreate_auto_join_room_preset` is set to private_chat or trusted_private_chat, such that "
  93. "Synapse knows who to send invitations from. Please "
  94. "configure `auto_join_mxid_localpart`."
  95. )
  96. self.auto_join_rooms_for_guests = config.get("auto_join_rooms_for_guests", True)
  97. self.enable_set_displayname = config.get("enable_set_displayname", True)
  98. self.enable_set_avatar_url = config.get("enable_set_avatar_url", True)
  99. self.enable_3pid_changes = config.get("enable_3pid_changes", True)
  100. self.disable_msisdn_registration = config.get(
  101. "disable_msisdn_registration", False
  102. )
  103. session_lifetime = config.get("session_lifetime")
  104. if session_lifetime is not None:
  105. session_lifetime = self.parse_duration(session_lifetime)
  106. self.session_lifetime = session_lifetime
  107. # The `refreshable_access_token_lifetime` applies for tokens that can be renewed
  108. # using a refresh token, as per MSC2918.
  109. # If it is `None`, the refresh token mechanism is disabled.
  110. refreshable_access_token_lifetime = config.get(
  111. "refreshable_access_token_lifetime",
  112. "5m",
  113. )
  114. if refreshable_access_token_lifetime is not None:
  115. refreshable_access_token_lifetime = self.parse_duration(
  116. refreshable_access_token_lifetime
  117. )
  118. self.refreshable_access_token_lifetime: Optional[
  119. int
  120. ] = refreshable_access_token_lifetime
  121. if (
  122. self.session_lifetime is not None
  123. and "refreshable_access_token_lifetime" in config
  124. ):
  125. if self.session_lifetime < self.refreshable_access_token_lifetime:
  126. raise ConfigError(
  127. "Both `session_lifetime` and `refreshable_access_token_lifetime` "
  128. "configuration options have been set, but `refreshable_access_token_lifetime` "
  129. " exceeds `session_lifetime`!"
  130. )
  131. # The `nonrefreshable_access_token_lifetime` applies for tokens that can NOT be
  132. # refreshed using a refresh token.
  133. # If it is None, then these tokens last for the entire length of the session,
  134. # which is infinite by default.
  135. # The intention behind this configuration option is to help with requiring
  136. # all clients to use refresh tokens, if the homeserver administrator requires.
  137. nonrefreshable_access_token_lifetime = config.get(
  138. "nonrefreshable_access_token_lifetime",
  139. None,
  140. )
  141. if nonrefreshable_access_token_lifetime is not None:
  142. nonrefreshable_access_token_lifetime = self.parse_duration(
  143. nonrefreshable_access_token_lifetime
  144. )
  145. self.nonrefreshable_access_token_lifetime = nonrefreshable_access_token_lifetime
  146. if (
  147. self.session_lifetime is not None
  148. and self.nonrefreshable_access_token_lifetime is not None
  149. ):
  150. if self.session_lifetime < self.nonrefreshable_access_token_lifetime:
  151. raise ConfigError(
  152. "Both `session_lifetime` and `nonrefreshable_access_token_lifetime` "
  153. "configuration options have been set, but `nonrefreshable_access_token_lifetime` "
  154. " exceeds `session_lifetime`!"
  155. )
  156. refresh_token_lifetime = config.get("refresh_token_lifetime")
  157. if refresh_token_lifetime is not None:
  158. refresh_token_lifetime = self.parse_duration(refresh_token_lifetime)
  159. self.refresh_token_lifetime: Optional[int] = refresh_token_lifetime
  160. if (
  161. self.session_lifetime is not None
  162. and self.refresh_token_lifetime is not None
  163. ):
  164. if self.session_lifetime < self.refresh_token_lifetime:
  165. raise ConfigError(
  166. "Both `session_lifetime` and `refresh_token_lifetime` "
  167. "configuration options have been set, but `refresh_token_lifetime` "
  168. " exceeds `session_lifetime`!"
  169. )
  170. # The fallback template used for authenticating using a registration token
  171. self.registration_token_template = self.read_template("registration_token.html")
  172. # The success template used during fallback auth.
  173. self.fallback_success_template = self.read_template("auth_success.html")
  174. self.inhibit_user_in_use_error = config.get("inhibit_user_in_use_error", False)
  175. def generate_config_section(
  176. self, generate_secrets: bool = False, **kwargs: Any
  177. ) -> str:
  178. if generate_secrets:
  179. registration_shared_secret = 'registration_shared_secret: "%s"' % (
  180. random_string_with_symbols(50),
  181. )
  182. return registration_shared_secret
  183. else:
  184. return ""
  185. @staticmethod
  186. def add_arguments(parser: argparse.ArgumentParser) -> None:
  187. reg_group = parser.add_argument_group("registration")
  188. reg_group.add_argument(
  189. "--enable-registration",
  190. action="store_true",
  191. default=None,
  192. help="Enable registration for new users.",
  193. )
  194. def read_arguments(self, args: argparse.Namespace) -> None:
  195. if args.enable_registration is not None:
  196. self.enable_registration = strtobool(str(args.enable_registration))