|
| 1 | +import { loadWavegridConfig } from '@wavegrid/layout'; |
| 2 | +import { openStore } from '@wavegrid/settings'; |
| 3 | +import { mkdtempSync } from 'fs'; |
| 4 | +import { tmpdir } from 'os'; |
| 5 | +import { join } from 'path'; |
| 6 | +import { WebSocket } from 'ws'; |
| 7 | + |
| 8 | +import { startServer, type ServerHandle } from '../src/server'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Revocation has to bite on the live socket, not just on the next request: an |
| 12 | + * operator booting a phone off the show watched it keep painting lasers. |
| 13 | + */ |
| 14 | +interface Client { |
| 15 | + ws: WebSocket; |
| 16 | + /** Resolves with the close code once the brain drops us. */ |
| 17 | + closed: Promise<{ code: number; reason: string }>; |
| 18 | +} |
| 19 | + |
| 20 | +/** Every client opened here, so none is left holding the event loop open. */ |
| 21 | +const opened: WebSocket[] = []; |
| 22 | + |
| 23 | +function connect(port: number, token: string): Promise<Client> { |
| 24 | + return new Promise((resolve, reject) => { |
| 25 | + const ws = new WebSocket(`ws://127.0.0.1:${port}/?token=${encodeURIComponent(token)}`); |
| 26 | + opened.push(ws); |
| 27 | + const closed = new Promise<{ code: number; reason: string }>((r) => { |
| 28 | + ws.on('close', (code, reason) => r({ code, reason: reason.toString() })); |
| 29 | + }); |
| 30 | + ws.on('open', () => resolve({ ws, closed })); |
| 31 | + ws.on('error', reject); |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +const wait = (ms: number): Promise<void> => new Promise((r) => setTimeout(() => r(), ms)); |
| 36 | + |
| 37 | +/** The close event, or null if the socket is still up after `ms`. */ |
| 38 | +async function closeWithin( |
| 39 | + client: Client, |
| 40 | + ms: number |
| 41 | +): Promise<{ code: number; reason: string } | null> { |
| 42 | + const timeout = wait(ms).then((): null => null); |
| 43 | + return Promise.race([client.closed, timeout]); |
| 44 | +} |
| 45 | + |
| 46 | +describe('session revocation', () => { |
| 47 | + const saved = { ...process.env }; |
| 48 | + let handle: ServerHandle; |
| 49 | + let port: number; |
| 50 | + let base: string; |
| 51 | + |
| 52 | + async function login(username: string, password: string): Promise<string> { |
| 53 | + const res = await fetch(`${base}/api/login`, { |
| 54 | + method: 'POST', |
| 55 | + headers: { 'Content-Type': 'application/json' }, |
| 56 | + body: JSON.stringify({ username, password }) |
| 57 | + }); |
| 58 | + const body = await res.json(); |
| 59 | + expect(body.token).toBeTruthy(); |
| 60 | + return body.token as string; |
| 61 | + } |
| 62 | + |
| 63 | + beforeAll(async () => { |
| 64 | + const store = mkdtempSync(join(tmpdir(), 'wg-revoke-store-')); |
| 65 | + const state = mkdtempSync(join(tmpdir(), 'wg-revoke-state-')); |
| 66 | + process.env.APPSTASH_BASE_DIR = store; |
| 67 | + process.env.WAVEGRID_PROJECT = 'demo'; |
| 68 | + process.env.WG_STATE_DIR = state; |
| 69 | + process.env.WG_JWT_SECRET = 'revoke-test-secret'; |
| 70 | + process.env.WG_SESSION_SWEEP_MS = '50'; |
| 71 | + delete process.env.WAVEGRID_LAYOUT; |
| 72 | + delete process.env.WAVEGRID_MODE; |
| 73 | + delete process.env.LIGHT_MAP_CONFIG; |
| 74 | + |
| 75 | + const s = openStore(); |
| 76 | + s.createProject('demo', { |
| 77 | + layout: { preset: 'grid-7x7' }, |
| 78 | + server: { host: '127.0.0.1', port: 0 } |
| 79 | + }); |
| 80 | + s.setActiveProject('demo'); |
| 81 | + s.addUser('demo', 'boss', 'bosspw'); // first user → admin |
| 82 | + s.addUser('demo', 'alice', 'alicepw'); |
| 83 | + s.addUser('demo', 'bob', 'bobpw'); |
| 84 | + |
| 85 | + handle = startServer(loadWavegridConfig(), { uiDir: null, advertise: false }); |
| 86 | + await handle.ready; |
| 87 | + const addr = handle.server.address(); |
| 88 | + port = typeof addr === 'object' && addr ? addr.port : 0; |
| 89 | + base = `http://127.0.0.1:${port}`; |
| 90 | + }); |
| 91 | + |
| 92 | + afterAll(() => { |
| 93 | + for (const ws of opened) ws.terminate(); |
| 94 | + handle.stop(); |
| 95 | + process.env = { ...saved }; |
| 96 | + }); |
| 97 | + |
| 98 | + it('drops the revoked user’s live socket and leaves everyone else connected', async () => { |
| 99 | + const [adminToken, aliceToken, bobToken] = await Promise.all([ |
| 100 | + login('boss', 'bosspw'), |
| 101 | + login('alice', 'alicepw'), |
| 102 | + login('bob', 'bobpw') |
| 103 | + ]); |
| 104 | + const alice = await connect(port, aliceToken); |
| 105 | + const bob = await connect(port, bobToken); |
| 106 | + |
| 107 | + const aliceSession = openStore() |
| 108 | + .listSessions('demo') |
| 109 | + .find((s) => s.username === 'alice'); |
| 110 | + expect(aliceSession).toBeDefined(); |
| 111 | + |
| 112 | + const res = await fetch(`${base}/api/admin/sessions/${aliceSession!.id}`, { |
| 113 | + method: 'DELETE', |
| 114 | + headers: { Authorization: `Bearer ${adminToken}` } |
| 115 | + }); |
| 116 | + expect(res.status).toBe(200); |
| 117 | + |
| 118 | + const closed = await closeWithin(alice, 2000); |
| 119 | + expect(closed?.code).toBe(4001); |
| 120 | + expect(closed?.reason).toBe('session-revoked'); |
| 121 | + expect(await closeWithin(bob, 200)).toBeNull(); |
| 122 | + |
| 123 | + // Her token is finished for HTTP too, and can't open a new socket. |
| 124 | + const me = await fetch(`${base}/api/me`, { |
| 125 | + headers: { Authorization: `Bearer ${aliceToken}` } |
| 126 | + }); |
| 127 | + expect(me.status).toBe(401); |
| 128 | + await expect(connect(port, aliceToken)).rejects.toThrow(/401/); |
| 129 | + |
| 130 | + // Bob is untouched: still authenticated on HTTP, still driving the show. |
| 131 | + const bobMe = await fetch(`${base}/api/me`, { |
| 132 | + headers: { Authorization: `Bearer ${bobToken}` } |
| 133 | + }); |
| 134 | + expect(bobMe.status).toBe(200); |
| 135 | + bob.ws.close(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('drops a socket when the session is revoked out-of-process (desktop app)', async () => { |
| 139 | + const token = await login('bob', 'bobpw'); |
| 140 | + const client = await connect(port, token); |
| 141 | + |
| 142 | + // The desktop app revokes by writing the same store — no HTTP call to hook. |
| 143 | + expect(openStore().revokeUserSessions('demo', 'bob')).toBeGreaterThan(0); |
| 144 | + |
| 145 | + const closed = await closeWithin(client, 2000); |
| 146 | + expect(closed?.code).toBe(4001); |
| 147 | + }); |
| 148 | + |
| 149 | + it('signing out ends the session, so the token is dead everywhere', async () => { |
| 150 | + const token = await login('alice', 'alicepw'); |
| 151 | + const client = await connect(port, token); |
| 152 | + |
| 153 | + const res = await fetch(`${base}/api/logout`, { |
| 154 | + method: 'POST', |
| 155 | + headers: { Authorization: `Bearer ${token}` } |
| 156 | + }); |
| 157 | + expect(res.status).toBe(200); |
| 158 | + |
| 159 | + expect(openStore().listSessions('demo').some((s) => s.username === 'alice')).toBe(false); |
| 160 | + expect((await closeWithin(client, 2000))?.code).toBe(4001); |
| 161 | + const me = await fetch(`${base}/api/me`, { headers: { Authorization: `Bearer ${token}` } }); |
| 162 | + expect(me.status).toBe(401); |
| 163 | + }); |
| 164 | +}); |
0 commit comments