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.0.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.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tynari_auth_sdk-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7214d3eeb5ce205162aa4ec7295a2a32f248e2772b5d9c71103b9a214322ad01
MD5 cf84e29536848bb625d91d48d4f8e95b
BLAKE2b-256 a2655b4d5eb435733453c219f81d27a1d74eb8b4793752e0d13597dd9b35b5ca

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tynari_auth_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db31523561de6596eb14015b0def01a062eb95d03d9425661e380070c650c807
MD5 56ab95de7a7092bbe4ff60e08b0a95fc
BLAKE2b-256 5b061e56921dee1128487672889d06e028a2348156b2fc5fda3a33cbe6520c97

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