Files
SnipzilandClaude Opus 5.5 e2454447d9 test(signin): Discord sign-ins take a 2FA code, and email recovery its limits
test/signin.js goes through both dashboards' Discord sign-in against a
stand-in for Discord's OAuth2 API (test/fake-discord.js, loaded into the
server with NODE_OPTIONS), and through email recovery against a mail
webhook of its own: Discord alone opens neither dashboard, a waiting
sign-in is used once and dies after five wrong codes, 2FA is removed by
email only with the password, not right after an email password reset and
never for staff, and one account gets three recovery emails an hour. It
fails without the two commits before it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-21 22:54:26 -04:00

30 lines
1.3 KiB
JavaScript

"use strict";
// loaded into the test server (NODE_OPTIONS --require, by test/signin.js):
// Discord's OAuth2 API answered here, so a sign-in with Discord can be gone
// through without Discord. the code Discord would send back names who signs
// in ("mira" -> Mira's Discord account); anything else goes out as usual.
const USERS = {
mira: {id: "100000000000000001", username: "mira_discord"},
hal: {id: "100000000000000002", username: "hal_discord"},
};
const realFetch = globalThis.fetch;
globalThis.fetch = async (url, options = {}) => {
const href = String(url);
if (!href.startsWith("https://discord.com/api/")) {
return realFetch(url, options);
}
const json = (status, body) => new Response(JSON.stringify(body), {status, headers: {"Content-Type": "application/json"}});
if (href.endsWith("/oauth2/token")) {
const code = new URLSearchParams(String(options.body || "")).get("code");
return USERS[code] ? json(200, {access_token: `token-${code}`, token_type: "Bearer"}) : json(400, {error: "invalid_grant"});
}
if (href.endsWith("/users/@me")) {
const user = USERS[String((options.headers || {}).Authorization || "").replace(/^Bearer token-/, "")];
return user ? json(200, {...user, discriminator: "0"}) : json(401, {});
}
return json(404, {});
};