registration.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  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. from distutils.util import strtobool
  16. from synapse.config._base import Config, ConfigError
  17. from synapse.types import RoomAlias
  18. from synapse.util.stringutils import random_string_with_symbols
  19. class RegistrationConfig(Config):
  20. def read_config(self, config):
  21. self.enable_registration = bool(
  22. strtobool(str(config["enable_registration"]))
  23. )
  24. if "disable_registration" in config:
  25. self.enable_registration = not bool(
  26. strtobool(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.registration_shared_secret = config.get("registration_shared_secret")
  31. self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
  32. self.trusted_third_party_id_servers = config["trusted_third_party_id_servers"]
  33. self.default_identity_server = config.get("default_identity_server")
  34. self.allow_guest_access = config.get("allow_guest_access", False)
  35. self.invite_3pid_guest = (
  36. self.allow_guest_access and config.get("invite_3pid_guest", False)
  37. )
  38. self.auto_join_rooms = config.get("auto_join_rooms", [])
  39. for room_alias in self.auto_join_rooms:
  40. if not RoomAlias.is_valid(room_alias):
  41. raise ConfigError('Invalid auto_join_rooms entry %s' % (room_alias,))
  42. self.autocreate_auto_join_rooms = config.get("autocreate_auto_join_rooms", True)
  43. def default_config(self, generate_secrets=False, **kwargs):
  44. if generate_secrets:
  45. registration_shared_secret = 'registration_shared_secret: "%s"' % (
  46. random_string_with_symbols(50),
  47. )
  48. else:
  49. registration_shared_secret = '# registration_shared_secret: <PRIVATE STRING>'
  50. return """\
  51. ## Registration ##
  52. # Enable registration for new users.
  53. enable_registration: False
  54. # The user must provide all of the below types of 3PID when registering.
  55. #
  56. # registrations_require_3pid:
  57. # - email
  58. # - msisdn
  59. # Mandate that users are only allowed to associate certain formats of
  60. # 3PIDs with accounts on this server.
  61. #
  62. # allowed_local_3pids:
  63. # - medium: email
  64. # pattern: ".*@matrix\\.org"
  65. # - medium: email
  66. # pattern: ".*@vector\\.im"
  67. # - medium: msisdn
  68. # pattern: "\\+44"
  69. # If set, allows registration by anyone who also has the shared
  70. # secret, even if registration is otherwise disabled.
  71. %(registration_shared_secret)s
  72. # Set the number of bcrypt rounds used to generate password hash.
  73. # Larger numbers increase the work factor needed to generate the hash.
  74. # The default number is 12 (which equates to 2^12 rounds).
  75. # N.B. that increasing this will exponentially increase the time required
  76. # to register or login - e.g. 24 => 2^24 rounds which will take >20 mins.
  77. bcrypt_rounds: 12
  78. # Allows users to register as guests without a password/email/etc, and
  79. # participate in rooms hosted on this server which have been made
  80. # accessible to anonymous users.
  81. allow_guest_access: False
  82. # The identity server which we suggest that clients should use when users log
  83. # in on this server.
  84. #
  85. # (By default, no suggestion is made, so it is left up to the client.
  86. # This setting is ignored unless public_baseurl is also set.)
  87. #
  88. # default_identity_server: https://matrix.org
  89. # The list of identity servers trusted to verify third party
  90. # identifiers by this server.
  91. #
  92. # Also defines the ID server which will be called when an account is
  93. # deactivated (one will be picked arbitrarily).
  94. trusted_third_party_id_servers:
  95. - matrix.org
  96. - vector.im
  97. # Users who register on this homeserver will automatically be joined
  98. # to these rooms
  99. #auto_join_rooms:
  100. # - "#example:example.com"
  101. # Where auto_join_rooms are specified, setting this flag ensures that the
  102. # the rooms exist by creating them when the first user on the
  103. # homeserver registers.
  104. # Setting to false means that if the rooms are not manually created,
  105. # users cannot be auto-joined since they do not exist.
  106. autocreate_auto_join_rooms: true
  107. """ % locals()
  108. def add_arguments(self, parser):
  109. reg_group = parser.add_argument_group("registration")
  110. reg_group.add_argument(
  111. "--enable-registration", action="store_true", default=None,
  112. help="Enable registration for new users."
  113. )
  114. def read_arguments(self, args):
  115. if args.enable_registration is not None:
  116. self.enable_registration = bool(
  117. strtobool(str(args.enable_registration))
  118. )