auth/daemon_token_middleware.ts view source
(state: DaemonTokenState, deps: QueryDeps, log: Logger): MiddlewareHandler import {create_daemon_token_middleware} from '@fuzdev/fuz_app/auth/daemon_token_middleware.js'; Create middleware that authenticates via daemon token.
Checks the X-Daemon-Token header. Behavior:
- No header: pass through (don't touch existing context).
- Header present +
Origin/Refererpresent: discard the credential (browser context) and pass through — daemon tokens are loopback-only and never carry anOriginin production, so a header-bearing request is not a legitimate daemon caller. Mirrors the bearer guard:next()rather than 401, so downstream auth enforcement returnscredential_type_required(not a hard fail). Silent on the wire (anti-enumeration); inDEVonly, setsX-Fuz-Auth-Debug: daemon_token_discarded_browser_context. - Header present + Zod-invalid (malformed): soft-fail discard (pass through,
not 401) — mirrors the bearer guard and the Rust spine's
resolve.rs(None). Downstream a daemon-gated action returnscredential_type_required; a public action proceeds anonymous. - Header present + invalid value (not the current/previous token): soft-fail discard (pass through, not 401) — same downstream behavior.
- Header present + valid +
keeper_account_idnull (still pre-bootstrap after the lazy refresh): soft-fail discard (pass through, not 503) — mirrors the Rust spine'sresolve.rs(None), so the request falls through to anonymous and a daemon-gated action returnscredential_type_requireddownstream. - Header present + valid + ok: set `c.var.auth_account_id =
state.keeper_account_id
,CREDENTIAL_TYPE_KEY = 'daemon_token'` (overrides any existing session / bearer identity).
Acting-actor resolution + RequestContext construction are deferred
to the dispatcher's authorization phase. Multi-actor keeper accounts
surface actor_required from there if a daemon caller doesn't pass
an explicit acting value.
state
the daemon token runtime state
deps
query dependencies (pool-level db for keeper-account resolution)
log
the logger instance
Loggerreturns
MiddlewareHandler mutates
Hono— context - sets `ACCOUNT_ID_KEY`, `CREDENTIAL_TYPE_KEY`, and `AUTH_API_TOKEN_ID_KEY` on a valid token