Tokens, keys and the doors
Three ways to present a credential on /v1 — a token pair with rotating refresh, an API key for a component, and the signup and anonymous doors a database declares — and what kills each of them.
Every route except the auth doors and a signed object URL wants a credential. Three presentations exist, and they compose.
The token pair
curl -X POST http://127.0.0.1:8000/v1/auth/signin \
-H 'Content-Type: application/json' \
-d '{"user": "ada", "password": "…"}'
# {"token":"…","refresh":"…","expires_in":43200}
A signin answers a pair: an access token (12 hours) and a refresh token
(30 days). Send the access token as Authorization: Bearer <token>. When it
expires — or a request answers 401 — trade the refresh for a new pair:
curl -X POST http://127.0.0.1:8000/v1/auth/refresh \
-H 'Content-Type: application/json' \
-d '{"refresh": "…"}'
# {"token":"…","refresh":"…","expires_in":43200}
Rotation is single-use: the presented refresh is retired and both halves are minted anew. A used refresh presented again inside a 10-second window is read as a retry and rotates again; past that window it is read as a copy, and the whole session family — every token and refresh descended from that signin — is revoked. If two devices hold the same refresh, the second one to use it ends the session for both: a refresh token is for one holder.
POST /v1/auth/signout revokes the family the presented token belongs to,
refresh included. Both halves also die with the node and with any change to
the user record — a password change signs every device out.
API keys
A key is declared in the language and belongs to a user:
DEFINE API KEY checkout OF USER svc_checkout;
DEFINE API KEY widget PUBLISHABLE OF USER svc_widget;
The statement answers the key once — only its digest is stored, so it
cannot be read back later. Send it in the apikey header:
- Alone, the key runs as its user: the key is the credential.
- Beside a Bearer token, the token names the person and the key gates the component — both must be good. This is the shape for "our checkout service, acting for this user".
A SECRET key is refused 403 when the request carries browser markers (a
User-Agent containing Mozilla/) — a secret is for servers, and the refusal
catches the common accident of shipping one to a page. A PUBLISHABLE key is
built for the browser and is not refused; it should belong to a user whose
grants are narrow, because anyone who reads your page holds it.
Keys skip the sign-in throttle — a 256-bit key is not guessable the way a password is — and die with the user record like a token.
The doors: signup and anonymous
A database opts into self-service accounts by declaring its doors:
DEFINE APP AUTH ON DATABASE app.main
ALLOW SIGNUP ROLE customer
ANONYMOUS AS USER guest;
With that declared:
curl -X POST http://127.0.0.1:8000/v1/app/main/auth/signup \
-H 'Content-Type: application/json' \
-d '{"user": "ada", "password": "…"}'
# {"token":"…","refresh":"…","expires_in":43200}
curl -X POST http://127.0.0.1:8000/v1/app/main/auth/anonymous
# {"token":"…","refresh":"…","expires_in":43200}
- Signup creates the user with the declared role's grants and no more — a
door never creates an owner, and a role that would is refused at declaration
time. A taken name answers
409. - Anonymous signs in as the declared anonymous user — one shared identity for "browsing before signin", useful for reads a grant opens to everyone.
- A database that declared no door answers both routes
404: the door is the declaration, and there is no implicit one.
Both doors spend the same sign-in budget as a password signin, so a script
hammering signup meets the same 429 a password guesser does.