auth.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2020 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. from typing import Any
  16. from synapse.types import JsonDict
  17. from ._base import Config
  18. class AuthConfig(Config):
  19. """Password and login configuration"""
  20. section = "auth"
  21. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  22. password_config = config.get("password_config", {})
  23. if password_config is None:
  24. password_config = {}
  25. passwords_enabled = password_config.get("enabled", True)
  26. # 'only_for_reauth' allows users who have previously set a password to use it,
  27. # even though passwords would otherwise be disabled.
  28. passwords_for_reauth_only = passwords_enabled == "only_for_reauth"
  29. self.password_enabled_for_login = (
  30. passwords_enabled and not passwords_for_reauth_only
  31. )
  32. self.password_enabled_for_reauth = (
  33. passwords_for_reauth_only or passwords_enabled
  34. )
  35. self.password_localdb_enabled = password_config.get("localdb_enabled", True)
  36. self.password_pepper = password_config.get("pepper", "")
  37. # Password policy
  38. self.password_policy = password_config.get("policy") or {}
  39. self.password_policy_enabled = self.password_policy.get("enabled", False)
  40. # User-interactive authentication
  41. ui_auth = config.get("ui_auth") or {}
  42. self.ui_auth_session_timeout = self.parse_duration(
  43. ui_auth.get("session_timeout", 0)
  44. )
  45. def generate_config_section(self, **kwargs: Any) -> str:
  46. return """\
  47. password_config:
  48. # Uncomment to disable password login.
  49. # Set to `only_for_reauth` to permit reauthentication for users that
  50. # have passwords and are already logged in.
  51. #
  52. #enabled: false
  53. # Uncomment to disable authentication against the local password
  54. # database. This is ignored if `enabled` is false, and is only useful
  55. # if you have other password_providers.
  56. #
  57. #localdb_enabled: false
  58. # Uncomment and change to a secret random string for extra security.
  59. # DO NOT CHANGE THIS AFTER INITIAL SETUP!
  60. #
  61. #pepper: "EVEN_MORE_SECRET"
  62. # Define and enforce a password policy. Each parameter is optional.
  63. # This is an implementation of MSC2000.
  64. #
  65. policy:
  66. # Whether to enforce the password policy.
  67. # Defaults to 'false'.
  68. #
  69. #enabled: true
  70. # Minimum accepted length for a password.
  71. # Defaults to 0.
  72. #
  73. #minimum_length: 15
  74. # Whether a password must contain at least one digit.
  75. # Defaults to 'false'.
  76. #
  77. #require_digit: true
  78. # Whether a password must contain at least one symbol.
  79. # A symbol is any character that's not a number or a letter.
  80. # Defaults to 'false'.
  81. #
  82. #require_symbol: true
  83. # Whether a password must contain at least one lowercase letter.
  84. # Defaults to 'false'.
  85. #
  86. #require_lowercase: true
  87. # Whether a password must contain at least one uppercase letter.
  88. # Defaults to 'false'.
  89. #
  90. #require_uppercase: true
  91. ui_auth:
  92. # The amount of time to allow a user-interactive authentication session
  93. # to be active.
  94. #
  95. # This defaults to 0, meaning the user is queried for their credentials
  96. # before every action, but this can be overridden to allow a single
  97. # validation to be re-used. This weakens the protections afforded by
  98. # the user-interactive authentication process, by allowing for multiple
  99. # (and potentially different) operations to use the same validation session.
  100. #
  101. # This is ignored for potentially "dangerous" operations (including
  102. # deactivating an account, modifying an account password, and
  103. # adding a 3PID).
  104. #
  105. # Uncomment below to allow for credential validation to last for 15
  106. # seconds.
  107. #
  108. #session_timeout: "15s"
  109. """