Skip to main content

FastAPI integrator for Qelos: middleware that identifies the user and active workspace from the Qelos SDK before your handlers run

Project description

qelos-integrator-fastapi

FastAPI / Starlette middleware that calls the qelos-sdk to identify the current user and their active workspace before your handlers run. The same contract as @qelos/integrator-express, @qelos/integrator-fastify, and the other Qelos integrators: attach context, resolve workspace, support token refresh.

Install

pip install qelos-integrator-fastapi qelos-sdk

Quick start

from typing import Annotated, Optional

from fastapi import Depends, FastAPI
from qelos_integrator_fastapi import (
    QelosConfig,
    QelosIntegratorMiddleware,
    QelosRequestContext,
    get_qelos,
    require_user,
)

app = FastAPI()
app.add_middleware(
    QelosIntegratorMiddleware,
    config=QelosConfig(app_url="https://yourdomain.com"),
)


@app.get("/me")
async def me(qelos: Annotated[Optional[QelosRequestContext], Depends(get_qelos)]):
    return {
        "user": qelos.user if qelos else None,
        "workspace": qelos.workspace if qelos else None,
    }


@app.get("/private")
async def private(qelos: Annotated[QelosRequestContext, Depends(require_user)]):
    return qelos.user

Context lives on request.state.qelos (QelosRequestContext). User and workspace are plain dicts matching the JSON returned by the Qelos API.

Behaviour

  1. Reads the access token from Authorization: Bearer … or q_access_token, and the refresh token from q_refresh_token.
  2. Builds a per-request QelosSDK bound to those tokens (create_request_sdk).
  3. Calls authentication.get_logged_in_user() and workspaces.get_list().
  4. Picks the active workspace (first list item by default, or your resolve_workspace callable).
  5. Attaches everything to request.state.qelos.

Anonymous requests leave user / workspace as None unless require_auth=True, which responds with 401 and body {"code": "UNAUTHORIZED"}.

Token refresh

When tokens rotate, the optional on_token_refresh hook runs. The default implementation schedules Set-Cookie headers on the outgoing response (HTTP-only, SameSite=Lax, Secure when app_url is HTTPS).

Because Starlette does not expose the final Response until after inner middleware runs, the default hook records cookies on an internal buffer that is flushed onto the real response when the request completes. Custom hooks receive the same buffer as TokenRefreshContext.response; call set_cookie on it if you write cookies from the hook.

Configuration

See QelosConfig in qelos_integrator_fastapi.config: app_url, optional api_token, cookie names, require_auth, skip_paths, and sdk_options (merged into QelosSDKOptions, with camelCase keys from Node configs normalized to snake_case).

Requirements

  • Python 3.9+
  • FastAPI / Starlette (middleware is ASGI-level)

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

qelos_integrator_fastapi-4.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

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

qelos_integrator_fastapi-4.0.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file qelos_integrator_fastapi-4.0.0.tar.gz.

File metadata

File hashes

Hashes for qelos_integrator_fastapi-4.0.0.tar.gz
Algorithm Hash digest
SHA256 6cb81334aa32d6ded43e38305d2db731cad37ce1e1e2f04e946ef0a1d241f1a3
MD5 18a1a8e08697968a158bf68981595a6d
BLAKE2b-256 c5e0079c265a4d3d349718d843ee2b614265fa3272b950818755a2a0f7b9933c

See more details on using hashes here.

File details

Details for the file qelos_integrator_fastapi-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for qelos_integrator_fastapi-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2080ffeecfa44775d7f751192fe38fca8adeccc5905ca011c75f590e3f0dc24
MD5 0b3fede15e20f638a6c5eb2d8d0c41dc
BLAKE2b-256 9d1f1a75306414b366f945f74410952fe2b4b7b00f2229168b012810e8431cad

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