The panel now pairs, fetches its image and files its logs against this application rather than against the TRMNL cloud. Four endpoints: /api/setup issues a token on first contact, /api/display hands back an image and a wake interval, /api/log stores firmware diagnostics, and /api/device/image/<hash> serves the bytes. The wake interval is where freshness and battery are traded off. In BYOS nothing can be pushed: the device sleeps, wakes, asks and sleeps again. So the interval is short while the shop trades and long overnight, and it is shortened further whenever a change of state falls inside it — the door opening in twenty minutes means waking in twenty-one, whatever the base interval says. The image filename is the hash of its own bytes. The firmware skips the redraw when the name is unchanged, which is the whole battery strategy, and the URL is immutable, unguessable and safe to cache forever. Two integration tests pin this: unchanged data must yield the same filename and store one row, changed hours must yield a different one. MAC addresses are normalised before use. They are a primary key here, and firmwares are inconsistent about case and separators; without this a panel could register twice by capitalising itself differently. Header names are read in both the hyphen and underscore spellings for the same reason — the TRMNL docs and the Seeed sources disagree, and being liberal costs nothing while being wrong costs a blank shop window. Pairing is deliberately made to survive a rendering failure. The token is issued once and only its digest is kept, so a device stranded by a failed response would be registered yet hold no credential, and unable to register again. The welcome image is worth far less than that. This was found by running the flow, not by reading it. satori, yoga and harfbuzz are marked external: bundling rewrites the relative path satori uses to load its WebAssembly, and the renderer dies on a missing hb.wasm. The integration tests run against a real Postgres, in CI too. Mocking Prisma here would only prove the mock works. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
/**
|
|
* Resolving which device is calling.
|
|
*
|
|
* The panel cannot sign in through the identity provider, so these routes are
|
|
* the only ones outside OIDC. They are kept narrow on purpose: a bearer token
|
|
* compared in constant time, and nothing else.
|
|
*/
|
|
|
|
import { prisma } from '@/lib/db';
|
|
|
|
import { hashToken, normaliseMac, tokenMatches } from './auth';
|
|
import { deviceHeader } from './headers';
|
|
|
|
export type DeviceRow = Awaited<ReturnType<typeof prisma.device.findFirst>>;
|
|
|
|
export async function authenticateDevice(request: Request): Promise<NonNullable<DeviceRow> | null> {
|
|
const token = deviceHeader(request, 'access-token') ?? bearer(request);
|
|
if (!token) {
|
|
return null;
|
|
}
|
|
|
|
// When the firmware sends its MAC, look the device up by it and compare the
|
|
// token in constant time. That is the path the spec asks for.
|
|
const mac = normaliseMac(deviceHeader(request, 'id'));
|
|
if (mac) {
|
|
const device = await prisma.device.findUnique({ where: { macAddress: mac } });
|
|
if (device && device.isActive && tokenMatches(token, device.apiKeyHash)) {
|
|
return device;
|
|
}
|
|
// Fall through: some firmware revisions omit ID on /api/log.
|
|
}
|
|
|
|
// Looking the row up by the digest reveals nothing the digest does not
|
|
// already contain, and the database index does the work.
|
|
const device = await prisma.device.findFirst({ where: { apiKeyHash: hashToken(token) } });
|
|
return device && device.isActive ? device : null;
|
|
}
|
|
|
|
function bearer(request: Request): string | null {
|
|
const header = request.headers.get('authorization');
|
|
if (!header?.toLowerCase().startsWith('bearer ')) {
|
|
return null;
|
|
}
|
|
return header.slice(7).trim() || null;
|
|
}
|