Skip to main content

Python SDK for Jengo Auth — session verification, FastAPI middleware, and role-based access control

Project description

jengolabs-auth

Python SDK for Jengo Auth — session verification, FastAPI middleware, and role-based access control.

Install

pip install jengolabs-auth[fastapi]

Quick Start (FastAPI)

Option 1: Dependency Injection

from fastapi import FastAPI, Depends
from jengolabs_auth import AuthClient, AuthClientConfig
from jengolabs_auth.fastapi import require_auth, require_app_role

auth_client = AuthClient(AuthClientConfig(
    auth_server_url="http://localhost:4000",
    tenant_api_key="your-tenant-api-key",
    app_slug="my-service",
    cache_ttl_seconds=30,
))

app = FastAPI()

@app.get("/api/me")
async def get_me(session=require_auth(auth_client)):
    return {"id": session.user.id, "email": session.user.email}

@app.delete("/api/resource/{resource_id}")
async def delete_resource(
    resource_id: str,
    session=require_app_role("admin", auth_client=auth_client),
):
    return {"deleted": resource_id}

Option 2: Middleware

from fastapi import FastAPI, Request
from jengolabs_auth import AuthClient, AuthClientConfig
from jengolabs_auth.fastapi import JengoAuthMiddleware

auth_client = AuthClient(AuthClientConfig(
    auth_server_url="http://localhost:4000",
    tenant_api_key="your-tenant-api-key",
    app_slug="my-service",
))

app = FastAPI()
app.add_middleware(
    JengoAuthMiddleware,
    auth_client=auth_client,
    public_paths=["/health", "/docs", "/openapi.json"],
)

@app.get("/api/me")
async def get_me(request: Request):
    user = request.state.auth_user
    return {"id": user.id, "email": user.email}

Option 3: Manual Verification

from jengolabs_auth import AuthClient, AuthClientConfig, AuthenticationError

auth_client = AuthClient(AuthClientConfig(
    auth_server_url="http://localhost:4000",
    tenant_api_key="your-tenant-api-key",
    app_slug="my-service",
))

async def verify_token(token: str):
    try:
        session = await auth_client.verify_session(f"Bearer {token}")
        return session.user
    except AuthenticationError:
        return None

Models

All models are Pydantic v2 and support both snake_case and camelCase fields:

Model Fields
AuthUser id, email, name, email_verified, image, role, created_at, updated_at
AuthSessionData id, token, expires_at
AuthAppGrant app_slug, role, granted_at
AuthSessionWithGrant user, session, app_grant, organization

Session Caching

Enable in-memory caching to reduce auth server calls:

auth_client = AuthClient(AuthClientConfig(
    auth_server_url="http://localhost:4000",
    tenant_api_key="your-tenant-api-key",
    app_slug="my-service",
    cache_ttl_seconds=30,  # cache sessions for 30 seconds
))

License

MIT

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

jengolabs_auth-0.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

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

jengolabs_auth-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for jengolabs_auth-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0e9504852a93651f6973699472ca4cae617d178f5b66d95d1bc7ab42104e9307
MD5 cd68ba79f2f052d1171ed18196d9c9fb
BLAKE2b-256 e28fae2ee11a1b10b70c6dce69a68b8d7d138c80afc8ff8d43a88279c3746970

See more details on using hashes here.

File details

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

File metadata

  • Download URL: jengolabs_auth-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for jengolabs_auth-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d18278f65365e14a3568c5ae58c16481b8b138b07399047ef2e31e81d344a7e2
MD5 f7f80b8c56f6a69dfe22612f71c595c0
BLAKE2b-256 47b208d38889be87e0f03c2e16aa43a68722950df8083116b88563e51130c166

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