Skip to main content

JWT verification SDK for Tynari microservices — JWKS fetching, caching, RBAC, and FastAPI middleware

Project description

tynari-auth-sdk

JWT verification SDK for Tynari microservices. Handles JWKS fetching, key caching, automatic key rotation, JWT verification, and role-based access control.

Installation

pip install tynari-auth-sdk

Quick Start

from fastapi import FastAPI, Depends
from auth_sdk import AuthMiddleware, get_current_user, require_roles, TokenUser

app = FastAPI()

# Add authentication middleware
app.add_middleware(
    AuthMiddleware,
    jwks_url="https://auth.tynari.com/.well-known/jwks.json",
    exclude_paths={"/health", "/docs", "/openapi.json"},
)

# Protected endpoint — any authenticated user
@app.get("/me")
async def me(user: TokenUser = Depends(get_current_user)):
    return {"user_id": user.sub, "role": user.role}

# Role-restricted endpoint
@app.get("/artists-only", dependencies=[Depends(require_roles(["artist"]))])
async def artists_only():
    return {"message": "Welcome, artist!"}

Configuration

AuthMiddleware

Parameter Type Default Description
jwks_url str required URL to the JWKS endpoint
cache_ttl float 3600.0 Key cache TTL in seconds
exclude_paths set[str] set() Paths to skip authentication
auto_error bool True If False, sets request.state.user = None instead of returning 401

TokenUser

The verified user object attached to request.state.user:

Field Type Description
sub str User ID (UUID string)
role str User role (e.g. "artist", "seller")
scope str Token scope (always "access")
exp int Expiration timestamp

How It Works

  1. Middleware intercepts each request and extracts the Authorization: Bearer <token> header
  2. JWKS client fetches the RSA public key from your auth service (cached for 1 hour by default)
  3. Token verification validates the RS256 signature, checks expiration, and enforces scope == "access"
  4. User attachment puts the decoded TokenUser on request.state.user
  5. RBAC — use require_roles(["role1", "role2"]) as a FastAPI dependency to restrict access

Key rotation is handled automatically: if a token has an unknown kid, the SDK re-fetches the JWKS.

Publishing

pip install build twine
python -m build
twine upload dist/*

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

tynari_auth_sdk-0.1.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

tynari_auth_sdk-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file tynari_auth_sdk-0.1.1.tar.gz.

File metadata

  • Download URL: tynari_auth_sdk-0.1.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for tynari_auth_sdk-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2800aebf99da300a5ed37b2f5b47bb1938a0d6e310b682011becf844c1302c9d
MD5 b59361a21b7ce76848b04d8cf18710e7
BLAKE2b-256 915f835eb3c518d8959a287bb2ee134a94f1a81c86782944d344c19b352c24ba

See more details on using hashes here.

File details

Details for the file tynari_auth_sdk-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tynari_auth_sdk-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dd2f1f26aef127c6b7d7e1df47fdc2f26c62c1109ea97b90bc863e5111a2417f
MD5 e222507ca7af858c4c563c7908c80878
BLAKE2b-256 915c35ae7e3e727ade010b83092021b27443284fd7283a880e58459a565daba7

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