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