Skip to content

Discovery Policy

Configurable security policy for OpenID Connect discovery validation.

A policy can be supplied directly to get_discovery_document (via DiscoveryDocumentRequest.policy) and to the cached token-validation path (via TokenValidationConfig.discovery_policy).

Cached token validation

TokenValidationConfig.discovery_policy applies a full policy while still using the built-in discovery and JWKS TTL caches — no need to fetch discovery yourself and re-implement caching. When it is None (the default) a policy is derived from require_https alone, so existing behavior is unchanged; when set, it takes precedence over require_https.

from py_identity_model import (
    DiscoveryPolicy,
    TokenValidationConfig,
    validate_token,
)

# The issuer publishes its JWKS on a separate CDN host — allow that authority
# while keeping every other endpoint pinned to the issuer.
config = TokenValidationConfig(
    perform_disco=True,
    audience="my-api",
    discovery_policy=DiscoveryPolicy(
        additional_endpoint_base_addresses=["https://keys.cdn.example"],
    ),
)

claims = validate_token(
    jwt=token,
    token_validation_config=config,
    disco_doc_address="https://issuer.example",
)

The discovery cache is partitioned by the full policy, so a document admitted under a lax policy is never served to a caller using a stricter one.

Policy Configuration

DiscoveryPolicy(require_https=True, allow_http_on_loopback=True, validate_issuer=True, validate_endpoints=True, require_key_set=True, additional_endpoint_base_addresses=list(), authority=None, allow_loopback_endpoints=False) dataclass

Configurable security policy for discovery document validation.

Controls how strictly the library validates discovery documents and their endpoints. The default is strict (production-safe).

Attributes:

Name Type Description
require_https bool

Require HTTPS for the discovery endpoint and all advertised endpoints. Set False for development against HTTP servers.

allow_http_on_loopback bool

Allow HTTP when the host is localhost, 127.0.0.1, or ::1. Only applies when require_https is True.

validate_issuer bool

Validate the issuer field in the discovery document.

validate_endpoints bool

Validate that advertised endpoint URLs are well-formed.

require_key_set bool

Require a jwks_uri in the discovery document.

additional_endpoint_base_addresses list[str]

Extra base URLs that advertised endpoints are allowed to use (for multi-domain or CDN setups).

authority str | None

Expected authority (scheme + host) for endpoint validation. When None, derived from the discovery URL.

allow_loopback_endpoints bool

Allow mtls_endpoint_aliases to target a loopback address (127.0.0.0/8 / ::1) for local development. Default False — a loopback-routed mTLS request is a limited SSRF to the client's own host, so it must be opted into. Link-local (cloud metadata, 169.254.0.0/16) and RFC1918/reserved hosts stay blocked regardless of this flag. Distinct from allow_http_on_loopback (which relaxes only the HTTPS scheme requirement, not internal-IP routing).

cache_key()

A hashable, canonical identity of this policy for cache partitioning.

The discovery cache keys entries on (address, policy.cache_key()) so a response admitted under a lax policy is never served to a caller running a stricter one — the policy-bypass guard that previously keyed on require_https alone. Every field that influences how a discovery document is validated (see validate_and_parse_discovery_response) is included, so two policies produce equal keys iff they would validate a document identically. additional_endpoint_base_addresses is frozen to a tuple so the whole result is hashable.

Endpoint Parsing

DiscoveryEndpoint(url, authority) dataclass

Parsed discovery endpoint URL with extracted authority.

Attributes:

Name Type Description
url str

The full discovery URL (with well-known path appended if not already present).

authority str

The scheme + host portion of the URL.

parse_discovery_url(url)

Parse a discovery URL and extract its authority.

If the URL does not end with the well-known path, it is appended automatically.

Parameters:

Name Type Description Default
url str

The discovery endpoint URL or base issuer URL.

required

Returns:

Type Description
DiscoveryEndpoint

DiscoveryEndpoint with the full URL and extracted authority.

Raises:

Type Description
ConfigurationException

If the URL is malformed.

Utilities

validate_url_scheme(url, policy)

Validate a URL's scheme against the discovery policy.

Parameters:

Name Type Description Default
url str | None

The URL to validate.

required
policy DiscoveryPolicy

The discovery policy to apply.

required

Raises:

Type Description
ConfigurationException

If the URL scheme violates the policy.

is_loopback(host)

Check if a host is a loopback address.

Recognizes localhost, 127.0.0.1, ::1, and 127.x.x.x addresses. Uses ipaddress for safe parsing so that DNS names like 127.evil.com are not matched.