You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

74 regels
2.4 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  2. # Copyright 2021 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. from typing import Any, List
  16. from synapse.config.sso import SsoAttributeRequirement
  17. from synapse.types import JsonDict
  18. from ._base import Config
  19. from ._util import validate_config
  20. class CasConfig(Config):
  21. """Cas Configuration
  22. cas_server_url: URL of CAS server
  23. """
  24. section = "cas"
  25. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  26. cas_config = config.get("cas_config", None)
  27. self.cas_enabled = cas_config and cas_config.get("enabled", True)
  28. if self.cas_enabled:
  29. self.cas_server_url = cas_config["server_url"]
  30. # TODO Update this to a _synapse URL.
  31. public_baseurl = self.root.server.public_baseurl
  32. self.cas_service_url = public_baseurl + "_matrix/client/r0/login/cas/ticket"
  33. self.cas_displayname_attribute = cas_config.get("displayname_attribute")
  34. required_attributes = cas_config.get("required_attributes") or {}
  35. self.cas_required_attributes = _parsed_required_attributes_def(
  36. required_attributes
  37. )
  38. else:
  39. self.cas_server_url = None
  40. self.cas_service_url = None
  41. self.cas_displayname_attribute = None
  42. self.cas_required_attributes = []
  43. # CAS uses a legacy required attributes mapping, not the one provided by
  44. # SsoAttributeRequirement.
  45. REQUIRED_ATTRIBUTES_SCHEMA = {
  46. "type": "object",
  47. "additionalProperties": {"anyOf": [{"type": "string"}, {"type": "null"}]},
  48. }
  49. def _parsed_required_attributes_def(
  50. required_attributes: Any,
  51. ) -> List[SsoAttributeRequirement]:
  52. validate_config(
  53. REQUIRED_ATTRIBUTES_SCHEMA,
  54. required_attributes,
  55. config_path=("cas_config", "required_attributes"),
  56. )
  57. return [SsoAttributeRequirement(k, v) for k, v in required_attributes.items()]