1
0

password_auth_providers.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Openmarket
  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, List
  16. from synapse.util.module_loader import load_module
  17. from ._base import Config
  18. LDAP_PROVIDER = "ldap_auth_provider.LdapAuthProvider"
  19. class PasswordAuthProviderConfig(Config):
  20. section = "authproviders"
  21. def read_config(self, config, **kwargs):
  22. self.password_providers = [] # type: List[Any]
  23. providers = []
  24. # We want to be backwards compatible with the old `ldap_config`
  25. # param.
  26. ldap_config = config.get("ldap_config", {})
  27. if ldap_config.get("enabled", False):
  28. providers.append({"module": LDAP_PROVIDER, "config": ldap_config})
  29. providers.extend(config.get("password_providers", []))
  30. for provider in providers:
  31. mod_name = provider["module"]
  32. # This is for backwards compat when the ldap auth provider resided
  33. # in this package.
  34. if mod_name == "synapse.util.ldap_auth_provider.LdapAuthProvider":
  35. mod_name = LDAP_PROVIDER
  36. (provider_class, provider_config) = load_module(
  37. {"module": mod_name, "config": provider["config"]}
  38. )
  39. self.password_providers.append((provider_class, provider_config))
  40. def generate_config_section(self, **kwargs):
  41. return """\
  42. #password_providers:
  43. # - module: "ldap_auth_provider.LdapAuthProvider"
  44. # config:
  45. # enabled: true
  46. # uri: "ldap://ldap.example.com:389"
  47. # start_tls: true
  48. # base: "ou=users,dc=example,dc=com"
  49. # attributes:
  50. # uid: "cn"
  51. # mail: "email"
  52. # name: "givenName"
  53. # #bind_dn:
  54. # #bind_password:
  55. # #filter: "(objectClass=posixAccount)"
  56. """