forked from WikiDeck/wikideck
Overnight build by 17 Claude agents (2026-11-16 22:45 → 01:30 EST), coordinated in [claude/wikideck milestone 63](https://gitea.eom.dev/claude/wikideck/milestone/63). **For Eric's review. Do not merge without it.** The full write-up is in `docs/night/REPORT.md`. **What's in it** (38 reviewed PRs, squash-merged into `night/2026-11-16`): - **Edit War:** - a 3-lane, 6-turn card game built on what WikiDeck cards actually hold - a pure, deterministic, off-chain rules engine (`shared/game/`, with the API in `docs/game/ENGINE_API.md`); rules are data and v1.1 is the default - a mobile-first Play UI with a lobby, deck builder, four loaner starter decks, a match against the AI, results and a tutorial - a landing page (fixes #3) - **Design and balance:** `docs/game/DESIGN.md`, `CARD_DATA.md` (236 sampled articles) and `BALANCE.md` (bot simulations). Starter pairings went from 23–77% on v1 to roughly even on v1.1. - **eom.dev accounts:** Dex OIDC with PKCE, plus off-chain wallet linking, where the client builds the message it signs (`server/account/`, `src/auth/`). - **Peer-hosted game modes:** modes are JSON data, never code. Node API at `/api/game/*`, with host credit and leaderboards. Three example modes (`shared/game/modes.ts`, `server/game/`, `docs/game/HOSTING.md`). - **Mobile:** Capacitor 7 Android app (a debug APK played a full match against the AI in the emulator), and an iOS project waiting on Xcode (`docs/night/ANDROID.md`, `docs/night/IOS.md`). - **Testnet tooling:** `deploy/night/` (LAN-only, 3 nodes) and an optional `WIKIDECK_USER_AGENT` (the default is unchanged). **Not touched:** consensus and protocol (`server/chain.ts`, `shared/protocol.ts`, `server/testnet/{consensus,ledger,…}.ts`, `native/`), the live deployment and mainnet. Matches run off-chain and never move or burn NFTs. **Checks:** the final integration gate passed in `node:24-alpine`: `npm test` 241/242 with 0 fail (1 skipped), and `npm run build` passes. The repo needs Node 24, because Node 20 fails 12 tests falsely. **Decisions for Eric** (see REPORT.md, Known issues and Next steps): 1. whether the live node should set `WIKIDECK_USER_AGENT` 2. the Xcode licence, which you should accept yourself 3. #49 in the fork, a bot change held after the freeze 4. on-chain host rewards (proposal only, in HOSTING.md) 🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: WikiDeck/wikideck#22 Co-authored-by: Claude <claude@eom.dev>
90 lines
5.5 KiB
TypeScript
90 lines
5.5 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { generateKeyPairSync, sign, type KeyObject } from 'node:crypto';
|
|
import { DexKeys, TokenError, displayName, oidcConfig, verifyIdToken } from '../server/account/oidc.ts';
|
|
|
|
const config = { ...oidcConfig({}), issuer: 'https://dex.test/' };
|
|
const NOW = 1_800_000_000_000;
|
|
const t = Math.floor(NOW / 1000);
|
|
|
|
function rsa() { return generateKeyPairSync('rsa', { modulusLength: 2048 }); }
|
|
const main = rsa(), other = rsa();
|
|
|
|
export function jwt(claims: Record<string, unknown>, key: KeyObject = main.privateKey, header: Record<string, unknown> = { alg: 'RS256', kid: 'k1' }): string {
|
|
const enc = (v: unknown) => Buffer.from(JSON.stringify(v)).toString('base64url');
|
|
const input = `${enc(header)}.${enc(claims)}`;
|
|
return `${input}.${sign('sha256', Buffer.from(input), key).toString('base64url')}`;
|
|
}
|
|
const good = (over: Record<string, unknown> = {}) => ({ iss: config.issuer, sub: 'user-1', aud: 'wikideck',
|
|
iat: t - 10, exp: t + 3600, name: 'Ada Lovelace', preferred_username: 'ada', email: 'ada@example.test', nonce: 'n'.repeat(16), ...over });
|
|
|
|
function fakeDex(calls: string[] = []) {
|
|
const jwks = { keys: [{ ...main.publicKey.export({ format: 'jwk' }), kid: 'k1', use: 'sig', alg: 'RS256' }] };
|
|
const fetcher = (async (url: URL | string) => {
|
|
const u = String(url); calls.push(u);
|
|
const json = (v: unknown) => new Response(JSON.stringify(v), { headers: { 'content-type': 'application/json' } });
|
|
if (u.endsWith('/.well-known/openid-configuration')) return json({ issuer: config.issuer,
|
|
authorization_endpoint: 'https://dex.test/auth', token_endpoint: 'https://dex.test/token', jwks_uri: 'https://dex.test/keys' });
|
|
if (u === 'https://dex.test/keys') return json(jwks);
|
|
return new Response('{}', { status: 404 });
|
|
}) as typeof fetch;
|
|
return new DexKeys(config, fetcher, () => NOW);
|
|
}
|
|
|
|
const rejects = (p: Promise<unknown>, msg: RegExp) => assert.rejects(p, e => e instanceof TokenError && msg.test(e.message));
|
|
|
|
test('account: a good ID token verifies against the JWKS', async () => {
|
|
const claims = await verifyIdToken(jwt(good()), fakeDex(), config, { now: NOW, nonce: 'n'.repeat(16) });
|
|
assert.equal(claims.sub, 'user-1');
|
|
assert.equal(displayName(claims), 'ada'); // username preferred over real name
|
|
});
|
|
|
|
test('account: expired tokens are rejected (beyond clock skew)', async () => {
|
|
await rejects(verifyIdToken(jwt(good({ exp: t - 61 })), fakeDex(), config, { now: NOW }), /expired/);
|
|
// Within the 60 s skew still passes.
|
|
await verifyIdToken(jwt(good({ exp: t - 30 })), fakeDex(), config, { now: NOW });
|
|
});
|
|
|
|
test('account: wrong audience and wrong issuer are rejected', async () => {
|
|
await rejects(verifyIdToken(jwt(good({ aud: 'www-membership' })), fakeDex(), config, { now: NOW }), /audience/);
|
|
await rejects(verifyIdToken(jwt(good({ aud: ['other', 'wikideck'] })), fakeDex(), config, { now: NOW }), /authorized party/);
|
|
await verifyIdToken(jwt(good({ aud: ['other', 'wikideck'], azp: 'wikideck' })), fakeDex(), config, { now: NOW });
|
|
await rejects(verifyIdToken(jwt(good({ iss: 'https://evil.test/' })), fakeDex(), config, { now: NOW }), /issuer/);
|
|
await rejects(verifyIdToken(jwt(good({ iss: 'https://dex.test' })), fakeDex(), config, { now: NOW }), /issuer/);
|
|
});
|
|
|
|
test('account: forged signatures, alg confusion and unknown keys are rejected', async () => {
|
|
await rejects(verifyIdToken(jwt(good(), other.privateKey), fakeDex(), config, { now: NOW }), /signature/);
|
|
const parts = jwt(good()).split('.');
|
|
const tampered = `${parts[0]}.${Buffer.from(JSON.stringify(good({ sub: 'admin' }))).toString('base64url')}.${parts[2]}`;
|
|
await rejects(verifyIdToken(tampered, fakeDex(), config, { now: NOW }), /signature/);
|
|
const none = `${Buffer.from(JSON.stringify({ alg: 'none', kid: 'k1' })).toString('base64url')}.${parts[1]}.`;
|
|
await rejects(verifyIdToken(none, fakeDex(), config, { now: NOW }), /algorithm/);
|
|
await rejects(verifyIdToken(jwt(good(), main.privateKey, { alg: 'RS256', kid: 'nope' }), fakeDex(), config, { now: NOW }), /signing key/);
|
|
await rejects(verifyIdToken('a.b', fakeDex(), config, { now: NOW }), /Malformed/);
|
|
});
|
|
|
|
test('account: nonce, future iat and nbf are enforced', async () => {
|
|
await rejects(verifyIdToken(jwt(good()), fakeDex(), config, { now: NOW, nonce: 'x'.repeat(16) }), /nonce/);
|
|
await rejects(verifyIdToken(jwt(good({ iat: t + 600 })), fakeDex(), config, { now: NOW }), /future/);
|
|
await rejects(verifyIdToken(jwt(good({ nbf: t + 600 })), fakeDex(), config, { now: NOW }), /not yet/);
|
|
});
|
|
|
|
test('account: JWKS is cached and discovery issuer must match', async () => {
|
|
const calls: string[] = [];
|
|
const keys = fakeDex(calls);
|
|
await verifyIdToken(jwt(good()), keys, config, { now: NOW });
|
|
await verifyIdToken(jwt(good()), keys, config, { now: NOW });
|
|
assert.equal(calls.filter(c => c.endsWith('/keys')).length, 1);
|
|
const bad = new DexKeys({ ...config, issuer: 'https://dex.test/other/' }, (async () =>
|
|
new Response(JSON.stringify({ issuer: 'https://dex.test/' }))) as typeof fetch);
|
|
await assert.rejects(bad.meta(), /issuer mismatch/);
|
|
});
|
|
|
|
test('account: display names never fall back to email and are sanitised', () => {
|
|
assert.equal(displayName({ sub: 'abcdef123', preferred_username: 'ada', name: 'Ada Lovelace' }), 'ada');
|
|
assert.equal(displayName({ sub: 'abcdef123', name: 'Ada Lovelace' }), 'Ada Lovelace');
|
|
assert.equal(displayName({ sub: 'abcdef123' }), 'player-abcdef');
|
|
assert.equal(displayName({ sub: 's', name: 'A\u0000B' + 'x'.repeat(100) }).length, 48);
|
|
});
|