- 앱이 서버를 알려 준 뒤에는 ALLOWED_GUILD_IDS 대신 그 서버 하나만 허용 - 앱에서 녹음할 서버를 바꾸면 사이트 서버에 바로 다시 알린다 (hello) - 로그인 화면: GET /auth/guild 로 서버 이름 안내 - 테스트 2개 (login-guild.test.ts) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.3 KiB
TypeScript
72 lines
3.3 KiB
TypeScript
// 사이트 로그인: 게임 PC 앱에서 녹음할 서버로 고른 서버의 멤버만 들어온다.
|
|
// 앱이 서버를 알려 주기 전에만 ALLOWED_GUILD_IDS 를 쓴다.
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import http from 'node:http';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { buildServer } from '../src/app.ts';
|
|
import type { Config } from '../src/config.ts';
|
|
|
|
const tmp = () => fs.mkdtempSync(path.join(process.env.TMPDIR || os.tmpdir(), 'gvr-guild-'));
|
|
|
|
// member1 은 111 서버에만 있다. 222 서버 이름은 "앱 서버"
|
|
async function fakeDiscord() {
|
|
const srv = http.createServer((req, res) => {
|
|
const u = new URL(req.url!, 'http://x');
|
|
res.setHeader('content-type', 'application/json');
|
|
if (u.pathname === '/oauth2/token') return res.end(JSON.stringify({ access_token: 'at' }));
|
|
if (u.pathname === '/users/@me') return res.end(JSON.stringify({ id: 'member1', username: 'm', global_name: '멤버', avatar: null }));
|
|
if (u.pathname === '/guilds/111/members/member1') return res.end(JSON.stringify({ nick: '멤버', avatar: null }));
|
|
if (u.pathname === '/guilds/222') return res.end(JSON.stringify({ id: '222', name: '앱 서버' }));
|
|
res.statusCode = 404;
|
|
res.end('{}');
|
|
});
|
|
await new Promise<void>((r) => srv.listen(0, '127.0.0.1', () => r()));
|
|
return { url: `http://127.0.0.1:${(srv.address() as any).port}`, close: () => srv.close() };
|
|
}
|
|
|
|
async function stack() {
|
|
const dc = await fakeDiscord();
|
|
const cfg: Config = {
|
|
port: 0, host: '127.0.0.1', publicUrl: 'https://gvr.example', clientId: 'cid', clientSecret: 's', botToken: 'bot',
|
|
allowedGuilds: ['111'], owners: [], superAdmins: [], sessionSecret: 'x'.repeat(32), dataDir: tmp(), webDir: '/nonexistent',
|
|
version: 'test', discordApi: dc.url, assetUpstream: {},
|
|
};
|
|
const { app, store } = await buildServer(cfg);
|
|
await app.listen({ host: '127.0.0.1', port: 0 });
|
|
const base = `http://127.0.0.1:${(app.server.address() as any).port}`;
|
|
const login = async () => {
|
|
const start = await fetch(base + '/auth/discord', { redirect: 'manual' });
|
|
const state = new URL(start.headers.get('location')!).searchParams.get('state')!;
|
|
const cb = await fetch(`${base}/auth/discord/callback?code=c&state=${state}`, { headers: { cookie: start.headers.getSetCookie()[0]!.split(';')[0]! }, redirect: 'manual' });
|
|
return cb.headers.get('location');
|
|
};
|
|
return { base, store, login, close: async () => (await app.close(), dc.close()) };
|
|
}
|
|
|
|
test('로그인: 앱이 서버를 알려 주기 전에는 ALLOWED_GUILD_IDS 멤버가 들어온다', async () => {
|
|
const s = await stack();
|
|
try {
|
|
assert.equal(await s.login(), '/');
|
|
} finally {
|
|
await s.close();
|
|
}
|
|
});
|
|
|
|
test('로그인: 앱에서 고른 서버의 멤버가 아니면 (ALLOWED_GUILD_IDS 멤버라도) 봇과 다른 서버라 막는다', async () => {
|
|
const s = await stack();
|
|
try {
|
|
const { code } = s.store.createCode('owner');
|
|
const dev = s.store.redeem(code, 'PC')!;
|
|
s.store.touchDevice(dev.deviceId, { guildId: '222' });
|
|
assert.equal(await s.login(), '/?login=not_member');
|
|
// 로그인 화면은 그 서버 이름으로 안내한다
|
|
const g = await (await fetch(s.base + '/auth/guild')).json();
|
|
assert.deepEqual(g, { name: '앱 서버' });
|
|
} finally {
|
|
await s.close();
|
|
}
|
|
});
|