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
scout/forge:scout/2593-an-edge-sign-in-for-someone-with-no-forg → main 2 commits fork
12 files
changed +57
−83
src/core/users.ts +6 −20
| @@ -6,7 +6,6 @@ | ||
| 6 | 6 | import { markUnproven, markVerified } from "./verify.ts"; |
| 7 | 7 | import type { Database } from "bun:sqlite"; |
| 8 | 8 | import { isSafeName } from "../git/guard.ts"; |
| 9 | import { noreplyAddress } from "./commit_identity.ts"; | |
| 10 | 9 | import { reservedName } from "./reserved.ts"; |
| 11 | 10 | import { id } from "../kernel/id.ts"; |
| 12 | 11 | import { err, ok, type Result } from "../kernel/result.ts"; |
| @@ -452,31 +451,18 @@ export function getUserByName( | ||
| 452 | 451 | |
| 453 | 452 | /** |
| 454 | 453 | * 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. | |
| 459 | 457 | */ |
| 460 | 458 | export async function ensureEdgeUser( |
| 461 | 459 | db: Database, |
| 462 | 460 | login: string, |
| 463 | host: string, | |
| 464 | 461 | ): Promise<User | undefined> { |
| 465 | 462 | 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; | |
| 480 | 466 | } |
| 481 | 467 | |
| 482 | 468 | /** 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 { | ||
| 17 | 17 | readonly scope: TokenScope | undefined; |
| 18 | 18 | /** `edge`: the platform in front of the forge signed this person in. */ |
| 19 | 19 | 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; | |
| 20 | 23 | /** Which signed-in browser asked, when `via` is `session`. */ |
| 21 | 24 | readonly sessionId?: string; |
| 22 | 25 | /** 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"; | ||
| 62 | 62 | import { createRepo, repositoryLimitMessage, resolveOwner } from "../actions/repos.ts"; |
| 63 | 63 | import { sessionOf } from "../core/sessions.ts"; |
| 64 | 64 | import { scopeAllows, verifyToken } from "../core/tokens.ts"; |
| 65 | import { forgeHost } from "../core/commit_identity.ts"; | |
| 66 | 65 | import { |
| 67 | 66 | ensureEdgeUser, |
| 68 | 67 | getUserByName, |
| @@ -70,7 +69,6 @@ import { | ||
| 70 | 69 | verifyPasswordThrottled, |
| 71 | 70 | } from "../core/users.ts"; |
| 72 | 71 | import { edgeIdentity } from "./edge.ts"; |
| 73 | import { signupClosed } from "../actions/users.ts"; | |
| 74 | 72 | import { liveResponse, readyResponse } from "./health.ts"; |
| 75 | 73 | import { running } from "../kernel/version.ts"; |
| 76 | 74 | import { runGit } from "../git/env.ts"; |
| @@ -502,27 +500,21 @@ import { getVerifiedDomain } from "../core/domains.ts"; | ||
| 502 | 500 | const NO_AUTH: Auth = { user: undefined, scope: undefined, via: "none" }; |
| 503 | 501 | |
| 504 | 502 | 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> { | |
| 505 | 510 | // The platform edge, when there is one, has already signed this person |
| 506 | 511 | // in; its word outranks a cookie of ours. |
| 507 | 512 | const login = edgeIdentity(request, app.config.edgeToken); |
| 508 | 513 | 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" }; | |
| 526 | 518 | } |
| 527 | 519 | |
| 528 | 520 | const session = cookieValue(request, "forge_session"); |
src/web/layout.ts +8 −6
| @@ -476,12 +476,14 @@ function topbar( | ||
| 476 | 476 | } |
| 477 | 477 | : { label: "Sign out", post: "/logout", icon: "signOut" }, |
| 478 | 478 | )}` |
| 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 | })}`} | |
| 485 | 487 | </span> |
| 486 | 488 | </header>`; |
| 487 | 489 | } |
src/web/pages/site.ts +1 −1
| @@ -263,7 +263,7 @@ function authForm( | ||
| 263 | 263 | <p class="muted small"> |
| 264 | 264 | ${kind === "signin" |
| 265 | 265 | ? `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.`} | |
| 267 | 267 | </p> |
| 268 | 268 | </div>`, |
| 269 | 269 | ); |
test/accounts.test.ts +4 −12
| @@ -121,18 +121,6 @@ describe("accounts: the owner sees, suspends and removes", () => { | ||
| 121 | 121 | expect(typeof made?.payload["address"]).toBe("string"); |
| 122 | 122 | }); |
| 123 | 123 | |
| 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 | ||
| 136 | 124 | test("the owner hears of a new account once", async () => { |
| 137 | 125 | await notifier.settled(); |
| 138 | 126 | const before = posts.length; |
| @@ -170,6 +158,10 @@ describe("accounts: the owner sees, suspends and removes", () => { | ||
| 170 | 158 | }); |
| 171 | 159 | |
| 172 | 160 | 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" } }); | |
| 173 | 165 | const res = await api("/api/v1/admin/accounts", { token: ownerToken }); |
| 174 | 166 | expect(res.status).toBe(200); |
| 175 | 167 | const { items } = (await res.json()) as { |
test/edge-auth.test.ts +13 −1
| @@ -3,6 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | 5 | import { createApp, type App } from "../src/app.ts"; |
| 6 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 6 | 7 | import { edgeIdentity, edgeLoginUrl, edgeLogoutUrl } from "../src/http/edge.ts"; |
| 7 | 8 | import { serve, type Servers } from "../src/http/server.ts"; |
| 8 | 9 | import { seedAdmin } from "./helpers/admin.ts"; |
| @@ -83,7 +84,18 @@ describe("a forge behind the platform edge", () => { | ||
| 83 | 84 | expect(page).not.toContain('href="/signin"'); |
| 84 | 85 | }); |
| 85 | 86 | |
| 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); | |
| 87 | 99 | const me = await fetch(`${base}/api/v1/user`, { headers: asAda }); |
| 88 | 100 | expect(me.status).toBe(200); |
| 89 | 101 | 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 () = | ||
| 88 | 88 | expect(admins(app)).toBe(0); |
| 89 | 89 | }); |
| 90 | 90 | |
| 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 () => { | |
| 92 | 92 | const app = await fresh(); |
| 93 | expect((await ensureEdgeUser(app.db, "edgy", "f.test"))?.isAdmin).toBe(false); | |
| 93 | expect(await ensureEdgeUser(app.db, "edgy")).toBeUndefined(); | |
| 94 | 94 | expect(admins(app)).toBe(0); |
| 95 | 95 | }); |
| 96 | 96 |
test/reserved-names.test.ts +3 −3
| @@ -154,11 +154,11 @@ describe("reserved names", () => { | ||
| 154 | 154 | }); |
| 155 | 155 | |
| 156 | 156 | 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(); | |
| 159 | 159 | expect(getUserByName(app.db, "sc0ut")).toBeUndefined(); |
| 160 | 160 | // 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( | |
| 162 | 162 | "trevato", |
| 163 | 163 | ); |
| 164 | 164 | }); |
test/signup-closed.test.ts +3 −1
| @@ -75,6 +75,7 @@ describe("closed signup", () => { | ||
| 75 | 75 | |
| 76 | 76 | test("existing accounts still sign in through the edge when closed", async () => { |
| 77 | 77 | mode("open"); |
| 78 | expect((await post("founder")).status).toBe(201); | |
| 78 | 79 | expect((await me("founder")).status).toBe(200); |
| 79 | 80 | mode("closed"); |
| 80 | 81 | expect((await me("founder")).status).toBe(200); |
| @@ -84,7 +85,8 @@ describe("closed signup", () => { | ||
| 84 | 85 | mode("open"); |
| 85 | 86 | expect(await (await fetch(`${base}/signup`)).text()).toContain('name="password"'); |
| 86 | 87 | 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(); | |
| 88 | 90 | mode("closed"); |
| 89 | 91 | expect((await post("late")).status).toBe(403); |
| 90 | 92 | expect((await me("later")).status).toBe(401); |
test/signup.test.ts +3 −1
| @@ -265,6 +265,8 @@ describe("signup closed (#2166)", () => { | ||
| 265 | 265 | |
| 266 | 266 | test("existing accounts still sign in by the edge while closed", async () => { |
| 267 | 267 | 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); | |
| 268 | 270 | expect((await edge("returning")).status).toBe(200); |
| 269 | 271 | setSignup(app.db, "closed", "boot"); |
| 270 | 272 | expect((await edge("returning")).status).toBe(200); |
| @@ -282,6 +284,6 @@ describe("signup closed (#2166)", () => { | ||
| 282 | 284 | expect((await post(newcomer())).status).toBe(403); |
| 283 | 285 | setSignup(app.db, "open", "boot"); |
| 284 | 286 | expect((await post(newcomer())).status).toBe(201); |
| 285 | expect((await edge("latecomer")).status).toBe(200); | |
| 287 | expect((await edge("latecomer")).status).toBe(401); | |
| 286 | 288 | }); |
| 287 | 289 | }); |
test/verify-email.test.ts −17
| @@ -212,23 +212,6 @@ describe("verified email", () => { | ||
| 212 | 212 | ).toBe(true); |
| 213 | 213 | }); |
| 214 | 214 | |
| 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 | ||
| 232 | 215 | test("a way in that does not vouch for the address is held back (fail closed)", async () => { |
| 233 | 216 | // Any future path that makes a person without saying "trusted" — a |
| 234 | 217 | // linked sign-in, an invite — starts unproven. |
new above
offline