Configuration¶
A typed, fail-closed configuration surface. Config resolves settings from
pluggable sources (environment variables by default), validates everything
up front, and either returns an immutable, fully-typed value or raises a
single ConfigError carrying every problem at once — no partial
configuration ever escapes, and error messages name keys, never values.
This is an opt-in layer: construct a Config and read its typed fields in
your own wiring. The library's existing per-call configuration
(TokenValidationConfig, request models) is unchanged.
from py_identity_model import ClientAuthMethod, Config, ConfigError
try:
cfg = Config.from_env(group="client", client_auth_method=ClientAuthMethod.CLIENT_SECRET_BASIC)
except ConfigError as e:
for issue in e.issues: # every problem, reported together
print(issue) # e.g. "CFG-001: required key 'client.id' is missing (client.id)"
raise
cfg.http_timeout # float, validated > 0
cfg.client_id # str
cfg.client_secret.expose() # Secret: redacted in repr/str, read explicitly
Sources and precedence¶
Values resolve per key, first hit wins:
overrides— explicit in-code values passed toConfig.build- your
sources, in the order given (earlier wins) - the environment (
EnvSource, appended last unlessenv=False) - the built-in default
from py_identity_model import Config, EnvSource, MappingSource
cfg = Config.build(
sources=[
MappingSource({"client.discovery_url": url, "client.id": cid}, name="vault"),
EnvSource(prefix="MYAPP_"),
],
overrides={"http.timeout": "5"},
env=False, # don't also read the default OIDC_-prefixed environment
group="client",
)
A custom source is anything satisfying the ConfigSource protocol — return
raw strings for the keys you can supply and nothing else; typing, defaulting,
and validation stay in the library. A source that raises fails construction
closed (CFG-004).
Keys¶
HTTP and cache keys read unprefixed environment variables; the client group
takes the EnvSource prefix (default OIDC_).
| Logical key | Env var (default) | Type | Default |
|---|---|---|---|
http.timeout |
HTTP_TIMEOUT |
float > 0 | 30.0 |
http.retry.max_attempts |
HTTP_RETRY_MAX_ATTEMPTS |
int >= 0 | 3 |
http.retry.base_delay |
HTTP_RETRY_BASE_DELAY |
float >= 0 | 1.0 |
jwks.max_size |
MAX_JWKS_SIZE |
int >= 1 | 524288 |
jwks.max_keys |
MAX_JWKS_KEYS |
int >= 1 | 100 |
jwks.cache.ttl |
JWKS_CACHE_TTL |
float, 60–86400 | 86400.0 |
discovery.cache.ttl |
DISCO_CACHE_TTL |
float, 60–86400 | 3600.0 |
jwks.kid_miss_cooldown |
KID_MISS_REFRESH_COOLDOWN |
float, 0–3600 | 5.0 |
jwks.cache.max_entries |
JWKS_CACHE_MAX_ENTRIES |
int >= 0 | 64 |
discovery.cache.max_entries |
DISCO_CACHE_MAX_ENTRIES |
int >= 0 | 64 |
client.discovery_url |
OIDC_DISCOVERY_URL |
absolute URL | required in group |
client.id |
OIDC_CLIENT_ID |
str | required in group |
client.secret |
OIDC_CLIENT_SECRET |
Secret |
— |
client.scope |
OIDC_SCOPE |
str | "openid profile email" |
client.audience |
OIDC_AUDIENCE |
str | falls back to client.id |
client.redirect_uri |
OIDC_REDIRECT_URI |
URL | "" |
client.post_login_redirect |
OIDC_POST_LOGIN_REDIRECT |
str | "/" |
client.post_logout_redirect |
OIDC_POST_LOGOUT_REDIRECT |
str | "/" |
client.excluded_paths |
OIDC_EXCLUDED_PATHS |
str list | ("/docs", "/openapi.json", "/health") |
The client group is validated as a unit: requesting group="client" requires
client.discovery_url and client.id (CFG-001), a client_auth_method
that needs a secret requires client.secret (CFG-002), and supplying a
client key without requesting the group is itself an error (CFG-002).
Issue codes: CFG-001 missing key, CFG-002 incomplete group, CFG-003
invalid value, CFG-004 source failure.
API¶
Config(http_timeout, http_retry_max_attempts, http_retry_base_delay, jwks_max_size, jwks_max_keys, jwks_cache_ttl, discovery_cache_ttl, jwks_kid_miss_cooldown, jwks_cache_max_entries, discovery_cache_max_entries, client_discovery_url, client_id, client_secret, client_scope, client_audience, client_redirect_uri, client_post_login_redirect, client_post_logout_redirect, client_excluded_paths)
dataclass
¶
A validated, immutable configuration value.
Construct via :meth:from_env (zero-config) or :meth:build (custom
sources / overrides). Never construct the dataclass directly — the fields
below are the result of validated resolution.
from_env(*, prefix='OIDC_', group=None, client_auth_method=ClientAuthMethod.NONE)
classmethod
¶
Build from environment variables (the default path).
build(*, sources=None, overrides=None, env=True, group=None, client_auth_method=ClientAuthMethod.NONE)
classmethod
¶
Resolve and validate a Config, failing closed with all issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sources
|
Sequence[ConfigSource] | None
|
consumer sources, highest precedence first. |
None
|
overrides
|
Mapping[str, str] | None
|
explicit in-code values (logical key id -> raw string); highest precedence of all. |
None
|
env
|
bool
|
append a default-prefix :class: |
True
|
group
|
str | None
|
which key group the construction requires ("client" or None). |
None
|
client_auth_method
|
ClientAuthMethod
|
when the client group is requested, decides
whether |
NONE
|
ConfigError(issues)
¶
Bases: Exception
Raised when strict construction fails. Carries every issue at once.
ConfigIssue(code, keys, message)
dataclass
¶
One problem found during construction. Names keys, never values.
Secret(value)
¶
Wraps a secret string so it can never be printed by accident.
The raw value is reachable only through :meth:expose; every rendering
(repr/str) shows the redaction placeholder.
expose()
¶
Return the raw secret. The only way to read the value.
ClientAuthMethod
¶
Bases: Enum
Client authentication method — decides whether client.secret is required.
Sources¶
ConfigSource
¶
Bases: Protocol
Supplies raw string values for the requested logical keys.
Implementations return only the subset they can supply, keyed by logical
key id. They MUST NOT type, default, clamp, or validate — the library does
that. A source that raises fails construction closed (CFG-004).
EnvSource(*, prefix='OIDC_', name='env')
¶
The default source: resolves logical keys from environment variables.
Client-group keys take prefix (default OIDC_). Reads happen here,
at :meth:resolve time — never at import.
MappingSource(values, *, name='mapping')
¶
A source backed by an in-process mapping keyed by logical key id.