password_auth_providers.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Copyright 2016 Openmarket
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Any, List, Tuple, Type
  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. section = "authproviders"
  20. def read_config(self, config, **kwargs):
  21. """Parses the old password auth providers config. The config format looks like this:
  22. password_providers:
  23. # Example config for an LDAP auth provider
  24. - module: "ldap_auth_provider.LdapAuthProvider"
  25. config:
  26. enabled: true
  27. uri: "ldap://ldap.example.com:389"
  28. start_tls: true
  29. base: "ou=users,dc=example,dc=com"
  30. attributes:
  31. uid: "cn"
  32. mail: "email"
  33. name: "givenName"
  34. #bind_dn:
  35. #bind_password:
  36. #filter: "(objectClass=posixAccount)"
  37. We expect admins to use modules for this feature (which is why it doesn't appear
  38. in the sample config file), but we want to keep support for it around for a bit
  39. for backwards compatibility.
  40. """
  41. self.password_providers: List[Tuple[Type, Any]] = []
  42. providers = []
  43. # We want to be backwards compatible with the old `ldap_config`
  44. # param.
  45. ldap_config = config.get("ldap_config", {})
  46. if ldap_config.get("enabled", False):
  47. providers.append({"module": LDAP_PROVIDER, "config": ldap_config})
  48. providers.extend(config.get("password_providers") or [])
  49. for i, provider in enumerate(providers):
  50. mod_name = provider["module"]
  51. # This is for backwards compat when the ldap auth provider resided
  52. # in this package.
  53. if mod_name == "synapse.util.ldap_auth_provider.LdapAuthProvider":
  54. mod_name = LDAP_PROVIDER
  55. (provider_class, provider_config) = load_module(
  56. {"module": mod_name, "config": provider["config"]},
  57. ("password_providers", "<item %i>" % i),
  58. )
  59. self.password_providers.append((provider_class, provider_config))