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.
 
 
 
 
 
 

92 rivejä
3.3 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, ConfigError
  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_protocol_version = cas_config.get("protocol_version")
  34. if (
  35. self.cas_protocol_version is not None
  36. and self.cas_protocol_version not in [1, 2, 3]
  37. ):
  38. raise ConfigError(
  39. "Unsupported CAS protocol version %s (only versions 1, 2, 3 are supported)"
  40. % (self.cas_protocol_version,),
  41. ("cas_config", "protocol_version"),
  42. )
  43. self.cas_displayname_attribute = cas_config.get("displayname_attribute")
  44. required_attributes = cas_config.get("required_attributes") or {}
  45. self.cas_required_attributes = _parsed_required_attributes_def(
  46. required_attributes
  47. )
  48. self.cas_enable_registration = cas_config.get("enable_registration", True)
  49. self.idp_name = cas_config.get("idp_name", "CAS")
  50. self.idp_icon = cas_config.get("idp_icon")
  51. self.idp_brand = cas_config.get("idp_brand")
  52. else:
  53. self.cas_server_url = None
  54. self.cas_service_url = None
  55. self.cas_protocol_version = None
  56. self.cas_displayname_attribute = None
  57. self.cas_required_attributes = []
  58. self.cas_enable_registration = False
  59. # CAS uses a legacy required attributes mapping, not the one provided by
  60. # SsoAttributeRequirement.
  61. REQUIRED_ATTRIBUTES_SCHEMA = {
  62. "type": "object",
  63. "additionalProperties": {"anyOf": [{"type": "string"}, {"type": "null"}]},
  64. }
  65. def _parsed_required_attributes_def(
  66. required_attributes: Any,
  67. ) -> List[SsoAttributeRequirement]:
  68. validate_config(
  69. REQUIRED_ATTRIBUTES_SCHEMA,
  70. required_attributes,
  71. config_path=("cas_config", "required_attributes"),
  72. )
  73. return [SsoAttributeRequirement(k, v) for k, v in required_attributes.items()]