2026-08-04 15:17:15 +02:00
|
|
|
# `gabriel20xx-shared-backend`
|
|
|
|
|
|
|
|
|
|
Shared Python backend infrastructure for services using `asyncpg`:
|
|
|
|
|
|
|
|
|
|
- validated `DATABASE_URL` and pool settings;
|
|
|
|
|
- one async pool owner with ping and graceful shutdown;
|
|
|
|
|
- bcrypt password and secure session-token helpers;
|
2026-08-05 14:57:01 +02:00
|
|
|
- short-lived, one-time admin reset codes printed to the server console;
|
2026-08-31 23:06:07 +02:00
|
|
|
- common account/session persistence for the single-admin or optional multi-user model;
|
|
|
|
|
- an `authclient` module for services that adopt AuthGuard, the centralized
|
|
|
|
|
identity service, instead of this package's own local Postgres-backed
|
|
|
|
|
auth (see below).
|
2026-08-04 15:17:15 +02:00
|
|
|
|
|
|
|
|
HTTP framework adapters stay in each service. Install this package from the
|
2026-08-06 16:55:51 +02:00
|
|
|
published index or directly from the shared repository with a subdirectory
|
2026-08-04 15:17:15 +02:00
|
|
|
install during development:
|
|
|
|
|
|
|
|
|
|
```bash
|
2026-08-06 09:44:54 +02:00
|
|
|
pip install "git+https://repos.gfranz.ch/Gabriel20xx/shared@v0.1.0#subdirectory=packages/python-backend"
|
2026-08-04 15:17:15 +02:00
|
|
|
```
|
2026-08-31 23:06:07 +02:00
|
|
|
|
|
|
|
|
## `authclient`: adopting AuthGuard instead of local auth
|
|
|
|
|
|
|
|
|
|
`shared_backend.authclient` is how a service trades its own local user table
|
|
|
|
|
for one HTTP call per request to a centralized AuthGuard instance's
|
|
|
|
|
`GET /api/session/validate`, gaining a single shared identity across every
|
|
|
|
|
project that does the same. It mirrors `shared/backend/authclient` (Go) and
|
|
|
|
|
`shared/packages/node-backend/src/authclient.js` (Node) — same shape,
|
|
|
|
|
translated to Python idioms. It's additive: switching to it is a deliberate
|
|
|
|
|
per-project decision, and no project is wired up to it automatically.
|
|
|
|
|
|
|
|
|
|
`AuthClient.validate` is `async` (stdlib `urllib` under the hood, run off
|
|
|
|
|
the event loop via `asyncio.to_thread`, so no new HTTP dependency), for the
|
|
|
|
|
same reason the Go/Node versions are async — it composes with an async
|
|
|
|
|
request handler instead of blocking it:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from shared_backend import AuthClient, AuthClientError, InvalidSessionError
|
|
|
|
|
|
|
|
|
|
auth_client = AuthClient("https://authguard.example.com")
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
user = await auth_client.validate(token) # AuthGuardUser(id, username, role)
|
|
|
|
|
except InvalidSessionError:
|
|
|
|
|
... # no/expired/unknown session - fail closed (401)
|
|
|
|
|
except AuthClientError:
|
|
|
|
|
... # AuthGuard unreachable or returned something unexpected - also fail closed
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
`token_from_request(headers, cookies)` extracts a token the same way the
|
|
|
|
|
Go/Node versions do — an `Authorization: Bearer <token>` header first,
|
|
|
|
|
falling back to a cookie (`auth_session` by default) — and `resolve_user`
|
|
|
|
|
combines that with `validate`. Both are plain functions over
|
|
|
|
|
headers/cookies mappings, not tied to any web framework (this package's
|
|
|
|
|
adapters stay in each service, per the note above); wire them into whatever
|
|
|
|
|
your framework's request pipeline looks like, e.g. as a FastAPI dependency:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
from fastapi import Depends, HTTPException, Request
|
|
|
|
|
from shared_backend.authclient import resolve_user
|
|
|
|
|
|
|
|
|
|
async def require_session(request: Request):
|
|
|
|
|
try:
|
|
|
|
|
return await resolve_user(auth_client, request.headers, request.cookies)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise HTTPException(status_code=401, detail="authentication required") from exc
|
|
|
|
|
|
|
|
|
|
@app.get("/me", dependencies=[Depends(require_session)])
|
|
|
|
|
...
|
|
|
|
|
```
|