@blumbo/auth
v0.1.1
Published
Blumbo platform: prove a request came through the edge, and read the identity it asserted.
Readme
@blumbo/auth
Two halves of "who is the caller", for an app running behind the Blumbo edge.
An app behind the gate never authenticates anyone. It does not verify a
token, hold a Clerk secret key, or decide who anyone is. Identity arrives in
X-Blumbo-* headers that the edge strips from the incoming request and
rewrites from the gate's answer, and this package is the one place that is
read, so every app reads it the same way.
Install
npm install @blumbo/authTwo entrypoints and no root export — import '@blumbo/auth' resolves to
nothing.
Server
import {
ANONYMOUS,
devIdentity,
GATEWAY_HEADER,
gatewaySecret,
IDENTITY_HEADERS,
identityFrom,
isFromGateway,
} from '@blumbo/auth/server'
// Once, at startup — not per request. A missing secret in production is then
// a failed deploy rather than a wall of 500s.
const GATEWAY_SECRET = gatewaySecret()
const DEV_IDENTITY = devIdentity()
app.use('*', (c, next) => {
if (!isFromGateway(c.req.header(GATEWAY_HEADER), GATEWAY_SECRET)) {
return c.text('Direct access is not permitted.', 403)
}
return next()
})
// Note the shape: `identityFrom` wants an object keyed `user` / `name` /
// `orgs`, not a bag of raw HTTP headers. Passing `c.req.header()` straight in
// typechecks and silently returns null forever. Take the names from
// `IDENTITY_HEADERS` so they cannot drift from what the gate sends.
const who = identityFrom({
user: c.req.header(IDENTITY_HEADERS.user),
name: c.req.header(IDENTITY_HEADERS.name),
orgs: c.req.header(IDENTITY_HEADERS.orgs),
}) ?? DEV_IDENTITY ?? ANONYMOUSThree things worth knowing:
- Call
gatewaySecret()from a module your server loads before it serves. Under a lazily-loaded route the throw happens on the first request instead of at boot, which gives you a deploy that passes its health check and then answers everything with a 500. - Exempt your health-check path from the gateway check. The platform probes it directly, so it cannot carry the header.
devIdentity()readsBLUMBO_DEV_USER/_NAME/_ORGSfor local work and throws if it is ever set in production, where a stand-in identity would be a complete authentication bypass.
Client
Session tokens live sixty seconds, and a background fetch cannot renew one:
the handshake that would is only issued for navigations. A page open longer
than a minute therefore loses its writes unless something in the page keeps a
live session. That is what this half is for.
import { authFetch, startAuth } from '@blumbo/auth/client'
// The publishable key is served by your own server at runtime, never bundled:
// a bundler inlines it, which makes the image specific to one Clerk instance.
const { publishableKey } = await (await fetch('/api/config')).json()
const fetcher = authFetch(await startAuth({ publishableKey }))
await fetcher('/api/save', { method: 'POST', body })startAuth returns null when no key is configured, and authFetch(null) is
plain fetch — so local development works with no Clerk instance at all.
authFetch retries once on a 401 with a force-minted token. That closes the
common case and not every case: a refresh timer is throttled in a background
tab and stops entirely while a device sleeps. Anything a user earned should
survive independently — hold it locally and flush it on next load rather than
reporting a failure.
