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
- Reads the access token from
Authorization: Bearer …orq_access_token, and the refresh token fromq_refresh_token. - Builds a per-request
QelosSDKbound to those tokens (create_request_sdk). - Calls
authentication.get_logged_in_user()andworkspaces.get_list(). - Picks the active workspace (first list item by default, or your
resolve_workspacecallable). - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file qelos_integrator_fastapi-4.0.0.tar.gz.
File metadata
- Download URL: qelos_integrator_fastapi-4.0.0.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6cb81334aa32d6ded43e38305d2db731cad37ce1e1e2f04e946ef0a1d241f1a3
|
|
| MD5 |
18a1a8e08697968a158bf68981595a6d
|
|
| BLAKE2b-256 |
c5e0079c265a4d3d349718d843ee2b614265fa3272b950818755a2a0f7b9933c
|
File details
Details for the file qelos_integrator_fastapi-4.0.0-py3-none-any.whl.
File metadata
- Download URL: qelos_integrator_fastapi-4.0.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f2080ffeecfa44775d7f751192fe38fca8adeccc5905ca011c75f590e3f0dc24
|
|
| MD5 |
0b3fede15e20f638a6c5eb2d8d0c41dc
|
|
| BLAKE2b-256 |
9d1f1a75306414b366f945f74410952fe2b4b7b00f2229168b012810e8431cad
|