**The API.** Every route now lives under `/api/v1`, with `/api/...` rewritten
onto it — one route, two spellings, so they cannot drift and the OpenAPI
document describes each endpoint once. Errors carry an `error` object with a
stable code, one human sentence and, for a validation failure, the fields that
were wrong; `detail` is untouched so nothing that reads it breaks. The whole
surface — 320 routes, their parameters and their status codes — is checked in
as `backend/tests/api-contract.json`, and a test fails on any difference,
naming the routes that moved. `docs/api.md` is the contract in prose.
**Refresh tokens**, so an app can stay signed in without keeping a password.
Rows rather than signatures: listable, withdrawable, stored as hashes, rotated
on every use. A spent token coming back ends the whole session, because a theft
and a replay look identical from the server and the safe reading is the unsafe
one. A browser is not given one — it has nowhere to put it and a person to ask.
**An end-to-end stack**: `docker-compose.test.yml` with its own Postgres and
Redis, `e2e/seed.py` for the smallest world the tests name, and Playwright with
five projects — desktop, iPhone, Pixel, iPad and a browserless API project.
Devices because every bug reported this week was a phone bug found by a person
looking at a screenshot; a desktop-only suite would have passed through all of
them. Forty tests, five clean runs.
It found four things in its first hour:
- **A fresh deploy could not start.** `create_all()` ran before
`CREATE EXTENSION vector`, so any database that had never had pgvector
installed died on the first table with a vector column. Invisible here
because this one has had the extension for a year.
- **A figure in a published article was a 404 for everyone but an admin.**
Media in the library is nobody's to read by default, and nothing made an
exception for a drawing an article actually shows — so every illustration
added this week was an empty box for every real user.
- **Every rate limit was one bucket for the whole site.** The backend saw
nginx's address for every request, so ten bad passwords from anybody locked
out everybody, and no log line could say who. nginx now takes the real
address from the proxy and overwrites the header on the way in; uvicorn runs
with --proxy-headers.
- **The reading page's breakpoints disagreed** — 1150px in the component,
820px in the stylesheet. Between them the menu button claimed the contents
drawer and then toggled a class on a rail that was still in the layout: the
contents did not open and the site menu did not either. The button was dead
on every tablet.
And two smaller ones: the login limiter counted successful sign-ins, so eleven
people behind one hospital NAT locked each other out — it is cleared by a
correct password now; and `/uploads/{path}` served GET and HEAD from one route
with one operation id, which makes every OpenAPI client generator refuse the
document.
The first admin's password is generated and printed once at first start when
`DEFAULT_ADMIN_PASSWORD` is blank, rather than the account not existing:
`docker compose logs backend | grep -A3 "FIRST ADMIN"`.
CI (`.forgejo/workflows/tests.yml`) runs the backend suite, the contract, the
frontend suite and the build on every push to dev, main or master, and the
end-to-end stack on those branches and on pull requests into them.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TqXevQJhxFrM7jJg82cgZN
106 lines
3.2 KiB
Python
106 lines
3.2 KiB
Python
from datetime import datetime
|
|
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
|
|
|
|
class UserCreate(BaseModel):
|
|
email: EmailStr
|
|
password: str
|
|
name: str
|
|
#: Solved hCaptcha challenge. Absent when the site has no secret configured.
|
|
captcha_token: str | None = None
|
|
#: Required only while the site is invite-only.
|
|
invite_code: str | None = None
|
|
|
|
|
|
class UserResponse(BaseModel):
|
|
id: int
|
|
email: str
|
|
name: str
|
|
role: str
|
|
is_unthrottled: int = 0
|
|
created_at: datetime
|
|
# Whether there is one at all, never anything about it. Settings has to say
|
|
# "Set a password" or "Change password", and it cannot tell from the outside.
|
|
has_password: bool = False
|
|
# What the role *means*, computed once here rather than in every page that
|
|
# asks. The interface had been checking `user.is_moderator` for months on a
|
|
# payload that has never carried it, so every moderator-only control was
|
|
# hidden from moderators — including the AI draft panel, which is why
|
|
# "Draft with AI" appeared to do nothing.
|
|
is_moderator: bool = False
|
|
is_admin: bool = False
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
@staticmethod
|
|
def of(user) -> "UserResponse":
|
|
return UserResponse(**{
|
|
"id": user.id, "email": user.email, "name": user.name, "role": user.role,
|
|
"is_unthrottled": user.is_unthrottled or 0, "created_at": user.created_at,
|
|
"has_password": bool(user.hashed_password),
|
|
"is_moderator": bool(user.is_moderator), "is_admin": bool(user.is_admin),
|
|
})
|
|
|
|
|
|
class Token(BaseModel):
|
|
access_token: str
|
|
token_type: str = "bearer"
|
|
#: Seconds, so a client can schedule its own refresh rather than waiting to
|
|
#: be told no. Absent means "we did not say" — not "it never expires".
|
|
expires_in: int | None = None
|
|
#: Only when one was asked for. A browser does not need it: it has a
|
|
#: session it can renew by asking the person again. An app does.
|
|
refresh_token: str | None = None
|
|
|
|
|
|
class LoginRequest(BaseModel):
|
|
email: EmailStr
|
|
password: str
|
|
#: An app asks for a refresh token; the web app does not, so nothing
|
|
#: long-lived is minted for a browser that will never use it.
|
|
refresh: bool = False
|
|
#: How the client describes itself, shown to the person in their list of
|
|
#: sessions. "PedsHub for iPhone", not a user agent string.
|
|
device: str | None = Field(default=None, max_length=120)
|
|
|
|
|
|
class RefreshRequest(BaseModel):
|
|
refresh_token: str
|
|
|
|
|
|
class LogoutRequest(BaseModel):
|
|
#: Ends this session. Omit it and, with `everywhere`, all of them.
|
|
refresh_token: str | None = None
|
|
everywhere: bool = False
|
|
|
|
|
|
class UserUpdateRole(BaseModel):
|
|
role: str
|
|
|
|
|
|
class UserUpdateMe(BaseModel):
|
|
name: str | None = None
|
|
current_password: str | None = None
|
|
new_password: str | None = None
|
|
|
|
|
|
class ForgotPasswordRequest(BaseModel):
|
|
email: EmailStr
|
|
|
|
|
|
class ResetPasswordRequest(BaseModel):
|
|
token: str
|
|
new_password: str
|
|
|
|
|
|
class LoginCodeRequest(BaseModel):
|
|
email: EmailStr
|
|
|
|
|
|
class LoginCodeVerify(BaseModel):
|
|
email: EmailStr
|
|
#: Typed by a person, so it arrives in whatever shape they typed it and is
|
|
#: normalised before it is compared.
|
|
code: str
|