Skip to content

Logout

OpenID Connect logout on the RP side: RP-Initiated Logout 1.0 (send the user agent to the OP's end-session endpoint and verify the round trip back) and Back-Channel Logout 1.0 (validate the Logout Token the OP POSTs directly to the RP).

RP-initiated logout

build_end_session_url builds the redirect to the OP's end_session_endpoint (from the discovery document); validate_post_logout_state verifies — in constant time — that the state echoed back to the post_logout_redirect_uri matches the one sent. Both are pure functions with no I/O, shared by the sync and async APIs.

from py_identity_model import build_end_session_url, validate_post_logout_state

logout_url = build_end_session_url(
    end_session_endpoint=disco.end_session_endpoint,
    id_token_hint=id_token,
    client_id=CLIENT_ID,
    post_logout_redirect_uri="https://app.example.com/logged-out",
    state=expected_state,
)
# Redirect the user agent to logout_url; then, on the post-logout callback:
validate_post_logout_state(expected_state, returned_state)  # raises on mismatch

Back-channel logout

validate_logout_token runs the standard JWT validation (signature via discovery/JWKS, iss, aud, exp) and then enforces the Logout-Token profile of Back-Channel Logout 1.0 §2.4: iss/aud/iat/jti/events must be present, the events object must contain the http://schemas.openid.net/event/backchannel-logout member, at least one of sub / sid must identify the session, and a nonce claim is prohibited (so a Logout Token can never pass as an ID Token). Set audience to your client_id and issuer to the OP issuer in the config.

from py_identity_model import TokenValidationConfig, validate_logout_token

claims = validate_logout_token(
    logout_token,  # the `logout_token` form field the OP POSTs to your backchannel_logout_uri
    TokenValidationConfig(perform_disco=True, audience=CLIENT_ID, issuer=ISSUER),
    disco_doc_address=DISCOVERY_URL,
)
sid = claims.get("sid")  # terminate the matching session(s)

Failures raise LogoutTokenValidationException (profile rule) or TokenValidationException (standard JWT failure) — both are caught by except TokenValidationException, so handlers fail closed. The state check raises LogoutStateValidationException.

RP-initiated logout API

build_end_session_url(end_session_endpoint, id_token_hint=None, client_id=None, post_logout_redirect_uri=None, state=None, logout_hint=None, ui_locales=None, **extra_params)

Build an OpenID Connect RP-Initiated Logout end-session URL.

Constructs the URL the RP redirects the End-User's user agent to in order to log out at the OP (OpenID Connect RP-Initiated Logout 1.0 §2). Only the parameters that are provided (non-None) are included; per the spec none of them is strictly required, so no parameter is hard-enforced here. When state is supplied it should be echoed back to post_logout_redirect_uri and checked with :func:validate_post_logout_state.

Parameters:

Name Type Description Default
end_session_endpoint str

The OP's end-session endpoint (typically DiscoveryDocumentResponse.end_session_endpoint).

required
id_token_hint str | None

A previously issued ID Token passed as a hint about the End-User's session (RECOMMENDED by the spec).

None
client_id str | None

The RP's client identifier.

None
post_logout_redirect_uri str | None

URL to redirect to after logout. Must be registered with the OP and, per spec, is only honoured when id_token_hint or client_id is also present.

None
state str | None

Opaque value round-tripped to post_logout_redirect_uri for CSRF protection.

None
logout_hint str | None

Hint about the End-User to log out (spec §2).

None
ui_locales str | None

Preferred languages for the logout UI.

None
**extra_params str

Additional query parameters.

{}

Returns:

Type Description
str

The full end-session URL ready for redirect.

Raises:

Type Description
ValueError

If end_session_endpoint is empty, or if extra_params collides with a reserved RP-Initiated Logout parameter.

validate_post_logout_state(expected_state, returned_state)

Validate the state returned to the post-logout redirect URI.

Verifies the RP-Initiated Logout state round-trip: the value returned by the OP to post_logout_redirect_uri must equal the value the RP sent to :func:build_end_session_url. The comparison uses hmac.compare_digest (constant-time) to avoid timing side-channels, mirroring :func:validate_authorize_callback_state.

Parameters:

Name Type Description Default
expected_state str | None

The state value the RP sent to the end-session endpoint (from the RP's session). None/empty means the RP has no stored state to compare against.

required
returned_state str | None

The state query parameter returned to the post-logout redirect URI.

required

Raises:

Type Description
LogoutStateValidationException

If expected_state is missing/empty, returned_state is missing/empty, or the two do not match.

Back-channel logout — Sync API

validate_logout_token(logout_token, token_validation_config, disco_doc_address=None, http_client=None)

Validate a Back-Channel Logout Token (Back-Channel Logout 1.0 §2.4).

Runs the standard JWT validation via validate_token (signature, iss, aud, iat and exp when present), then enforces the Logout-Token-specific rules: required events member, sub/sid presence, and the nonce prohibition.

Parameters:

Name Type Description Default
logout_token str

The compact-serialized Logout Token JWT received at the RP's backchannel_logout_uri.

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

Returns:

Name Type Description
dict dict

The decoded and validated Logout Token claims.

Raises:

Type Description
TokenValidationException

If standard JWT validation fails (bad signature, wrong issuer/audience, expired token).

LogoutTokenValidationException

If a Logout-Token-specific rule fails.

ConfigurationException

If the configuration is invalid.

Back-channel logout — Async API

validate_logout_token(logout_token, token_validation_config, disco_doc_address=None, http_client=None) async

Validate a Back-Channel Logout Token (async) (Back-Channel Logout 1.0 §2.4).

Runs the standard async JWT validation via validate_token (signature, iss, aud, iat and exp when present), then enforces the Logout-Token-specific rules: required events member, sub/sid presence, and the nonce prohibition.

Parameters:

Name Type Description Default
logout_token str

The compact-serialized Logout Token JWT received at the RP's backchannel_logout_uri.

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

Returns:

Name Type Description
dict dict

The decoded and validated Logout Token claims.

Raises:

Type Description
TokenValidationException

If standard JWT validation fails (bad signature, wrong issuer/audience, expired token).

LogoutTokenValidationException

If a Logout-Token-specific rule fails.

ConfigurationException

If the configuration is invalid.

Claim-level building block

For callers that validate the JWT themselves, the Logout-Token profile checks are available as a pure function over an already-validated claim set:

validate_logout_token_claims(claims)

Validate the Logout-Token-specific claim rules (Back-Channel Logout 1.0 §2.4).

Assumes the JWT signature, iss, aud, iat and (when present) exp have ALREADY been verified by the standard token-validation path. This function enforces only the rules unique to Logout Tokens:

  1. iss, aud, iat, jti and events MUST be present.
  2. events MUST be a JSON object containing the http://schemas.openid.net/event/backchannel-logout member.
  3. sub and/or sid MUST be present (at least one).
  4. A nonce claim MUST NOT be present (a Logout Token must not be usable as an ID Token).

Parameters:

Name Type Description Default
claims dict

The decoded (and already signature/iss/aud/exp-validated) JWT claim set.

required

Raises:

Type Description
LogoutTokenValidationException

If any Logout-Token rule is violated.

Exceptions

LogoutTokenValidationException(message, token_part=None, details=None)

Bases: TokenValidationException

Raised when Back-Channel Logout token validation fails.

Covers the Logout-Token-specific rules from OpenID Connect Back-Channel Logout 1.0 §2.4 (required events member, sub/sid presence, and the nonce prohibition) that sit on top of standard JWT validation.

LogoutStateValidationException(message, details=None)

Bases: ValidationException

Raised when the state returned to the post-logout redirect URI does not match the value sent to the end-session endpoint.

Covers the RP-Initiated Logout state round-trip check (OpenID Connect RP-Initiated Logout 1.0 §2/§3). It is a plain callback CSRF check — not a JWT/token error — so it subclasses ValidationException directly rather than TokenValidationException.