AiTools/test/helpers/server.js
Tony AVERTY e9164b323c feat(auth): connexion Google et Apple, sessions liées aux comptes
- Google (PKCE) et Apple (response_mode=form_post, secret client JWT ES256 signé avec node:crypto), sans nouvelle dépendance ;
- id_token vérifié pour les deux : signature RS256 via les JWKS, émetteur, audience, expiration, nonce, email vérifié ;
- state à usage unique lié au navigateur par un cookie SameSite=None limité à /auth (le retour d'Apple est un POST cross-site) ;
- sessions liées à users.id ; un compte suspendu ne peut ni se connecter ni utiliser une session existante ;
- ALLOWED_EMAILS supprimée, ADMIN_EMAILS désigne les admins, variables APPLE_* toutes ou aucune.
2026-10-22 13:40:25 +02:00

50 lines
2.3 KiB
JavaScript

// Serveur HTTP de test : vraie application sur un port libre, base de test, comptes créés à la demande.
import { randomBytes } from 'node:crypto';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { now } from '../../src/db.js';
import { createApp } from '../../src/server.js';
import { testDb } from './db.js';
import { addUser } from './users.js';
export const APP_URL = 'https://ai.tony-averty.dev';
export async function startApp({ cfg = {}, worker = false } = {}) {
const tmp = mkdtempSync(join(tmpdir(), 'aitools-srv-'));
const config = {
appUrl: APP_URL, google: { clientId: 'gid', clientSecret: 'gsecret' }, apple: null, adminEmails: [],
secretKey: randomBytes(32), dataDir: tmp, claudeBin: 'claude', thermalMax: 75, ...cfg,
};
const app = await createApp(config, { db: await testDb(), worker });
await new Promise((r) => app.server.listen(0, '127.0.0.1', r));
const base = `http://127.0.0.1:${app.server.address().port}`;
/** Requêtes avec la session d'un cookie (ou sans). */
const client = (cookie) => ({
get: (p, opts = {}) => fetch(base + p, { redirect: 'manual', headers: { ...(cookie ? { cookie } : {}), accept: 'application/json', ...opts.headers } }),
post: (p, body = {}, { origin = APP_URL, form = false } = {}) => fetch(base + p, {
method: 'POST', redirect: 'manual',
headers: {
...(cookie ? { cookie } : {}), ...(origin ? { origin } : {}), accept: 'application/json',
'content-type': form ? 'application/x-www-form-urlencoded' : 'application/json',
},
body: form ? new URLSearchParams(body).toString() : JSON.stringify(body),
}),
});
/** Crée un compte avec une session ouverte. */
let n = 0;
const newUser = async (opts = {}) => {
n++;
const email = opts.email || `user${n}@test.fr`;
const id = await addUser(app.db, { ...opts, email });
const sid = `sess-${id}-${n}`;
await app.db.prepare('INSERT INTO sessions(id, user_id, created_at, expires_at) VALUES(?, ?, ?, ?)').run(sid, id, now(), now() + 3600);
const cookie = `sid=${sid}`;
return { id, email, cookie, ...client(cookie) };
};
const close = async () => { await app.close(); rmSync(tmp, { recursive: true, force: true }); };
return { app, db: app.db, base, tmp, client, newUser, close, anon: client(null) };
}