Skip to main content

The three routes

The state service has three routes, one table, and Fastify underneath, plus the sign-in routes the vendored ward-auth library mounts under /api/auth/. Anything else is a 404. No static files, no directory listing, no fallback handler.

A service whose only job is to hold documents has no business serving anything else, and every route that does not exist is a route that cannot be wrong.

It is served behind handle_path /sports-app-api/*, which strips the prefix, so the service keeps serving its own bare /api/* paths.

MethodPathWhat it does
GET/api/healthLiveness. No auth, no database read, no information.
GET/api/stateThe caller's newest snapshot bytes, or 404.
PUT/api/stateValidate shallowly, store verbatim, prune to the cap.
GET/api/auth/sessionWho the cookie belongs to (user: null for nobody), and whether Ward is on. The library's.
POST/api/auth/login, logout, password, ward/*Sign in, sign out, change a password, the Ward round trip. The library's; there is no register.

Who the caller is​

/api/state needs a session. The service has its own accounts, and every route but GET /api/health and the sign-in routes under /api/auth/ is guarded by the sports_app_session cookie. A person gets one by signing in with a password, or with Ward, an optional second way in that the service reaches through OpenID Connect. Local sign-in never calls Ward, so the service works while Ward is down. The signed-in account's username is the stream key.

SituationAnswer
No session, or an expired one401
A session that began with Ward, while Ward cannot answer401, reason: "ward_unavailable"
A session that began with Ward, which Ward has ended401, reason: "ward_session_ended"

A Ward-originated session is checked against Ward at most every 30 seconds, so a revoke at Ward ends it within 30 seconds.

GET /api/auth/session says who the cookie belongs to. It is how the app learns who it is after Ward sends the browser back: the login page asks it, writes the username into the browser, and goes on to the app. The browser keeps that name, so the app opens offline afterwards. A Ward account with no sports-app grant never gets a session. Ward refuses it, and the callback lands on the login page with ?ward_error=access_denied, which the page puts into words.

What Ward changed​

The service used to hold one shared secret in a header — the same string for every caller, with ?user= naming whose document was wanted.

That secret authenticated the installation, never a person. Anyone holding it could read or write anyone's training history by changing a query parameter, and the file header said so plainly rather than pretending otherwise.

A session authenticates a person, and its account's username is the document key. (From 2026-09-06 to 2026-10-13 the key was the Ward subject; the service's own accounts replaced that.) So ?user= stops being a target a caller chooses and becomes a value the server already knows. The client no longer sends it, and the server ignores one if it is there. That is the whole security change, and it is the largest one in the repo's history.

The five things Fastify must not do​

On 2026-07-29 Fastify replaced hand-rolled node:http routing without changing one byte of the REST contract — same paths, methods, status codes, headers and bodies. The Ward cutover changed the contract later, on purpose. The 73 tests in server/__tests__/ are what make that claim checkable rather than hopeful: they were written against the wire, not the implementation.

Five framework conveniences are switched off. Each is load-bearing and each is pinned by a test.

  1. GET /api/state returns the stored bytes verbatim. Fastify would parse and re-serialise JSON for you, which destroys the codec's layout while remaining "valid JSON". So the JSON body parser is replaced with one that hands the handler the raw string, the stored document goes out as a Buffer, and there is deliberately no response schema on that route's 200 — a serialiser attached to it would rewrite the very bytes it exists to preserve. The client's crash-safe save round-trips through a byte comparison, so this is not cosmetic.
  2. The session is checked before any validation. It is a route-level onRequest hook — the earliest point in Fastify's lifecycle, ahead of body parsing and ahead of schema validation. A caller without a session gets a 401 and never a 400 that would say anything about the document. The not-found handler checks too, so an unauthorised caller cannot tell a 405 on /api/state from a 404.
  3. A document whose username is not the session's subject is a 400, not a 409, and stores nothing. It refuses a write into somebody else's stream as well as a confused client.
  4. Usernames are rejected, never case-folded. Folding would make the stream key disagree with the document's own username, which is exactly what (3) refuses. The rule lives in shared/username.ts and says why.
  5. Paths match exactly. ignoreTrailingSlash stays off and exposeHeadRoutes is switched off — otherwise Fastify would helpfully add HEAD /api/state, which today is a 405.
And one more, the loudest

Fastify logs requests by default. logger: false turns the framework's logging off completely, so there is no logger for a request body to reach even in principle. The only log in the process is a callback called with fixed strings the caller composes.

The test that pins it sends a distinctive password to the old login path and searches the database files and every captured log line for it.

Validation comes from one declaration​

The wire shapes are TypeBox declarations in shared/api.ts, so the same declaration that guards the service also types the client.

A Type.Object({...}) value is a JSON Schema object, so Fastify's AJV reads the response schemas directly with no adapter, and Static<typeof X> gives the TypeScript type from the same expression. Hand-written JSON Schema plus a separately maintained interface would recreate exactly the duplication shared/ exists to delete.

The PUT body is checked with TypeCompiler rather than by Fastify, because it must stay a raw string (see 1 above) and a body schema would require Fastify to have parsed it first. One declaration, two entry points into it — not two declarations.

The generated wire contract reference is TypeDoc over that package.