mirror of
https://github.com/MoonshotAI/kimi-code.git
synced 2026-08-25 08:34:39 +00:00
- add persistent bearer-token auth (token store, credentials, password hashing) - gate HTTP and WebSocket (bearer subprotocol) upgrades behind auth - classify loopback vs non-loopback binds and validate hostnames/origin - add rate limiting and security headers middleware - add GUI store service and routes - add process file locking
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import { mkdtemp, rm } from 'node:fs/promises';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
import { type RunningServer, startServer } from '../src/start';
|
|
|
|
describe('server-v2 /api/v1 bearer auth', () => {
|
|
let server: RunningServer | undefined;
|
|
let home: string | undefined;
|
|
|
|
beforeEach(async () => {
|
|
home = await mkdtemp(join(tmpdir(), 'kimi-server-v2-auth-middleware-'));
|
|
});
|
|
|
|
afterEach(async () => {
|
|
if (server !== undefined) {
|
|
await server.close();
|
|
server = undefined;
|
|
}
|
|
if (home !== undefined) {
|
|
await rm(home, { recursive: true, force: true });
|
|
home = undefined;
|
|
}
|
|
});
|
|
|
|
it('allows healthz without a token', async () => {
|
|
server = await startServer({ host: '127.0.0.1', port: 0, homeDir: home, logLevel: 'silent' });
|
|
const res = await server.app.inject({ method: 'GET', url: '/api/v1/healthz' });
|
|
expect(res.statusCode).toBe(200);
|
|
});
|
|
|
|
it('rejects /api/v1/auth without a token with 40101', async () => {
|
|
server = await startServer({ host: '127.0.0.1', port: 0, homeDir: home, logLevel: 'silent' });
|
|
const res = await server.app.inject({ method: 'GET', url: '/api/v1/auth' });
|
|
expect(res.statusCode).toBe(401);
|
|
const body = res.json() as Record<string, unknown>;
|
|
expect(body['code']).toBe(40101);
|
|
});
|
|
|
|
it('rejects /api/v1/auth with a wrong token', async () => {
|
|
server = await startServer({ host: '127.0.0.1', port: 0, homeDir: home, logLevel: 'silent' });
|
|
const res = await server.app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/auth',
|
|
headers: { authorization: 'Bearer wrong-token' },
|
|
});
|
|
expect(res.statusCode).toBe(401);
|
|
const body = res.json() as Record<string, unknown>;
|
|
expect(body['code']).toBe(40101);
|
|
});
|
|
|
|
it('accepts /api/v1/auth with the persistent token', async () => {
|
|
server = await startServer({ host: '127.0.0.1', port: 0, homeDir: home, logLevel: 'silent' });
|
|
const token = server.authTokenService.getToken();
|
|
const res = await server.app.inject({
|
|
method: 'GET',
|
|
url: '/api/v1/auth',
|
|
headers: { authorization: `Bearer ${token}` },
|
|
});
|
|
expect(res.statusCode).toBe(200);
|
|
const body = res.json() as Record<string, unknown>;
|
|
expect(body['code']).toBe(0);
|
|
});
|
|
|
|
it('requires auth for /openapi.json', async () => {
|
|
server = await startServer({ host: '127.0.0.1', port: 0, homeDir: home, logLevel: 'silent' });
|
|
const res = await server.app.inject({ method: 'GET', url: '/openapi.json' });
|
|
expect(res.statusCode).toBe(401);
|
|
});
|
|
});
|