Skip to main content

RS256 JWT auth client for FastAPI services — validate tokens via public key / JWKS, no database needed

Project description

FastAPI Auth Client

RS256 JWT auth client for FastAPI services. No database, no Redis, no SMTP.

Validates JWT tokens locally using the RSA public key obtained from the auth service's JWKS endpoint — no shared secret, no network call on every request.

Install

pip install fastapi-auth-client

Usage

from fastapi import FastAPI, Depends
from fastapi_auth_client import AuthClient

app = FastAPI()

auth = AuthClient(
    jwks_url="http://auth:8080/.well-known/jwks.json",
    # Or provide the public key directly:
    # jwt_public_key="-----BEGIN PUBLIC KEY-----\nMIIB...\n-----END PUBLIC KEY-----",
)

# Protect any route
@app.get("/api/data")
def get_data(user=Depends(auth.require_user)):
    return {"user": user["sub"], "role": user["role"]}

@app.get("/api/admin")
def admin_only(user=Depends(auth.require_admin)):
    return {"admin": user["sub"]}

# Optional: for guests and authenticated users alike
@app.get("/api/public")
def public_route(user=Depends(auth.optional_user)):
    if user:
        return {"message": f"Hello {user['sub']}"}
    return {"message": "Hello guest"}

How it works

  • The auth service signs JWTs with an RSA private key (RS256).
  • This client verifies them with the corresponding public key, fetched from the auth service's /.well-known/jwks.json endpoint.
  • No shared secret, no database connection, no Redis — zero infrastructure.
  • JWKS response is cached (default: 1 hour) to avoid fetching on every request.

Optional: Real-time revocation

For endpoints that need immediate token revocation checks, enable introspection:

auth = AuthClient(
    jwks_url="http://auth:8080/.well-known/jwks.json",
    auth_service_url="http://auth:8080",
    enable_introspection=True,  # calls /api/auth/introspect on every request
)

Token payload

The user dict contains:

  • sub — username
  • uid — user_id
  • role — "user" or "admin"
  • token_type — "access"
  • token_ver — token version (used for revocation)
  • jti — unique token ID
  • exp — expiry timestamp

发布到 PyPI

PyPI 账号和 API Token 由项目 Owner 保管。

首次发布

# 安装发布工具
pip install build twine

# 构建
python -m build

# 上传(输入 username: __token__,password: 你的 API Token)
python -m twine upload dist/*

以后更新代码后重复 python -m build && python -m twine upload dist/* 即可。

版本管理

  • 0.1.0 — 初始版本
  • 0.1.1 — bug 修复
  • 0.2.0 — 新增功能
  • 1.0.0 — 稳定版

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_auth_client-0.1.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_auth_client-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_auth_client-0.1.0.tar.gz.

File metadata

  • Download URL: fastapi_auth_client-0.1.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.14

File hashes

Hashes for fastapi_auth_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ded82f25503d18189d3608caf1146062bcc98be7387b2f015bc94985f190a99
MD5 a61cc26e395cbbf248a13896ee876608
BLAKE2b-256 49d979fda85cf191b57dc6a60ec19599bd21cddf53303c47516446f6e08ca85a

See more details on using hashes here.

File details

Details for the file fastapi_auth_client-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_auth_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6e879df422d7e923f3115657b2c488d764444a19990b59aafd6d966654e108c9
MD5 c432bee83af7365aebe3e67ca3e323b4
BLAKE2b-256 b3b5a5e2a043fafb751c655b6e2b9ac6eb2de0c4a9eb4d90852ca39455bfc975

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page