oidc_config.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2020 Quentin Gliech
  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 synapse.python_dependencies import DependencyException, check_requirements
  16. from synapse.util.module_loader import load_module
  17. from ._base import Config, ConfigError
  18. DEFAULT_USER_MAPPING_PROVIDER = "synapse.handlers.oidc_handler.JinjaOidcMappingProvider"
  19. class OIDCConfig(Config):
  20. section = "oidc"
  21. def read_config(self, config, **kwargs):
  22. self.oidc_enabled = False
  23. oidc_config = config.get("oidc_config")
  24. if not oidc_config or not oidc_config.get("enabled", False):
  25. return
  26. try:
  27. check_requirements("oidc")
  28. except DependencyException as e:
  29. raise ConfigError(e.message)
  30. public_baseurl = self.public_baseurl
  31. if public_baseurl is None:
  32. raise ConfigError("oidc_config requires a public_baseurl to be set")
  33. self.oidc_callback_url = public_baseurl + "_synapse/oidc/callback"
  34. self.oidc_enabled = True
  35. self.oidc_discover = oidc_config.get("discover", True)
  36. self.oidc_issuer = oidc_config["issuer"]
  37. self.oidc_client_id = oidc_config["client_id"]
  38. self.oidc_client_secret = oidc_config["client_secret"]
  39. self.oidc_client_auth_method = oidc_config.get(
  40. "client_auth_method", "client_secret_basic"
  41. )
  42. self.oidc_scopes = oidc_config.get("scopes", ["openid"])
  43. self.oidc_authorization_endpoint = oidc_config.get("authorization_endpoint")
  44. self.oidc_token_endpoint = oidc_config.get("token_endpoint")
  45. self.oidc_userinfo_endpoint = oidc_config.get("userinfo_endpoint")
  46. self.oidc_jwks_uri = oidc_config.get("jwks_uri")
  47. self.oidc_skip_verification = oidc_config.get("skip_verification", False)
  48. ump_config = oidc_config.get("user_mapping_provider", {})
  49. ump_config.setdefault("module", DEFAULT_USER_MAPPING_PROVIDER)
  50. ump_config.setdefault("config", {})
  51. (
  52. self.oidc_user_mapping_provider_class,
  53. self.oidc_user_mapping_provider_config,
  54. ) = load_module(ump_config)
  55. # Ensure loaded user mapping module has defined all necessary methods
  56. required_methods = [
  57. "get_remote_user_id",
  58. "map_user_attributes",
  59. ]
  60. missing_methods = [
  61. method
  62. for method in required_methods
  63. if not hasattr(self.oidc_user_mapping_provider_class, method)
  64. ]
  65. if missing_methods:
  66. raise ConfigError(
  67. "Class specified by oidc_config."
  68. "user_mapping_provider.module is missing required "
  69. "methods: %s" % (", ".join(missing_methods),)
  70. )
  71. def generate_config_section(self, config_dir_path, server_name, **kwargs):
  72. return """\
  73. # OpenID Connect integration. The following settings can be used to make Synapse
  74. # use an OpenID Connect Provider for authentication, instead of its internal
  75. # password database.
  76. #
  77. # See https://github.com/matrix-org/synapse/blob/master/openid.md.
  78. #
  79. oidc_config:
  80. # Uncomment the following to enable authorization against an OpenID Connect
  81. # server. Defaults to false.
  82. #
  83. #enabled: true
  84. # Uncomment the following to disable use of the OIDC discovery mechanism to
  85. # discover endpoints. Defaults to true.
  86. #
  87. #discover: false
  88. # the OIDC issuer. Used to validate tokens and (if discovery is enabled) to
  89. # discover the provider's endpoints.
  90. #
  91. # Required if 'enabled' is true.
  92. #
  93. #issuer: "https://accounts.example.com/"
  94. # oauth2 client id to use.
  95. #
  96. # Required if 'enabled' is true.
  97. #
  98. #client_id: "provided-by-your-issuer"
  99. # oauth2 client secret to use.
  100. #
  101. # Required if 'enabled' is true.
  102. #
  103. #client_secret: "provided-by-your-issuer"
  104. # auth method to use when exchanging the token.
  105. # Valid values are 'client_secret_basic' (default), 'client_secret_post' and
  106. # 'none'.
  107. #
  108. #client_auth_method: client_secret_post
  109. # list of scopes to request. This should normally include the "openid" scope.
  110. # Defaults to ["openid"].
  111. #
  112. #scopes: ["openid", "profile"]
  113. # the oauth2 authorization endpoint. Required if provider discovery is disabled.
  114. #
  115. #authorization_endpoint: "https://accounts.example.com/oauth2/auth"
  116. # the oauth2 token endpoint. Required if provider discovery is disabled.
  117. #
  118. #token_endpoint: "https://accounts.example.com/oauth2/token"
  119. # the OIDC userinfo endpoint. Required if discovery is disabled and the
  120. # "openid" scope is not requested.
  121. #
  122. #userinfo_endpoint: "https://accounts.example.com/userinfo"
  123. # URI where to fetch the JWKS. Required if discovery is disabled and the
  124. # "openid" scope is used.
  125. #
  126. #jwks_uri: "https://accounts.example.com/.well-known/jwks.json"
  127. # Uncomment to skip metadata verification. Defaults to false.
  128. #
  129. # Use this if you are connecting to a provider that is not OpenID Connect
  130. # compliant.
  131. # Avoid this in production.
  132. #
  133. #skip_verification: true
  134. # An external module can be provided here as a custom solution to mapping
  135. # attributes returned from a OIDC provider onto a matrix user.
  136. #
  137. user_mapping_provider:
  138. # The custom module's class. Uncomment to use a custom module.
  139. # Default is {mapping_provider!r}.
  140. #
  141. # See https://github.com/matrix-org/synapse/blob/master/docs/sso_mapping_providers.md#openid-mapping-providers
  142. # for information on implementing a custom mapping provider.
  143. #
  144. #module: mapping_provider.OidcMappingProvider
  145. # Custom configuration values for the module. This section will be passed as
  146. # a Python dictionary to the user mapping provider module's `parse_config`
  147. # method.
  148. #
  149. # The examples below are intended for the default provider: they should be
  150. # changed if using a custom provider.
  151. #
  152. config:
  153. # name of the claim containing a unique identifier for the user.
  154. # Defaults to `sub`, which OpenID Connect compliant providers should provide.
  155. #
  156. #subject_claim: "sub"
  157. # Jinja2 template for the localpart of the MXID.
  158. #
  159. # When rendering, this template is given the following variables:
  160. # * user: The claims returned by the UserInfo Endpoint and/or in the ID
  161. # Token
  162. #
  163. # This must be configured if using the default mapping provider.
  164. #
  165. localpart_template: "{{{{ user.preferred_username }}}}"
  166. # Jinja2 template for the display name to set on first login.
  167. #
  168. # If unset, no displayname will be set.
  169. #
  170. #display_name_template: "{{{{ user.given_name }}}} {{{{ user.last_name }}}}"
  171. """.format(
  172. mapping_provider=DEFAULT_USER_MAPPING_PROVIDER
  173. )