registration.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.util.stringutils import random_string_with_symbols
  17. from ._base import Config
  18. class RegistrationConfig(Config):
  19. def read_config(self, config):
  20. self.enable_registration = bool(
  21. strtobool(str(config["enable_registration"]))
  22. )
  23. if "disable_registration" in config:
  24. self.enable_registration = not bool(
  25. strtobool(str(config["disable_registration"]))
  26. )
  27. self.registrations_require_3pid = config.get("registrations_require_3pid", [])
  28. self.allowed_local_3pids = config.get("allowed_local_3pids", [])
  29. self.registration_shared_secret = config.get("registration_shared_secret")
  30. self.bcrypt_rounds = config.get("bcrypt_rounds", 12)
  31. self.trusted_third_party_id_servers = config["trusted_third_party_id_servers"]
  32. self.allow_guest_access = config.get("allow_guest_access", False)
  33. self.invite_3pid_guest = (
  34. self.allow_guest_access and config.get("invite_3pid_guest", False)
  35. )
  36. self.auto_join_rooms = config.get("auto_join_rooms", [])
  37. def default_config(self, **kwargs):
  38. registration_shared_secret = random_string_with_symbols(50)
  39. return """\
  40. ## Registration ##
  41. # Enable registration for new users.
  42. enable_registration: False
  43. # The user must provide all of the below types of 3PID when registering.
  44. #
  45. # registrations_require_3pid:
  46. # - email
  47. # - msisdn
  48. # Mandate that users are only allowed to associate certain formats of
  49. # 3PIDs with accounts on this server.
  50. #
  51. # allowed_local_3pids:
  52. # - medium: email
  53. # pattern: ".*@matrix\\.org"
  54. # - medium: email
  55. # pattern: ".*@vector\\.im"
  56. # - medium: msisdn
  57. # pattern: "\\+44"
  58. # If set, allows registration by anyone who also has the shared
  59. # secret, even if registration is otherwise disabled.
  60. registration_shared_secret: "%(registration_shared_secret)s"
  61. # Set the number of bcrypt rounds used to generate password hash.
  62. # Larger numbers increase the work factor needed to generate the hash.
  63. # The default number is 12 (which equates to 2^12 rounds).
  64. # N.B. that increasing this will exponentially increase the time required
  65. # to register or login - e.g. 24 => 2^24 rounds which will take >20 mins.
  66. bcrypt_rounds: 12
  67. # Allows users to register as guests without a password/email/etc, and
  68. # participate in rooms hosted on this server which have been made
  69. # accessible to anonymous users.
  70. allow_guest_access: False
  71. # The list of identity servers trusted to verify third party
  72. # identifiers by this server.
  73. trusted_third_party_id_servers:
  74. - matrix.org
  75. - vector.im
  76. - riot.im
  77. # Users who register on this homeserver will automatically be joined
  78. # to these rooms
  79. #auto_join_rooms:
  80. # - "#example:example.com"
  81. """ % locals()
  82. def add_arguments(self, parser):
  83. reg_group = parser.add_argument_group("registration")
  84. reg_group.add_argument(
  85. "--enable-registration", action="store_true", default=None,
  86. help="Enable registration for new users."
  87. )
  88. def read_arguments(self, args):
  89. if args.enable_registration is not None:
  90. self.enable_registration = bool(
  91. strtobool(str(args.enable_registration))
  92. )