Skip to content

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 carry azp, and a present azp must equal your client_id (taken from token_validation_config.audience).
  • nonce — when you pass nonce=, the claim must be present and equal (compared in constant time).
  • auth_time / max_age — when you pass max_age=, auth_time must be present and recent enough (now - auth_time <= max_age + leeway).
  • at_hash / c_hash — when you pass access_token= / code=, the corresponding left-half hash must match, computed under the token's signature-verified header alg. 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. audience should be the RP's client_id and issuer the OP issuer.

required
disco_doc_address str | None

Discovery document address (required when perform_disco is True).

None
http_client HTTPClient | None

Optional managed HTTP client (see validate_token for the cache-bypass caveats).

None
nonce str | None

The nonce the RP sent on the authorization request. When provided, the token's nonce MUST match it (§3.1.3.7 step 11).

None
access_token str | None

The access token from the same response. When provided, the token's at_hash MUST match it (§3.3.2.11).

None
code str | None

The authorization code from the same response. When provided, the token's c_hash MUST match it (§3.3.2.11).

None
max_age int | None

The max_age the RP requested. When provided, auth_time MUST be present and recent enough (§3.1.3.7 step 12).

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. audience should be the RP's client_id and issuer the OP issuer.

required
disco_doc_address str | None

Discovery document address (required when perform_disco is True).

None
http_client AsyncHTTPClient | None

Optional managed async HTTP client (see validate_token for the cache-bypass caveats).

None
nonce str | None

The nonce the RP sent on the authorization request. When provided, the token's nonce MUST match it (§3.1.3.7 step 11).

None
access_token str | None

The access token from the same response. When provided, the token's at_hash MUST match it (§3.3.2.11).

None
code str | None

The authorization code from the same response. When provided, the token's c_hash MUST match it (§3.3.2.11).

None
max_age int | None

The max_age the RP requested. When provided, auth_time MUST be present and recent enough (§3.1.3.7 step 12).

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 alg value from the ID Token's JOSE header, used to select the hash for at_hash/c_hash.

required
client_id str | None

The relying party's client_id (i.e. TokenValidationConfig.audience). When set, an azp present in the token MUST equal it (§3.1.3.7 step 6).

None
nonce str | None

When provided, the token's nonce MUST be present and equal this value (§3.1.3.7 step 11).

None
access_token str | None

When provided, the token's at_hash MUST be present and match the left-half hash of this access token (§3.3.2.11).

None
code str | None

When provided, the token's c_hash MUST be present and match the left-half hash of this authorization code (§3.3.2.11).

None
max_age int | None

When provided, auth_time MUST be present and satisfy now - auth_time <= max_age + leeway (§3.1.3.7 step 12).

None
leeway float

Clock-skew tolerance (seconds) applied to the max_age check.

0.0
now float | None

Current POSIX time (seconds), injected for deterministic tests. Defaults to :func:time.time when omitted.

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.