amber-backend/pb_hooks/whoami.pb.js

37 lines
1.8 KiB
JavaScript
Raw Normal View History

/// <reference path="../pb_data/types.d.ts" />
// Token introspection for amber-api (the facts service).
//
// GET /api/amber/whoami (auth: users) → { id, nsfwEnabled }
//
// WHY THIS EXISTS: amber-api has to know whether an inbound request carries a
// valid Amber user token before it will spend an ffprobe on a caller-supplied
// URL. PocketBase auth tokens are stateless JWTs signed with a per-user secret
// that only PocketBase holds, so **offline verification is impossible by
// design** — a third-party service cannot check one itself. The only correct
// check is to present the token here and see whether PocketBase accepts it,
// which `$apis.requireAuth("users")` does before this handler ever runs.
//
// So the handler body is deliberately trivial: reaching it *is* the answer. It
// returns the two facts amber-api needs and nothing else — no email, no
// username, no profile list. A token-introspection endpoint is a tempting place
// to leak account data, and amber-api has no business knowing any of it.
//
// `nsfwEnabled` is included because it's the same admin-only flag that gates
// adult release channels, and a future amber-api surface may need to scope
// results by it. It is read-only here.
//
// amber-api caches positives for ~10 minutes and negatives for ~30 seconds, so
// this route sees roughly one request per device per 10 minutes — cheap enough
// to sit in front of every probe lookup.
//
// NOTE: PocketBase runs each routerAdd handler in its own isolated JSVM, so
// anything a handler needs must be declared INSIDE it — file-scope helpers throw
// ReferenceError at request time. Nothing is hoisted here; keep it that way.
routerAdd("GET", "/api/amber/whoami", (e) => {
return e.json(200, {
id: e.auth.id,
nsfwEnabled: e.auth.getBool("nsfwEnabled"),
})
}, $apis.requireAuth("users"))