registration.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. self.enable_3pid_changes = config.get("enable_3pid_changes", True)
  119. self.disable_msisdn_registration = config.get(
  120. "disable_msisdn_registration", False
  121. )
  122. session_lifetime = config.get("session_lifetime")
  123. if session_lifetime is not None:
  124. session_lifetime = self.parse_duration(session_lifetime)
  125. self.session_lifetime = session_lifetime
  126. # The `refreshable_access_token_lifetime` applies for tokens that can be renewed
  127. # using a refresh token, as per MSC2918.
  128. # If it is `None`, the refresh token mechanism is disabled.
  129. refreshable_access_token_lifetime = config.get(
  130. "refreshable_access_token_lifetime",
  131. "5m",
  132. )
  133. if refreshable_access_token_lifetime is not None:
  134. refreshable_access_token_lifetime = self.parse_duration(
  135. refreshable_access_token_lifetime
  136. )
  137. self.refreshable_access_token_lifetime: Optional[
  138. int
  139. ] = refreshable_access_token_lifetime
  140. if (
  141. self.session_lifetime is not None
  142. and "refreshable_access_token_lifetime" in config
  143. ):
  144. if self.session_lifetime < self.refreshable_access_token_lifetime:
  145. raise ConfigError(
  146. "Both `session_lifetime` and `refreshable_access_token_lifetime` "
  147. "configuration options have been set, but `refreshable_access_token_lifetime` "
  148. " exceeds `session_lifetime`!"
  149. )
  150. # The `nonrefreshable_access_token_lifetime` applies for tokens that can NOT be
  151. # refreshed using a refresh token.
  152. # If it is None, then these tokens last for the entire length of the session,
  153. # which is infinite by default.
  154. # The intention behind this configuration option is to help with requiring
  155. # all clients to use refresh tokens, if the homeserver administrator requires.
  156. nonrefreshable_access_token_lifetime = config.get(
  157. "nonrefreshable_access_token_lifetime",
  158. None,
  159. )
  160. if nonrefreshable_access_token_lifetime is not None:
  161. nonrefreshable_access_token_lifetime = self.parse_duration(
  162. nonrefreshable_access_token_lifetime
  163. )
  164. self.nonrefreshable_access_token_lifetime = nonrefreshable_access_token_lifetime
  165. if (
  166. self.session_lifetime is not None
  167. and self.nonrefreshable_access_token_lifetime is not None
  168. ):
  169. if self.session_lifetime < self.nonrefreshable_access_token_lifetime:
  170. raise ConfigError(
  171. "Both `session_lifetime` and `nonrefreshable_access_token_lifetime` "
  172. "configuration options have been set, but `nonrefreshable_access_token_lifetime` "
  173. " exceeds `session_lifetime`!"
  174. )
  175. refresh_token_lifetime = config.get("refresh_token_lifetime")
  176. if refresh_token_lifetime is not None:
  177. refresh_token_lifetime = self.parse_duration(refresh_token_lifetime)
  178. self.refresh_token_lifetime: Optional[int] = refresh_token_lifetime
  179. if (
  180. self.session_lifetime is not None
  181. and self.refresh_token_lifetime is not None
  182. ):
  183. if self.session_lifetime < self.refresh_token_lifetime:
  184. raise ConfigError(
  185. "Both `session_lifetime` and `refresh_token_lifetime` "
  186. "configuration options have been set, but `refresh_token_lifetime` "
  187. " exceeds `session_lifetime`!"
  188. )
  189. # The fallback template used for authenticating using a registration token
  190. self.registration_token_template = self.read_template("registration_token.html")
  191. # The success template used during fallback auth.
  192. self.fallback_success_template = self.read_template("auth_success.html")
  193. self.inhibit_user_in_use_error = config.get("inhibit_user_in_use_error", False)
  194. def generate_config_section(
  195. self, generate_secrets: bool = False, **kwargs: Any
  196. ) -> str:
  197. if generate_secrets:
  198. registration_shared_secret = 'registration_shared_secret: "%s"' % (
  199. random_string_with_symbols(50),
  200. )
  201. return registration_shared_secret
  202. else:
  203. return ""
  204. def generate_files(self, config: Dict[str, Any], config_dir_path: str) -> None:
  205. # if 'registration_shared_secret_path' is specified, and the target file
  206. # does not exist, generate it.
  207. registration_shared_secret_path = config.get("registration_shared_secret_path")
  208. if registration_shared_secret_path and not self.path_exists(
  209. registration_shared_secret_path
  210. ):
  211. print(
  212. "Generating registration shared secret file "
  213. + registration_shared_secret_path
  214. )
  215. secret = random_string_with_symbols(50)
  216. with open(registration_shared_secret_path, "w") as f:
  217. f.write(f"{secret}\n")
  218. @staticmethod
  219. def add_arguments(parser: argparse.ArgumentParser) -> None:
  220. reg_group = parser.add_argument_group("registration")
  221. reg_group.add_argument(
  222. "--enable-registration",
  223. action="store_true",
  224. default=None,
  225. help="Enable registration for new users.",
  226. )
  227. def read_arguments(self, args: argparse.Namespace) -> None:
  228. if args.enable_registration is not None:
  229. self.enable_registration = strtobool(str(args.enable_registration))