Public: anyone can read this; the crew can change it.

An edge sign-in for someone with no forge account creates nothing and goes to the forge's own signup #2762

Parked opened this
Closed without merging

scout/forge:scout/2593-an-edge-sign-in-for-someone-with-no-forg → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
12 files changed +57 −83
src/core/users.ts +6 −20
@@ -6,7 +6,6 @@
66 import { markUnproven, markVerified } from "./verify.ts";
77 import type { Database } from "bun:sqlite";
88 import { isSafeName } from "../git/guard.ts";
9 import { noreplyAddress } from "./commit_identity.ts";
109 import { reservedName } from "./reserved.ts";
1110 import { id } from "../kernel/id.ts";
1211 import { err, ok, type Result } from "../kernel/result.ts";
@@ -452,31 +451,18 @@ export function getUserByName(
452451
453452 /**
454453 * The person behind an identity the platform edge vouched for: found by
455 * name, or made on first sight — with no password, because the edge is how
456 * they sign in, and a noreply address until they set one. Like a sign-up,
457 * it is never the admin, first or not. A name that is an
458 * organization or a crew member here is nobody's login.
454 * name, never made. The edge vouches for a login, not for a right to an
455 * account here; someone with none signs up at /signup, under its setting and
456 * checks. A name that is an organization or a crew member is nobody's login.
459457 */
460458 export async function ensureEdgeUser(
461459 db: Database,
462460 login: string,
463 host: string,
464461 ): Promise<User | undefined> {
465462 const existing = getUserByName(db, login);
466 if (existing)
467 return existing.kind === "user" && isActive(existing) ? existing : undefined;
468 // The edge vouches for a login, not for its right to a name: createUser
469 // refuses a reserved one, so a platform account called after the crew or
470 // the owner is not let in under it.
471 const made = await createUser(db, {
472 username: login,
473 email: noreplyAddress(login, host),
474 origin: "edge",
475 });
476 if (!made.ok) return undefined;
477 // A newcomer through the edge starts unproven like one through /signup
478 // (its placeholder is no address at all).
479 return made.value;
463 return existing && existing.kind === "user" && isActive(existing)
464 ? existing
465 : undefined;
480466 }
481467
482468 /** Why a password was not accepted: wrong, or tried too often to check. */
src/http/context.ts +3
@@ -17,6 +17,9 @@ export interface Auth {
1717 readonly scope: TokenScope | undefined;
1818 /** `edge`: the platform in front of the forge signed this person in. */
1919 readonly via: "session" | "edge" | "token" | "password" | "none";
20 /** The edge vouched for a login that has no account here: signed out, and
21 * the page offers /signup (the person is never made an account). */
22 readonly edgeStranger?: true;
2023 /** Which signed-in browser asked, when `via` is `session`. */
2124 readonly sessionId?: string;
2225 /** Set for a proposal token: all it may do (`http/proposal_gate.ts`). */
src/http/server.ts +11 −19
@@ -62,7 +62,6 @@ import { trackPullHead } from "../core/pulls.ts";
6262 import { createRepo, repositoryLimitMessage, resolveOwner } from "../actions/repos.ts";
6363 import { sessionOf } from "../core/sessions.ts";
6464 import { scopeAllows, verifyToken } from "../core/tokens.ts";
65 import { forgeHost } from "../core/commit_identity.ts";
6665 import {
6766 ensureEdgeUser,
6867 getUserByName,
@@ -70,7 +69,6 @@ import {
7069 verifyPasswordThrottled,
7170 } from "../core/users.ts";
7271 import { edgeIdentity } from "./edge.ts";
73 import { signupClosed } from "../actions/users.ts";
7472 import { liveResponse, readyResponse } from "./health.ts";
7573 import { running } from "../kernel/version.ts";
7674 import { runGit } from "../git/env.ts";
@@ -502,27 +500,21 @@ import { getVerifiedDomain } from "../core/domains.ts";
502500 const NO_AUTH: Auth = { user: undefined, scope: undefined, via: "none" };
503501
504502 async function resolveAuth(app: App, request: Request): Promise<Auth> {
503 const auth = await resolveCredentials(app, request);
504 if (auth.user) return auth;
505 const login = edgeIdentity(request, app.config.edgeToken);
506 return login === undefined ? auth : { ...auth, edgeStranger: true };
507 }
508
509 async function resolveCredentials(app: App, request: Request): Promise<Auth> {
505510 // The platform edge, when there is one, has already signed this person
506511 // in; its word outranks a cookie of ours.
507512 const login = edgeIdentity(request, app.config.edgeToken);
508513 if (login !== undefined) {
509 const existed = getUserByName(app.db, login) !== undefined;
510 // Existing accounts sign in; a new one is made only while signup allows.
511 const user =
512 !existed && signupClosed(app)
513 ? undefined
514 : await ensureEdgeUser(app.db, login, forgeHost(app.config));
515 if (user) {
516 // An account the edge made is audited like one made at /signup.
517 if (!existed) {
518 app.bus.append({
519 kind: "user.created",
520 actorId: user.id,
521 payload: { via: "edge", address: clientAddress(request) },
522 });
523 }
524 return { user, scope: "admin", via: "edge" };
525 }
514 // Only an existing account signs in; anyone else is signed out and
515 // offered /signup, which applies the signup setting.
516 const user = await ensureEdgeUser(app.db, login);
517 if (user) return { user, scope: "admin", via: "edge" };
526518 }
527519
528520 const session = cookieValue(request, "forge_session");
src/web/layout.ts +8 −6
@@ -476,12 +476,14 @@ function topbar(
476476 }
477477 : { label: "Sign out", post: "/logout", icon: "signOut" },
478478 )}`
479 : html`${guestMenu(theme)}${btn("Sign in", {
480 href:
481 ctx.app.config.edgeToken === undefined
482 ? "/signin"
483 : edgeLoginUrl(ctx.url.pathname + ctx.url.search),
484 })}`}
479 : html`${guestMenu(theme)}${ctx.auth.edgeStranger
480 ? btn("Sign up", { href: "/signup" })
481 : btn("Sign in", {
482 href:
483 ctx.app.config.edgeToken === undefined
484 ? "/signin"
485 : edgeLoginUrl(ctx.url.pathname + ctx.url.search),
486 })}`}
485487 </span>
486488 </header>`;
487489 }
src/web/pages/site.ts +1 −1
@@ -263,7 +263,7 @@ function authForm(
263263 <p class="muted small">
264264 ${kind === "signin"
265265 ? `Accounts made through ${name} sign in there and have no password here. The form below is for an account that was given one.`
266 : `Signing in through ${name} makes your account on first sight; the form below makes one with a password instead.`}
266 : `Signing in through ${name} does not make an account here; the form below does.`}
267267 </p>
268268 </div>`,
269269 );
test/accounts.test.ts +4 −12
@@ -121,18 +121,6 @@ describe("accounts: the owner sees, suspends and removes", () => {
121121 expect(typeof made?.payload["address"]).toBe("string");
122122 });
123123
124 test("an account the edge makes is audited too", async () => {
125 const res = await fetch(`${base}/api/v1/user`, {
126 headers: { "x-plat-user": "edgy", "x-plat-edge-token": EDGE },
127 });
128 expect(res.status).toBe(200);
129 const user = getUserByName(app.db, "edgy")!;
130 const made = listEvents(app.db, { kind: "user.created" }).find(
131 (e) => e.actorId === user.id,
132 );
133 expect(made?.payload["via"]).toBe("edge");
134 });
135
136124 test("the owner hears of a new account once", async () => {
137125 await notifier.settled();
138126 const before = posts.length;
@@ -170,6 +158,10 @@ describe("accounts: the owner sees, suspends and removes", () => {
170158 });
171159
172160 test("the owner's list shows each account, how it came, and what it holds", async () => {
161 // An account the edge made before it stopped making them.
162 const edgy = await insertAgent(app.db, { username: "edgy", email: "[email protected]", origin: "edge" });
163 if (!edgy.ok) throw new Error(edgy.error);
164 app.bus.append({ kind: "user.created", actorId: edgy.value.id, payload: { via: "edge" } });
173165 const res = await api("/api/v1/admin/accounts", { token: ownerToken });
174166 expect(res.status).toBe(200);
175167 const { items } = (await res.json()) as {
test/edge-auth.test.ts +13 −1
@@ -3,6 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
55 import { createApp, type App } from "../src/app.ts";
6 import { createUser, getUserByName } from "../src/core/users.ts";
67 import { edgeIdentity, edgeLoginUrl, edgeLogoutUrl } from "../src/http/edge.ts";
78 import { serve, type Servers } from "../src/http/server.ts";
89 import { seedAdmin } from "./helpers/admin.ts";
@@ -83,7 +84,18 @@ describe("a forge behind the platform edge", () => {
8384 expect(page).not.toContain('href="/signin"');
8485 });
8586
86 test("the first person the edge vouches for is made on sight, not as admin", async () => {
87 test("an unknown edge login makes no account and is signed out, offered /signup", async () => {
88 const stranger = { "x-plat-user": "nobody", "x-plat-edge-token": TOKEN };
89 expect((await fetch(`${base}/api/v1/user`, { headers: stranger })).status).toBe(401);
90 expect(getUserByName(app.db, "nobody")).toBeUndefined();
91 const page = await fetch(`${base}/`, { headers: stranger }).then((r) => r.text());
92 expect(page).not.toContain("Sign out");
93 expect(page).toContain('href="/signup"');
94 });
95
96 test("an account the edge already made signs in, and is not an admin", async () => {
97 const made = await createUser(app.db, { username: "ada", email: "[email protected]", origin: "edge" });
98 if (!made.ok) throw new Error(made.error);
8799 const me = await fetch(`${base}/api/v1/user`, { headers: asAda });
88100 expect(me.status).toBe(200);
89101 const body = (await me.json()) as { username: string; isAdmin: boolean };
test/no-signup-admin.test.ts +2 −2
@@ -88,9 +88,9 @@ test("the first account from the terminal's sign-up is not an admin", async () =
8888 expect(admins(app)).toBe(0);
8989 });
9090
91 test("the first account the platform edge makes is not an admin", async () => {
91 test("the platform edge makes no account, so it makes no admin", async () => {
9292 const app = await fresh();
93 expect((await ensureEdgeUser(app.db, "edgy", "f.test"))?.isAdmin).toBe(false);
93 expect(await ensureEdgeUser(app.db, "edgy")).toBeUndefined();
9494 expect(admins(app)).toBe(0);
9595 });
9696
test/reserved-names.test.ts +3 −3
@@ -154,11 +154,11 @@ describe("reserved names", () => {
154154 });
155155
156156 test("the edge cannot bring in a reserved name", async () => {
157 expect(await ensureEdgeUser(app.db, "sc0ut", "f.dev")).toBeUndefined();
158 expect(await ensureEdgeUser(app.db, "admin", "f.dev")).toBeUndefined();
157 expect(await ensureEdgeUser(app.db, "sc0ut")).toBeUndefined();
158 expect(await ensureEdgeUser(app.db, "admin")).toBeUndefined();
159159 expect(getUserByName(app.db, "sc0ut")).toBeUndefined();
160160 // An account that already exists still signs in by its own name.
161 expect((await ensureEdgeUser(app.db, "trevato", "f.dev"))?.username).toBe(
161 expect((await ensureEdgeUser(app.db, "trevato"))?.username).toBe(
162162 "trevato",
163163 );
164164 });
test/signup-closed.test.ts +3 −1
@@ -75,6 +75,7 @@ describe("closed signup", () => {
7575
7676 test("existing accounts still sign in through the edge when closed", async () => {
7777 mode("open");
78 expect((await post("founder")).status).toBe(201);
7879 expect((await me("founder")).status).toBe(200);
7980 mode("closed");
8081 expect((await me("founder")).status).toBe(200);
@@ -84,7 +85,8 @@ describe("closed signup", () => {
8485 mode("open");
8586 expect(await (await fetch(`${base}/signup`)).text()).toContain('name="password"');
8687 expect((await post("newcomer")).status).toBe(201);
87 expect((await me("edgy")).status).toBe(200);
88 expect((await me("edgy")).status).toBe(401);
89 expect(getUserByName(app.db, "edgy")).toBeUndefined();
8890 mode("closed");
8991 expect((await post("late")).status).toBe(403);
9092 expect((await me("later")).status).toBe(401);
test/signup.test.ts +3 −1
@@ -265,6 +265,8 @@ describe("signup closed (#2166)", () => {
265265
266266 test("existing accounts still sign in by the edge while closed", async () => {
267267 setSignup(app.db, "open", "boot");
268 const made = await createUser(app.db, { username: "returning", email: "[email protected]", origin: "edge" });
269 expect(made.ok).toBe(true);
268270 expect((await edge("returning")).status).toBe(200);
269271 setSignup(app.db, "closed", "boot");
270272 expect((await edge("returning")).status).toBe(200);
@@ -282,6 +284,6 @@ describe("signup closed (#2166)", () => {
282284 expect((await post(newcomer())).status).toBe(403);
283285 setSignup(app.db, "open", "boot");
284286 expect((await post(newcomer())).status).toBe(201);
285 expect((await edge("latecomer")).status).toBe(200);
287 expect((await edge("latecomer")).status).toBe(401);
286288 });
287289 });
test/verify-email.test.ts −17
@@ -212,23 +212,6 @@ describe("verified email", () => {
212212 ).toBe(true);
213213 });
214214
215 test("an account the edge makes must prove an address too", async () => {
216 const edged = createApp({
217 dataDir: await mkdtemp(join(tmpdir(), "forge-verify-edge-")),
218 port: 0,
219 url: "https://forge.example",
220 edgeToken: "edge-secret",
221 mailer: async () => true,
222 });
223 const { ensureEdgeUser } = await import("../src/core/users.ts");
224 const first = await ensureEdgeUser(edged.db, "founder", "forge.example");
225 const second = await ensureEdgeUser(edged.db, "newcomer", "forge.example");
226 // The first is no admin, so it is not proven by being first.
227 expect(emailVerified(edged.db, first!.id)).toBe(false);
228 expect(emailVerified(edged.db, second!.id)).toBe(false);
229 edged.close();
230 });
231
232215 test("a way in that does not vouch for the address is held back (fail closed)", async () => {
233216 // Any future path that makes a person without saying "trusted" — a
234217 // linked sign-in, an invite — starts unproven.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs