Public: anyone can read this; the crew can change it.
The owner can close signup: one live setting, open or closed, kept on every way an account is made #2089
scout/forge:scout/1142-the-owner-can-close-signup-one-live-sett → main 10 commits fork 2 threads · 2 unresolved
12 files
changed +351
−6
README.md +2 −2
| @@ -129,7 +129,7 @@ to `/recover`: give the email on your account and, if it is verified, a link | ||
| 129 | 129 | that lasts 30 minutes and works once registers a new passkey and signs you in. |
| 130 | 130 | The relying party is the address the browser used (or |
| 131 | 131 | `FORGE_URL`), so a passkey does not follow a forge to another hostname. |
| 132 | Sign-up is a username, an email address and a passkey (`/signup`, `src/web/pages/signup-passkey.ts`); the running server refuses a password-only sign-up on the form, `POST /api/v1/users` and the terminal (`passkeyOnlySignup`, set in `main.ts`). Accounts that already have a password still sign in. The owner claim of a spawned forge (`/claim`, `web/pages/claim.ts`) and an admin's everyday account (`web/pages/everyday.ts`) make their account with a passkey too (`web/passkey-ceremony.ts`, challenges from `POST /claim/options` and `POST /settings/everyday/options`); neither reads a password, and without JavaScript the page says a passkey needs it. A claim post with no passkey is refused and does not spend the link. | |
| 132 | Sign-up is a username, an email address and a passkey (`/signup`, `src/web/pages/signup-passkey.ts`); the running server refuses a password-only sign-up on the form, `POST /api/v1/users` and the terminal (`passkeyOnlySignup`, set in `main.ts`). Accounts that already have a password still sign in. Who can sign up is one live setting (`signup`: open, invite or closed), refused on every door including the passkey one, and set on the capacity page, `PATCH /api/v1/admin/settings`, the `capacity` MCP tool or the terminal's Signup command (`o`, `i`, `c`). The owner claim of a spawned forge (`/claim`, `web/pages/claim.ts`) and an admin's everyday account (`web/pages/everyday.ts`) make their account with a passkey too (`web/passkey-ceremony.ts`, challenges from `POST /claim/options` and `POST /settings/everyday/options`); neither reads a password, and without JavaScript the page says a passkey needs it. A claim post with no passkey is refused and does not spend the link. | |
| 133 | 133 | |
| 134 | 134 | Your SSH key is your identity. A key the forge knows signs you straight in; any |
| 135 | 135 | other key browses public repos as a guest and can press `s` to sign in with a |
| @@ -169,7 +169,7 @@ adds `FORGE_URL` (the canonical public base URL, needed for OAuth callbacks), | ||
| 169 | 169 | `FORGE_ORIGIN_ALLOW` (hosts an operator vouches for on a LAN). Behind a |
| 170 | 170 | platform edge that signs people in for every app it serves, `FORGE_EDGE_TOKEN` |
| 171 | 171 | is the edge's secret: a request carrying it and `X-Plat-User` is that person, |
| 172 | signed in by the platform, with an account made on first sight. Mail out | |
| 172 | signed in by the platform, with an account made on first sight. `FORGE_SIGNUP` (`open`, `invite` or `closed`; a wrong word fails the boot) says who may make an account; an admin changes it live under Settings → Capacity, `PATCH /api/v1/admin/settings` or the `capacity` MCP tool, and closed also stops the edge's first-sight accounts (the forge's first account is always let in; `invite` acts as closed for now). Mail out | |
| 173 | 173 | (the link that proves a newcomer's email) is SMTP submission over TLS, |
| 174 | 174 | configured by the forge-level secrets `MAIL_SMTP_URL` |
| 175 | 175 | (`smtps://user:pass@host:465`) and `MAIL_FROM`, set live under Settings → |
src/actions/users.ts +4 −1
| @@ -34,6 +34,7 @@ import { | ||
| 34 | 34 | type UpdateProfileError, |
| 35 | 35 | type User, |
| 36 | 36 | } from "../core/users.ts"; |
| 37 | import { signupOpen } from "../core/capacity.ts"; | |
| 37 | 38 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 38 | 39 | import { requireUser } from "./access.ts"; |
| 39 | 40 | |
| @@ -45,7 +46,7 @@ export interface SignUpInput { | ||
| 45 | 46 | humanToken?: string; |
| 46 | 47 | } |
| 47 | 48 | |
| 48 | export type SignUpError = CreateUserError | "human_check" | "passkey_required"; | |
| 49 | export type SignUpError = CreateUserError | "human_check" | "passkey_required" | "signup_closed"; | |
| 49 | 50 | |
| 50 | 51 | /** What anyone is told at a door that still offers a password-only sign-up. */ |
| 51 | 52 | export const PASSKEY_REQUIRED_MESSAGE = |
| @@ -84,6 +85,7 @@ export async function signUp( | ||
| 84 | 85 | // when every such account has a passkey (Settings → Passkeys shows who is |
| 85 | 86 | // left) and recovery without a password exists (#1618): then delete this |
| 86 | 87 | // action's password path, `verifyPassword`'s doors and the password column. |
| 88 | if (!signupOpen(app.db)) return err("signup_closed"); | |
High: this guard covers only the password |
||
| 87 | 89 | if (app.config.passkeyOnlySignup === true) return err("passkey_required"); |
| 88 | 90 | const secret = app.config.turnstileSecret; |
| 89 | 91 | const bootstrap = from.door === "terminal" && !hasAnyUser(app.db); |
| @@ -131,6 +133,7 @@ export async function signUpWithPasskey( | ||
| 131 | 133 | input: PasskeySignUpInput, |
| 132 | 134 | from: { address?: string | undefined } = {}, |
| 133 | 135 | ): Promise<Result<User, SignUpError | "passkey_taken">> { |
| 136 | if (!signupOpen(app.db)) return err("signup_closed"); | |
| 134 | 137 | const secret = app.config.turnstileSecret; |
| 135 | 138 | if (secret !== undefined) { |
| 136 | 139 | const verify = app.config.verifyHuman ?? turnstileVerifier(secret); |
src/core/capacity.ts +24 −1
| @@ -21,7 +21,8 @@ export type CapacityKey = | ||
| 21 | 21 | | "checks.shards" |
| 22 | 22 | | "children.max" |
| 23 | 23 | | "cells.egress" |
| 24 | | "guard_new_accounts"; | |
| 24 | | "guard_new_accounts" | |
| 25 | | "signup"; | |
| 25 | 26 | |
| 26 | 27 | interface Definition { |
| 27 | 28 | /** What the settings page and the API call it. */ |
| @@ -80,6 +81,13 @@ export const CAPACITY: Record<CapacityKey, Definition> = { | ||
| 80 | 81 | fallback: "off", |
| 81 | 82 | help: "on refuses repository creation and forks for accounts younger than 7 days (agents and admins are exempt); off leaves it to the usual permissions. Takes effect immediately. Replaces the hand-installed database trigger that used to do this with no toggle.", |
| 82 | 83 | }, |
| 84 | signup: { | |
| 85 | label: "Who can sign up", | |
| 86 | env: "FORGE_SIGNUP", | |
| 87 | kind: { type: "choice", values: ["open", "invite", "closed"] }, | |
| 88 | fallback: "open", | |
| 89 | help: "open lets anyone make an account; closed makes none, at /signup, in the API or through the platform edge. invite is kept and acts like closed for now. The forge's first account can always be made, and no one already signed in is affected. Takes effect immediately.", | |
| 90 | }, | |
| 83 | 91 | }; |
| 84 | 92 | |
| 85 | 93 | export const CAPACITY_KEYS = Object.keys(CAPACITY) as CapacityKey[]; |
| @@ -191,6 +199,21 @@ export function capacityCount( | ||
| 191 | 199 | return Number.isInteger(n) && n > 0 ? n : fallback; |
| 192 | 200 | } |
| 193 | 201 | |
| 202 | /** | |
| 203 | * Whether a new account may be made now: the forge says open, or has no | |
| 204 | * account yet (the first one is always let in). `invite` acts as `closed`. | |
| 205 | * Read on every attempt, so a change needs no restart. | |
| 206 | */ | |
| 207 | export function signupOpen(db: Database): boolean { | |
| 208 | return ( | |
| 209 | capacityValue(db, "signup") === "open" || | |
| 210 | db.query("SELECT 1 FROM user LIMIT 1").get() === null | |
| 211 | ); | |
| 212 | } | |
| 213 | ||
| 214 | /** The one sentence a refused sign-up carries, on every path. */ | |
| 215 | export const SIGNUP_CLOSED_MESSAGE = "Signup is closed on this forge."; | |
| 216 | ||
| 194 | 217 | /** How many hosted children this forge may hold now; read on every use. */ |
| 195 | 218 | export function maxChildren(db: Database): number { |
| 196 | 219 | return Number(capacityValue(db, "children.max")); |
src/core/users.ts +3
| @@ -7,6 +7,7 @@ import { markUnproven, markVerified } from "./verify.ts"; | ||
| 7 | 7 | import type { Database } from "bun:sqlite"; |
| 8 | 8 | import { isSafeName } from "../git/guard.ts"; |
| 9 | 9 | import { noreplyAddress } from "./commit_identity.ts"; |
| 10 | import { signupOpen } from "./capacity.ts"; | |
| 10 | 11 | import { reservedName } from "./reserved.ts"; |
| 11 | 12 | import { id } from "../kernel/id.ts"; |
| 12 | 13 | import { err, ok, type Result } from "../kernel/result.ts"; |
| @@ -333,6 +334,8 @@ export async function ensureEdgeUser( | ||
| 333 | 334 | const existing = getUserByName(db, login); |
| 334 | 335 | if (existing) |
| 335 | 336 | return existing.kind === "user" && isActive(existing) ? existing : undefined; |
| 337 | // A closed forge makes no account on sight; the first one is still let in. | |
| 338 | if (!signupOpen(db)) return undefined; | |
| 336 | 339 | // The edge vouches for a login, not for its right to a name: createUser |
| 337 | 340 | // refuses a reserved one, so a platform account called after the crew or |
| 338 | 341 | // the owner is not let in under it. |
src/http/api.ts +36
| @@ -23,6 +23,11 @@ import { mayView, mayViewId, sealedScope } from "../core/sealed.ts"; | ||
| 23 | 23 | import { changeCapacity, listCapacity } from "../actions/capacity.ts"; |
| 24 | 24 | import { changeLimit, listLimits } from "../actions/limits.ts"; |
| 25 | 25 | import { changeQuotas, readQuotas } from "../actions/quotas.ts"; |
| 26 | import { | |
| 27 | isCapacityKey, | |
| 28 | SIGNUP_CLOSED_MESSAGE, | |
| 29 | validCapacity, | |
| 30 | } from "../core/capacity.ts"; | |
| 26 | 31 | import { |
| 27 | 32 | accountSignIns, |
| 28 | 33 | endAccountSession, |
| @@ -1137,6 +1142,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1137 | 1142 | }, |
| 1138 | 1143 | { address: clientAddress(ctx.request) }, |
| 1139 | 1144 | ); |
| 1145 | if (!created.ok && created.error === "signup_closed") | |
| 1146 | return apiError("signup_closed", 403, SIGNUP_CLOSED_MESSAGE); | |
| 1140 | 1147 | if (!created.ok && created.error === "passkey_required") |
| 1141 | 1148 | return apiError("passkey_required", 422, PASSKEY_REQUIRED_MESSAGE); |
| 1142 | 1149 | if (!created.ok && created.error === "human_check") |
| @@ -1407,6 +1414,35 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 1407 | 1414 | ); |
| 1408 | 1415 | }); |
| 1409 | 1416 | |
| 1417 | // Settings by name, several at once: `{"signup": "closed"}`. Every value is | |
| 1418 | // checked before any is applied, so a bad one changes nothing. | |
| 1419 | router.add("PATCH", "/api/v1/admin/settings", async (ctx) => { | |
| 1420 | const denied = accountAdmin(ctx, "admin"); | |
| 1421 | if (denied) return denied; | |
| 1422 | const input = await body(ctx.request); | |
| 1423 | const entries = Object.entries(input); | |
| 1424 | if (entries.length === 0) | |
| 1425 | return apiError("invalid_value", 422, "no setting named"); | |
| 1426 | for (const [key, value] of entries) { | |
| 1427 | if (!isCapacityKey(key)) | |
| 1428 | return apiError("no_such_setting", 422, `no such setting: ${key}`); | |
| 1429 | if (typeof value !== "string" || !validCapacity(key, value.trim())) | |
| 1430 | return apiError( | |
| 1431 | "invalid_value", | |
| 1432 | 422, | |
| 1433 | `that value is outside what ${key} takes`, | |
| 1434 | ); | |
| 1435 | } | |
| 1436 | const changed = []; | |
| 1437 | for (const [key, value] of entries) { | |
| 1438 | const done = changeCapacity(ctx.app, ctx.auth.user, key, str(value).trim()); | |
| 1439 | if (!done.ok) | |
| 1440 | return apiError("not_applied", 409, `${key} was not applied; ${changed.length} earlier change(s) were`); | |
| 1441 | changed.push(done.value); | |
| 1442 | } | |
| 1443 | return json({ changed }); | |
| 1444 | }); | |
| 1445 | ||
| 1410 | 1446 | // The forge's limits (`settings/limits.ts`): the same door as capacity. |
| 1411 | 1447 | router.add("GET", "/api/v1/admin/limits", (ctx) => { |
| 1412 | 1448 | const denied = accountAdmin(ctx, "ro"); |
src/http/mcp.ts +2 −2
| @@ -2034,13 +2034,13 @@ const TOOLS: readonly Tool[] = [ | ||
| 2034 | 2034 | { |
| 2035 | 2035 | name: "capacity", |
| 2036 | 2036 | description: |
| 2037 | "The forge's capacity and guards, live: crew runs at once (crew.concurrency), checks at once (checks.concurrency), test shards per check (checks.shards), whether a cell's undeclared egress is refused or reported (cells.egress), and whether new accounts may create repositories and forks (guard_new_accounts: on refuses accounts younger than 7 days, off is the default). Without `key`, each setting, its value and where it comes from, and the recent changes. With `key` and `value` (\"\" goes back to the boot default), the change it would make; with dryRun false, made, by a forge admin with an admin-scope token, never an agent. A change is honoured by the next run, check or cell, never by a restart. The same as Settings → Capacity and /api/v1/admin/capacity.", | |
| 2037 | "The forge's capacity and guards, live: crew runs at once (crew.concurrency), checks at once (checks.concurrency), test shards per check (checks.shards), whether a cell's undeclared egress is refused or reported (cells.egress), and whether new accounts may create repositories and forks (guard_new_accounts: on refuses accounts younger than 7 days, off is the default), and who can sign up (signup: open, invite or closed; invite acts as closed for now, the first account is always let in). Without `key`, each setting, its value and where it comes from, and the recent changes. With `key` and `value` (\"\" goes back to the boot default), the change it would make; with dryRun false, made, by a forge admin with an admin-scope token, never an agent. A change is honoured by the next run, check or cell, never by a restart. The same as Settings → Capacity and /api/v1/admin/capacity.", | |
| 2038 | 2038 | inputSchema: { |
| 2039 | 2039 | type: "object", |
| 2040 | 2040 | properties: { |
| 2041 | 2041 | key: { |
| 2042 | 2042 | type: "string", |
| 2043 | enum: ["crew.concurrency", "checks.concurrency", "checks.shards", "cells.egress", "guard_new_accounts"], | |
| 2043 | enum: ["crew.concurrency", "checks.concurrency", "checks.shards", "cells.egress", "guard_new_accounts", "signup"], | |
| 2044 | 2044 | }, |
| 2045 | 2045 | value: { type: "string", maxLength: 16 }, |
| 2046 | 2046 | dryRun: { type: "boolean", description: "Plan only (the default). Send false to change it." }, |
src/main.ts +10
| @@ -137,6 +137,16 @@ if (lingerMs === null) { | ||
| 137 | 137 | process.exit(1); |
| 138 | 138 | } |
| 139 | 139 | |
| 140 | // FORGE_SIGNUP is who may make an account; a wrong word fails the boot rather | |
| 141 | // than leaving a forge open that was meant to be closed. | |
| 142 | { | |
| 143 | const signup = (process.env.FORGE_SIGNUP ?? "").trim(); | |
| 144 | if (signup !== "" && !["open", "invite", "closed"].includes(signup)) { | |
| 145 | console.error(`FORGE_SIGNUP: "${signup}" is not open, invite or closed`); | |
| 146 | process.exit(1); | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 140 | 150 | const trustedProxies = (process.env.FORGE_TRUSTED_PROXIES ?? "") |
| 141 | 151 | .split(",") |
| 142 | 152 | .map((s) => s.trim()) |
src/tui/forge.ts +11
| @@ -41,6 +41,7 @@ import { accessScreen } from "./screens/access.ts"; | ||
| 41 | 41 | import { domainsScreen } from "./screens/domains.ts"; |
| 42 | 42 | import { resilienceLines, resilienceScreen } from "./screens/resilience.ts"; |
| 43 | 43 | import { flowLines, flowScreen } from "./screens/flow.ts"; |
| 44 | import { signupScreen } from "./screens/signup.ts"; | |
| 44 | 45 | import { releasesScreen } from "./screens/releases.ts"; |
| 45 | 46 | import { mayRead } from "../release/gate.ts"; |
| 46 | 47 | import { stalledScreen } from "./screens/stalled.ts"; |
| @@ -236,6 +237,16 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] { | ||
| 236 | 237 | hint: "the pipeline's bottleneck and throughput", |
| 237 | 238 | run: () => ctx.push(flowScreen(ctx, flowLines(session.db))), |
| 238 | 239 | }); |
| 240 | // Who can sign up, open, invite or closed: a site admin's (#1142). | |
| 241 | if (session.user?.isAdmin === true) { | |
| 242 | const admin = session.user; | |
| 243 | commands.push({ | |
| 244 | id: "signup", | |
| 245 | title: "Signup", | |
| 246 | hint: "who can sign up: open, invite or closed", | |
| 247 | run: () => ctx.push(signupScreen(session.app, admin, ctx)), | |
| 248 | }); | |
| 249 | } | |
| 239 | 250 | // Each THREATS.md protection's standing, as /resilience says it: a site admin's. |
| 240 | 251 | if (session.user?.isAdmin === true) |
| 241 | 252 | commands.push({ |
src/tui/screens/signup.ts added +45
| @@ -0,0 +1,45 @@ | ||
| 1 | /** | |
| 2 | * Who can sign up, in the terminal, a forge admin's (#1142): the live | |
| 3 | * `signup` setting and a key for each value — o open, i invite, c closed — | |
| 4 | * applied through `changeCapacity`, the door the page, API and MCP use. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../../app.ts"; | |
| 8 | import { changeCapacity } from "../../actions/capacity.ts"; | |
| 9 | import { capacityValue } from "../../core/capacity.ts"; | |
| 10 | import type { User } from "../../core/users.ts"; | |
| 11 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 12 | import { binding } from "../app/keys.ts"; | |
| 13 | import { dialog } from "../ui/widgets.ts"; | |
| 14 | ||
| 15 | const KEYS: Record<string, string> = { o: "open", i: "invite", c: "closed" }; | |
| 16 | ||
| 17 | /** The line the dialog leads with. */ | |
| 18 | export function signupLine(app: App): string { | |
| 19 | return `Signup is ${capacityValue(app.db, "signup")}.`; | |
| 20 | } | |
| 21 | ||
| 22 | export function signupScreen(app: App, user: User, ctx: Ctx): Screen { | |
| 23 | return { | |
| 24 | title: "Signup", | |
| 25 | keys: [binding(["o"], "open"), binding(["i"], "invite"), binding(["c"], "closed"), binding(["enter", "esc", "q"], "close")], | |
| 26 | render(area, s) { | |
| 27 | const buttons = dialog(s, area, ctx.theme.c, { | |
| 28 | title: "Signup", | |
| 29 | body: [signupLine(app), "", "o open · i invite · c closed"], | |
| 30 | actions: [{ label: "Close" }], | |
| 31 | selected: 0, | |
| 32 | width: 48, | |
| 33 | }); | |
| 34 | buttons.forEach((rect) => ctx.hit(rect, () => ctx.pop())); | |
| 35 | }, | |
| 36 | key(e) { | |
| 37 | if (e.key === "enter" || e.key === "esc" || e.key === "q") ctx.pop(); | |
| 38 | else if (KEYS[e.key] !== undefined) { | |
| 39 | const done = changeCapacity(app, user, "signup", KEYS[e.key]!); | |
| 40 | ctx.toast(done.ok ? `Signup is ${done.value.to}` : "Signup was not changed", done.ok ? "default" : "attn"); | |
| 41 | } | |
| 42 | return true; | |
| 43 | }, | |
| 44 | }; | |
| 45 | } |
src/web/pages/signup-passkey.ts +4
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { newChallenge, verifyRegistration } from "../../auth/webauthn.ts"; |
| 10 | 10 | import { plausibleEmail, signUpWithPasskey, HUMAN_CHECK_MESSAGE } from "../../actions/users.ts"; |
| 11 | import { SIGNUP_CLOSED_MESSAGE, signupOpen } from "../../core/capacity.ts"; | |
| 11 | 12 | import { createSession } from "../../core/sessions.ts"; |
| 12 | 13 | import { getUserByName } from "../../core/users.ts"; |
| 13 | 14 | import { isSafeName } from "../../git/guard.ts"; |
| @@ -52,6 +53,7 @@ function guard(ctx: Ctx): Response | undefined { | ||
| 52 | 53 | export async function signupOptions(ctx: Ctx): Promise<Response> { |
| 53 | 54 | const denied = guard(ctx); |
| 54 | 55 | if (denied) return denied; |
| 56 | if (!signupOpen(ctx.app.db)) return refuse(403, SIGNUP_CLOSED_MESSAGE); | |
| 55 | 57 | const data = await boundedBody(ctx.request, STRANGER_MAX); |
| 56 | 58 | if (data === "too_large") return refuse(413, "payload too large"); |
| 57 | 59 | const username = str(data.username).trim(); |
| @@ -90,6 +92,8 @@ export async function signupPasskey(ctx: Ctx): Promise<Response> { | ||
| 90 | 92 | { username, email, passkey: made, passkeyName: "Passkey", humanToken: str(data.humanToken) }, |
| 91 | 93 | { address: clientAddress(ctx.request) }, |
| 92 | 94 | ); |
| 95 | if (!created.ok && created.error === "signup_closed") | |
| 96 | return refuse(403, SIGNUP_CLOSED_MESSAGE); | |
| 93 | 97 | if (!created.ok) |
| 94 | 98 | return refuse(created.error === "human_check" ? 422 : 409, WORDS[created.error] ?? "Sign-up did not work. Try again."); |
| 95 | 99 | const session = createSession(ctx.app.db, created.value.id); |
src/web/pages/site.ts +17
| @@ -14,6 +14,7 @@ import { clientAddress } from "../../http/peer.ts"; | ||
| 14 | 14 | import { verifyEmail } from "../../actions/verify.ts"; |
| 15 | 15 | import { STRANGER_MAX } from "../../http/limits.ts"; |
| 16 | 16 | import { createRepo } from "../../actions/repos.ts"; |
| 17 | import { SIGNUP_CLOSED_MESSAGE, signupOpen } from "../../core/capacity.ts"; | |
| 17 | 18 | import { HUMAN_CHECK_MESSAGE, PASSKEY_REQUIRED_MESSAGE, signUp } from "../../actions/users.ts"; |
| 18 | 19 | import { accessAllows, listOrgsForUser, orgAccess } from "../../core/orgs.ts"; |
| 19 | 20 | import { listPins } from "../../core/pins.ts"; |
| @@ -391,7 +392,21 @@ function siteKey(ctx: Ctx): string | undefined { | ||
| 391 | 392 | return turnstileSecret === undefined ? undefined : turnstileSiteKey; |
| 392 | 393 | } |
| 393 | 394 | |
| 395 | function signupClosed(ctx: Ctx): Response { | |
| 396 | return page( | |
| 397 | { | |
| 398 | ctx, | |
| 399 | title: "Sign up", | |
| 400 | body: html`<div class="page-head"><h1>Sign up</h1></div> | |
| 401 | <p id="signup-closed">${SIGNUP_CLOSED_MESSAGE}</p> | |
| 402 | <p><a href="/signin">Sign in</a></p>`, | |
| 403 | }, | |
| 404 | 403, | |
| 405 | ); | |
| 406 | } | |
| 407 | ||
| 394 | 408 | export function signupPage(ctx: Ctx): Response { |
| 409 | if (!signupOpen(ctx.app.db)) return signupClosed(ctx); | |
| 395 | 410 | return page({ |
| 396 | 411 | ctx, |
| 397 | 412 | title: "Sign up", |
| @@ -507,6 +522,7 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 507 | 522 | // Posted by people with no account yet: a stranger's form. |
| 508 | 523 | const form = await readForm(ctx, undefined, STRANGER_MAX); |
| 509 | 524 | if (form instanceof Response) return form; |
| 525 | if (!signupOpen(ctx.app.db)) return signupClosed(ctx); | |
| 510 | 526 | const username = field(form, "username"); |
| 511 | 527 | const email = field(form, "email"); |
| 512 | 528 | const created = await signUp( |
| @@ -530,6 +546,7 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 530 | 546 | weak_password: { password: "Passwords need at least 10 characters." }, |
| 531 | 547 | human_check: { human: HUMAN_CHECK_MESSAGE }, |
| 532 | 548 | passkey_required: { human: PASSKEY_REQUIRED_MESSAGE }, |
| 549 | signup_closed: { human: SIGNUP_CLOSED_MESSAGE }, | |
| 533 | 550 | }[created.error] ?? { username: "Something went wrong." }; |
| 534 | 551 | return page( |
| 535 | 552 | { |
test/signup-closed.test.ts added +193
| @@ -0,0 +1,193 @@ | ||
| 1 | /** | |
| 2 | * Who can sign up is one live setting (#1142): open, invite or closed. Closed | |
| 3 | * (and invite, for now) makes no account on any path — the page, the API, the | |
| 4 | * platform edge — yet the forge's first account can always be made, and an | |
| 5 | * admin's existing session never depends on it. A change is live, audited. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { signUpWithPasskey } from "../src/actions/users.ts"; | |
| 13 | import { signupScreen } from "../src/tui/screens/signup.ts"; | |
| 14 | import { changeCapacity } from "../src/actions/capacity.ts"; | |
| 15 | import { createApp, type App } from "../src/app.ts"; | |
| 16 | import { capacityChanges, signupOpen } from "../src/core/capacity.ts"; | |
| 17 | import { createToken } from "../src/core/tokens.ts"; | |
| 18 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 19 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 20 | ||
| 21 | const EDGE = "edge-secret-0123456789"; | |
| 22 | ||
| 23 | describe("signup setting", () => { | |
| 24 | let dir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let owner: User; | |
| 28 | let adminToken: string; | |
| 29 | ||
| 30 | const setSignup = (value: string) => changeCapacity(app, owner, "signup", value); | |
| 31 | const post = (username: string) => | |
| 32 | fetch(`${servers.url}/api/v1/users`, { | |
| 33 | method: "POST", | |
| 34 | headers: { "content-type": "application/json" }, | |
| 35 | body: JSON.stringify({ | |
| 36 | username, | |
| 37 | email: `${username}@example.test`, | |
| 38 | password: "a-good-passphrase", | |
| 39 | }), | |
| 40 | }); | |
| 41 | const edge = (login: string) => | |
| 42 | fetch(`${servers.url}/api/v1/user`, { | |
| 43 | headers: { "x-plat-user": login, "x-plat-edge-token": EDGE }, | |
| 44 | }); | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dir = await mkdtemp(join(tmpdir(), "forge-signup-")); | |
| 48 | app = createApp({ dataDir: dir, port: 0, edgeToken: EDGE }); | |
| 49 | servers = await serve(app); | |
| 50 | }); | |
| 51 | afterAll(async () => { | |
| 52 | servers.stop(); | |
| 53 | app.close(); | |
| 54 | await rm(dir, { recursive: true, force: true }); | |
| 55 | }); | |
| 56 | ||
| 57 | test("a fresh forge makes its first account, closed or not", async () => { | |
| 58 | // Closing needs an admin, so close the store directly on the empty forge. | |
| 59 | app.db | |
| 60 | .query( | |
| 61 | "INSERT INTO forge_setting (key, value, updated_by, updated_at) VALUES ('signup','closed','boot','now')", | |
| 62 | ) | |
| 63 | .run(); | |
| 64 | expect(signupOpen(app.db)).toBe(true); | |
| 65 | const res = await post("ada"); | |
| 66 | expect(res.status).toBe(201); | |
| 67 | owner = getUserByName(app.db, "ada")!; | |
| 68 | expect(owner.isAdmin).toBe(true); | |
| 69 | expect(signupOpen(app.db)).toBe(false); | |
| 70 | adminToken = createToken(app.db, owner.id, "t", "admin").token; | |
| 71 | }); | |
| 72 | ||
| 73 | test("closed: page, API and edge make no account; sign-in still works", async () => { | |
| 74 | const page = await fetch(`${servers.url}/signup`); | |
| 75 | expect(page.status).toBe(403); | |
| 76 | expect(await page.text()).toContain("Signup is closed on this forge."); | |
| 77 | ||
| 78 | const api = await post("bob"); | |
| 79 | expect(api.status).toBe(403); | |
| 80 | expect(((await api.json()) as { message?: string }).message ?? "").toContain("closed"); | |
| 81 | ||
| 82 | const form = await fetch(`${servers.url}/signup`, { | |
| 83 | method: "POST", | |
| 84 | headers: { | |
| 85 | "content-type": "application/x-www-form-urlencoded", | |
| 86 | origin: servers.url, | |
| 87 | }, | |
| 88 | body: new URLSearchParams({ | |
| 89 | username: "bob", | |
| 90 | email: "[email protected]", | |
| 91 | password: "a-good-passphrase", | |
| 92 | }), | |
| 93 | }); | |
| 94 | expect(form.status).toBe(403); | |
| 95 | ||
| 96 | await edge("carol"); | |
| 97 | expect(getUserByName(app.db, "carol")).toBeUndefined(); | |
| 98 | expect(getUserByName(app.db, "bob")).toBeUndefined(); | |
| 99 | ||
| 100 | // The admin's own edge identity and password session are untouched. | |
| 101 | const me = await edge("ada"); | |
| 102 | expect(me.status).toBe(200); | |
| 103 | }); | |
| 104 | ||
| 105 | test("invite acts like closed for now", async () => { | |
| 106 | expect(setSignup("invite").ok).toBe(true); | |
| 107 | expect((await post("bob")).status).toBe(403); | |
| 108 | expect((await fetch(`${servers.url}/signup`)).status).toBe(403); | |
| 109 | }); | |
| 110 | ||
| 111 | test("a live change by PATCH, with no restart, opens every path", async () => { | |
| 112 | const res = await fetch(`${servers.url}/api/v1/admin/settings`, { | |
| 113 | method: "PATCH", | |
| 114 | headers: { | |
| 115 | authorization: `Bearer ${adminToken}`, | |
| 116 | "content-type": "application/json", | |
| 117 | }, | |
| 118 | body: JSON.stringify({ signup: "open" }), | |
| 119 | }); | |
| 120 | expect(res.status).toBe(200); | |
| 121 | expect((await fetch(`${servers.url}/signup`)).status).toBe(200); | |
| 122 | expect((await post("bob")).status).toBe(201); | |
| 123 | await edge("carol"); | |
| 124 | expect(getUserByName(app.db, "carol")).toBeDefined(); | |
| 125 | }); | |
| 126 | ||
| 127 | test("a wrong value or key is refused and changes nothing; others are not admin", async () => { | |
| 128 | const patch = (token: string, body: unknown) => | |
| 129 | fetch(`${servers.url}/api/v1/admin/settings`, { | |
| 130 | method: "PATCH", | |
| 131 | headers: { | |
| 132 | authorization: `Bearer ${token}`, | |
| 133 | "content-type": "application/json", | |
| 134 | }, | |
| 135 | body: JSON.stringify(body), | |
| 136 | }); | |
| 137 | expect((await patch(adminToken, { signup: "sometimes" })).status).toBe(422); | |
| 138 | expect((await patch(adminToken, { nope: "x" })).status).toBe(422); | |
| 139 | expect(changeCapacity(app, owner, "signup", "sometimes").ok).toBe(false); | |
| 140 | const bob = getUserByName(app.db, "bob")!; | |
| 141 | const bobToken = createToken(app.db, bob.id, "t", "admin").token; | |
| 142 | expect((await patch(bobToken, { signup: "closed" })).status).toBe(404); | |
| 143 | expect(signupOpen(app.db)).toBe(true); | |
| 144 | }); | |
| 145 | ||
| 146 | test("closed: the passkey door gives no challenge and makes no account", async () => { | |
| 147 | setSignup("closed"); | |
| 148 | const res = await fetch(`${servers.url}/signup/passkey/options`, { | |
| 149 | method: "POST", | |
| 150 | headers: { "content-type": "application/json", origin: servers.url }, | |
| 151 | body: JSON.stringify({ username: "erin", email: "[email protected]" }), | |
| 152 | }); | |
| 153 | expect(res.status).toBe(403); | |
| 154 | expect(await res.text()).not.toContain("challenge"); | |
| 155 | const made = await signUpWithPasskey(app, { | |
| 156 | username: "erin", | |
| 157 | email: "[email protected]", | |
| 158 | passkey: { id: "k", publicKey: "p", counter: 0 } as never, | |
| 159 | passkeyName: "Passkey", | |
| 160 | }); | |
| 161 | expect(made).toEqual({ ok: false, error: "signup_closed" }); | |
| 162 | expect(getUserByName(app.db, "erin")).toBeUndefined(); | |
| 163 | setSignup("open"); | |
| 164 | }); | |
| 165 | ||
| 166 | test("the terminal screen shows and sets the value", () => { | |
| 167 | const toasts: string[] = []; | |
| 168 | const ctx = { pop() {}, hit() {}, toast: (m: string) => toasts.push(m) } as never; | |
| 169 | const screen = signupScreen(app, owner, ctx); | |
| 170 | screen.key({ key: "c", ctrl: false, alt: false, shift: false } as never); | |
| 171 | expect(signupOpen(app.db)).toBe(false); | |
| 172 | screen.key({ key: "o", ctrl: false, alt: false, shift: false } as never); | |
| 173 | expect(signupOpen(app.db)).toBe(true); | |
| 174 | expect(toasts).toEqual(["Signup is closed", "Signup is open"]); | |
| 175 | }); | |
| 176 | ||
| 177 | test("every change is kept with who and when", () => { | |
| 178 | setSignup("closed"); | |
| 179 | const last = capacityChanges(app.db).find((c) => c.key === "signup")!; | |
| 180 | expect(last.to).toBe("closed"); | |
| 181 | expect(last.actorName).toBe("ada"); | |
| 182 | expect(last.at).toMatch(/^\d{4}-/); | |
| 183 | }); | |
| 184 | ||
| 185 | test("createUser still makes accounts directly (the owner's own paths)", async () => { | |
| 186 | const made = await createUser(app.db, { | |
| 187 | username: "dan", | |
| 188 | email: "[email protected]", | |
| 189 | password: "a-good-passphrase", | |
| 190 | }); | |
| 191 | expect(made.ok).toBe(true); | |
| 192 | }); | |
| 193 | }); |
new above
offline
Merge conflict markers are committed here (through line 136). Keep main's paragraph and add this pull's sentence about the
signupsetting, then delete the markers.