oidc_config.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. self.oidc_user_profile_method = oidc_config.get("user_profile_method", "auto")
  49. self.oidc_allow_existing_users = oidc_config.get("allow_existing_users", False)
  50. ump_config = oidc_config.get("user_mapping_provider", {})
  51. ump_config.setdefault("module", DEFAULT_USER_MAPPING_PROVIDER)
  52. ump_config.setdefault("config", {})
  53. (
  54. self.oidc_user_mapping_provider_class,
  55. self.oidc_user_mapping_provider_config,
  56. ) = load_module(ump_config, ("oidc_config", "user_mapping_provider"))
  57. # Ensure loaded user mapping module has defined all necessary methods
  58. required_methods = [
  59. "get_remote_user_id",
  60. "map_user_attributes",
  61. ]
  62. missing_methods = [
  63. method
  64. for method in required_methods
  65. if not hasattr(self.oidc_user_mapping_provider_class, method)
  66. ]
  67. if missing_methods:
  68. raise ConfigError(
  69. "Class specified by oidc_config."
  70. "user_mapping_provider.module is missing required "
  71. "methods: %s" % (", ".join(missing_methods),)
  72. )
  73. def generate_config_section(self, config_dir_path, server_name, **kwargs):
  74. return """\
  75. # Enable OpenID Connect (OIDC) / OAuth 2.0 for registration and login.
  76. #
  77. # See https://github.com/matrix-org/synapse/blob/master/docs/openid.md
  78. # for some example configurations.
  79. #
  80. oidc_config:
  81. # Uncomment the following to enable authorization against an OpenID Connect
  82. # server. Defaults to false.
  83. #
  84. #enabled: true
  85. # Uncomment the following to disable use of the OIDC discovery mechanism to
  86. # discover endpoints. Defaults to true.
  87. #
  88. #discover: false
  89. # the OIDC issuer. Used to validate tokens and (if discovery is enabled) to
  90. # discover the provider's endpoints.
  91. #
  92. # Required if 'enabled' is true.
  93. #
  94. #issuer: "https://accounts.example.com/"
  95. # oauth2 client id to use.
  96. #
  97. # Required if 'enabled' is true.
  98. #
  99. #client_id: "provided-by-your-issuer"
  100. # oauth2 client secret to use.
  101. #
  102. # Required if 'enabled' is true.
  103. #
  104. #client_secret: "provided-by-your-issuer"
  105. # auth method to use when exchanging the token.
  106. # Valid values are 'client_secret_basic' (default), 'client_secret_post' and
  107. # 'none'.
  108. #
  109. #client_auth_method: client_secret_post
  110. # list of scopes to request. This should normally include the "openid" scope.
  111. # Defaults to ["openid"].
  112. #
  113. #scopes: ["openid", "profile"]
  114. # the oauth2 authorization endpoint. Required if provider discovery is disabled.
  115. #
  116. #authorization_endpoint: "https://accounts.example.com/oauth2/auth"
  117. # the oauth2 token endpoint. Required if provider discovery is disabled.
  118. #
  119. #token_endpoint: "https://accounts.example.com/oauth2/token"
  120. # the OIDC userinfo endpoint. Required if discovery is disabled and the
  121. # "openid" scope is not requested.
  122. #
  123. #userinfo_endpoint: "https://accounts.example.com/userinfo"
  124. # URI where to fetch the JWKS. Required if discovery is disabled and the
  125. # "openid" scope is used.
  126. #
  127. #jwks_uri: "https://accounts.example.com/.well-known/jwks.json"
  128. # Uncomment to skip metadata verification. Defaults to false.
  129. #
  130. # Use this if you are connecting to a provider that is not OpenID Connect
  131. # compliant.
  132. # Avoid this in production.
  133. #
  134. #skip_verification: true
  135. # Whether to fetch the user profile from the userinfo endpoint. Valid
  136. # values are: "auto" or "userinfo_endpoint".
  137. #
  138. # Defaults to "auto", which fetches the userinfo endpoint if "openid" is included
  139. # in `scopes`. Uncomment the following to always fetch the userinfo endpoint.
  140. #
  141. #user_profile_method: "userinfo_endpoint"
  142. # Uncomment to allow a user logging in via OIDC to match a pre-existing account instead
  143. # of failing. This could be used if switching from password logins to OIDC. Defaults to false.
  144. #
  145. #allow_existing_users: true
  146. # An external module can be provided here as a custom solution to mapping
  147. # attributes returned from a OIDC provider onto a matrix user.
  148. #
  149. user_mapping_provider:
  150. # The custom module's class. Uncomment to use a custom module.
  151. # Default is {mapping_provider!r}.
  152. #
  153. # See https://github.com/matrix-org/synapse/blob/master/docs/sso_mapping_providers.md#openid-mapping-providers
  154. # for information on implementing a custom mapping provider.
  155. #
  156. #module: mapping_provider.OidcMappingProvider
  157. # Custom configuration values for the module. This section will be passed as
  158. # a Python dictionary to the user mapping provider module's `parse_config`
  159. # method.
  160. #
  161. # The examples below are intended for the default provider: they should be
  162. # changed if using a custom provider.
  163. #
  164. config:
  165. # name of the claim containing a unique identifier for the user.
  166. # Defaults to `sub`, which OpenID Connect compliant providers should provide.
  167. #
  168. #subject_claim: "sub"
  169. # Jinja2 template for the localpart of the MXID.
  170. #
  171. # When rendering, this template is given the following variables:
  172. # * user: The claims returned by the UserInfo Endpoint and/or in the ID
  173. # Token
  174. #
  175. # If this is not set, the user will be prompted to choose their
  176. # own username.
  177. #
  178. #localpart_template: "{{{{ user.preferred_username }}}}"
  179. # Jinja2 template for the display name to set on first login.
  180. #
  181. # If unset, no displayname will be set.
  182. #
  183. #display_name_template: "{{{{ user.given_name }}}} {{{{ user.last_name }}}}"
  184. # Jinja2 templates for extra attributes to send back to the client during
  185. # login.
  186. #
  187. # Note that these are non-standard and clients will ignore them without modifications.
  188. #
  189. #extra_attributes:
  190. #birthdate: "{{{{ user.birthdate }}}}"
  191. """.format(
  192. mapping_provider=DEFAULT_USER_MAPPING_PROVIDER
  193. )