sso.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright 2020 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 ._base import Config
  18. logger = logging.getLogger(__name__)
  19. LEGACY_TEMPLATE_DIR_WARNING = """
  20. This server's configuration file is using the deprecated 'template_dir' setting in the
  21. 'sso' section. Support for this setting has been deprecated and will be removed in a
  22. future version of Synapse. Server admins should instead use the new
  23. 'custom_templates_directory' setting documented here:
  24. https://matrix-org.github.io/synapse/latest/templates.html
  25. ---------------------------------------------------------------------------------------"""
  26. @attr.s(frozen=True)
  27. class SsoAttributeRequirement:
  28. """Object describing a single requirement for SSO attributes."""
  29. attribute = attr.ib(type=str)
  30. # If a value is not given, than the attribute must simply exist.
  31. value = attr.ib(type=Optional[str])
  32. JSON_SCHEMA = {
  33. "type": "object",
  34. "properties": {"attribute": {"type": "string"}, "value": {"type": "string"}},
  35. "required": ["attribute", "value"],
  36. }
  37. class SSOConfig(Config):
  38. """SSO Configuration"""
  39. section = "sso"
  40. def read_config(self, config, **kwargs):
  41. sso_config: Dict[str, Any] = config.get("sso") or {}
  42. # The sso-specific template_dir
  43. self.sso_template_dir = sso_config.get("template_dir")
  44. if self.sso_template_dir is not None:
  45. logger.warning(LEGACY_TEMPLATE_DIR_WARNING)
  46. # Read templates from disk
  47. custom_template_directories = (
  48. self.root.server.custom_template_directory,
  49. self.sso_template_dir,
  50. )
  51. (
  52. self.sso_login_idp_picker_template,
  53. self.sso_redirect_confirm_template,
  54. self.sso_auth_confirm_template,
  55. self.sso_error_template,
  56. sso_account_deactivated_template,
  57. sso_auth_success_template,
  58. self.sso_auth_bad_user_template,
  59. ) = self.read_templates(
  60. [
  61. "sso_login_idp_picker.html",
  62. "sso_redirect_confirm.html",
  63. "sso_auth_confirm.html",
  64. "sso_error.html",
  65. "sso_account_deactivated.html",
  66. "sso_auth_success.html",
  67. "sso_auth_bad_user.html",
  68. ],
  69. (td for td in custom_template_directories if td),
  70. )
  71. # These templates have no placeholders, so render them here
  72. self.sso_account_deactivated_template = (
  73. sso_account_deactivated_template.render()
  74. )
  75. self.sso_auth_success_template = sso_auth_success_template.render()
  76. self.sso_client_whitelist = sso_config.get("client_whitelist") or []
  77. self.sso_update_profile_information = (
  78. sso_config.get("update_profile_information") or False
  79. )
  80. # Attempt to also whitelist the server's login fallback, since that fallback sets
  81. # the redirect URL to itself (so it can process the login token then return
  82. # gracefully to the client). This would make it pointless to ask the user for
  83. # confirmation, since the URL the confirmation page would be showing wouldn't be
  84. # the client's.
  85. # public_baseurl is an optional setting, so we only add the fallback's URL to the
  86. # list if it's provided (because we can't figure out what that URL is otherwise).
  87. if self.root.server.public_baseurl:
  88. login_fallback_url = (
  89. self.root.server.public_baseurl + "_matrix/static/client/login"
  90. )
  91. self.sso_client_whitelist.append(login_fallback_url)
  92. def generate_config_section(self, **kwargs):
  93. return """\
  94. # Additional settings to use with single-sign on systems such as OpenID Connect,
  95. # SAML2 and CAS.
  96. #
  97. # Server admins can configure custom templates for pages related to SSO. See
  98. # https://matrix-org.github.io/synapse/latest/templates.html for more information.
  99. #
  100. sso:
  101. # A list of client URLs which are whitelisted so that the user does not
  102. # have to confirm giving access to their account to the URL. Any client
  103. # whose URL starts with an entry in the following list will not be subject
  104. # to an additional confirmation step after the SSO login is completed.
  105. #
  106. # WARNING: An entry such as "https://my.client" is insecure, because it
  107. # will also match "https://my.client.evil.site", exposing your users to
  108. # phishing attacks from evil.site. To avoid this, include a slash after the
  109. # hostname: "https://my.client/".
  110. #
  111. # If public_baseurl is set, then the login fallback page (used by clients
  112. # that don't natively support the required login flows) is whitelisted in
  113. # addition to any URLs in this list.
  114. #
  115. # By default, this list is empty.
  116. #
  117. #client_whitelist:
  118. # - https://riot.im/develop
  119. # - https://my.custom.client/
  120. # Uncomment to keep a user's profile fields in sync with information from
  121. # the identity provider. Currently only syncing the displayname is
  122. # supported. Fields are checked on every SSO login, and are updated
  123. # if necessary.
  124. #
  125. # Note that enabling this option will override user profile information,
  126. # regardless of whether users have opted-out of syncing that
  127. # information when first signing in. Defaults to false.
  128. #
  129. #update_profile_information: true
  130. """