sso.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 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. import os
  16. from typing import Any, Dict
  17. import pkg_resources
  18. from ._base import Config
  19. class SSOConfig(Config):
  20. """SSO Configuration
  21. """
  22. section = "sso"
  23. def read_config(self, config, **kwargs):
  24. sso_config = config.get("sso") or {} # type: Dict[str, Any]
  25. # Pick a template directory in order of:
  26. # * The sso-specific template_dir
  27. # * /path/to/synapse/install/res/templates
  28. template_dir = sso_config.get("template_dir")
  29. if not template_dir:
  30. template_dir = pkg_resources.resource_filename("synapse", "res/templates",)
  31. self.sso_template_dir = template_dir
  32. self.sso_account_deactivated_template = self.read_file(
  33. os.path.join(self.sso_template_dir, "sso_account_deactivated.html"),
  34. "sso_account_deactivated_template",
  35. )
  36. self.sso_auth_success_template = self.read_file(
  37. os.path.join(self.sso_template_dir, "sso_auth_success.html"),
  38. "sso_auth_success_template",
  39. )
  40. self.sso_client_whitelist = sso_config.get("client_whitelist") or []
  41. # Attempt to also whitelist the server's login fallback, since that fallback sets
  42. # the redirect URL to itself (so it can process the login token then return
  43. # gracefully to the client). This would make it pointless to ask the user for
  44. # confirmation, since the URL the confirmation page would be showing wouldn't be
  45. # the client's.
  46. # public_baseurl is an optional setting, so we only add the fallback's URL to the
  47. # list if it's provided (because we can't figure out what that URL is otherwise).
  48. if self.public_baseurl:
  49. login_fallback_url = self.public_baseurl + "_matrix/static/client/login"
  50. self.sso_client_whitelist.append(login_fallback_url)
  51. def generate_config_section(self, **kwargs):
  52. return """\
  53. # Additional settings to use with single-sign on systems such as OpenID Connect,
  54. # SAML2 and CAS.
  55. #
  56. sso:
  57. # A list of client URLs which are whitelisted so that the user does not
  58. # have to confirm giving access to their account to the URL. Any client
  59. # whose URL starts with an entry in the following list will not be subject
  60. # to an additional confirmation step after the SSO login is completed.
  61. #
  62. # WARNING: An entry such as "https://my.client" is insecure, because it
  63. # will also match "https://my.client.evil.site", exposing your users to
  64. # phishing attacks from evil.site. To avoid this, include a slash after the
  65. # hostname: "https://my.client/".
  66. #
  67. # If public_baseurl is set, then the login fallback page (used by clients
  68. # that don't natively support the required login flows) is whitelisted in
  69. # addition to any URLs in this list.
  70. #
  71. # By default, this list is empty.
  72. #
  73. #client_whitelist:
  74. # - https://riot.im/develop
  75. # - https://my.custom.client/
  76. # Directory in which Synapse will try to find the template files below.
  77. # If not set, default templates from within the Synapse package will be used.
  78. #
  79. # DO NOT UNCOMMENT THIS SETTING unless you want to customise the templates.
  80. # If you *do* uncomment it, you will need to make sure that all the templates
  81. # below are in the directory.
  82. #
  83. # Synapse will look for the following templates in this directory:
  84. #
  85. # * HTML page for a confirmation step before redirecting back to the client
  86. # with the login token: 'sso_redirect_confirm.html'.
  87. #
  88. # When rendering, this template is given three variables:
  89. # * redirect_url: the URL the user is about to be redirected to. Needs
  90. # manual escaping (see
  91. # https://jinja.palletsprojects.com/en/2.11.x/templates/#html-escaping).
  92. #
  93. # * display_url: the same as `redirect_url`, but with the query
  94. # parameters stripped. The intention is to have a
  95. # human-readable URL to show to users, not to use it as
  96. # the final address to redirect to. Needs manual escaping
  97. # (see https://jinja.palletsprojects.com/en/2.11.x/templates/#html-escaping).
  98. #
  99. # * server_name: the homeserver's name.
  100. #
  101. # * HTML page which notifies the user that they are authenticating to confirm
  102. # an operation on their account during the user interactive authentication
  103. # process: 'sso_auth_confirm.html'.
  104. #
  105. # When rendering, this template is given the following variables:
  106. # * redirect_url: the URL the user is about to be redirected to. Needs
  107. # manual escaping (see
  108. # https://jinja.palletsprojects.com/en/2.11.x/templates/#html-escaping).
  109. #
  110. # * description: the operation which the user is being asked to confirm
  111. #
  112. # * HTML page shown after a successful user interactive authentication session:
  113. # 'sso_auth_success.html'.
  114. #
  115. # Note that this page must include the JavaScript which notifies of a successful authentication
  116. # (see https://matrix.org/docs/spec/client_server/r0.6.0#fallback).
  117. #
  118. # This template has no additional variables.
  119. #
  120. # * HTML page shown during single sign-on if a deactivated user (according to Synapse's database)
  121. # attempts to login: 'sso_account_deactivated.html'.
  122. #
  123. # This template has no additional variables.
  124. #
  125. # * HTML page to display to users if something goes wrong during the
  126. # OpenID Connect authentication process: 'sso_error.html'.
  127. #
  128. # When rendering, this template is given two variables:
  129. # * error: the technical name of the error
  130. # * error_description: a human-readable message for the error
  131. #
  132. # You can see the default templates at:
  133. # https://github.com/matrix-org/synapse/tree/master/synapse/res/templates
  134. #
  135. #template_dir: "res/templates"
  136. """