pygrindvakt
Python bindings for grindvakt 0.9.1, a runtime-agnostic Rust library for OAuth 2.0, OpenID Connect and OpenID Federation 1.1: an OpenID Provider engine, a Relying Party toolkit, federation trust-chain resolution, DPoP (RFC 9449), and the JOSE/key primitives underneath. Built with PyO3 0.29 + maturin (abi3, Python ≥ 3.10).
The binding mirrors grindvakt's modules as Python submodules:
pygrindvakt.{http, keys, client, metadata, request, tokens, provider, dpop, rp, federation, discovery, jwt, pkce, mac, util}. It is web-framework agnostic:
requests come in as plain dicts/strings, responses go out as a Response
(status, headers, body), and the same Provider object works under Flask,
Django, FastAPI, or anything else. See examples/ for complete adapters.
Example - an OpenID Provider token endpoint (Flask)
from flask import Flask, request, Response as FlaskResponse
from pygrindvakt import OAuthError, client, keys, metadata, provider, tokens
ISSUER = "https://op.example.com"
op = provider.Provider(
metadata.ProviderMetadata(ISSUER),
keys.signing_key_from_pem(open("op-key.pem", "rb").read(), kid="op-1"),
client.InMemoryClientStore(
[
client.Client(
"demo",
client_secret="s3cret",
redirect_uris=["https://rp.example.com/cb"],
),
]
),
tokens.TokenCodec("a-long-random-secret"),
token_use_store=provider.InMemoryTokenUseStore(), # or RedisStore(...) across workers
)
app = Flask(__name__)
def to_flask(r):
return FlaskResponse(r.body, status=r.status, headers=r.headers)
@app.post("/token")
def token():
try:
tr = op.handle_token_request(
list(request.form.items(multi=True)),
f"{ISSUER}/token", # from config, never from Host
auth_header=request.headers.get("Authorization"),
)
return to_flask(tr.to_response())
except OAuthError as e:
return to_flask(e.to_response()) # correct status, JSON body, no-store
Discovery, JWKS, authorization and userinfo follow the same shape:
op.discovery_document(), op.jwks_document(),
op.validate_authorization_request(req) / op.authorization_redirect(req, sub, claims),
op.userinfo(access_token).
Example - a Relying Party
from pygrindvakt import http, rp, pkce, util
http_client = http.ReqwestClient() # never follows redirects, bounded body
md = rp.discover(http_client, "https://op.example.com")
info = rp.ProviderInfo.from_metadata(md)
me = rp.RpClient("demo", "https://rp.example.com/cb", client_secret="s3cret")
state, nonce, verifier = (
util.random_token(24),
util.random_token(24),
util.random_token(32),
)
url = rp.authorization_url(
info, me, state, nonce, code_challenge=pkce.s256_challenge(verifier)
)
# ... redirect the browser to `url`; on the callback:
tokens_ = rp.exchange_code(http_client, info, me, code, code_verifier=verifier)
jwks = rp.fetch_jwks(http_client, info.jwks_uri, info.issuer)
claims = rp.verify_id_token(
jwks, tokens_.id_token, info.issuer, "demo", nonce, ["ES256"]
)
if info.userinfo_endpoint is not None: # OPs may omit UserInfo
userinfo = rp.fetch_userinfo(
http_client,
info.userinfo_endpoint,
tokens_.access_token,
claims["sub"],
info.issuer,
)
Note:
from pygrindvakt import httpshadows the standard-libraryhttppackage in that module. If you also needhttp.HTTPStatusand friends, usefrom pygrindvakt import http as gv_httporimport pygrindvakt.http.
Security:
verify_id_tokenrequires the expected nonce and an explicit signing-algorithm allowlist. PassingNoneraises unless you also passunsafe_skip_nonce_check=True, which emits aUserWarning. Other guards the binding adds on top of grindvakt: duplicateclient_ids and unknownClientfields are rejected, reserved id_token claim names inextra_claimsraise, and the built-in HTTP client refuses to follow redirects. Never sendstr(exc)of aGrindvaktErrorto a client; onlyOAuthError.to_response()/.to_redirect()output is client-safe. See the security guide.
Concurrency, workers and stores
grindvakt is async Rust; the Python API is synchronous. Each call runs on a
process-wide tokio runtime with the GIL released, so threads (gunicorn gthread,
FastAPI's threadpool) run in parallel. The runtime is rebuilt lazily after
fork, so a Provider built in a gunicorn master keeps working in workers,
unless it uses a RedisStore, which is process-bound and fails closed in a
forked child (build it and its Provider in each worker).
http.ReqwestClient is frozen and safe to share across threads (the GIL is
released during I/O, so calls run in parallel); http=None uses a process-wide
default instance with the same properties. Its connection pool is keyed by PID
and rebuilt lazily in a forked child, inherited connections are never reused,
and a fork while another thread has a request in flight is not supported. Fork only while no other thread is inside a pygrindvakt call: an
internal lock held by another thread at fork time stays locked in the child
and can deadlock it. Create the ReqwestClient before or after fork, but a RedisStore
after. A Python HTTP client runs on the calling thread holding the GIL, so if
you share one across threads it must be thread-safe itself; re-entrant calls
into pygrindvakt from it cost a helper thread. See
ADR 0001.
State that must be shared across processes:
- Token-use store (one-time codes / refresh tokens / assertion
jtis):provider.RedisStore(url)(construct it after fork) or any object withconsume(token_hash, ttl_secs) -> bool. - Client store:
client.InMemoryClientStoreor any object withget(client_id)/put(client)(Django ORM, a cache, ...). - DPoP replay store:
dpop.InMemoryReplayStoreor any object withrecord(jti, ttl_secs) -> bool. - Outbound HTTP:
http.ReqwestClientor any object withget(url)/post_form(url, form, headers)returning(status, body, content_type).
Python-implemented stores fail closed: an exception is logged through
sys.unraisablehook and surfaces to the client as server_error.
HSM / PKCS#11 signing
from pygrindvakt import keys
key = keys.signing_key_from_pkcs11(
"/usr/lib/softhsm/libsofthsm2.so", "1234", "op-signing-key", "ES256", kid="hsm-1"
)
Prefer building the wheel on (or against) the target host when deploying with an HSM: the PKCS#11 module is
dlopen-ed at runtime from that host.
Development
Installs must go through sfw (Socket Firewall).
A Rust toolchain matching Cargo.toml's rust-version (1.88 or newer) is required.
sfw uv sync --frozen --only-group dev # the locked test / lint / typecheck tools, as in CI
sfw uv pip install --python .venv/bin/python "maturin==1.14.1"
VIRTUAL_ENV=$PWD/.venv .venv/bin/maturin develop --release --uv
.venv/bin/python -m pytest tests/ # PKCS#11 / Redis tests auto-skip
cargo clippy --release -- -D warnings && cargo fmt --check
REDIS_URL=redis://127.0.0.1/ .venv/bin/python -m pytest tests/ -m redis
just wraps the same gates: just check runs ruff lint, ruff format check,
mypy --strict over tests/typecheck_usage.py, pytest and just docs-version
(which fails if the grindvakt version documented here and in docs/index.rst
has drifted from Cargo.lock, or the docs/conf.py fallback release from
Cargo.toml; it does not read CHANGELOG.md); just --list shows the rest. just release <version> creates and pushes the signed tag that triggers the release workflow
(it refuses a tag that disagrees with Cargo.toml / pyproject.toml).
When the grindvakt dependency is bumped, update the version in this README, in
docs/index.rst (prose and the compatibility table) and in CHANGELOG.md in
the same commit; just docs-version checks the README and docs/index.rst, not
the changelog.
Documentation
Guides and the full API reference: https://pygrindvakt.readthedocs.io/en/latest/.
Design decisions live in docs/adr/; release-by-release changes, including
every breaking change, in CHANGELOG.md.
Type stubs
The package ships .pyi stubs for every submodule (py.typed marks the package as fully typed).
License
BSD-2-Clause.
Metadata
Release files for pygrindvakt 0.3.0
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 |
|---|---|---|---|---|
| pygrindvakt-0.3.0.tar.gz | 538.3 kB | With provenance | Details for pygrindvakt-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pygrindvakt-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl | CPython 3.10 | abi3 | Linux glibc 2.28+ x86-64 | Details for pygrindvakt-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl |
Total release size: 4.6 MB
Release files / pygrindvakt-0.3.0.tar.gz
| Download URL | pygrindvakt-0.3.0.tar.gz |
|---|---|
| Size | 538.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
85758343f73a7965354f2a66eea3ea7991d58184f36f02d2fd5ad631a33c9828
|
|
BLAKE2b-256 checksum How to use checksums |
c00865aef8e4f8985c5af43cc60694d03e689c44ce652cc758316cf896b26a9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.
Transparency logRelease files / pygrindvakt-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl
| Download URL | pygrindvakt-0.3.0-cp310-abi3-manylinux_2_28_x86_64.whl |
|---|---|
| Size | 4.1 MB |
| Tags | CPython 3.10 Linux glibc 2.28+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
74172779105a7490ed3a6cb91a746b4062caf6263b8ccd9196838a54cac5f0dd
|
|
BLAKE2b-256 checksum How to use checksums |
c586f5501a6f6f204a1d6ad040dfb02f342f65ca414cf64b16a3052336019d07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.
Transparency logSecurity for pygrindvakt 0.3.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for pygrindvakt-0.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.
Transparency logA 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.