First half of docs/music.md's phase 1 (Accounts): Discord sign-in and basic accounts. The OIDC provider (Music as a trusted client, the `imvu` scope) is the next PR.
**Discord sign-in** (on when `DISCORD_CLIENT_ID` and `DISCORD_CLIENT_SECRET` are set; Discord redirects to `<API_URL>/api/auth/callback/discord`):
- The first time, it makes a **basic account** (`user.kind = "basic"`): Discord's name and email, no username or password. Basic accounts are for T5DE's other sites (Music); on the hub, every route but `/me` and `/account/finish` answers 403 `finish_setup`, and the website shows "Finish Setting Up Your Account" in place of every page.
- Only a **verified** Discord email (`user.validateUserInfo`): `email_not_verified`, or `email_not_found` for Discord accounts without one.
- **Never linked by matching email** (`accountLinking.disableImplicitLinking`): a Discord account whose email belongs to another T5DE account ends with `account_not_linked`, and the sign-in page says to sign in and link Discord from Account.
- Basic accounts don't count toward "the first account is an admin".
**Finishing setup**: `POST /api/v1/account/finish {username, password}` adds a password sign-in and a username in one transaction and makes the account a member (waiting for approval if registration is closed). Discord stays linked.
**Members** link Discord from the Account page (`/api/auth/link-social`; `account_already_linked_to_different_user` if it belongs to another account; merging basic accounts into members is phase 5) and unlink it (`DELETE /api/v1/account/discord`, audited). The console tags basic accounts "Discord only".
First half of docs/music.md's phase 1 (Accounts): Discord sign-in and basic accounts. The OIDC provider (Music as a trusted client, the `imvu` scope) is the next PR.
**Discord sign-in** (on when `DISCORD_CLIENT_ID` and `DISCORD_CLIENT_SECRET` are set; Discord redirects to `<API_URL>/api/auth/callback/discord`):
- The first time, it makes a **basic account** (`user.kind = "basic"`): Discord's name and email, no username or password. Basic accounts are for T5DE's other sites (Music); on the hub, every route but `/me` and `/account/finish` answers 403 `finish_setup`, and the website shows "Finish Setting Up Your Account" in place of every page.
- Only a **verified** Discord email (`user.validateUserInfo`): `email_not_verified`, or `email_not_found` for Discord accounts without one.
- **Never linked by matching email** (`accountLinking.disableImplicitLinking`): a Discord account whose email belongs to another T5DE account ends with `account_not_linked`, and the sign-in page says to sign in and link Discord from Account.
- Basic accounts don't count toward "the first account is an admin".
**Finishing setup**: `POST /api/v1/account/finish {username, password}` adds a password sign-in and a username in one transaction and makes the account a member (waiting for approval if registration is closed). Discord stays linked.
**Members** link Discord from the Account page (`/api/auth/link-social`; `account_already_linked_to_different_user` if it belongs to another account; merging basic accounts into members is phase 5) and unlink it (`DELETE /api/v1/account/discord`, audited). The console tags basic accounts "Discord only".
Also: Better Auth 1.7.14 → 1.7.26 (the OAuth provider package in the next PR needs it); migration `0021_account_kind` (one column, default `member`).
Tests: `apps/api/test/discord.test.ts` runs Better Auth's real social sign-in and callback against a stand-in for Discord's token and profile endpoints (basic account, verified email only, no linking by email, finishing setup, closed registration, linking and unlinking). Full suite 68/68, `check-types` and `build` pass locally. The new pages (sign-in's Discord button, Finish Setting Up, the Discord card) aren't checked in a browser yet: that needs the Discord application, then staging.
Before deploying: a Discord application with both redirect URLs (prod and staging), and its ID and secret in the hub stacks' secrets (dhkatz/infra).
Also: Better Auth 1.7.14 → 1.7.26 (the OAuth provider package in the next PR needs it); migration `0021_account_kind` (one column, default `member`).
Tests: `apps/api/test/discord.test.ts` runs Better Auth's real social sign-in and callback against a stand-in for Discord's token and profile endpoints (basic account, verified email only, no linking by email, finishing setup, closed registration, linking and unlinking). Full suite 68/68, `check-types` and `build` pass locally. The new pages (sign-in's Discord button, Finish Setting Up, the Discord card) aren't checked in a browser yet: that needs the Discord application, then staging.
Before deploying: a Discord application with both redirect URLs (prod and staging), and its ID and secret in the hub stacks' secrets (dhkatz/infra).
Co-authored-by: dhkatz <[email protected]>
110 lines
6.1 KiB
TypeScript
110 lines
6.1 KiB
TypeScript
// Registration: open (new accounts can use the hub at once) or closed (they sign in, and wait for a moderator's
|
|
// approval); admins switch it in the console, and the environment gives the default.
|
|
import { afterAll, beforeAll, expect, test } from "bun:test";
|
|
import { openDatabase, type Database } from "@t5de/db";
|
|
import { createApp } from "../src/app";
|
|
import { createAuth } from "../src/auth";
|
|
import { ImvuClient } from "../src/imvu";
|
|
import { Settings } from "../src/settings";
|
|
import { twoFactorOn } from "./two-factor";
|
|
|
|
const webURL = "http://web.test";
|
|
let db: Database;
|
|
let close: () => Promise<void>;
|
|
let base: string;
|
|
let stop: () => void;
|
|
|
|
beforeAll(async () => {
|
|
({ db, close } = await openDatabase("memory"));
|
|
// Closed by default (as on staging), until an admin changes it.
|
|
const settings = new Settings(db, { registration: "closed" });
|
|
const auth = createAuth(db, { apiURL: "http://api.test", webURL, secret: "test-secret-test-secret-test-secret-1234", rateLimit: false, settings });
|
|
const imvu = new ImvuClient("http://imvu.test", (async () => Response.json({ denormalized: {} })) as unknown as typeof fetch);
|
|
const hub = createApp({ db, auth, imvu, webURL, settings });
|
|
hub.app.listen({ port: 0, hostname: "127.0.0.1" });
|
|
const server = hub.app.server as unknown as { port: number; stop: () => void };
|
|
stop = () => server.stop();
|
|
base = `http://127.0.0.1:${server.port}`;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
stop();
|
|
await close();
|
|
});
|
|
|
|
async function call(cookie: { v: string }, method: string, path: string, body?: unknown) {
|
|
const res = await fetch(base + path, {
|
|
method,
|
|
headers: { Origin: webURL, ...(body !== undefined ? { "Content-Type": "application/json" } : {}), ...(cookie.v ? { Cookie: cookie.v } : {}) },
|
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
});
|
|
const set = res.headers.getSetCookie();
|
|
if (set.length) cookie.v = set.map((c) => c.split(";")[0]).join("; ");
|
|
const text = await res.text();
|
|
return { status: res.status, json: text ? JSON.parse(text) : undefined };
|
|
}
|
|
|
|
async function signUp(name: string) {
|
|
const cookie = { v: "" };
|
|
expect((await call(cookie, "POST", "/api/auth/sign-up/email", { email: `${name}@example.test`, password: "a long password", name, username: name })).status).toBe(200);
|
|
return cookie;
|
|
}
|
|
|
|
test("closed: new accounts wait for approval; the first one runs the hub", async () => {
|
|
expect((await call({ v: "" }, "GET", "/api/v1/hub")).json).toEqual({ registration: "closed", discord: false });
|
|
const admin = await signUp("ada");
|
|
expect((await call(admin, "GET", "/api/v1/me")).json.person).toMatchObject({ role: "admin", approved: true });
|
|
expect((await call(admin, "GET", "/api/v1/account")).status).toBe(200);
|
|
|
|
const bo = await signUp("bo_");
|
|
// Signed in, but nothing else.
|
|
expect((await call(bo, "GET", "/api/v1/me")).json.person).toMatchObject({ role: "user", approved: false });
|
|
expect(await call(bo, "GET", "/api/v1/account")).toMatchObject({ status: 403, json: { error: "awaiting_approval" } });
|
|
expect((await call(bo, "POST", "/api/v1/account/links", { username: "someone" })).status).toBe(403);
|
|
expect((await call(bo, "GET", "/api/v1/console")).status).toBe(403);
|
|
|
|
// Moderators see who's waiting, and approve them.
|
|
expect((await call(admin, "GET", "/api/v1/console")).json.moderation.counts.waiting).toBe(1);
|
|
const waiting = (await call(admin, "GET", "/api/v1/console/people?waiting=true")).json.people;
|
|
expect(waiting.map((p: any) => p.username)).toEqual(["bo_"]);
|
|
expect((await call(admin, "POST", `/api/v1/console/people/${waiting[0].id}/approve`)).json.person.approved).toBe(true);
|
|
expect((await call(bo, "GET", "/api/v1/account")).status).toBe(200);
|
|
expect((await call(admin, "GET", "/api/v1/console/people?waiting=true")).json.people).toEqual([]);
|
|
// An approved account can't be declined.
|
|
expect((await call(admin, "POST", `/api/v1/console/people/${waiting[0].id}/decline`)).status).toBe(400);
|
|
});
|
|
|
|
test("declining deletes the account", async () => {
|
|
const admin = { v: "" };
|
|
await call(admin, "POST", "/api/auth/sign-in/username", { username: "ada", password: "a long password" });
|
|
const cy = await signUp("cyd");
|
|
const [p] = (await call(admin, "GET", "/api/v1/console/people?waiting=true")).json.people;
|
|
expect(p.username).toBe("cyd");
|
|
expect((await call(admin, "POST", `/api/v1/console/people/${p.id}/decline`)).status).toBe(200);
|
|
expect((await call(cy, "GET", "/api/v1/me")).status).toBe(401);
|
|
expect((await call({ v: "" }, "POST", "/api/auth/sign-in/username", { username: "cyd", password: "a long password" })).status).toBe(401);
|
|
});
|
|
|
|
test("only admins open or close registration", async () => {
|
|
const admin = { v: "" };
|
|
await call(admin, "POST", "/api/auth/sign-in/username", { username: "ada", password: "a long password" });
|
|
const mod = await signUp("dot");
|
|
const dot = (await call(admin, "GET", "/api/v1/console/people?waiting=true")).json.people[0];
|
|
await call(admin, "POST", `/api/v1/console/people/${dot.id}/approve`);
|
|
// Roles and hub settings are sensitive: two-factor sign-in first.
|
|
expect((await call(admin, "POST", `/api/v1/console/people/${dot.id}/role`, { role: "moderator" })).json.error).toBe("two_factor_required");
|
|
expect((await call(admin, "PUT", "/api/v1/console/settings", { registration: "open" })).json.error).toBe("two_factor_required");
|
|
await twoFactorOn(db, "ada");
|
|
await call(admin, "POST", `/api/v1/console/people/${dot.id}/role`, { role: "moderator" });
|
|
expect((await call(mod, "GET", "/api/v1/console/settings")).json).toEqual({ registration: "closed" });
|
|
expect((await call(mod, "PUT", "/api/v1/console/settings", { registration: "open" })).status).toBe(403);
|
|
|
|
expect((await call(admin, "PUT", "/api/v1/console/settings", { registration: "open" })).json).toEqual({ registration: "open" });
|
|
expect((await call({ v: "" }, "GET", "/api/v1/hub")).json).toEqual({ registration: "open", discord: false });
|
|
// Open: new accounts can use the hub at once.
|
|
const eve = await signUp("eve");
|
|
expect((await call(eve, "GET", "/api/v1/account")).status).toBe(200);
|
|
const audit = (await call(admin, "GET", "/api/v1/console")).json.audit.map((e: any) => e.action);
|
|
expect(audit).toContain("hub.registration");
|
|
});
|