py-identity-model
Production-grade OIDC/OAuth2.0 client library for Python. Dual sync/async API with comprehensive RFC coverage. Inspired by Duende.IdentityModel.
OpenID Certified
py-identity-model is OpenID Certified® by the OpenID Foundation as a Relying Party (RP) for the Basic RP, Config RP, and Form Post Basic RP conformance profiles (certified 2 July 2026, py-identity-model 3.1.0). See the certification docs for the conformance harness and submission process.
OpenID Certified is a certification mark of the OpenID Foundation.
Installation
pip install py-identity-model
Or with uv:
uv add py-identity-model
Requirements: Python 3.12+
Features
Protocol Support
| Feature | RFC/Spec | Status |
|---|---|---|
| OpenID Connect Discovery 1.0 | OIDC Discovery | Done |
| JSON Web Key Sets | RFC 7517 | Done |
| JWT Validation | RFC 7519 | Done |
| Client Credentials Grant | RFC 6749 | Done |
| Authorization Code + PKCE | RFC 7636 | Done |
| Refresh Token Grant | RFC 6749 | Done |
| Token Introspection | RFC 7662 | Done |
| Token Revocation | RFC 7009 | Done |
| Device Authorization | RFC 8628 | Done |
| Token Exchange | RFC 8693 | Done |
| DPoP | RFC 9449 | Done |
| Pushed Authorization Requests | RFC 9126 | Done |
| JWT Secured Authorization Request | RFC 9101 | Done |
| JWT Secured Authorization Response Mode (JARM) | JARM | Done |
| private_key_jwt Client Authentication | RFC 7523 | Done |
| Mutual-TLS Client Auth + Certificate-Bound Tokens | RFC 8705 | Done |
| FAPI 2.0 Security Profile | FAPI 2.0 | Done |
| UserInfo Endpoint | OIDC Core | Done |
| OAuth Callback State Validation | RFC 6749 | Done |
| AS Issuer Identification (Mix-Up Defense) | RFC 9207 | Done |
| Policy-Based Configuration | — | Done |
Architecture
- Dual API: Synchronous (
py_identity_model) and asynchronous (py_identity_model.aio) - Thread-safe: Sync uses thread-local HTTP clients; async uses singleton with lock protection
- Modular: Shared business logic in
core/, thin sync/async HTTP wrappers - Connection pooling: httpx-based with configurable timeouts and retry logic
Quick Start
Discovery
from py_identity_model import DiscoveryDocumentRequest, get_discovery_document
response = get_discovery_document(DiscoveryDocumentRequest(address="https://issuer.example.com"))
if response.is_successful:
print(f"Token Endpoint: {response.token_endpoint}")
Token Validation
from py_identity_model import TokenValidationConfig, validate_token
config = TokenValidationConfig(perform_disco=True, audience="my-api")
claims = validate_token(jwt=token, token_validation_config=config, disco_doc_address="https://issuer.example.com")
Async API
from py_identity_model.aio import get_discovery_document, validate_token
from py_identity_model import DiscoveryDocumentRequest
response = await get_discovery_document(DiscoveryDocumentRequest(address=url))
Client Credentials
from py_identity_model import ClientCredentialsTokenRequest, request_client_credentials_token
token = request_client_credentials_token(ClientCredentialsTokenRequest(
client_id="my-client", client_secret="secret",
address=token_endpoint, scope="api"
))
Examples
Each protocol feature has a standalone example in examples/:
| Example | Feature |
|---|---|
| sync_examples.py | Discovery, JWKS, token validation, client credentials |
| async_examples.py | Async versions of all core operations |
| auth_code_pkce_example.py | Authorization Code + PKCE flow |
| introspection_example.py | Token introspection (RFC 7662) |
| revocation_example.py | Token revocation (RFC 7009) |
| dpop_example.py | DPoP proof creation (RFC 9449) |
| par_example.py | Pushed Authorization Requests (RFC 9126) |
| jar_example.py | JWT Secured Authorization Request (RFC 9101) |
| jarm_example.py | JWT Secured Authorization Response Mode (JARM) |
| private_key_jwt_example.py | private_key_jwt client authentication (RFC 7523) |
| mtls_example.py | mTLS client auth + certificate-bound tokens (RFC 8705) |
| fapi_example.py | FAPI 2.0 Security Profile |
| device_auth_example.py | Device Authorization Grant (RFC 8628) |
| token_exchange_example.py | Token Exchange (RFC 8693) |
Framework Integration
py-identity-model is framework-agnostic — call validate_token (or py_identity_model.aio.validate_token) from any Python framework's auth layer. For FastAPI, examples/fastapi/ ships a complete, production-shaped integration you can copy into your own project:
TokenValidationMiddleware— Bearer token extraction, JWKS-cached validation, configurable excluded paths, pluggable custom-claim validators- Dependency injection —
CurrentUser,Claims,Tokenannotated types for typed access to the authenticated principal from route handlers - Authorization guards —
require_claim(type, value)andrequire_scope(scope)factories that plug intoDepends(...)to enforce RBAC/ABAC at the route level - Token refresh helper — Proactive refresh of expiring access tokens for service-to-service flows
from fastapi import Depends, FastAPI
from .middleware import TokenValidationMiddleware
from .dependencies import CurrentUser, require_scope
app = FastAPI()
app.add_middleware(
TokenValidationMiddleware,
discovery_url="https://issuer.example.com/.well-known/openid-configuration",
audience="my-api",
excluded_paths=["/health", "/docs", "/openapi.json"],
)
@app.get("/me")
async def me(user: CurrentUser) -> dict:
return {"sub": user.identity.name}
@app.get("/admin", dependencies=[Depends(require_scope("api.admin"))])
async def admin() -> dict:
return {"status": "ok"}
| Integration | Path | Highlights |
|---|---|---|
| FastAPI | examples/fastapi/ |
Middleware, DI, authorization guards, token refresh |
| Descope (FastAPI) | examples/descope/ |
Extends the FastAPI base with Descope-specific issuer/tenant handling |
Configuration
Environment Variables
| Variable | Default | Description |
|---|---|---|
HTTP_TIMEOUT |
30.0 | Request timeout (seconds) |
HTTP_RETRY_COUNT |
3 | Retries for rate-limited requests |
HTTP_RETRY_BASE_DELAY |
1.0 | Base delay for exponential backoff |
SSL_CERT_FILE |
— | CA bundle path (recommended) |
REQUESTS_CA_BUNDLE |
— | CA bundle path (legacy compat) |
Documentation
Full docs: jamescrowley321.github.io/identity-model
License
Apache License 2.0
Metadata
Release files for py-identity-model 4.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_identity_model-4.0.4.tar.gz | 157.3 kB | Without provenance | Details for py_identity_model-4.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_identity_model-4.0.4-py3-none-any.whl | Python 3 | none | any | Details for py_identity_model-4.0.4-py3-none-any.whl |
Total release size: 358.8 kB
Release files / py_identity_model-4.0.4.tar.gz
| Download URL | py_identity_model-4.0.4.tar.gz |
|---|---|
| Size | 157.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8976b7ca9feb16362ffbc1b1301ade21d99f360e6aa6033acfbf887b63395cec
|
|
BLAKE2b-256 checksum How to use checksums |
5f6605e83e832adcb77cca414c4a636e1361fc3f14a2c2f8f73afdce4c6e0eee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / py_identity_model-4.0.4-py3-none-any.whl
| Download URL | py_identity_model-4.0.4-py3-none-any.whl |
|---|---|
| Size | 201.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4f4bf27855f5316fb33e75d3f56c8423ba5422010730ae68d5073c040b47f31
|
|
BLAKE2b-256 checksum How to use checksums |
29626c5a7f09816167049b5936f82fc7a1b74872d00879da57a5169e26b5b528
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for py-identity-model 4.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.