ID Token Validation¶
First-class validation of OpenID Connect ID Tokens. validate_id_token runs
the standard JWT validation (signature via discovery/JWKS, iss, aud,
exp) and then enforces the ID-token profile of OIDC Core 1.0 §3.1.3.7 /
§3.3.2.11 on top:
sub— required and non-empty, always.azp— a multi-audience token must carryazp, and a presentazpmust equal yourclient_id(taken fromtoken_validation_config.audience).nonce— when you passnonce=, the claim must be present and equal (compared in constant time).auth_time/max_age— when you passmax_age=,auth_timemust be present and recent enough (now - auth_time <= max_age + leeway).at_hash/c_hash— when you passaccess_token=/code=, the corresponding left-half hash must match, computed under the token's signature-verified headeralg. An unmappable algorithm fails closed.
The optional checks activate only when you supply the corresponding argument,
so validation is exactly as strict as the flow you are running. Set
token_validation_config.audience to your client_id.
from py_identity_model import TokenValidationConfig, validate_id_token
claims = validate_id_token(
id_token,
TokenValidationConfig(perform_disco=True, audience=CLIENT_ID, issuer=ISSUER),
disco_doc_address=DISCOVERY_URL,
nonce=expected_nonce, # from the authorization request
access_token=access_token, # enforce at_hash (hybrid/implicit)
code=authorization_code, # enforce c_hash (hybrid)
max_age=300, # require a recent authentication
)
Profile failures raise IdTokenValidationException; standard JWT failures
raise TokenValidationException. The former subclasses the latter, so
except TokenValidationException fails closed on both.
The Go library ships the same profile as
pkg/idtoken
(ValidateIDToken / ValidateClaims) and the Rust library as
validate_id_token with
IdTokenValidationOptions; all three are driven by the shared conformance
vectors.
Sync API¶
validate_id_token(id_token, token_validation_config, disco_doc_address=None, http_client=None, *, nonce=None, access_token=None, code=None, max_age=None)
¶
Validate an OpenID Connect ID Token (OIDC Core §3.1.3.7 / §3.3.2.11).
Runs the standard JWT validation via validate_token (signature,
iss, aud, iat and exp when present) — set
token_validation_config.audience to the RP's client_id so the
aud check is enforced there — then applies the ID-Token profile:
required sub, the azp authorized-party rules, and the optional
nonce / max_age / at_hash / c_hash bindings that are checked
only when the corresponding argument is supplied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_token
|
str
|
The compact-serialized ID Token JWT. |
required |
token_validation_config
|
TokenValidationConfig
|
Validation configuration. |
required |
disco_doc_address
|
str | None
|
Discovery document address (required when
|
None
|
http_client
|
HTTPClient | None
|
Optional managed HTTP client (see |
None
|
nonce
|
str | None
|
The |
None
|
access_token
|
str | None
|
The access token from the same response. When provided,
the token's |
None
|
code
|
str | None
|
The authorization code from the same response. When provided, the
token's |
None
|
max_age
|
int | None
|
The |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The decoded and validated ID Token claims. |
Raises:
| Type | Description |
|---|---|
TokenValidationException
|
If standard JWT validation fails (bad signature, wrong issuer/audience, expired token). |
IdTokenValidationException
|
If an ID-Token-profile rule fails. |
ConfigurationException
|
If the configuration is invalid. |
Async API¶
validate_id_token(id_token, token_validation_config, disco_doc_address=None, http_client=None, *, nonce=None, access_token=None, code=None, max_age=None)
async
¶
Validate an OpenID Connect ID Token (async) (OIDC Core §3.1.3.7 / §3.3.2.11).
Runs the standard async JWT validation via validate_token (signature,
iss, aud, iat and exp when present) — set
token_validation_config.audience to the RP's client_id so the
aud check is enforced there — then applies the ID-Token profile:
required sub, the azp authorized-party rules, and the optional
nonce / max_age / at_hash / c_hash bindings that are checked
only when the corresponding argument is supplied.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id_token
|
str
|
The compact-serialized ID Token JWT. |
required |
token_validation_config
|
TokenValidationConfig
|
Validation configuration. |
required |
disco_doc_address
|
str | None
|
Discovery document address (required when
|
None
|
http_client
|
AsyncHTTPClient | None
|
Optional managed async HTTP client (see |
None
|
nonce
|
str | None
|
The |
None
|
access_token
|
str | None
|
The access token from the same response. When provided,
the token's |
None
|
code
|
str | None
|
The authorization code from the same response. When provided, the
token's |
None
|
max_age
|
int | None
|
The |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The decoded and validated ID Token claims. |
Raises:
| Type | Description |
|---|---|
TokenValidationException
|
If standard JWT validation fails (bad signature, wrong issuer/audience, expired token). |
IdTokenValidationException
|
If an ID-Token-profile rule fails. |
ConfigurationException
|
If the configuration is invalid. |
Claim-level building block¶
For callers that verify the JWT themselves, the profile checks are available as a pure function over an already-validated claim set:
validate_id_token_claims(claims, header_alg, *, client_id=None, nonce=None, access_token=None, code=None, max_age=None, leeway=0.0, now=None)
¶
Validate the ID-Token-specific claim rules (OIDC Core §3.1.3.7 / §3.3.2.11).
Assumes the JWT signature, iss, aud, iat and (when present)
exp have ALREADY been verified by the standard token-validation path,
and that the caller's client_id has already been enforced as an
aud member there. This function enforces only the rules unique to ID
Tokens; every violation raises :class:IdTokenValidationException.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
claims
|
dict
|
The decoded (and already signature/iss/aud/exp-validated) claim set. |
required |
header_alg
|
str | None
|
The |
required |
client_id
|
str | None
|
The relying party's |
None
|
nonce
|
str | None
|
When provided, the token's |
None
|
access_token
|
str | None
|
When provided, the token's |
None
|
code
|
str | None
|
When provided, the token's |
None
|
max_age
|
int | None
|
When provided, |
None
|
leeway
|
float
|
Clock-skew tolerance (seconds) applied to the |
0.0
|
now
|
float | None
|
Current POSIX time (seconds), injected for deterministic tests.
Defaults to :func: |
None
|
Raises:
| Type | Description |
|---|---|
IdTokenValidationException
|
If any ID-Token profile rule is violated. |
Exceptions¶
IdTokenValidationException(message, token_part=None, details=None)
¶
Bases: TokenValidationException
Raised when OpenID Connect ID Token profile validation fails.
Covers the ID-Token-specific rules from OpenID Connect Core 1.0 §3.1.3.7
and §3.3.2.11 (required sub, the azp authorized-party rules,
nonce binding, auth_time/max_age freshness, and the
at_hash/c_hash token/code binding) that sit on top of standard JWT
validation.
Subclasses TokenValidationException (mirroring
LogoutTokenValidationException) rather than ValidationException
directly: an ID-Token profile failure is a token-validation failure, so
callers using the idiomatic except TokenValidationException fail
closed on it. As a sibling it would escape that handler and fail open.