Cogneris
Environmental Compliance & Inspection

Intent · Every environmental obligation current and evidenced — zero reportable exceedances at Woodbine (UTS).

Operating tolerance: issues closed in ≤ 5 days · no repeat findings · every permit condition met.

← Documentsdocs/OPS-device-credential.md837 words

The device credential — minting it, setting it, rotating it

  • Date: 2026-08-05 · Claude Opus 5 (Claude Code, session 0add9fc1)
  • Why: worklist S3 / docs/AUDIT-submissions-2026-08-04.md §4. Until 2026-08-05 the console accepted a compliance record from anyone who could reach it, and served every arrival — GPS, raiser, dictated transcript — to anyone who asked.
  • NOT EXECUTED. Nothing here has been run against Cloudflare from this session. The commands are written to be run by an operator; treat the console as still open until one of them has been.

What changed

POST /api/ncrs, GET /api/ncrs, GET /api/ncrs/[id] and GET /api/submissions now require a device credential: Authorization: Bearer <token>, checked against ESG_DEVICE_TOKENS (app/lib/deviceAuth.ts).

It fails closed. A deployment with no ESG_DEVICE_TOKENS refuses device traffic with a 503 that says which variable is missing. That is deliberate — a control that switches itself off when unconfigured is not a control — and it means the deployed console will stop accepting tickets until the secret is set.

The console's own Submissions page does not go through these routes any more. It reads the store in its own server render (app/lib/arrivals.ts), so closing them cost the browser nothing.

What it does NOT do

It does not authenticate a human with a browser. There is no identity provider here, and no header a browser sends can prove who is behind it — a header is written by whoever makes the request. Human access to the console is Cloudflare Access's job, in front of the origin. Anything in code claiming to do it would be theatre.

It is also not a secret from whoever holds the handset: the token is embedded in the app bundle and its manifest. What it establishes is that a caller is a build this organisation issued — the difference between "our phones can file tickets" and "the internet can file tickets". Treat a lost handset as a lost token and rotate.


1 · Mint a token

Any high-entropy string. 32 bytes of base64 is ample:

openssl rand -base64 32

Label it per handset or per site so a response names which device delivered — the label appears in {"device": "..."}, the secret never does:

woodbine-01:<secret>

2 · Set it on the console

Multiple credentials are whitespace- or comma-separated, so devices can be added and removed one at a time without reissuing everyone's build.

cd /Users/pjones/MyProjects/MyDemos/esgm
CLOUDFLARE_ACCOUNT_ID=a10d32c7… npx wrangler secret put ESG_DEVICE_TOKENS
# paste: woodbine-01:<secret> eglinton-02:<secret>

For a local next dev console, the same variable in .env.local:

ESG_DEVICE_TOKENS=lan-test:<secret>

Deliberately running open (a demo box, a throwaway LAN test) — say so in writing, and it is recorded in every response as anonymous-allowed:

ESG_ALLOW_ANONYMOUS_SUBMISSIONS=true

A configured ESG_DEVICE_TOKENS always wins: the hatch is for having no credentials, not for bypassing the ones you have.

3 · Build the app with it

cd apps/esg-field
EXPO_PUBLIC_PORTAL_URL=https://esg.cogneris.com \
EXPO_PUBLIC_DEVICE_TOKEN=<secret> \
npx expo run:ios      # or run:android

app.config.js copies it to extra.deviceToken, omitted entirely when unset — never null, which Expo's serializer writes into the manifest as a truthy {} (that bug once killed the app on launch; see lib/ncr/submit.ts).

Diagnostics shows whether a credential is configured, never its value.

4 · Check it

On the device: Diagnostics → Device credential reads configured. Raise a ticket; Submissions should move it from "sending" to "at the portal".

From a shell — the failure modes are distinguishable on purpose:

# No credential: 401 with the marker the device keys on.
curl -s -o /dev/null -w '%{http_code}\n' https://esg.cogneris.com/api/submissions

# With one: 200 and the arrivals JSON.
curl -s -H "Authorization: Bearer <secret>" https://esg.cogneris.com/api/submissions | head -c 400
What you seeWhat it means
503 + ESG_DEVICE_TOKENS in the bodyThe console has no credentials configured. Step 2.
401 + "auth":"device-token"Wrong or missing token. The device keeps the ticket and retries.
401/403 with an HTML login pageCloudflare Access in front of the origin, not this control.

5 · Rotate

  1. wrangler secret put ESG_DEVICE_TOKENS with both the old and new entries.
  2. Reissue and install the builds carrying the new one.
  3. wrangler secret put ESG_DEVICE_TOKENS again with the old entry removed.

Nothing is lost mid-rotation: a handset that cannot authenticate keeps every ticket and delivers them when it can (lib/ncr/delivery.ts treats an unauthorized answer as pending, never terminal).

What is still open after this

  • Human access to the console pages is unauthenticated unless Cloudflare Access is configured in front of the origin. That is a dashboard change, not a code change, and it is not done.
  • The token is per-build, not per-device. Two handsets on one build share a credential and cannot be revoked independently. A per-device enrolment (a secret minted at first launch and held in expo-secure-store) is the next step if that matters; the seam is deviceToken().