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.
 
 
 
 
 
 

110 line
3.9 KiB

  1. # Copyright 2020-2021 The Matrix.org Foundation C.I.C.
  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. import logging
  15. from typing import Any, Dict, Optional
  16. import attr
  17. from synapse.types import JsonDict
  18. from ._base import Config
  19. logger = logging.getLogger(__name__)
  20. LEGACY_TEMPLATE_DIR_WARNING = """
  21. This server's configuration file is using the deprecated 'template_dir' setting in the
  22. 'sso' section. Support for this setting has been deprecated and will be removed in a
  23. future version of Synapse. Server admins should instead use the new
  24. 'custom_template_directory' setting documented here:
  25. https://matrix-org.github.io/synapse/latest/templates.html
  26. ---------------------------------------------------------------------------------------"""
  27. @attr.s(frozen=True, auto_attribs=True)
  28. class SsoAttributeRequirement:
  29. """Object describing a single requirement for SSO attributes."""
  30. attribute: str
  31. # If a value is not given, than the attribute must simply exist.
  32. value: Optional[str]
  33. JSON_SCHEMA = {
  34. "type": "object",
  35. "properties": {"attribute": {"type": "string"}, "value": {"type": "string"}},
  36. "required": ["attribute", "value"],
  37. }
  38. class SSOConfig(Config):
  39. """SSO Configuration"""
  40. section = "sso"
  41. def read_config(self, config: JsonDict, **kwargs: Any) -> None:
  42. sso_config: Dict[str, Any] = config.get("sso") or {}
  43. # The sso-specific template_dir
  44. self.sso_template_dir = sso_config.get("template_dir")
  45. if self.sso_template_dir is not None:
  46. logger.warning(LEGACY_TEMPLATE_DIR_WARNING)
  47. # Read templates from disk
  48. custom_template_directories = (
  49. self.root.server.custom_template_directory,
  50. self.sso_template_dir,
  51. )
  52. (
  53. self.sso_login_idp_picker_template,
  54. self.sso_redirect_confirm_template,
  55. self.sso_auth_confirm_template,
  56. self.sso_error_template,
  57. sso_account_deactivated_template,
  58. sso_auth_success_template,
  59. self.sso_auth_bad_user_template,
  60. ) = self.read_templates(
  61. [
  62. "sso_login_idp_picker.html",
  63. "sso_redirect_confirm.html",
  64. "sso_auth_confirm.html",
  65. "sso_error.html",
  66. "sso_account_deactivated.html",
  67. "sso_auth_success.html",
  68. "sso_auth_bad_user.html",
  69. ],
  70. (td for td in custom_template_directories if td),
  71. )
  72. # These templates have no placeholders, so render them here
  73. self.sso_account_deactivated_template = (
  74. sso_account_deactivated_template.render()
  75. )
  76. self.sso_auth_success_template = sso_auth_success_template.render()
  77. self.sso_client_whitelist = sso_config.get("client_whitelist") or []
  78. self.sso_update_profile_information = (
  79. sso_config.get("update_profile_information") or False
  80. )
  81. # Attempt to also whitelist the server's login fallback, since that fallback sets
  82. # the redirect URL to itself (so it can process the login token then return
  83. # gracefully to the client). This would make it pointless to ask the user for
  84. # confirmation, since the URL the confirmation page would be showing wouldn't be
  85. # the client's.
  86. login_fallback_url = (
  87. self.root.server.public_baseurl + "_matrix/static/client/login"
  88. )
  89. self.sso_client_whitelist.append(login_fallback_url)