Additional type hints for config module. (#11465)

This adds some misc. type hints to helper methods used
in the `synapse.config` module.
This commit is contained in:
Patrick Cloke 2021-12-01 07:28:23 -05:00 committed by GitHub
parent a265fbd397
commit f44d729d4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 129 additions and 99 deletions

View file

@ -14,7 +14,7 @@
# limitations under the License.
from collections import Counter
from typing import Collection, Iterable, List, Mapping, Optional, Tuple, Type
from typing import Any, Collection, Iterable, List, Mapping, Optional, Tuple, Type
import attr
@ -36,7 +36,7 @@ LEGACY_USER_MAPPING_PROVIDER = "synapse.handlers.oidc_handler.JinjaOidcMappingPr
class OIDCConfig(Config):
section = "oidc"
def read_config(self, config, **kwargs):
def read_config(self, config, **kwargs) -> None:
self.oidc_providers = tuple(_parse_oidc_provider_configs(config))
if not self.oidc_providers:
return
@ -66,7 +66,7 @@ class OIDCConfig(Config):
# OIDC is enabled if we have a provider
return bool(self.oidc_providers)
def generate_config_section(self, config_dir_path, server_name, **kwargs):
def generate_config_section(self, config_dir_path, server_name, **kwargs) -> str:
return """\
# List of OpenID Connect (OIDC) / OAuth 2.0 identity providers, for registration
# and login.
@ -495,89 +495,89 @@ def _parse_oidc_config_dict(
)
@attr.s(slots=True, frozen=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class OidcProviderClientSecretJwtKey:
# a pem-encoded signing key
key = attr.ib(type=str)
key: str
# properties to include in the JWT header
jwt_header = attr.ib(type=Mapping[str, str])
jwt_header: Mapping[str, str]
# properties to include in the JWT payload.
jwt_payload = attr.ib(type=Mapping[str, str])
jwt_payload: Mapping[str, str]
@attr.s(slots=True, frozen=True)
@attr.s(slots=True, frozen=True, auto_attribs=True)
class OidcProviderConfig:
# 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)
idp_id: str
# user-facing name for this identity provider.
idp_name = attr.ib(type=str)
idp_name: str
# Optional MXC URI for icon for this IdP.
idp_icon = attr.ib(type=Optional[str])
idp_icon: Optional[str]
# Optional brand identifier for this IdP.
idp_brand = attr.ib(type=Optional[str])
idp_brand: Optional[str]
# whether the OIDC discovery mechanism is used to discover endpoints
discover = attr.ib(type=bool)
discover: bool
# the OIDC issuer. Used to validate tokens and (if discovery is enabled) to
# discover the provider's endpoints.
issuer = attr.ib(type=str)
issuer: str
# oauth2 client id to use
client_id = attr.ib(type=str)
client_id: str
# oauth2 client secret to use. if `None`, use client_secret_jwt_key to generate
# a secret.
client_secret = attr.ib(type=Optional[str])
client_secret: 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])
client_secret_jwt_key: Optional[OidcProviderClientSecretJwtKey]
# 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)
client_auth_method: str
# list of scopes to request
scopes = attr.ib(type=Collection[str])
scopes: Collection[str]
# the oauth2 authorization endpoint. Required if discovery is disabled.
authorization_endpoint = attr.ib(type=Optional[str])
authorization_endpoint: Optional[str]
# the oauth2 token endpoint. Required if discovery is disabled.
token_endpoint = attr.ib(type=Optional[str])
token_endpoint: 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])
userinfo_endpoint: 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])
jwks_uri: Optional[str]
# Whether to skip metadata verification
skip_verification = attr.ib(type=bool)
skip_verification: 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)
user_profile_method: 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)
allow_existing_users: bool
# the class of the user mapping provider
user_mapping_provider_class = attr.ib(type=Type)
user_mapping_provider_class: Type
# the config of the user mapping provider
user_mapping_provider_config = attr.ib()
user_mapping_provider_config: Any
# required attributes to require in userinfo to allow login/registration
attribute_requirements = attr.ib(type=List[SsoAttributeRequirement])
attribute_requirements: List[SsoAttributeRequirement]