Adds a dedicated Node 22 + ws arena server (src/arena) that owns live Ordnance Disposal matches: drift-corrected 20Hz tick loop with capped catch-up and tick-drift logging, per-socket snapshot fan-out, 30s jose ticket auth, and awards via the idempotent results callback route. Client gains a transport abstraction (transport.ts): WsTransport first when NEXT_PUBLIC_ARENA_URL is baked into the bundle, HttpTransport (today's actions + SSE + guarded poll, behavior unchanged) as automatic fallback after two connection failures or a 5s setup timeout; absent env var means pure HTTP (kill switch / rollback path). Next actions proxy the match lifecycle to the arena when ARENA_SECRET + ARENA_INTERNAL_URL are set; the in-process registry stays untouched as the default backend. Deploy wiring: runner-arena Dockerfile target, compose service, env docs, DEPLOYMENT.md checklist. Tests: arena module suite (19), live server WS suite (3), prediction harness ws-grade latency scenario.
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
/**
|
|
* Arena connection tickets.
|
|
*
|
|
* A ticket is a short-lived (30s) JWT minted by the Next app
|
|
* (`createArenaTicket` in the bomperator actions) and carried in the WebSocket
|
|
* connect query string, because browsers cannot attach custom headers to a WS
|
|
* handshake. It proves "this logged-in user id wants to watch/play this match
|
|
* id" without exposing the Payload session to the arena process.
|
|
*
|
|
* Deliberate tradeoff (agreed in the plan): there is no single-use replay
|
|
* store. The 30s expiry plus the narrow uid+matchId scope is the defense; a
|
|
* stolen ticket can only impersonate its own seat in its own match for half a
|
|
* minute.
|
|
*/
|
|
|
|
import { jwtVerify } from "jose";
|
|
|
|
export interface ArenaTicketClaims {
|
|
/** User id of the connecting player. */
|
|
uid: number;
|
|
/** Match the ticket grants access to. */
|
|
mid: string;
|
|
}
|
|
|
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
|
/**
|
|
* Verify a ticket signature + expiry and check that the uid/matchId pair is
|
|
* coherent (positive integer id, uuid match). Returns the claims or null;
|
|
* never throws.
|
|
*/
|
|
export async function verifyArenaTicket(
|
|
token: string,
|
|
secret: string,
|
|
): Promise<ArenaTicketClaims | null> {
|
|
if (!token || !secret) return null;
|
|
try {
|
|
// Buffer (not TextEncoder) so the key is a plain Node Uint8Array even when
|
|
// this runs inside a jsdom test realm with its own intrinsics (jose's
|
|
// instanceof checks are realm-sensitive).
|
|
const key = Buffer.from(secret, "utf8");
|
|
const { payload } = await jwtVerify(token, key, {
|
|
algorithms: ["HS256"],
|
|
clockTolerance: 5,
|
|
});
|
|
const uid = typeof payload.uid === "number" ? payload.uid : Number(payload.uid);
|
|
const mid = payload.matchId;
|
|
if (!Number.isInteger(uid) || uid <= 0) return null;
|
|
if (typeof mid !== "string" || !UUID_RE.test(mid)) return null;
|
|
return { uid, mid };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|