Download server/test-admin.js from abr00kx/taper: direct link, hf CLI and curl.
- Browser
- Download file 9.49 kB
-
https://huggingface.co/abr00kx/taper/resolve/main/server/test-admin.js
- Command line
-
hf download hf://abr00kx/taper/server/test-admin.js
-
curl -L -o test-admin.js https://huggingface.co/abr00kx/taper/resolve/main/server/test-admin.js
9.49 kB
| // Operator tools: the admin socket and command line. | |
| import { spawn, spawnSync } from 'node:child_process'; | |
| import { mkdtempSync, rmSync, statSync } from 'node:fs'; | |
| import { tmpdir } from 'node:os'; | |
| import { join } from 'node:path'; | |
| import assert from 'node:assert/strict'; | |
| import { withUrl } from './test-client.js'; | |
| const PORT = 21000 + Math.floor(Math.random() * 1000); | |
| const Client = withUrl(`ws://127.0.0.1:${PORT}/_taper/gateway/v1`); | |
| const dir = mkdtempSync(join(tmpdir(), 'taper-admin-')); | |
| const socket = join(dir, 'admin.sock'); | |
| const admin = (...args) => spawnSync(process.execPath, ['admin.js', '--socket', socket, ...args], { encoding: 'utf8' }); | |
| const server = spawn(process.execPath, ['index.js'], { | |
| env: { ...process.env, PORT: String(PORT), TAPER_REGISTER_POW_BITS: '8', TAPER_ADMIN_SOCKET: socket }, | |
| stdio: ['ignore', 'pipe', 'pipe'], | |
| }); | |
| let log = ''; | |
| server.stderr.on('data', (d) => { log += d; }); | |
| server.stdout.on('data', (d) => { log += d; }); | |
| await new Promise((resolve) => { const t = setInterval(() => { if (log.includes('Admin socket ready') && log.includes('Taper gateway')) { clearInterval(t); resolve(); } }, 50); }); | |
| const results = []; | |
| const test = async (name, fn) => { | |
| try { await fn(); results.push(['PASS', name]); } catch (e) { results.push(['FAIL', name, e.message || String(e.stack ?? e)]); } | |
| }; | |
| try { | |
| const alice = await Client.connect(); | |
| const aliceAuth = await alice.register('alice', 'alice-alice-alice'); | |
| await alice.identify(aliceAuth.access_token); | |
| await Client.connect().then((c) => c.register('bob', 'bob-bob-bob-bob')); | |
| await test('the socket is private to the operator', () => { | |
| assert.equal(statSync(socket).mode & 0o777, 0o600); | |
| }); | |
| await test('lists accounts, with no message data', () => { | |
| const r = admin('users'); | |
| assert.equal(r.status, 0, r.stderr); | |
| assert.match(r.stdout, /@alice:localhost/); | |
| assert.match(r.stdout, /@bob:localhost/); | |
| }); | |
| await test('suspending signs the account out and stops login; unsuspending restores it', async () => { | |
| assert.equal(admin('suspend', 'alice').status, 0); | |
| assert.equal((await alice.closed).code, 4004); | |
| const c = await Client.connect(); | |
| await assert.rejects(c.login('alice', 'alice-alice-alice'), /suspended/); | |
| assert.match(admin('users').stdout, /@alice:localhost.*SUSPENDED/); | |
| assert.equal(admin('unsuspend', 'alice').status, 0); | |
| assert.match((await c.login('alice', 'alice-alice-alice')).access_token, /^tp_/); | |
| }); | |
| await test('a login started before a suspension cannot finish after it', async () => { | |
| const c = await Client.connect(); | |
| const held = await c.reauth('bob', 'bob-bob-bob-bob'); | |
| assert.equal(admin('suspend', 'bob').status, 0); | |
| const r = await c.request('t.opaque.login.finish', { login_id: held.login_id, ke3: held.ke3 }); | |
| assert.equal(r.ok, false); | |
| assert.equal(admin('unsuspend', 'bob').status, 0); | |
| }); | |
| await test('registration can be closed and opened while the server runs', async () => { | |
| assert.equal(admin('limits', 'registration_open', 'false').status, 0); | |
| await assert.rejects((await Client.connect()).register('carol', 'carol-carol-carol'), /Registration is closed/); | |
| assert.equal(admin('limits', 'registration_open', 'true').status, 0); | |
| assert.equal((await (await Client.connect()).register('carol', 'carol-carol-carol')).user_id, '@carol:localhost'); | |
| }); | |
| await test('limits change live, and bad values are refused', async () => { | |
| assert.equal(admin('limits', 'register_pow_bits', '10').status, 0); | |
| assert.equal(JSON.parse(admin('limits').stdout).register_pow_bits, 10); | |
| for (const bad of [['register_pow_bits', '99'], ['rate_burst', '0'], ['nonsense', '1'], ['rate_per_sec', '"fast"']]) { | |
| const r = admin('limits', ...bad); | |
| assert.equal(r.status, 1, bad.join(' ')); | |
| } | |
| assert.equal(JSON.parse(admin('limits').stdout).register_pow_bits, 10); | |
| // A tighter rate limit applies to new connections at once. | |
| assert.equal(admin('limits', 'rate_burst', '3').status, 0); | |
| const c = await Client.connect(); | |
| const answers = await Promise.all(Array.from({ length: 8 }, () => c.request('t.nonexistent', {}))); | |
| assert.ok(answers.some((r) => r.error?.errcode === 'T_LIMIT_EXCEEDED'), 'no request was rate limited'); | |
| assert.equal(admin('limits', 'rate_burst', '20').status, 0); | |
| }); | |
| await test('revoke signs every device out but keeps the account', async () => { | |
| const c = await Client.connect(); | |
| const auth = await c.login('bob', 'bob-bob-bob-bob'); | |
| await c.identify(auth.access_token); | |
| assert.equal(admin('revoke', 'bob').status, 0); | |
| assert.equal((await c.closed).code, 4004); | |
| assert.match((await (await Client.connect()).login('bob', 'bob-bob-bob-bob')).access_token, /^tp_/); | |
| }); | |
| await test('unlock clears the security keys of someone who lost them, and signs them out', async () => { | |
| const { SoftKey } = await import('./softkey.js'); | |
| const origin = `http://127.0.0.1:${PORT}`; | |
| const WS = (await import('ws')).default; | |
| const c = await Client.connectTo(`ws://127.0.0.1:${PORT}/_taper/gateway/v1`, { open: (u) => new WS(u, { origin }) }); | |
| const auth = await c.register('dora', 'dora-dora-dora-dora'); | |
| await c.identify(auth.access_token); | |
| const key = new SoftKey(); | |
| const start = await c.call('t.webauthn.register.start', { auth: await c.reauth('dora', 'dora-dora-dora-dora') }); | |
| await c.call('t.webauthn.register.finish', { challenge_id: start.challenge_id, credential: key.create(start.options, origin) }); | |
| assert.match(admin('users').stdout, /@dora:localhost.*1 key/); | |
| assert.equal(admin('unlock', 'dora').status, 0); | |
| assert.equal((await c.closed).code, 4004); | |
| assert.doesNotMatch(admin('users').stdout, /@dora:localhost.*key/); | |
| assert.match((await (await Client.connect()).login('dora', 'dora-dora-dora-dora')).access_token, /^tp_/); | |
| }); | |
| await test('delete removes the account, reserves the name, and the log never names anyone', async () => { | |
| assert.equal(admin('delete', '@carol:localhost').status, 0); | |
| assert.doesNotMatch(admin('users').stdout, /carol/); | |
| await assert.rejects((await Client.connect()).register('carol', 'something-else-1'), /T_USER_IN_USE/); | |
| assert.match(log, /admin: delete/); | |
| assert.doesNotMatch(log, /alice|bob|carol/); | |
| }); | |
| await test('unknown commands, unknown users and other servers are refused', () => { | |
| assert.equal(admin('explode').status, 1); | |
| assert.match(admin('suspend', 'nobody').stderr, /No such account/); | |
| assert.match(admin('suspend', '@alice:elsewhere.example').stderr, /not an account on this server/); | |
| }); | |
| await test('stats are counters only', () => { | |
| const s = JSON.parse(admin('stats').stdout); | |
| assert.deepEqual(Object.keys(s).sort(), ['limits', 'rooms', 'sessions', 'suspended', 'users']); | |
| }); | |
| await test('invite-only mode requires a single-use invite token from admin.js invite', async () => { | |
| assert.equal(admin('limits', 'invite_only', 'true').status, 0); | |
| await assert.rejects((await Client.connect()).register('erin', 'erin-erin-erin-erin'), /Missing 'invite_token'/); | |
| const inv = JSON.parse(admin('invite', '24').stdout); | |
| assert.match(inv.invite_token, /^ti_/); | |
| const { register } = await import('../client/opaque.js'); | |
| const c1 = await Client.connect(); | |
| const ok = await register((m, b) => c1.call(m, b), c1.hello.body.server_name, 'erin', 'erin-erin-erin-erin', { inviteToken: inv.invite_token }); | |
| assert.equal(ok.user_id, '@erin:localhost'); | |
| // Reusing the same single-use invite token is refused. | |
| const c2 = await Client.connect(); | |
| await assert.rejects( | |
| register((m, b) => c2.call(m, b), c2.hello.body.server_name, 'frank', 'frank-frank-frank', { inviteToken: inv.invite_token }), | |
| /Invalid, used, or expired invite/, | |
| ); | |
| assert.equal(admin('limits', 'invite_only', 'false').status, 0); | |
| await c1.close(); | |
| await c2.close(); | |
| }); | |
| await test('users can list active devices and revoke a single stolen device without changing password', async () => { | |
| const d1 = await Client.connect(); | |
| const a1 = await d1.login('bob', 'bob-bob-bob-bob'); | |
| await d1.identify(a1.access_token); | |
| const d2 = await Client.connect(); | |
| const a2 = await d2.login('bob', 'bob-bob-bob-bob'); | |
| await d2.identify(a2.access_token); | |
| const { devices } = await d1.call('t.devices.list', {}); | |
| assert.ok(devices.some((d) => d.device_id === a1.device_id && d.current)); | |
| assert.ok(devices.some((d) => d.device_id === a2.device_id && !d.current)); | |
| // Revoking needs fresh password proof. | |
| assert.equal((await d1.request('t.devices.delete', { device_id: a2.device_id })).error?.needs, 'password'); | |
| await d1.call('t.devices.delete', { device_id: a2.device_id, auth: await d1.reauth('bob', 'bob-bob-bob-bob') }); | |
| assert.equal((await d2.closed).code, 4004); | |
| // Current device stays signed in! | |
| assert.equal((await d1.call('t.whoami', {})).device_id, a1.device_id); | |
| await d1.close(); | |
| }); | |
| } finally { | |
| server.kill('SIGTERM'); | |
| await new Promise((r) => server.once('exit', r)); | |
| rmSync(dir, { recursive: true, force: true }); | |
| } | |
| let failed = 0; | |
| for (const [status, name, detail] of results) { | |
| console.log(`${status} ${name}${detail ? `\n ${detail}` : ''}`); | |
| if (status === 'FAIL') failed++; | |
| } | |
| console.log(`\n${results.length - failed}/${results.length} passed`); | |
| process.exit(failed ? 1 : 0); | |