No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

97 líneas
3.3 KiB

  1. # Copyright 2019 New Vector Ltd
  2. # Copyright 2019 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. import logging
  16. import re
  17. from typing import TYPE_CHECKING
  18. from synapse.api.errors import Codes, PasswordRefusedError
  19. if TYPE_CHECKING:
  20. from synapse.server import HomeServer
  21. logger = logging.getLogger(__name__)
  22. class PasswordPolicyHandler:
  23. def __init__(self, hs: "HomeServer"):
  24. self.policy = hs.config.auth.password_policy
  25. self.enabled = hs.config.auth.password_policy_enabled
  26. # Regexps for the spec'd policy parameters.
  27. self.regexp_digit = re.compile("[0-9]")
  28. self.regexp_symbol = re.compile("[^a-zA-Z0-9]")
  29. self.regexp_uppercase = re.compile("[A-Z]")
  30. self.regexp_lowercase = re.compile("[a-z]")
  31. def validate_password(self, password: str) -> None:
  32. """Checks whether a given password complies with the server's policy.
  33. Args:
  34. password: The password to check against the server's policy.
  35. Raises:
  36. PasswordRefusedError: The password doesn't comply with the server's policy.
  37. """
  38. if not self.enabled:
  39. return
  40. minimum_accepted_length = self.policy.get("minimum_length", 0)
  41. if len(password) < minimum_accepted_length:
  42. raise PasswordRefusedError(
  43. msg=(
  44. "The password must be at least %d characters long"
  45. % minimum_accepted_length
  46. ),
  47. errcode=Codes.PASSWORD_TOO_SHORT,
  48. )
  49. if (
  50. self.policy.get("require_digit", False)
  51. and self.regexp_digit.search(password) is None
  52. ):
  53. raise PasswordRefusedError(
  54. msg="The password must include at least one digit",
  55. errcode=Codes.PASSWORD_NO_DIGIT,
  56. )
  57. if (
  58. self.policy.get("require_symbol", False)
  59. and self.regexp_symbol.search(password) is None
  60. ):
  61. raise PasswordRefusedError(
  62. msg="The password must include at least one symbol",
  63. errcode=Codes.PASSWORD_NO_SYMBOL,
  64. )
  65. if (
  66. self.policy.get("require_uppercase", False)
  67. and self.regexp_uppercase.search(password) is None
  68. ):
  69. raise PasswordRefusedError(
  70. msg="The password must include at least one uppercase letter",
  71. errcode=Codes.PASSWORD_NO_UPPERCASE,
  72. )
  73. if (
  74. self.policy.get("require_lowercase", False)
  75. and self.regexp_lowercase.search(password) is None
  76. ):
  77. raise PasswordRefusedError(
  78. msg="The password must include at least one lowercase letter",
  79. errcode=Codes.PASSWORD_NO_LOWERCASE,
  80. )