password_policy.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # -*- coding: utf-8 -*-
  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. from synapse.http.servlet import RestServlet
  17. from ._base import client_patterns
  18. logger = logging.getLogger(__name__)
  19. class PasswordPolicyServlet(RestServlet):
  20. PATTERNS = client_patterns("/password_policy$")
  21. def __init__(self, hs):
  22. """
  23. Args:
  24. hs (synapse.server.HomeServer): server
  25. """
  26. super().__init__()
  27. self.policy = hs.config.password_policy
  28. self.enabled = hs.config.password_policy_enabled
  29. def on_GET(self, request):
  30. if not self.enabled or not self.policy:
  31. return (200, {})
  32. policy = {}
  33. for param in [
  34. "minimum_length",
  35. "require_digit",
  36. "require_symbol",
  37. "require_lowercase",
  38. "require_uppercase",
  39. ]:
  40. if param in self.policy:
  41. policy["m.%s" % param] = self.policy[param]
  42. return (200, policy)
  43. def register_servlets(hs, http_server):
  44. PasswordPolicyServlet(hs).register(http_server)