Home / Developers / Quickstarts / Python
Quickstart: Python
Verify the sign-in token, handle back-channel logout, and verify then handle lifecycle webhooks with authvertex. CPython 3.11 or newer.
Install
Coming — available at release. When the package is released, you will install it with:
pip install authvertex
pip install 'authvertex[fastapi]' # optional FastAPI adapter
pip install 'authvertex[django]' # optional Django adapterThe authvertex package verifies ID and access tokens, back-channel logout tokens and webhook signatures. It needs CPython 3.11 or newer and has one runtime dependency, cryptography. The WSGI and ASGI middleware need no extra.
Sign-in itself is standard OpenID Connect: any OIDC client library performs the authorization code flow with PKCE. The SDK checks the tokens you get back.
Configure
import os
import authvertex as av
ISSUER = os.environ["AUTHVERTEX_ISSUER"]
CLIENT_ID = os.environ["AUTHVERTEX_CLIENT_ID"]
# The issuer's signing keys, found through OIDC discovery. Cached, and refreshed when a token names a new key.
keys = av.RemoteJWKS(av.discover_jwks_uri(ISSUER))
tokens = av.TokenVerifier(ISSUER, CLIENT_ID, keys)
logout_verifier = av.LogoutVerifier(ISSUER, CLIENT_ID, keys)Verify the sign-in token
After your OIDC client library has exchanged the authorization code at the token endpoint, verify the ID token with the nonce you sent:
claims = tokens.verify_id_token(id_token, nonce=expected_nonce)
# claims.subject: the stable user ID. Never an email.
# claims.tenant_id: your own tenant ID for the organization, as you set it when you created the organization.
# claims.org_id and claims.membership_epoch: keep the highest membership_epoch seen per (subject, org_id); a lower one is stale.
# epochs is your own store: record_highest saves the value and returns False when a higher one is already stored.
if not epochs.record_highest(claims.subject, claims.org_id, claims.membership_epoch):
raise PermissionError("stale membership: sign in again") # fail closed: do not create the session
sessions.create(user_id=claims.subject, tenant_id=claims.tenant_id, session_id=claims.session_id)The verifier checks the signature, iss, aud, exp, nbf and iat with a 60-second clock skew, the nonce, and the organization claims. Every failure raises AuthVertexError with a stable code; nothing returns a bare boolean. A verified token proves who the user is. What the user may do in your product is your own decision.

Back-channel logout
# End every local session with this session ID, or every session of the subject when it has none.
logout = av.LogoutHandler(
logout_verifier,
on_logout=lambda t: sessions.end(session_id=t.session_id, user_id=t.subject),
)The handler is a WSGI application, or call logout.handle(method, content_type, body) from any framework; it returns (status, headers, body). It answers 200, or 400 with a JSON OAuth error, always with Cache-Control: no-store. If on_logout raises, the token is released so the retry is accepted. A token you already processed is answered 200 without calling you again (AuthVertex is retrying after a lost 200); while its first delivery is still running, a repeat gets 503 with Retry-After. MemoryReplayCache protects one process; with several replicas, implement ReplayCache on a shared store and pass it as replay=.
Webhooks: verify, then handle
Webhook endpoints are signed with scheme v2 by default: AuthVertex signs each delivery and you verify it with AuthVertex's published public keys, so you hold no shared secret. The signature is bound to your endpoint ID, which you receive when you register the endpoint.
import urllib.request
APPLICATION_ID = os.environ["AUTHVERTEX_APPLICATION_ID"]
# AuthVertex's webhook signing keys. Old and new keys overlap during a rotation: reload them at most every five minutes.
with urllib.request.urlopen(os.environ["AUTHVERTEX_API_URL"] + "/v1/webhook-signing-keys") as response:
signing_keys = av.webhook_keys_from_jwks(response.read())
def handle_event(event: av.Event) -> None:
# The signature, timestamp window and event ID were checked; duplicates never reach this point.
if event.application_id != APPLICATION_ID:
return
if event.type in ("membership.suspended", "membership.deprovisioned", "session.revoked"):
sessions.end_all_for_membership(event.membership_id)
elif event.type == "membership.updated" and "roles" in event.raw.get("changed", []):
roles.refresh(event.membership_id) # role values are never sent
# Other types, including types newer than your code: acknowledge and ignore.
webhooks = av.WebhookHandler(
ed25519_keys=signing_keys,
endpoint_id=os.environ["AUTHVERTEX_WEBHOOK_ENDPOINT_ID"],
require_v2=True, # refuse deliveries without a v2 signature
on_event=handle_event,
)The handler answers 401 for a bad signature, 400 for a malformed body, 413 when the body is too large, 200 for a duplicate (on_event is not called twice) and 500 when on_event raises. Return only once the change is stored: AuthVertex retries anything but a 2xx. With several replicas, pass a shared dedupe= cache.
There is no typed events package for Python yet: event carries the envelope fields, membership_id, lifecycle_epoch and revocation_id, and event.raw holds the whole verified body. Every field is listed in the webhook event catalog.
After you have enforced a suspension, deprovisioning or session revocation, report it with Acknowledge revocation, using event.revocation_id.
Endpoints that keep a shared secret (scheme v1 or v1+v2) pass {kid: secret} as the first argument instead of, or next to, ed25519_keys.
Mount the receivers
# Any WSGI application. The receivers read the raw body themselves.
app.wsgi_app = av.mount(app.wsgi_app, logout=logout, webhooks=webhooks)
# POST /auth/backchannel-logout and POST /webhooks/authvertexFor ASGI applications, authvertex.asgi.AuthVertexASGI; for FastAPI, authvertex.fastapi.receivers_router; for Django, authvertex.django.receiver_urls.
Errors
Every failure raises AuthVertexError with a stable code, the same in every AuthVertex SDK. keys_unavailable means the issuer's keys could not be fetched: treat it as a temporary failure, never as success. See Error codes.
Next steps
- Run the flow end to end in the sandbox.
- Test your logout and webhook endpoints with the conformance kit.
- The integration guide covers client registration, claims and webhooks in full.