registration.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. # Copyright 2015, 2016 OpenMarket Ltd
  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. from synapse.api.constants import RoomCreationPreset
  15. from synapse.config._base import Config, ConfigError
  16. from synapse.types import RoomAlias, UserID
  17. from synapse.util.stringutils import random_string_with_symbols, strtobool
  18. class RegistrationConfig(Config):
  19. section = "registration"
  20. def read_config(self, config, **kwargs):
  21. self.enable_registration = strtobool(
  22. str(config.get("enable_registration", False))
  23. )
  24. if "disable_registration" in config:
  25. self.enable_registration = not strtobool(
  26. str(config["disable_registration"])
  27. )
  28. self.registrations_require_3pid = config.get("registrations_require_3pid", [])
  29. self.allowed_local_3pids = config.get("allowed_local_3pids", [])
  30. self.enable_3pid_lookup = config.get("enable_3pid_lookup", True)
  31. self.registration_shared_secret = config.get("registration_shared_secret")
  32. self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
  33. self.trusted_third_party_id_servers = config.get(
  34. "trusted_third_party_id_servers", ["matrix.org", "vector.im"]
  35. )
  36. account_threepid_delegates = config.get("account_threepid_delegates") or {}
  37. self.account_threepid_delegate_email = account_threepid_delegates.get("email")
  38. self.account_threepid_delegate_msisdn = account_threepid_delegates.get("msisdn")
  39. if self.account_threepid_delegate_msisdn and not self.public_baseurl:
  40. raise ConfigError(
  41. "The configuration option `public_baseurl` is required if "
  42. "`account_threepid_delegate.msisdn` is set, such that "
  43. "clients know where to submit validation tokens to. Please "
  44. "configure `public_baseurl`."
  45. )
  46. self.default_identity_server = config.get("default_identity_server")
  47. self.allow_guest_access = config.get("allow_guest_access", False)
  48. if config.get("invite_3pid_guest", False):
  49. raise ConfigError("invite_3pid_guest is no longer supported")
  50. self.auto_join_rooms = config.get("auto_join_rooms", [])
  51. for room_alias in self.auto_join_rooms:
  52. if not RoomAlias.is_valid(room_alias):
  53. raise ConfigError("Invalid auto_join_rooms entry %s" % (room_alias,))
  54. # Options for creating auto-join rooms if they do not exist yet.
  55. self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
  56. self.autocreate_auto_join_rooms_federated = config.get(
  57. "autocreate_auto_join_rooms_federated", True
  58. )
  59. self.autocreate_auto_join_room_preset = (
  60. config.get("autocreate_auto_join_room_preset")
  61. or RoomCreationPreset.PUBLIC_CHAT
  62. )
  63. self.auto_join_room_requires_invite = self.autocreate_auto_join_room_preset in {
  64. RoomCreationPreset.PRIVATE_CHAT,
  65. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  66. }
  67. # Pull the creator/inviter from the configuration, this gets used to
  68. # send invites for invite-only rooms.
  69. mxid_localpart = config.get("auto_join_mxid_localpart")
  70. self.auto_join_user_id = None
  71. if mxid_localpart:
  72. # Convert the localpart to a full mxid.
  73. self.auto_join_user_id = UserID(
  74. mxid_localpart, self.server_name
  75. ).to_string()
  76. if self.autocreate_auto_join_rooms:
  77. # Ensure the preset is a known value.
  78. if self.autocreate_auto_join_room_preset not in {
  79. RoomCreationPreset.PUBLIC_CHAT,
  80. RoomCreationPreset.PRIVATE_CHAT,
  81. RoomCreationPreset.TRUSTED_PRIVATE_CHAT,
  82. }:
  83. raise ConfigError("Invalid value for autocreate_auto_join_room_preset")
  84. # If the preset requires invitations to be sent, ensure there's a
  85. # configured user to send them from.
  86. if self.auto_join_room_requires_invite:
  87. if not mxid_localpart:
  88. raise ConfigError(
  89. "The configuration option `auto_join_mxid_localpart` is required if "
  90. "`autocreate_auto_join_room_preset` is set to private_chat or trusted_private_chat, such that "
  91. "Synapse knows who to send invitations from. Please "
  92. "configure `auto_join_mxid_localpart`."
  93. )
  94. self.auto_join_rooms_for_guests = config.get("auto_join_rooms_for_guests", True)
  95. self.enable_set_displayname = config.get("enable_set_displayname", True)
  96. self.enable_set_avatar_url = config.get("enable_set_avatar_url", True)
  97. self.enable_3pid_changes = config.get("enable_3pid_changes", True)
  98. self.disable_msisdn_registration = config.get(
  99. "disable_msisdn_registration", False
  100. )
  101. session_lifetime = config.get("session_lifetime")
  102. if session_lifetime is not None:
  103. session_lifetime = self.parse_duration(session_lifetime)
  104. self.session_lifetime = session_lifetime
  105. # The success template used during fallback auth.
  106. self.fallback_success_template = self.read_template("auth_success.html")
  107. def generate_config_section(self, generate_secrets=False, **kwargs):
  108. if generate_secrets:
  109. registration_shared_secret = 'registration_shared_secret: "%s"' % (
  110. random_string_with_symbols(50),
  111. )
  112. else:
  113. registration_shared_secret = "#registration_shared_secret: <PRIVATE STRING>"
  114. return (
  115. """\
  116. ## Registration ##
  117. #
  118. # Registration can be rate-limited using the parameters in the "Ratelimiting"
  119. # section of this file.
  120. # Enable registration for new users.
  121. #
  122. #enable_registration: false
  123. # Time that a user's session remains valid for, after they log in.
  124. #
  125. # Note that this is not currently compatible with guest logins.
  126. #
  127. # Note also that this is calculated at login time: changes are not applied
  128. # retrospectively to users who have already logged in.
  129. #
  130. # By default, this is infinite.
  131. #
  132. #session_lifetime: 24h
  133. # The user must provide all of the below types of 3PID when registering.
  134. #
  135. #registrations_require_3pid:
  136. # - email
  137. # - msisdn
  138. # Explicitly disable asking for MSISDNs from the registration
  139. # flow (overrides registrations_require_3pid if MSISDNs are set as required)
  140. #
  141. #disable_msisdn_registration: true
  142. # Mandate that users are only allowed to associate certain formats of
  143. # 3PIDs with accounts on this server.
  144. #
  145. #allowed_local_3pids:
  146. # - medium: email
  147. # pattern: '^[^@]+@matrix\\.org$'
  148. # - medium: email
  149. # pattern: '^[^@]+@vector\\.im$'
  150. # - medium: msisdn
  151. # pattern: '\\+44'
  152. # Enable 3PIDs lookup requests to identity servers from this server.
  153. #
  154. #enable_3pid_lookup: true
  155. # If set, allows registration of standard or admin accounts by anyone who
  156. # has the shared secret, even if registration is otherwise disabled.
  157. #
  158. %(registration_shared_secret)s
  159. # Set the number of bcrypt rounds used to generate password hash.
  160. # Larger numbers increase the work factor needed to generate the hash.
  161. # The default number is 12 (which equates to 2^12 rounds).
  162. # N.B. that increasing this will exponentially increase the time required
  163. # to register or login - e.g. 24 => 2^24 rounds which will take >20 mins.
  164. #
  165. #bcrypt_rounds: 12
  166. # Allows users to register as guests without a password/email/etc, and
  167. # participate in rooms hosted on this server which have been made
  168. # accessible to anonymous users.
  169. #
  170. #allow_guest_access: false
  171. # The identity server which we suggest that clients should use when users log
  172. # in on this server.
  173. #
  174. # (By default, no suggestion is made, so it is left up to the client.
  175. # This setting is ignored unless public_baseurl is also set.)
  176. #
  177. #default_identity_server: https://matrix.org
  178. # Handle threepid (email/phone etc) registration and password resets through a set of
  179. # *trusted* identity servers. Note that this allows the configured identity server to
  180. # reset passwords for accounts!
  181. #
  182. # Be aware that if `email` is not set, and SMTP options have not been
  183. # configured in the email config block, registration and user password resets via
  184. # email will be globally disabled.
  185. #
  186. # Additionally, if `msisdn` is not set, registration and password resets via msisdn
  187. # will be disabled regardless, and users will not be able to associate an msisdn
  188. # identifier to their account. This is due to Synapse currently not supporting
  189. # any method of sending SMS messages on its own.
  190. #
  191. # To enable using an identity server for operations regarding a particular third-party
  192. # identifier type, set the value to the URL of that identity server as shown in the
  193. # examples below.
  194. #
  195. # Servers handling the these requests must answer the `/requestToken` endpoints defined
  196. # by the Matrix Identity Service API specification:
  197. # https://matrix.org/docs/spec/identity_service/latest
  198. #
  199. # If a delegate is specified, the config option public_baseurl must also be filled out.
  200. #
  201. account_threepid_delegates:
  202. #email: https://example.com # Delegate email sending to example.com
  203. #msisdn: http://localhost:8090 # Delegate SMS sending to this local process
  204. # Whether users are allowed to change their displayname after it has
  205. # been initially set. Useful when provisioning users based on the
  206. # contents of a third-party directory.
  207. #
  208. # Does not apply to server administrators. Defaults to 'true'
  209. #
  210. #enable_set_displayname: false
  211. # Whether users are allowed to change their avatar after it has been
  212. # initially set. Useful when provisioning users based on the contents
  213. # of a third-party directory.
  214. #
  215. # Does not apply to server administrators. Defaults to 'true'
  216. #
  217. #enable_set_avatar_url: false
  218. # Whether users can change the 3PIDs associated with their accounts
  219. # (email address and msisdn).
  220. #
  221. # Defaults to 'true'
  222. #
  223. #enable_3pid_changes: false
  224. # Users who register on this homeserver will automatically be joined
  225. # to these rooms.
  226. #
  227. # By default, any room aliases included in this list will be created
  228. # as a publicly joinable room when the first user registers for the
  229. # homeserver. This behaviour can be customised with the settings below.
  230. # If the room already exists, make certain it is a publicly joinable
  231. # room. The join rule of the room must be set to 'public'.
  232. #
  233. #auto_join_rooms:
  234. # - "#example:example.com"
  235. # Where auto_join_rooms are specified, setting this flag ensures that the
  236. # the rooms exist by creating them when the first user on the
  237. # homeserver registers.
  238. #
  239. # By default the auto-created rooms are publicly joinable from any federated
  240. # server. Use the autocreate_auto_join_rooms_federated and
  241. # autocreate_auto_join_room_preset settings below to customise this behaviour.
  242. #
  243. # Setting to false means that if the rooms are not manually created,
  244. # users cannot be auto-joined since they do not exist.
  245. #
  246. # Defaults to true. Uncomment the following line to disable automatically
  247. # creating auto-join rooms.
  248. #
  249. #autocreate_auto_join_rooms: false
  250. # Whether the auto_join_rooms that are auto-created are available via
  251. # federation. Only has an effect if autocreate_auto_join_rooms is true.
  252. #
  253. # Note that whether a room is federated cannot be modified after
  254. # creation.
  255. #
  256. # Defaults to true: the room will be joinable from other servers.
  257. # Uncomment the following to prevent users from other homeservers from
  258. # joining these rooms.
  259. #
  260. #autocreate_auto_join_rooms_federated: false
  261. # The room preset to use when auto-creating one of auto_join_rooms. Only has an
  262. # effect if autocreate_auto_join_rooms is true.
  263. #
  264. # This can be one of "public_chat", "private_chat", or "trusted_private_chat".
  265. # If a value of "private_chat" or "trusted_private_chat" is used then
  266. # auto_join_mxid_localpart must also be configured.
  267. #
  268. # Defaults to "public_chat", meaning that the room is joinable by anyone, including
  269. # federated servers if autocreate_auto_join_rooms_federated is true (the default).
  270. # Uncomment the following to require an invitation to join these rooms.
  271. #
  272. #autocreate_auto_join_room_preset: private_chat
  273. # The local part of the user id which is used to create auto_join_rooms if
  274. # autocreate_auto_join_rooms is true. If this is not provided then the
  275. # initial user account that registers will be used to create the rooms.
  276. #
  277. # The user id is also used to invite new users to any auto-join rooms which
  278. # are set to invite-only.
  279. #
  280. # It *must* be configured if autocreate_auto_join_room_preset is set to
  281. # "private_chat" or "trusted_private_chat".
  282. #
  283. # Note that this must be specified in order for new users to be correctly
  284. # invited to any auto-join rooms which have been set to invite-only (either
  285. # at the time of creation or subsequently).
  286. #
  287. # Note that, if the room already exists, this user must be joined and
  288. # have the appropriate permissions to invite new members.
  289. #
  290. #auto_join_mxid_localpart: system
  291. # When auto_join_rooms is specified, setting this flag to false prevents
  292. # guest accounts from being automatically joined to the rooms.
  293. #
  294. # Defaults to true.
  295. #
  296. #auto_join_rooms_for_guests: false
  297. """
  298. % locals()
  299. )
  300. @staticmethod
  301. def add_arguments(parser):
  302. reg_group = parser.add_argument_group("registration")
  303. reg_group.add_argument(
  304. "--enable-registration",
  305. action="store_true",
  306. default=None,
  307. help="Enable registration for new users.",
  308. )
  309. def read_arguments(self, args):
  310. if args.enable_registration is not None:
  311. self.enable_registration = bool(strtobool(str(args.enable_registration)))