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.
 
 
 
 
 
 

107 lines
3.7 KiB

  1. # Copyright 2015, 2016 OpenMarket Ltd
  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
  15. from synapse.config.sso import SsoAttributeRequirement
  16. from ._base import Config, ConfigError
  17. from ._util import validate_config
  18. class CasConfig(Config):
  19. """Cas Configuration
  20. cas_server_url: URL of CAS server
  21. """
  22. section = "cas"
  23. def read_config(self, config, **kwargs):
  24. cas_config = config.get("cas_config", None)
  25. self.cas_enabled = cas_config and cas_config.get("enabled", True)
  26. if self.cas_enabled:
  27. self.cas_server_url = cas_config["server_url"]
  28. # The public baseurl is required because it is used by the redirect
  29. # template.
  30. public_baseurl = self.public_baseurl
  31. if not public_baseurl:
  32. raise ConfigError("cas_config requires a public_baseurl to be set")
  33. # TODO Update this to a _synapse URL.
  34. self.cas_service_url = public_baseurl + "_matrix/client/r0/login/cas/ticket"
  35. self.cas_displayname_attribute = cas_config.get("displayname_attribute")
  36. required_attributes = cas_config.get("required_attributes") or {}
  37. self.cas_required_attributes = _parsed_required_attributes_def(
  38. required_attributes
  39. )
  40. else:
  41. self.cas_server_url = None
  42. self.cas_service_url = None
  43. self.cas_displayname_attribute = None
  44. self.cas_required_attributes = []
  45. def generate_config_section(self, config_dir_path, server_name, **kwargs):
  46. return """\
  47. # Enable Central Authentication Service (CAS) for registration and login.
  48. #
  49. cas_config:
  50. # Uncomment the following to enable authorization against a CAS server.
  51. # Defaults to false.
  52. #
  53. #enabled: true
  54. # The URL of the CAS authorization endpoint.
  55. #
  56. #server_url: "https://cas-server.com"
  57. # The attribute of the CAS response to use as the display name.
  58. #
  59. # If unset, no displayname will be set.
  60. #
  61. #displayname_attribute: name
  62. # It is possible to configure Synapse to only allow logins if CAS attributes
  63. # match particular values. All of the keys in the mapping below must exist
  64. # and the values must match the given value. Alternately if the given value
  65. # is None then any value is allowed (the attribute just must exist).
  66. # All of the listed attributes must match for the login to be permitted.
  67. #
  68. #required_attributes:
  69. # userGroup: "staff"
  70. # department: None
  71. """
  72. # CAS uses a legacy required attributes mapping, not the one provided by
  73. # SsoAttributeRequirement.
  74. REQUIRED_ATTRIBUTES_SCHEMA = {
  75. "type": "object",
  76. "additionalProperties": {"anyOf": [{"type": "string"}, {"type": "null"}]},
  77. }
  78. def _parsed_required_attributes_def(
  79. required_attributes: Any,
  80. ) -> List[SsoAttributeRequirement]:
  81. validate_config(
  82. REQUIRED_ATTRIBUTES_SCHEMA,
  83. required_attributes,
  84. config_path=("cas_config", "required_attributes"),
  85. )
  86. return [SsoAttributeRequirement(k, v) for k, v in required_attributes.items()]