auth.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. # Copyright 2020 The Matrix.org Foundation C.I.C.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from ._base import Config
  17. class AuthConfig(Config):
  18. """Password and login configuration"""
  19. section = "auth"
  20. def read_config(self, config, **kwargs):
  21. password_config = config.get("password_config", {})
  22. if password_config is None:
  23. password_config = {}
  24. self.password_enabled = password_config.get("enabled", True)
  25. self.password_localdb_enabled = password_config.get("localdb_enabled", True)
  26. self.password_pepper = password_config.get("pepper", "")
  27. # Password policy
  28. self.password_policy = password_config.get("policy") or {}
  29. self.password_policy_enabled = self.password_policy.get("enabled", False)
  30. # User-interactive authentication
  31. ui_auth = config.get("ui_auth") or {}
  32. self.ui_auth_session_timeout = self.parse_duration(
  33. ui_auth.get("session_timeout", 0)
  34. )
  35. def generate_config_section(self, config_dir_path, server_name, **kwargs):
  36. return """\
  37. password_config:
  38. # Uncomment to disable password login
  39. #
  40. #enabled: false
  41. # Uncomment to disable authentication against the local password
  42. # database. This is ignored if `enabled` is false, and is only useful
  43. # if you have other password_providers.
  44. #
  45. #localdb_enabled: false
  46. # Uncomment and change to a secret random string for extra security.
  47. # DO NOT CHANGE THIS AFTER INITIAL SETUP!
  48. #
  49. #pepper: "EVEN_MORE_SECRET"
  50. # Define and enforce a password policy. Each parameter is optional.
  51. # This is an implementation of MSC2000.
  52. #
  53. policy:
  54. # Whether to enforce the password policy.
  55. # Defaults to 'false'.
  56. #
  57. #enabled: true
  58. # Minimum accepted length for a password.
  59. # Defaults to 0.
  60. #
  61. #minimum_length: 15
  62. # Whether a password must contain at least one digit.
  63. # Defaults to 'false'.
  64. #
  65. #require_digit: true
  66. # Whether a password must contain at least one symbol.
  67. # A symbol is any character that's not a number or a letter.
  68. # Defaults to 'false'.
  69. #
  70. #require_symbol: true
  71. # Whether a password must contain at least one lowercase letter.
  72. # Defaults to 'false'.
  73. #
  74. #require_lowercase: true
  75. # Whether a password must contain at least one lowercase letter.
  76. # Defaults to 'false'.
  77. #
  78. #require_uppercase: true
  79. ui_auth:
  80. # The amount of time to allow a user-interactive authentication session
  81. # to be active.
  82. #
  83. # This defaults to 0, meaning the user is queried for their credentials
  84. # before every action, but this can be overridden to allow a single
  85. # validation to be re-used. This weakens the protections afforded by
  86. # the user-interactive authentication process, by allowing for multiple
  87. # (and potentially different) operations to use the same validation session.
  88. #
  89. # Uncomment below to allow for credential validation to last for 15
  90. # seconds.
  91. #
  92. #session_timeout: "15s"
  93. """