2020-05-08 08:30:40 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2020 Quentin Gliech
|
2021-01-15 08:22:12 -05:00
|
|
|
# Copyright 2020-2021 The Matrix.org Foundation C.I.C.
|
2020-05-08 08:30:40 -04:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-01-21 08:20:58 -05:00
|
|
|
from collections import Counter
|
2021-03-16 11:46:07 -04:00
|
|
|
from typing import Iterable, List, Mapping, Optional, Tuple, Type
|
2020-12-18 07:13:03 -05:00
|
|
|
|
|
|
|
import attr
|
|
|
|
|
2020-12-18 08:34:59 -05:00
|
|
|
from synapse.config._util import validate_config
|
2021-03-16 11:46:07 -04:00
|
|
|
from synapse.config.sso import SsoAttributeRequirement
|
2020-05-08 08:30:40 -04:00
|
|
|
from synapse.python_dependencies import DependencyException, check_requirements
|
2020-12-18 07:13:03 -05:00
|
|
|
from synapse.types import Collection, JsonDict
|
2020-05-08 08:30:40 -04:00
|
|
|
from synapse.util.module_loader import load_module
|
2021-01-20 08:15:14 -05:00
|
|
|
from synapse.util.stringutils import parse_and_validate_mxc_uri
|
2020-05-08 08:30:40 -04:00
|
|
|
|
2021-03-09 10:03:37 -05:00
|
|
|
from ._base import Config, ConfigError, read_file
|
2020-05-08 08:30:40 -04:00
|
|
|
|
|
|
|
DEFAULT_USER_MAPPING_PROVIDER = "synapse.handlers.oidc_handler.JinjaOidcMappingProvider"
|
|
|
|
|
|
|
|
|
|
|
|
class OIDCConfig(Config):
|
|
|
|
section = "oidc"
|
|
|
|
|
|
|
|
def read_config(self, config, **kwargs):
|
2021-01-15 11:55:29 -05:00
|
|
|
self.oidc_providers = tuple(_parse_oidc_provider_configs(config))
|
|
|
|
if not self.oidc_providers:
|
2020-05-08 08:30:40 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
check_requirements("oidc")
|
|
|
|
except DependencyException as e:
|
2021-03-16 14:19:27 -04:00
|
|
|
raise ConfigError(
|
|
|
|
e.message # noqa: B306, DependencyException.message is a property
|
|
|
|
) from e
|
2020-05-08 08:30:40 -04:00
|
|
|
|
2021-01-21 08:20:58 -05:00
|
|
|
# check we don't have any duplicate idp_ids now. (The SSO handler will also
|
|
|
|
# check for duplicates when the REST listeners get registered, but that happens
|
|
|
|
# after synapse has forked so doesn't give nice errors.)
|
|
|
|
c = Counter([i.idp_id for i in self.oidc_providers])
|
|
|
|
for idp_id, count in c.items():
|
|
|
|
if count > 1:
|
|
|
|
raise ConfigError(
|
|
|
|
"Multiple OIDC providers have the idp_id %r." % idp_id
|
|
|
|
)
|
|
|
|
|
2021-02-11 11:16:54 -05:00
|
|
|
public_baseurl = self.public_baseurl
|
|
|
|
if public_baseurl is None:
|
|
|
|
raise ConfigError("oidc_config requires a public_baseurl to be set")
|
|
|
|
self.oidc_callback_url = public_baseurl + "_synapse/client/oidc/callback"
|
2020-05-08 08:30:40 -04:00
|
|
|
|
2020-12-18 07:13:03 -05:00
|
|
|
@property
|
|
|
|
def oidc_enabled(self) -> bool:
|
|
|
|
# OIDC is enabled if we have a provider
|
2021-01-15 11:55:29 -05:00
|
|
|
return bool(self.oidc_providers)
|
2020-05-08 08:30:40 -04:00
|
|
|
|
|
|
|
def generate_config_section(self, config_dir_path, server_name, **kwargs):
|
|
|
|
return """\
|
2021-01-15 11:55:29 -05:00
|
|
|
# List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
|
|
|
|
# and login.
|
|
|
|
#
|
|
|
|
# Options for each entry include:
|
|
|
|
#
|
|
|
|
# idp_id: a unique identifier for this identity provider. Used internally
|
|
|
|
# by Synapse; should be a single word such as 'github'.
|
|
|
|
#
|
|
|
|
# Note that, if this is changed, users authenticating via that provider
|
|
|
|
# will no longer be recognised as the same user!
|
|
|
|
#
|
2021-03-29 10:40:11 -04:00
|
|
|
# (Use "oidc" here if you are migrating from an old "oidc_config"
|
|
|
|
# configuration.)
|
|
|
|
#
|
2021-01-15 11:55:29 -05:00
|
|
|
# idp_name: A user-facing name for this identity provider, which is used to
|
|
|
|
# offer the user a choice of login mechanisms.
|
|
|
|
#
|
2021-01-20 08:15:14 -05:00
|
|
|
# idp_icon: An optional icon for this identity provider, which is presented
|
2021-01-27 16:31:45 -05:00
|
|
|
# by clients and Synapse's own IdP picker page. If given, must be an
|
|
|
|
# MXC URI of the format mxc://<server-name>/<media-id>. (An easy way to
|
|
|
|
# obtain such an MXC URI is to upload an image to an (unencrypted) room
|
|
|
|
# and then copy the "url" from the source of the event.)
|
|
|
|
#
|
|
|
|
# idp_brand: An optional brand for this identity provider, allowing clients
|
|
|
|
# to style the login flow according to the identity provider in question.
|
|
|
|
# See the spec for possible options here.
|
2021-01-20 08:15:14 -05:00
|
|
|
#
|
2021-01-15 11:55:29 -05:00
|
|
|
# discover: set to 'false' to disable the use of the OIDC discovery mechanism
|
|
|
|
# to discover endpoints. Defaults to true.
|
|
|
|
#
|
|
|
|
# issuer: Required. The OIDC issuer. Used to validate tokens and (if discovery
|
|
|
|
# is enabled) to discover the provider's endpoints.
|
|
|
|
#
|
|
|
|
# client_id: Required. oauth2 client id to use.
|
|
|
|
#
|
2021-03-09 10:03:37 -05:00
|
|
|
# client_secret: oauth2 client secret to use. May be omitted if
|
|
|
|
# client_secret_jwt_key is given, or if client_auth_method is 'none'.
|
|
|
|
#
|
|
|
|
# client_secret_jwt_key: Alternative to client_secret: details of a key used
|
|
|
|
# to create a JSON Web Token to be used as an OAuth2 client secret. If
|
|
|
|
# given, must be a dictionary with the following properties:
|
|
|
|
#
|
|
|
|
# key: a pem-encoded signing key. Must be a suitable key for the
|
|
|
|
# algorithm specified. Required unless 'key_file' is given.
|
|
|
|
#
|
|
|
|
# key_file: the path to file containing a pem-encoded signing key file.
|
|
|
|
# Required unless 'key' is given.
|
|
|
|
#
|
|
|
|
# jwt_header: a dictionary giving properties to include in the JWT
|
|
|
|
# header. Must include the key 'alg', giving the algorithm used to
|
|
|
|
# sign the JWT, such as "ES256", using the JWA identifiers in
|
|
|
|
# RFC7518.
|
|
|
|
#
|
|
|
|
# jwt_payload: an optional dictionary giving properties to include in
|
|
|
|
# the JWT payload. Normally this should include an 'iss' key.
|
2021-01-15 11:55:29 -05:00
|
|
|
#
|
|
|
|
# client_auth_method: auth method to use when exchanging the token. Valid
|
|
|
|
# values are 'client_secret_basic' (default), 'client_secret_post' and
|
|
|
|
# 'none'.
|
|
|
|
#
|
|
|
|
# scopes: list of scopes to request. This should normally include the "openid"
|
|
|
|
# scope. Defaults to ["openid"].
|
|
|
|
#
|
|
|
|
# authorization_endpoint: the oauth2 authorization endpoint. Required if
|
|
|
|
# provider discovery is disabled.
|
|
|
|
#
|
|
|
|
# token_endpoint: the oauth2 token endpoint. Required if provider discovery is
|
|
|
|
# disabled.
|
|
|
|
#
|
|
|
|
# userinfo_endpoint: the OIDC userinfo endpoint. Required if discovery is
|
|
|
|
# disabled and the 'openid' scope is not requested.
|
|
|
|
#
|
|
|
|
# jwks_uri: URI where to fetch the JWKS. Required if discovery is disabled and
|
|
|
|
# the 'openid' scope is used.
|
|
|
|
#
|
|
|
|
# skip_verification: set to 'true' to skip metadata verification. Use this if
|
|
|
|
# you are connecting to a provider that is not OpenID Connect compliant.
|
|
|
|
# Defaults to false. Avoid this in production.
|
|
|
|
#
|
|
|
|
# user_profile_method: Whether to fetch the user profile from the userinfo
|
|
|
|
# endpoint. Valid values are: 'auto' or 'userinfo_endpoint'.
|
|
|
|
#
|
|
|
|
# Defaults to 'auto', which fetches the userinfo endpoint if 'openid' is
|
|
|
|
# included in 'scopes'. Set to 'userinfo_endpoint' to always fetch the
|
|
|
|
# userinfo endpoint.
|
|
|
|
#
|
|
|
|
# allow_existing_users: set to 'true' to allow a user logging in via OIDC to
|
|
|
|
# match a pre-existing account instead of failing. This could be used if
|
|
|
|
# switching from password logins to OIDC. Defaults to false.
|
|
|
|
#
|
|
|
|
# user_mapping_provider: Configuration for how attributes returned from a OIDC
|
|
|
|
# provider are mapped onto a matrix user. This setting has the following
|
|
|
|
# sub-properties:
|
|
|
|
#
|
|
|
|
# module: The class name of a custom mapping module. Default is
|
|
|
|
# {mapping_provider!r}.
|
|
|
|
# See https://github.com/matrix-org/synapse/blob/master/docs/sso_mapping_providers.md#openid-mapping-providers
|
|
|
|
# for information on implementing a custom mapping provider.
|
|
|
|
#
|
|
|
|
# config: Configuration for the mapping provider module. This section will
|
|
|
|
# be passed as a Python dictionary to the user mapping provider
|
|
|
|
# module's `parse_config` method.
|
|
|
|
#
|
|
|
|
# For the default provider, the following settings are available:
|
|
|
|
#
|
2021-01-27 16:28:59 -05:00
|
|
|
# subject_claim: name of the claim containing a unique identifier
|
|
|
|
# for the user. Defaults to 'sub', which OpenID Connect
|
|
|
|
# compliant providers should provide.
|
2021-01-15 11:55:29 -05:00
|
|
|
#
|
|
|
|
# localpart_template: Jinja2 template for the localpart of the MXID.
|
|
|
|
# If this is not set, the user will be prompted to choose their
|
2021-02-01 10:52:50 -05:00
|
|
|
# own username (see 'sso_auth_account_details.html' in the 'sso'
|
|
|
|
# section of this file).
|
2021-01-15 11:55:29 -05:00
|
|
|
#
|
|
|
|
# display_name_template: Jinja2 template for the display name to set
|
|
|
|
# on first login. If unset, no displayname will be set.
|
|
|
|
#
|
2021-01-27 16:28:59 -05:00
|
|
|
# email_template: Jinja2 template for the email address of the user.
|
|
|
|
# If unset, no email address will be added to the account.
|
|
|
|
#
|
2021-01-15 11:55:29 -05:00
|
|
|
# extra_attributes: a map of Jinja2 templates for extra attributes
|
|
|
|
# to send back to the client during login.
|
|
|
|
# Note that these are non-standard and clients will ignore them
|
|
|
|
# without modifications.
|
|
|
|
#
|
|
|
|
# When rendering, the Jinja2 templates are given a 'user' variable,
|
|
|
|
# which is set to the claims returned by the UserInfo Endpoint and/or
|
|
|
|
# in the ID Token.
|
2020-06-03 16:13:17 -04:00
|
|
|
#
|
2021-03-16 11:46:07 -04:00
|
|
|
# It is possible to configure Synapse to only allow logins if certain attributes
|
|
|
|
# match particular values in the OIDC userinfo. The requirements can be listed under
|
|
|
|
# `attribute_requirements` as shown below. All of the listed attributes must
|
|
|
|
# match for the login to be permitted. Additional attributes can be added to
|
|
|
|
# userinfo by expanding the `scopes` section of the OIDC config to retrieve
|
|
|
|
# additional information from the OIDC provider.
|
|
|
|
#
|
|
|
|
# If the OIDC claim is a list, then the attribute must match any value in the list.
|
|
|
|
# Otherwise, it must exactly match the value of the claim. Using the example
|
|
|
|
# below, the `family_name` claim MUST be "Stephensson", but the `groups`
|
|
|
|
# claim MUST contain "admin".
|
|
|
|
#
|
|
|
|
# attribute_requirements:
|
|
|
|
# - attribute: family_name
|
|
|
|
# value: "Stephensson"
|
|
|
|
# - attribute: groups
|
|
|
|
# value: "admin"
|
|
|
|
#
|
2020-10-30 10:01:59 -04:00
|
|
|
# See https://github.com/matrix-org/synapse/blob/master/docs/openid.md
|
2021-01-15 11:55:29 -05:00
|
|
|
# for information on how to configure these options.
|
2020-05-08 08:30:40 -04:00
|
|
|
#
|
2021-01-15 11:55:29 -05:00
|
|
|
# For backwards compatibility, it is also possible to configure a single OIDC
|
|
|
|
# provider via an 'oidc_config' setting. This is now deprecated and admins are
|
2021-01-21 07:25:02 -05:00
|
|
|
# advised to migrate to the 'oidc_providers' format. (When doing that migration,
|
|
|
|
# use 'oidc' for the idp_id to ensure that existing users continue to be
|
|
|
|
# recognised.)
|
2021-01-15 11:55:29 -05:00
|
|
|
#
|
|
|
|
oidc_providers:
|
|
|
|
# Generic example
|
2020-06-03 16:13:17 -04:00
|
|
|
#
|
2021-01-15 11:55:29 -05:00
|
|
|
#- idp_id: my_idp
|
|
|
|
# idp_name: "My OpenID provider"
|
2021-01-21 07:25:02 -05:00
|
|
|
# idp_icon: "mxc://example.com/mediaid"
|
2021-01-15 11:55:29 -05:00
|
|
|
# discover: false
|
|
|
|
# issuer: "https://accounts.example.com/"
|
|
|
|
# client_id: "provided-by-your-issuer"
|
|
|
|
# client_secret: "provided-by-your-issuer"
|
|
|
|
# client_auth_method: client_secret_post
|
|
|
|
# scopes: ["openid", "profile"]
|
|
|
|
# authorization_endpoint: "https://accounts.example.com/oauth2/auth"
|
|
|
|
# token_endpoint: "https://accounts.example.com/oauth2/token"
|
|
|
|
# userinfo_endpoint: "https://accounts.example.com/userinfo"
|
|
|
|
# jwks_uri: "https://accounts.example.com/.well-known/jwks.json"
|
|
|
|
# skip_verification: true
|
2021-01-27 16:28:59 -05:00
|
|
|
# user_mapping_provider:
|
|
|
|
# config:
|
|
|
|
# subject_claim: "id"
|
2021-02-04 09:06:20 -05:00
|
|
|
# localpart_template: "{{{{ user.login }}}}"
|
|
|
|
# display_name_template: "{{{{ user.name }}}}"
|
|
|
|
# email_template: "{{{{ user.email }}}}"
|
2021-03-16 11:46:07 -04:00
|
|
|
# attribute_requirements:
|
|
|
|
# - attribute: userGroup
|
|
|
|
# value: "synapseUsers"
|
2020-05-08 08:30:40 -04:00
|
|
|
""".format(
|
|
|
|
mapping_provider=DEFAULT_USER_MAPPING_PROVIDER
|
|
|
|
)
|
2020-12-18 07:13:03 -05:00
|
|
|
|
|
|
|
|
2020-12-18 08:34:59 -05:00
|
|
|
# jsonschema definition of the configuration settings for an oidc identity provider
|
|
|
|
OIDC_PROVIDER_CONFIG_SCHEMA = {
|
|
|
|
"type": "object",
|
2021-03-09 10:03:37 -05:00
|
|
|
"required": ["issuer", "client_id"],
|
2020-12-18 08:34:59 -05:00
|
|
|
"properties": {
|
2021-01-27 16:31:45 -05:00
|
|
|
"idp_id": {
|
|
|
|
"type": "string",
|
|
|
|
"minLength": 1,
|
|
|
|
# MSC2858 allows a maxlen of 255, but we prefix with "oidc-"
|
|
|
|
"maxLength": 250,
|
|
|
|
"pattern": "^[A-Za-z0-9._~-]+$",
|
|
|
|
},
|
2021-01-15 08:22:12 -05:00
|
|
|
"idp_name": {"type": "string"},
|
2021-01-20 08:15:14 -05:00
|
|
|
"idp_icon": {"type": "string"},
|
2021-01-27 16:31:45 -05:00
|
|
|
"idp_brand": {
|
|
|
|
"type": "string",
|
2021-03-16 07:21:26 -04:00
|
|
|
"minLength": 1,
|
|
|
|
"maxLength": 255,
|
|
|
|
"pattern": "^[a-z][a-z0-9_.-]*$",
|
|
|
|
},
|
|
|
|
"idp_unstable_brand": {
|
|
|
|
"type": "string",
|
2021-01-27 16:31:45 -05:00
|
|
|
"minLength": 1,
|
|
|
|
"maxLength": 255,
|
|
|
|
"pattern": "^[a-z][a-z0-9_.-]*$",
|
|
|
|
},
|
2020-12-18 08:34:59 -05:00
|
|
|
"discover": {"type": "boolean"},
|
|
|
|
"issuer": {"type": "string"},
|
|
|
|
"client_id": {"type": "string"},
|
|
|
|
"client_secret": {"type": "string"},
|
2021-03-09 10:03:37 -05:00
|
|
|
"client_secret_jwt_key": {
|
|
|
|
"type": "object",
|
|
|
|
"required": ["jwt_header"],
|
|
|
|
"oneOf": [
|
|
|
|
{"required": ["key"]},
|
|
|
|
{"required": ["key_file"]},
|
|
|
|
],
|
|
|
|
"properties": {
|
|
|
|
"key": {"type": "string"},
|
|
|
|
"key_file": {"type": "string"},
|
|
|
|
"jwt_header": {
|
|
|
|
"type": "object",
|
|
|
|
"required": ["alg"],
|
|
|
|
"properties": {
|
|
|
|
"alg": {"type": "string"},
|
|
|
|
},
|
|
|
|
"additionalProperties": {"type": "string"},
|
|
|
|
},
|
|
|
|
"jwt_payload": {
|
|
|
|
"type": "object",
|
|
|
|
"additionalProperties": {"type": "string"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-18 08:34:59 -05:00
|
|
|
"client_auth_method": {
|
|
|
|
"type": "string",
|
|
|
|
# the following list is the same as the keys of
|
|
|
|
# authlib.oauth2.auth.ClientAuth.DEFAULT_AUTH_METHODS. We inline it
|
|
|
|
# to avoid importing authlib here.
|
|
|
|
"enum": ["client_secret_basic", "client_secret_post", "none"],
|
|
|
|
},
|
|
|
|
"scopes": {"type": "array", "items": {"type": "string"}},
|
|
|
|
"authorization_endpoint": {"type": "string"},
|
|
|
|
"token_endpoint": {"type": "string"},
|
|
|
|
"userinfo_endpoint": {"type": "string"},
|
|
|
|
"jwks_uri": {"type": "string"},
|
|
|
|
"skip_verification": {"type": "boolean"},
|
|
|
|
"user_profile_method": {
|
|
|
|
"type": "string",
|
|
|
|
"enum": ["auto", "userinfo_endpoint"],
|
|
|
|
},
|
|
|
|
"allow_existing_users": {"type": "boolean"},
|
|
|
|
"user_mapping_provider": {"type": ["object", "null"]},
|
2021-03-16 11:46:07 -04:00
|
|
|
"attribute_requirements": {
|
|
|
|
"type": "array",
|
|
|
|
"items": SsoAttributeRequirement.JSON_SCHEMA,
|
|
|
|
},
|
2020-12-18 08:34:59 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-15 11:55:29 -05:00
|
|
|
# the same as OIDC_PROVIDER_CONFIG_SCHEMA, but with compulsory idp_id and idp_name
|
|
|
|
OIDC_PROVIDER_CONFIG_WITH_ID_SCHEMA = {
|
|
|
|
"allOf": [OIDC_PROVIDER_CONFIG_SCHEMA, {"required": ["idp_id", "idp_name"]}]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# the `oidc_providers` list can either be None (as it is in the default config), or
|
|
|
|
# a list of provider configs, each of which requires an explicit ID and name.
|
|
|
|
OIDC_PROVIDER_LIST_SCHEMA = {
|
|
|
|
"oneOf": [
|
|
|
|
{"type": "null"},
|
|
|
|
{"type": "array", "items": OIDC_PROVIDER_CONFIG_WITH_ID_SCHEMA},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
# the `oidc_config` setting can either be None (which it used to be in the default
|
2020-12-18 08:34:59 -05:00
|
|
|
# config), or an object. If an object, it is ignored unless it has an "enabled: True"
|
|
|
|
# property.
|
|
|
|
#
|
|
|
|
# It's *possible* to represent this with jsonschema, but the resultant errors aren't
|
|
|
|
# particularly clear, so we just check for either an object or a null here, and do
|
|
|
|
# additional checks in the code.
|
|
|
|
OIDC_CONFIG_SCHEMA = {"oneOf": [{"type": "null"}, {"type": "object"}]}
|
|
|
|
|
2021-01-15 11:55:29 -05:00
|
|
|
# the top-level schema can contain an "oidc_config" and/or an "oidc_providers".
|
2020-12-18 08:34:59 -05:00
|
|
|
MAIN_CONFIG_SCHEMA = {
|
|
|
|
"type": "object",
|
2021-01-15 11:55:29 -05:00
|
|
|
"properties": {
|
|
|
|
"oidc_config": OIDC_CONFIG_SCHEMA,
|
|
|
|
"oidc_providers": OIDC_PROVIDER_LIST_SCHEMA,
|
|
|
|
},
|
2020-12-18 08:34:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-15 11:55:29 -05:00
|
|
|
def _parse_oidc_provider_configs(config: JsonDict) -> Iterable["OidcProviderConfig"]:
|
|
|
|
"""extract and parse the OIDC provider configs from the config dict
|
|
|
|
|
|
|
|
The configuration may contain either a single `oidc_config` object with an
|
|
|
|
`enabled: True` property, or a list of provider configurations under
|
|
|
|
`oidc_providers`, *or both*.
|
|
|
|
|
|
|
|
Returns a generator which yields the OidcProviderConfig objects
|
|
|
|
"""
|
|
|
|
validate_config(MAIN_CONFIG_SCHEMA, config, ())
|
|
|
|
|
2021-01-19 09:56:54 -05:00
|
|
|
for i, p in enumerate(config.get("oidc_providers") or []):
|
|
|
|
yield _parse_oidc_config_dict(p, ("oidc_providers", "<item %i>" % (i,)))
|
2021-01-15 11:55:29 -05:00
|
|
|
|
|
|
|
# for backwards-compatibility, it is also possible to provide a single "oidc_config"
|
|
|
|
# object with an "enabled: True" property.
|
|
|
|
oidc_config = config.get("oidc_config")
|
|
|
|
if oidc_config and oidc_config.get("enabled", False):
|
|
|
|
# MAIN_CONFIG_SCHEMA checks that `oidc_config` is an object, but not that
|
|
|
|
# it matches OIDC_PROVIDER_CONFIG_SCHEMA (see the comments on OIDC_CONFIG_SCHEMA
|
|
|
|
# above), so now we need to validate it.
|
|
|
|
validate_config(OIDC_PROVIDER_CONFIG_SCHEMA, oidc_config, ("oidc_config",))
|
2021-01-19 09:56:54 -05:00
|
|
|
yield _parse_oidc_config_dict(oidc_config, ("oidc_config",))
|
2021-01-15 11:55:29 -05:00
|
|
|
|
|
|
|
|
2021-01-19 09:56:54 -05:00
|
|
|
def _parse_oidc_config_dict(
|
|
|
|
oidc_config: JsonDict, config_path: Tuple[str, ...]
|
|
|
|
) -> "OidcProviderConfig":
|
2020-12-18 07:13:03 -05:00
|
|
|
"""Take the configuration dict and parse it into an OidcProviderConfig
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ConfigError if the configuration is malformed.
|
|
|
|
"""
|
|
|
|
ump_config = oidc_config.get("user_mapping_provider", {})
|
|
|
|
ump_config.setdefault("module", DEFAULT_USER_MAPPING_PROVIDER)
|
|
|
|
ump_config.setdefault("config", {})
|
|
|
|
|
|
|
|
(
|
|
|
|
user_mapping_provider_class,
|
|
|
|
user_mapping_provider_config,
|
2021-01-19 09:56:54 -05:00
|
|
|
) = load_module(ump_config, config_path + ("user_mapping_provider",))
|
2020-12-18 07:13:03 -05:00
|
|
|
|
|
|
|
# Ensure loaded user mapping module has defined all necessary methods
|
|
|
|
required_methods = [
|
|
|
|
"get_remote_user_id",
|
|
|
|
"map_user_attributes",
|
|
|
|
]
|
|
|
|
missing_methods = [
|
|
|
|
method
|
|
|
|
for method in required_methods
|
|
|
|
if not hasattr(user_mapping_provider_class, method)
|
|
|
|
]
|
|
|
|
if missing_methods:
|
|
|
|
raise ConfigError(
|
2021-01-19 09:56:54 -05:00
|
|
|
"Class %s is missing required "
|
|
|
|
"methods: %s"
|
|
|
|
% (
|
|
|
|
user_mapping_provider_class,
|
|
|
|
", ".join(missing_methods),
|
|
|
|
),
|
|
|
|
config_path + ("user_mapping_provider", "module"),
|
2020-12-18 07:13:03 -05:00
|
|
|
)
|
|
|
|
|
2021-01-15 08:22:12 -05:00
|
|
|
idp_id = oidc_config.get("idp_id", "oidc")
|
2021-01-21 07:25:02 -05:00
|
|
|
|
|
|
|
# prefix the given IDP with a prefix specific to the SSO mechanism, to avoid
|
|
|
|
# clashes with other mechs (such as SAML, CAS).
|
|
|
|
#
|
|
|
|
# We allow "oidc" as an exception so that people migrating from old-style
|
|
|
|
# "oidc_config" format (which has long used "oidc" as its idp_id) can migrate to
|
|
|
|
# a new-style "oidc_providers" entry without changing the idp_id for their provider
|
|
|
|
# (and thereby invalidating their user_external_ids data).
|
|
|
|
|
|
|
|
if idp_id != "oidc":
|
|
|
|
idp_id = "oidc-" + idp_id
|
|
|
|
|
2021-01-20 08:15:14 -05:00
|
|
|
# MSC2858 also specifies that the idp_icon must be a valid MXC uri
|
|
|
|
idp_icon = oidc_config.get("idp_icon")
|
|
|
|
if idp_icon is not None:
|
|
|
|
try:
|
|
|
|
parse_and_validate_mxc_uri(idp_icon)
|
|
|
|
except ValueError as e:
|
|
|
|
raise ConfigError(
|
|
|
|
"idp_icon must be a valid MXC URI", config_path + ("idp_icon",)
|
|
|
|
) from e
|
|
|
|
|
2021-03-09 10:03:37 -05:00
|
|
|
client_secret_jwt_key_config = oidc_config.get("client_secret_jwt_key")
|
|
|
|
client_secret_jwt_key = None # type: Optional[OidcProviderClientSecretJwtKey]
|
|
|
|
if client_secret_jwt_key_config is not None:
|
|
|
|
keyfile = client_secret_jwt_key_config.get("key_file")
|
|
|
|
if keyfile:
|
|
|
|
key = read_file(keyfile, config_path + ("client_secret_jwt_key",))
|
|
|
|
else:
|
|
|
|
key = client_secret_jwt_key_config["key"]
|
|
|
|
client_secret_jwt_key = OidcProviderClientSecretJwtKey(
|
|
|
|
key=key,
|
|
|
|
jwt_header=client_secret_jwt_key_config["jwt_header"],
|
|
|
|
jwt_payload=client_secret_jwt_key_config.get("jwt_payload", {}),
|
|
|
|
)
|
2021-03-16 11:46:07 -04:00
|
|
|
# parse attribute_requirements from config (list of dicts) into a list of SsoAttributeRequirement
|
|
|
|
attribute_requirements = [
|
|
|
|
SsoAttributeRequirement(**x)
|
|
|
|
for x in oidc_config.get("attribute_requirements", [])
|
|
|
|
]
|
2021-03-09 10:03:37 -05:00
|
|
|
|
2020-12-18 07:13:03 -05:00
|
|
|
return OidcProviderConfig(
|
2021-01-15 08:22:12 -05:00
|
|
|
idp_id=idp_id,
|
|
|
|
idp_name=oidc_config.get("idp_name", "OIDC"),
|
2021-01-20 08:15:14 -05:00
|
|
|
idp_icon=idp_icon,
|
2021-01-27 16:31:45 -05:00
|
|
|
idp_brand=oidc_config.get("idp_brand"),
|
2021-03-16 07:21:26 -04:00
|
|
|
unstable_idp_brand=oidc_config.get("unstable_idp_brand"),
|
2020-12-18 07:13:03 -05:00
|
|
|
discover=oidc_config.get("discover", True),
|
|
|
|
issuer=oidc_config["issuer"],
|
|
|
|
client_id=oidc_config["client_id"],
|
2021-03-09 10:03:37 -05:00
|
|
|
client_secret=oidc_config.get("client_secret"),
|
|
|
|
client_secret_jwt_key=client_secret_jwt_key,
|
2020-12-18 07:13:03 -05:00
|
|
|
client_auth_method=oidc_config.get("client_auth_method", "client_secret_basic"),
|
|
|
|
scopes=oidc_config.get("scopes", ["openid"]),
|
|
|
|
authorization_endpoint=oidc_config.get("authorization_endpoint"),
|
|
|
|
token_endpoint=oidc_config.get("token_endpoint"),
|
|
|
|
userinfo_endpoint=oidc_config.get("userinfo_endpoint"),
|
|
|
|
jwks_uri=oidc_config.get("jwks_uri"),
|
|
|
|
skip_verification=oidc_config.get("skip_verification", False),
|
|
|
|
user_profile_method=oidc_config.get("user_profile_method", "auto"),
|
|
|
|
allow_existing_users=oidc_config.get("allow_existing_users", False),
|
|
|
|
user_mapping_provider_class=user_mapping_provider_class,
|
|
|
|
user_mapping_provider_config=user_mapping_provider_config,
|
2021-03-16 11:46:07 -04:00
|
|
|
attribute_requirements=attribute_requirements,
|
2020-12-18 07:13:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-09 10:03:37 -05:00
|
|
|
@attr.s(slots=True, frozen=True)
|
|
|
|
class OidcProviderClientSecretJwtKey:
|
|
|
|
# a pem-encoded signing key
|
|
|
|
key = attr.ib(type=str)
|
|
|
|
|
|
|
|
# properties to include in the JWT header
|
|
|
|
jwt_header = attr.ib(type=Mapping[str, str])
|
|
|
|
|
|
|
|
# properties to include in the JWT payload.
|
|
|
|
jwt_payload = attr.ib(type=Mapping[str, str])
|
|
|
|
|
|
|
|
|
2021-01-15 08:22:12 -05:00
|
|
|
@attr.s(slots=True, frozen=True)
|
2020-12-18 07:13:03 -05:00
|
|
|
class OidcProviderConfig:
|
2021-01-15 08:22:12 -05:00
|
|
|
# a unique identifier for this identity provider. Used in the 'user_external_ids'
|
|
|
|
# table, as well as the query/path parameter used in the login protocol.
|
|
|
|
idp_id = attr.ib(type=str)
|
|
|
|
|
|
|
|
# user-facing name for this identity provider.
|
|
|
|
idp_name = attr.ib(type=str)
|
|
|
|
|
2021-01-20 08:15:14 -05:00
|
|
|
# Optional MXC URI for icon for this IdP.
|
|
|
|
idp_icon = attr.ib(type=Optional[str])
|
|
|
|
|
2021-01-27 16:31:45 -05:00
|
|
|
# Optional brand identifier for this IdP.
|
|
|
|
idp_brand = attr.ib(type=Optional[str])
|
|
|
|
|
2021-03-16 07:21:26 -04:00
|
|
|
# Optional brand identifier for the unstable API (see MSC2858).
|
|
|
|
unstable_idp_brand = attr.ib(type=Optional[str])
|
|
|
|
|
2020-12-18 07:13:03 -05:00
|
|
|
# whether the OIDC discovery mechanism is used to discover endpoints
|
|
|
|
discover = attr.ib(type=bool)
|
|
|
|
|
|
|
|
# the OIDC issuer. Used to validate tokens and (if discovery is enabled) to
|
|
|
|
# discover the provider's endpoints.
|
|
|
|
issuer = attr.ib(type=str)
|
|
|
|
|
|
|
|
# oauth2 client id to use
|
|
|
|
client_id = attr.ib(type=str)
|
|
|
|
|
2021-03-09 10:03:37 -05:00
|
|
|
# oauth2 client secret to use. if `None`, use client_secret_jwt_key to generate
|
|
|
|
# a secret.
|
|
|
|
client_secret = attr.ib(type=Optional[str])
|
|
|
|
|
|
|
|
# key to use to construct a JWT to use as a client secret. May be `None` if
|
|
|
|
# `client_secret` is set.
|
|
|
|
client_secret_jwt_key = attr.ib(type=Optional[OidcProviderClientSecretJwtKey])
|
2020-12-18 07:13:03 -05:00
|
|
|
|
|
|
|
# auth method to use when exchanging the token.
|
|
|
|
# Valid values are 'client_secret_basic', 'client_secret_post' and
|
|
|
|
# 'none'.
|
|
|
|
client_auth_method = attr.ib(type=str)
|
|
|
|
|
|
|
|
# list of scopes to request
|
|
|
|
scopes = attr.ib(type=Collection[str])
|
|
|
|
|
|
|
|
# the oauth2 authorization endpoint. Required if discovery is disabled.
|
|
|
|
authorization_endpoint = attr.ib(type=Optional[str])
|
|
|
|
|
|
|
|
# the oauth2 token endpoint. Required if discovery is disabled.
|
|
|
|
token_endpoint = attr.ib(type=Optional[str])
|
|
|
|
|
|
|
|
# the OIDC userinfo endpoint. Required if discovery is disabled and the
|
|
|
|
# "openid" scope is not requested.
|
|
|
|
userinfo_endpoint = attr.ib(type=Optional[str])
|
|
|
|
|
|
|
|
# URI where to fetch the JWKS. Required if discovery is disabled and the
|
|
|
|
# "openid" scope is used.
|
|
|
|
jwks_uri = attr.ib(type=Optional[str])
|
|
|
|
|
|
|
|
# Whether to skip metadata verification
|
|
|
|
skip_verification = attr.ib(type=bool)
|
|
|
|
|
|
|
|
# Whether to fetch the user profile from the userinfo endpoint. Valid
|
|
|
|
# values are: "auto" or "userinfo_endpoint".
|
|
|
|
user_profile_method = attr.ib(type=str)
|
|
|
|
|
|
|
|
# whether to allow a user logging in via OIDC to match a pre-existing account
|
|
|
|
# instead of failing
|
|
|
|
allow_existing_users = attr.ib(type=bool)
|
|
|
|
|
|
|
|
# the class of the user mapping provider
|
|
|
|
user_mapping_provider_class = attr.ib(type=Type)
|
|
|
|
|
|
|
|
# the config of the user mapping provider
|
|
|
|
user_mapping_provider_config = attr.ib()
|
2021-03-16 11:46:07 -04:00
|
|
|
|
|
|
|
# required attributes to require in userinfo to allow login/registration
|
|
|
|
attribute_requirements = attr.ib(type=List[SsoAttributeRequirement])
|