Public: anyone can read this; the crew can change it.
open-platform.sh sign-in is a linked method, not a back door: it never creates an account outside the signup rules #744
mason/forge:mason/668-open-platform-sh-sign-in-is-a-linked-met → main 1 commit fork
16 files
changed +529
−79
src/actions/links.ts added +65
| @@ -0,0 +1,65 @@ | ||
| 1 | /** | |
| 2 | * Linking and unlinking a sign-in method (`core/links.ts`). The identity | |
| 3 | * linked is always one something vouched for on this very request — the | |
| 4 | * edge's `X-Plat-User` behind its secret — never a name a form carries, so | |
| 5 | * nobody links an identity they have not just proven they hold. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { App } from "../app.ts"; | |
| 9 | import { | |
| 10 | addLink, | |
| 11 | hasPassword, | |
| 12 | removeLink, | |
| 13 | type Link, | |
| 14 | type LinkError, | |
| 15 | type LinkMethod, | |
| 16 | } from "../core/links.ts"; | |
| 17 | import type { User } from "../core/users.ts"; | |
| 18 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 19 | import { requireUser } from "./access.ts"; | |
| 20 | ||
| 21 | /** Tie a proven identity to the actor's account. */ | |
| 22 | export function linkSignIn( | |
| 23 | app: App, | |
| 24 | actor: User | undefined, | |
| 25 | method: LinkMethod, | |
| 26 | provenSubject: string, | |
| 27 | ): Result<Link, "unauthorized" | LinkError> { | |
| 28 | const user = requireUser(actor); | |
| 29 | if (!user.ok) return user; | |
| 30 | // Only a person signs in; an agent or an organization holds no link. | |
| 31 | if (user.value.kind !== "user") return err("unauthorized"); | |
| 32 | const linked = addLink(app.db, user.value.id, method, provenSubject); | |
| 33 | if (!linked.ok) return linked; | |
| 34 | app.bus.append({ | |
| 35 | kind: "user.linked", | |
| 36 | actorId: user.value.id, | |
| 37 | payload: { method }, | |
| 38 | }); | |
| 39 | return linked; | |
| 40 | } | |
| 41 | ||
| 42 | export type UnlinkError = "unauthorized" | "not_linked" | "only_way_in"; | |
| 43 | ||
| 44 | /** | |
| 45 | * Remove the actor's link for a method. An account with no password of its | |
| 46 | * own — one the edge made before links existed — would be left with no way | |
| 47 | * in at all, so its link stays until it has another. | |
| 48 | */ | |
| 49 | export function unlinkSignIn( | |
| 50 | app: App, | |
| 51 | actor: User | undefined, | |
| 52 | method: LinkMethod, | |
| 53 | ): Result<void, UnlinkError> { | |
| 54 | const user = requireUser(actor); | |
| 55 | if (!user.ok) return user; | |
| 56 | if (user.value.kind !== "user") return err("unauthorized"); | |
| 57 | if (!hasPassword(app.db, user.value.id)) return err("only_way_in"); | |
| 58 | if (!removeLink(app.db, user.value.id, method)) return err("not_linked"); | |
| 59 | app.bus.append({ | |
| 60 | kind: "user.unlinked", | |
| 61 | actorId: user.value.id, | |
| 62 | payload: { method }, | |
| 63 | }); | |
| 64 | return ok(); | |
| 65 | } |
src/core/event_audience.ts +8 −1
| @@ -35,7 +35,14 @@ export function eventAudience(kind: string): EventAudience { | ||
| 35 | 35 | ) |
| 36 | 36 | return "admins"; |
| 37 | 37 | // A fleet operation spans repositories; its receipt is its actor's. |
| 38 | if (kind === "repo.pinned" || kind === "repo.unpinned" || kind.startsWith("fleet.")) | |
| 38 | // So is linking a sign-in method: how one person gets in is theirs. | |
| 39 | if ( | |
| 40 | kind === "repo.pinned" || | |
| 41 | kind === "repo.unpinned" || | |
| 42 | kind.startsWith("fleet.") || | |
| 43 | kind === "user.linked" || | |
| 44 | kind === "user.unlinked" | |
| 45 | ) | |
| 39 | 46 | return "actor"; |
| 40 | 47 | return "readers"; |
| 41 | 48 | } |
src/core/links.ts added +166
| @@ -0,0 +1,166 @@ | ||
| 1 | /** | |
| 2 | * Linked sign-in methods: an identity somewhere else — today only the | |
| 3 | * platform edge — that a person has tied to the account they already have | |
| 4 | * here. A link is the only way such an identity reaches an account: it is | |
| 5 | * never matched by name and never makes an account (#668). There is one | |
| 6 | * way in, the forge's own signup; a link is a second key to a door that | |
| 7 | * already exists. | |
| 8 | * | |
| 9 | * `subject` is the identity as the method names it, folded to lower case | |
| 10 | * so the edge's `ADA` and `ada` are one person. One account holds at most | |
| 11 | * one link per method, and one identity is linked to at most one account. | |
| 12 | */ | |
| 13 | ||
| 14 | import type { Database } from "bun:sqlite"; | |
| 15 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 16 | import { getUserById, getUsersByIds, type User } from "./users.ts"; | |
| 17 | ||
| 18 | export type LinkMethod = "edge"; | |
| 19 | ||
| 20 | export interface Link { | |
| 21 | readonly method: LinkMethod; | |
| 22 | readonly subject: string; | |
| 23 | readonly userId: string; | |
| 24 | readonly createdAt: string; | |
| 25 | } | |
| 26 | ||
| 27 | interface LinkRow { | |
| 28 | method: LinkMethod; | |
| 29 | subject: string; | |
| 30 | user_id: string; | |
| 31 | created_at: string; | |
| 32 | } | |
| 33 | ||
| 34 | const toLink = (row: LinkRow): Link => ({ | |
| 35 | method: row.method, | |
| 36 | subject: row.subject, | |
| 37 | userId: row.user_id, | |
| 38 | createdAt: row.created_at, | |
| 39 | }); | |
| 40 | ||
| 41 | const MAX_SUBJECT = 200; | |
| 42 | ||
| 43 | /** An identity as it is stored, or undefined when it cannot be one. */ | |
| 44 | export function cleanSubject(raw: string): string | undefined { | |
| 45 | const subject = raw.trim().toLowerCase(); | |
| 46 | if (subject === "" || subject.length > MAX_SUBJECT) return undefined; | |
| 47 | // eslint-disable-next-line no-control-regex | |
| 48 | if (/[\u0000-\u001f\u007f]/.test(subject)) return undefined; | |
| 49 | return subject; | |
| 50 | } | |
| 51 | ||
| 52 | /** | |
| 53 | * The person an identity is linked to, if any. An organization or a crew | |
| 54 | * member is nobody's login, whatever a row says. | |
| 55 | */ | |
| 56 | export function linkedUser( | |
| 57 | db: Database, | |
| 58 | method: LinkMethod, | |
| 59 | raw: string, | |
| 60 | ): User | undefined { | |
| 61 | const subject = cleanSubject(raw); | |
| 62 | if (subject === undefined) return undefined; | |
| 63 | const row = db | |
| 64 | .query< | |
| 65 | { user_id: string }, | |
| 66 | [string, string] | |
| 67 | >("SELECT user_id FROM user_link WHERE method = ?1 AND subject = ?2") | |
| 68 | .get(method, subject); | |
| 69 | const user = row ? getUserById(db, row.user_id) : undefined; | |
| 70 | return user?.kind === "user" ? user : undefined; | |
| 71 | } | |
| 72 | ||
| 73 | /** The account's link for a method, if it has one. */ | |
| 74 | export function linkOf( | |
| 75 | db: Database, | |
| 76 | userId: string, | |
| 77 | method: LinkMethod, | |
| 78 | ): Link | undefined { | |
| 79 | const row = db | |
| 80 | .query< | |
| 81 | LinkRow, | |
| 82 | [string, string] | |
| 83 | >("SELECT * FROM user_link WHERE user_id = ?1 AND method = ?2") | |
| 84 | .get(userId, method); | |
| 85 | return row ? toLink(row) : undefined; | |
| 86 | } | |
| 87 | ||
| 88 | export type LinkError = "invalid_subject" | "already_linked" | "subject_taken"; | |
| 89 | ||
| 90 | export function addLink( | |
| 91 | db: Database, | |
| 92 | userId: string, | |
| 93 | method: LinkMethod, | |
| 94 | raw: string, | |
| 95 | ): Result<Link, LinkError> { | |
| 96 | const subject = cleanSubject(raw); | |
| 97 | if (subject === undefined) return err("invalid_subject"); | |
| 98 | // Both uniqueness rules are constraints, so a race is settled by the | |
| 99 | // INSERT, not by these reads; they only name which rule refused. | |
| 100 | if (linkOf(db, userId, method)) return err("already_linked"); | |
| 101 | const changed = db | |
| 102 | .query( | |
| 103 | `INSERT OR IGNORE INTO user_link (method, subject, user_id) VALUES (?1, ?2, ?3)`, | |
| 104 | ) | |
| 105 | .run(method, subject, userId).changes; | |
| 106 | if (changed === 0) | |
| 107 | return err(linkOf(db, userId, method) ? "already_linked" : "subject_taken"); | |
| 108 | return ok(linkOf(db, userId, method)!); | |
| 109 | } | |
| 110 | ||
| 111 | /** Remove the account's link for a method; false when there was none. */ | |
| 112 | export function removeLink( | |
| 113 | db: Database, | |
| 114 | userId: string, | |
| 115 | method: LinkMethod, | |
| 116 | ): boolean { | |
| 117 | return ( | |
| 118 | db | |
| 119 | .query("DELETE FROM user_link WHERE user_id = ?1 AND method = ?2") | |
| 120 | .run(userId, method).changes > 0 | |
| 121 | ); | |
| 122 | } | |
| 123 | ||
| 124 | /** Can this account sign in with a password of its own? */ | |
| 125 | export function hasPassword(db: Database, userId: string): boolean { | |
| 126 | return ( | |
| 127 | db | |
| 128 | .query("SELECT 1 FROM user WHERE id = ?1 AND password_hash != ''") | |
| 129 | .get(userId) !== null | |
| 130 | ); | |
| 131 | } | |
| 132 | ||
| 133 | export interface LinkedAccount { | |
| 134 | readonly user: User; | |
| 135 | readonly link: Link; | |
| 136 | /** False for an account the edge made before #668: the link is its way in. */ | |
| 137 | readonly hasPassword: boolean; | |
| 138 | } | |
| 139 | ||
| 140 | /** Every linked account, newest link first, bounded — the owner's list. */ | |
| 141 | export function listLinkedAccounts( | |
| 142 | db: Database, | |
| 143 | limit = 200, | |
| 144 | ): LinkedAccount[] { | |
| 145 | const rows = db | |
| 146 | .query< | |
| 147 | LinkRow & { has_password: number }, | |
| 148 | [number] | |
| 149 | >( | |
| 150 | `SELECT l.*, (u.password_hash != '') AS has_password | |
| 151 | FROM user_link l JOIN user u ON u.id = l.user_id | |
| 152 | ORDER BY l.created_at DESC, l.subject LIMIT ?1`, | |
| 153 | ) | |
| 154 | .all(Math.min(Math.max(limit, 1), 500)); | |
| 155 | const users = getUsersByIds( | |
| 156 | db, | |
| 157 | rows.map((r) => r.user_id), | |
| 158 | ); | |
| 159 | const out: LinkedAccount[] = []; | |
| 160 | for (const row of rows) { | |
| 161 | const user = users.get(row.user_id); | |
| 162 | if (user) | |
| 163 | out.push({ user, link: toLink(row), hasPassword: row.has_password === 1 }); | |
| 164 | } | |
| 165 | return out; | |
| 166 | } |
src/core/schema.ts +17
| @@ -1215,4 +1215,21 @@ export const migrations: readonly string[] = [ | ||
| 1215 | 1215 | ); |
| 1216 | 1216 | CREATE INDEX IF NOT EXISTS report_waiting ON report(repo_id, state, id); |
| 1217 | 1217 | `, |
| 1218 | // 48 — linked sign-in methods (`core/links.ts`, #668). The edge no longer | |
| 1219 | // matches a login to an account by name, so every account it made before | |
| 1220 | // this — the ones without a password, which nothing but the edge creates — | |
| 1221 | // keeps its way in as a link written down here. | |
| 1222 | ` | |
| 1223 | CREATE TABLE IF NOT EXISTS user_link ( | |
| 1224 | method TEXT NOT NULL CHECK (method IN ('edge')), | |
| 1225 | subject TEXT NOT NULL, | |
| 1226 | user_id TEXT NOT NULL REFERENCES user(id) ON DELETE CASCADE, | |
| 1227 | created_at TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), | |
| 1228 | PRIMARY KEY (method, subject) | |
| 1229 | ); | |
| 1230 | CREATE UNIQUE INDEX IF NOT EXISTS user_link_one ON user_link(user_id, method); | |
| 1231 | INSERT OR IGNORE INTO user_link (method, subject, user_id, created_at) | |
| 1232 | SELECT 'edge', lower(username), id, created_at FROM user | |
| 1233 | WHERE kind = 'user' AND is_agent = 0 AND password_hash = ''; | |
| 1234 | `, | |
| 1218 | 1235 | ]; |
src/core/users.ts +3 −29
| @@ -5,8 +5,6 @@ | ||
| 5 | 5 | |
| 6 | 6 | import type { Database } from "bun:sqlite"; |
| 7 | 7 | import { isSafeName } from "../git/guard.ts"; |
| 8 | import { noreplyAddress } from "./commit_identity.ts"; | |
| 9 | import { reservedName } from "./reserved.ts"; | |
| 10 | 8 | import { id } from "../kernel/id.ts"; |
| 11 | 9 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 12 | 10 | |
| @@ -75,8 +73,9 @@ export interface CreateUserInput { | ||
| 75 | 73 | username: string; |
| 76 | 74 | email: string; |
| 77 | 75 | /** |
| 78 | * Absent, no password is stored and none ever verifies: the account is | |
| 79 | * signed in by something that vouches for it — the platform edge. | |
| 76 | * Absent, no password is stored and none ever verifies. Signup always | |
| 77 | * gives one; the accounts without are those the platform edge made before | |
| 78 | * it became a linked method (#668), signed in by their link. | |
| 80 | 79 | */ |
| 81 | 80 | password?: string; |
| 82 | 81 | /** @default "" */ |
| @@ -249,31 +248,6 @@ export function getUserByName( | ||
| 249 | 248 | return row ? toUser(row) : undefined; |
| 250 | 249 | } |
| 251 | 250 | |
| 252 | /** | |
| 253 | * The person behind an identity the platform edge vouched for: found by | |
| 254 | * name, or made on first sight — with no password, because the edge is how | |
| 255 | * they sign in, and a noreply address until they set one. The first account | |
| 256 | * on a fresh forge is the admin, exactly as at sign-up. A name that is an | |
| 257 | * organization or a crew member here is nobody's login. | |
| 258 | */ | |
| 259 | export async function ensureEdgeUser( | |
| 260 | db: Database, | |
| 261 | login: string, | |
| 262 | host: string, | |
| 263 | ): Promise<User | undefined> { | |
| 264 | const existing = getUserByName(db, login); | |
| 265 | if (existing) return existing.kind === "user" ? existing : undefined; | |
| 266 | // The edge vouches for a login, not for its right to a name: a platform | |
| 267 | // account called after the crew or the owner is not let in under it. | |
| 268 | if (reservedName(db, login) !== undefined) return undefined; | |
| 269 | const made = await createUser(db, { | |
| 270 | username: login, | |
| 271 | email: noreplyAddress(login, host), | |
| 272 | adminIfFirst: true, | |
| 273 | }); | |
| 274 | return made.ok ? made.value : undefined; | |
| 275 | } | |
| 276 | ||
| 277 | 251 | /** Constant-shape verification: always burns an argon2 verify, hit or miss. */ |
| 278 | 252 | export async function verifyPassword( |
| 279 | 253 | db: Database, |
src/http/context.ts +7
| @@ -17,6 +17,13 @@ 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 | /** | |
| 21 | * The identity the platform edge vouched for on this request, linked to | |
| 22 | * an account or not. Present with `via` other than `edge`, it is a person | |
| 23 | * the edge knows whom no account here has linked (`core/links.ts`): they | |
| 24 | * sign up or sign in the forge's own way, and may then link it. | |
| 25 | */ | |
| 26 | readonly edge?: string; | |
| 20 | 27 | } |
| 21 | 28 | |
| 22 | 29 | export interface Ctx { |
src/http/edge.ts +23
| @@ -6,9 +6,15 @@ | ||
| 6 | 6 | * signed in by the platform. Without the secret the headers are a claim |
| 7 | 7 | * anyone could write, and they are ignored. Signing in and out are the |
| 8 | 8 | * edge's own entry points on this host. |
| 9 | * | |
| 10 | * The edge is a linked method, not a way to make an account (#668): the | |
| 11 | * identity it vouches for reaches only the account that linked it | |
| 12 | * (`core/links.ts`), and a person it knows whom nobody linked signs up the | |
| 13 | * forge's own way. | |
| 9 | 14 | */ |
| 10 | 15 | |
| 11 | 16 | import { createHash, timingSafeEqual } from "node:crypto"; |
| 17 | import type { Ctx } from "./context.ts"; | |
| 12 | 18 | |
| 13 | 19 | /** The account the edge vouches for on this request, if it does. */ |
| 14 | 20 | export function edgeIdentity( |
| @@ -30,6 +36,23 @@ function sameSecret(a: string, b: string): boolean { | ||
| 30 | 36 | return timingSafeEqual(ha, hb); |
| 31 | 37 | } |
| 32 | 38 | |
| 39 | /** | |
| 40 | * Is the edge where a stranger starts signing in? Only when there is one and | |
| 41 | * it has not already vouched for this person: someone it knows whom no | |
| 42 | * account has linked would come straight back unlinked, so for them the | |
| 43 | * door is the forge's own. | |
| 44 | */ | |
| 45 | export function edgeIsTheDoor(ctx: Pick<Ctx, "app" | "auth">): boolean { | |
| 46 | return ctx.app.config.edgeToken !== undefined && ctx.auth.edge === undefined; | |
| 47 | } | |
| 48 | ||
| 49 | /** Where signing in starts, coming back to `next` on this host after. */ | |
| 50 | export function signInUrl(ctx: Pick<Ctx, "app" | "auth">, next: string): string { | |
| 51 | return edgeIsTheDoor(ctx) | |
| 52 | ? edgeLoginUrl(next) | |
| 53 | : `/signin?next=${encodeURIComponent(next)}`; | |
| 54 | } | |
| 55 | ||
| 33 | 56 | /** Where the edge starts a sign-in, coming back to `path` on this host. */ |
| 34 | 57 | export function edgeLoginUrl(path: string): string { |
| 35 | 58 | return `/_plat/login?rd=${encodeURIComponent(samePath(path))}`; |
src/http/server.ts +20 −7
| @@ -10,8 +10,8 @@ import { trackPullHead } from "../core/pulls.ts"; | ||
| 10 | 10 | import { createRepo } from "../actions/repos.ts"; |
| 11 | 11 | import { sessionUser } from "../core/sessions.ts"; |
| 12 | 12 | import { scopeAllows, verifyToken } from "../core/tokens.ts"; |
| 13 | import { forgeHost } from "../core/commit_identity.ts"; | |
| 14 | import { ensureEdgeUser, verifyPassword } from "../core/users.ts"; | |
| 13 | import { linkedUser } from "../core/links.ts"; | |
| 14 | import { verifyPassword } from "../core/users.ts"; | |
| 15 | 15 | import { edgeIdentity } from "./edge.ts"; |
| 16 | 16 | import { healthResponse } from "./health.ts"; |
| 17 | 17 | import { runGit } from "../git/env.ts"; |
| @@ -280,6 +280,11 @@ import { | ||
| 280 | 280 | } from "../web/pages/origins.ts"; |
| 281 | 281 | import { pushThrough } from "../origins/push-through.ts"; |
| 282 | 282 | import { notFoundPage } from "../web/chrome.ts"; |
| 283 | import { | |
| 284 | edgeLinkSubmit, | |
| 285 | edgeUnlinkSubmit, | |
| 286 | linksPage, | |
| 287 | } from "../web/pages/links.ts"; | |
| 283 | 288 | import { targetForShortCode } from "../origins/oauth.ts"; |
| 284 | 289 | import { apiRoutes } from "./api.ts"; |
| 285 | 290 | import { feedRoutes } from "./feed.ts"; |
| @@ -331,13 +336,18 @@ const NO_AUTH: Auth = { user: undefined, scope: undefined, via: "none" }; | ||
| 331 | 336 | |
| 332 | 337 | async function resolveAuth(app: App, request: Request): Promise<Auth> { |
| 333 | 338 | // The platform edge, when there is one, has already signed this person |
| 334 | // in; its word outranks a cookie of ours. | |
| 339 | // in; its word outranks a cookie of ours — but only for an identity an | |
| 340 | // account here has linked. An unknown one makes nothing (#668): it rides | |
| 341 | // along so the pages can send that person to the forge's own signup. | |
| 335 | 342 | const login = edgeIdentity(request, app.config.edgeToken); |
| 336 | if (login !== undefined) { | |
| 337 | const user = await ensureEdgeUser(app.db, login, forgeHost(app.config)); | |
| 338 | if (user) return { user, scope: "admin", via: "edge" }; | |
| 339 | } | |
| 343 | if (login === undefined) return resolveCredential(app, request); | |
| 344 | const user = linkedUser(app.db, "edge", login); | |
| 345 | if (user) return { user, scope: "admin", via: "edge", edge: login }; | |
| 346 | return { ...(await resolveCredential(app, request)), edge: login }; | |
| 347 | } | |
| 340 | 348 | |
| 349 | /** The forge's own credentials: a session cookie, a token, a password. */ | |
| 350 | async function resolveCredential(app: App, request: Request): Promise<Auth> { | |
| 341 | 351 | const session = cookieValue(request, "forge_session"); |
| 342 | 352 | if (session !== undefined) { |
| 343 | 353 | const user = sessionUser(app.db, session); |
| @@ -503,6 +513,9 @@ export function buildRouter(): Router<Handler> { | ||
| 503 | 513 | router.add("POST", "/settings/notify", notifyUpdate); |
| 504 | 514 | router.add("GET", "/settings/password", passwordPage); |
| 505 | 515 | router.add("POST", "/settings/password", passwordUpdate); |
| 516 | router.add("GET", "/settings/links", linksPage); | |
| 517 | router.add("POST", "/settings/links/edge/link", edgeLinkSubmit); | |
| 518 | router.add("POST", "/settings/links/edge/unlink", edgeUnlinkSubmit); | |
| 506 | 519 | router.add("GET", "/settings/tokens", tokensPage); |
| 507 | 520 | router.add("POST", "/settings/tokens", tokenCreate); |
| 508 | 521 | router.add("POST", "/settings/tokens/:id/delete", tokenDelete); |
src/main.ts +8 −2
| @@ -61,8 +61,14 @@ const sshPort = Number(process.env.FORGE_SSH_PORT ?? (hosted ? 0 : 2222)); | ||
| 61 | 61 | // terminates TLS. Parsed once here so a typo fails the boot, not a request. |
| 62 | 62 | const canonicalUrl = hosted ? hostedAt : (process.env.FORGE_URL ?? "").trim(); |
| 63 | 63 | // FORGE_EDGE_TOKEN is the platform edge's secret: with it, the edge's |
| 64 | // identity headers sign people in; without it they are ignored. | |
| 65 | const edgeToken = (process.env.FORGE_EDGE_TOKEN ?? "").trim(); | |
| 64 | // identity headers sign in the accounts that linked them; without it they | |
| 65 | // are ignored. The edge is a linked method (#668), and FORGE_LINK_EDGE=off | |
| 66 | // turns it off with the secret still set: everyone signs in the forge's own | |
| 67 | // way, and the links stay for when it comes back on. | |
| 68 | const edgeToken = | |
| 69 | process.env.FORGE_LINK_EDGE === "off" | |
| 70 | ? "" | |
| 71 | : (process.env.FORGE_EDGE_TOKEN ?? "").trim(); | |
| 66 | 72 | |
| 67 | 73 | const dataDir = |
| 68 | 74 | process.env.FORGE_DATA ?? |
src/web/chrome.ts +2 −4
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { getAppByRepo } from "../core/apps.ts"; |
| 12 | import { edgeLoginUrl } from "../http/edge.ts"; | |
| 12 | import { signInUrl } from "../http/edge.ts"; | |
| 13 | 13 | import { |
| 14 | 14 | canAdmin, |
| 15 | 15 | canRead, |
| @@ -87,11 +87,9 @@ export function signInFirst(ctx: Ctx): Response { | ||
| 87 | 87 | |
| 88 | 88 | /** Where signing in starts, coming back to this page after. */ |
| 89 | 89 | export function signInHref(ctx: Ctx): string { |
| 90 | const next = ctx.url.pathname + ctx.url.search; | |
| 91 | 90 | // Behind a platform edge the edge is how people sign in; an account it |
| 92 | 91 | // made has no password here, so the password form would only refuse them. |
| 93 | if (ctx.app.config.edgeToken !== undefined) return edgeLoginUrl(next); | |
| 94 | return `/signin?next=${encodeURIComponent(next)}`; | |
| 92 | return signInUrl(ctx, ctx.url.pathname + ctx.url.search); | |
| 95 | 93 | } |
| 96 | 94 | |
| 97 | 95 | // ---- repo header ---- |
src/web/layout.ts +4 −5
| @@ -11,7 +11,7 @@ import { appsOf, askCountFor } from "./studio/asks.ts"; | ||
| 11 | 11 | import { listReposVisibleTo } from "../core/repos.ts"; |
| 12 | 12 | import type { User } from "../core/users.ts"; |
| 13 | 13 | import type { Ctx } from "../http/context.ts"; |
| 14 | import { edgeLoginUrl, edgeLogoutUrl } from "../http/edge.ts"; | |
| 14 | import { edgeIsTheDoor, edgeLoginUrl, edgeLogoutUrl } from "../http/edge.ts"; | |
| 15 | 15 | import { json } from "../http/respond.ts"; |
| 16 | 16 | import { sha256 } from "../kernel/hash.ts"; |
| 17 | 17 | import { secret } from "../kernel/id.ts"; |
| @@ -465,10 +465,9 @@ function topbar( | ||
| 465 | 465 | : { label: "Sign out", post: "/logout", icon: "signOut" }, |
| 466 | 466 | )}` |
| 467 | 467 | : btn("Sign in", { |
| 468 | href: | |
| 469 | ctx.app.config.edgeToken === undefined | |
| 470 | ? "/signin" | |
| 471 | : edgeLoginUrl(ctx.url.pathname + ctx.url.search), | |
| 468 | href: edgeIsTheDoor(ctx) | |
| 469 | ? edgeLoginUrl(ctx.url.pathname + ctx.url.search) | |
| 470 | : "/signin", | |
| 472 | 471 | })} |
| 473 | 472 | </span> |
| 474 | 473 | </header>`; |
src/web/pages/account.ts +1 −1
| @@ -49,7 +49,7 @@ import { | ||
| 49 | 49 | field as uiField, |
| 50 | 50 | } from "../ui.ts"; |
| 51 | 51 | |
| 52 | type Section = "profile" | "password" | "tokens" | "keys"; | |
| 52 | type Section = "profile" | "password" | "links" | "tokens" | "keys"; | |
| 53 | 53 | |
| 54 | 54 | // The section list lives beside the newest page that joins it, so adding one |
| 55 | 55 | // cannot leave the others pointing at a nav that has forgotten it. |
src/web/pages/links.ts added +158
| @@ -0,0 +1,158 @@ | ||
| 1 | /** | |
| 2 | * Settings → Sign-in methods: link and unlink the platform edge identity | |
| 3 | * (`core/links.ts`, #668). The identity linked is the one the edge vouched | |
| 4 | * for on the request that links it — never a name the form carries. A forge | |
| 5 | * admin also sees every linked account, so the ones the edge made before | |
| 6 | * links existed are listed with how they get in. | |
| 7 | */ | |
| 8 | ||
| 9 | import { linkSignIn, unlinkSignIn } from "../../actions/links.ts"; | |
| 10 | import { hasPassword, linkOf, listLinkedAccounts } from "../../core/links.ts"; | |
| 11 | import type { Ctx } from "../../http/context.ts"; | |
| 12 | import { edgeLoginUrl } from "../../http/edge.ts"; | |
| 13 | import { signInFirst } from "../chrome.ts"; | |
| 14 | import { readForm } from "../form.ts"; | |
| 15 | import { html, when, type Html } from "../html.ts"; | |
| 16 | import { flashTo } from "../layout.ts"; | |
| 17 | import { accountShell } from "./account.ts"; | |
| 18 | import { badge, btn, card, time } from "../ui.ts"; | |
| 19 | ||
| 20 | const HERE = "/settings/links"; | |
| 21 | ||
| 22 | function edgeCard(ctx: Ctx): Html { | |
| 23 | const user = ctx.auth.user!; | |
| 24 | const name = ctx.app.config.edgeName ?? "the platform"; | |
| 25 | if (ctx.app.config.edgeToken === undefined) | |
| 26 | return card( | |
| 27 | html`<p class="muted"> | |
| 28 | No linked sign-in methods are turned on for this forge. You sign in | |
| 29 | with your username and password. | |
| 30 | </p>`, | |
| 31 | ); | |
| 32 | const link = linkOf(ctx.app.db, user.id, "edge"); | |
| 33 | const head = html`<h3>${name}</h3>`; | |
| 34 | if (link) { | |
| 35 | const own = hasPassword(ctx.app.db, user.id); | |
| 36 | return card( | |
| 37 | html`<p> | |
| 38 | Linked as <strong>${link.subject}</strong> ${time(link.createdAt)}. | |
| 39 | ${name} signs you in to this account. | |
| 40 | </p> | |
| 41 | ${own | |
| 42 | ? html`<form method="post" action="${HERE}/edge/unlink" class="mt"> | |
| 43 | ${btn("Unlink", { variant: "danger", size: "sm" })} | |
| 44 | </form>` | |
| 45 | : html`<p class="muted small mt"> | |
| 46 | This account has no password, so ${name} is its only way in and | |
| 47 | the link stays. | |
| 48 | </p>`}`, | |
| 49 | { head }, | |
| 50 | ); | |
| 51 | } | |
| 52 | if (ctx.auth.edge !== undefined) | |
| 53 | return card( | |
| 54 | html`<p> | |
| 55 | ${name} has you signed in as <strong>${ctx.auth.edge}</strong>, and | |
| 56 | no account here is linked to it. | |
| 57 | </p> | |
| 58 | <form method="post" action="${HERE}/edge/link" class="mt"> | |
| 59 | ${btn(`Link ${ctx.auth.edge}`, { variant: "primary", size: "sm" })} | |
| 60 | </form>`, | |
| 61 | { head }, | |
| 62 | ); | |
| 63 | return card( | |
| 64 | html`<p> | |
| 65 | Not linked. Sign in through ${name} while signed in here, then link it | |
| 66 | on this page. | |
| 67 | </p> | |
| 68 | <div class="mt"> | |
| 69 | ${btn(`Sign in through ${name}`, { href: edgeLoginUrl(HERE), size: "sm" })} | |
| 70 | </div>`, | |
| 71 | { head }, | |
| 72 | ); | |
| 73 | } | |
| 74 | ||
| 75 | function linkedAccountsCard(ctx: Ctx): Html { | |
| 76 | const accounts = listLinkedAccounts(ctx.app.db); | |
| 77 | return card( | |
| 78 | accounts.length === 0 | |
| 79 | ? html`<p class="muted">No account has a linked sign-in method.</p>` | |
| 80 | : html`<div class="table-scroll"> | |
| 81 | <table class="table"> | |
| 82 | <thead> | |
| 83 | <tr> | |
| 84 | <th>Account</th> | |
| 85 | <th>Method</th> | |
| 86 | <th>Identity</th> | |
| 87 | <th>Password</th> | |
| 88 | <th>Linked</th> | |
| 89 | </tr> | |
| 90 | </thead> | |
| 91 | <tbody> | |
| 92 | ${accounts.map( | |
| 93 | (a) => | |
| 94 | html`<tr> | |
| 95 | <td><a href="/${a.user.username}">${a.user.username}</a></td> | |
| 96 | <td>${badge(ctx.app.config.edgeName ?? "edge")}</td> | |
| 97 | <td class="mono">${a.link.subject}</td> | |
| 98 | <td class="muted"> | |
| 99 | ${a.hasPassword ? "yes" : "none — made by the edge"} | |
| 100 | </td> | |
| 101 | <td class="muted">${time(a.link.createdAt)}</td> | |
| 102 | </tr>`, | |
| 103 | )} | |
| 104 | </tbody> | |
| 105 | </table> | |
| 106 | </div>`, | |
| 107 | { head: html`<h3>Linked accounts</h3>`, pad: accounts.length === 0 }, | |
| 108 | ); | |
| 109 | } | |
| 110 | ||
| 111 | // GET /settings/links | |
| 112 | export function linksPage(ctx: Ctx): Response { | |
| 113 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 114 | return accountShell( | |
| 115 | ctx, | |
| 116 | "links", | |
| 117 | "Sign-in methods", | |
| 118 | html`${edgeCard(ctx)} | |
| 119 | ${when(ctx.auth.user.isAdmin, () => linkedAccountsCard(ctx))}`, | |
| 120 | ); | |
| 121 | } | |
| 122 | ||
| 123 | // POST /settings/links/edge/link | |
| 124 | export async function edgeLinkSubmit(ctx: Ctx): Promise<Response> { | |
| 125 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 126 | const form = await readForm(ctx, "admin"); | |
| 127 | if (form instanceof Response) return form; | |
| 128 | if (ctx.auth.edge === undefined) | |
| 129 | return flashTo(ctx, HERE, { | |
| 130 | tone: "danger", | |
| 131 | message: "Sign in through the platform first, then link it here.", | |
| 132 | }); | |
| 133 | const linked = linkSignIn(ctx.app, ctx.auth.user, "edge", ctx.auth.edge); | |
| 134 | if (linked.ok) return flashTo(ctx, HERE, { message: "Linked." }); | |
| 135 | const message = { | |
| 136 | unauthorized: "Only a person's account can link a sign-in method.", | |
| 137 | invalid_subject: "That identity cannot be linked.", | |
| 138 | already_linked: "This account is already linked. Unlink it first.", | |
| 139 | subject_taken: "That identity is linked to another account.", | |
| 140 | }[linked.error]; | |
| 141 | return flashTo(ctx, HERE, { tone: "danger", message }); | |
| 142 | } | |
| 143 | ||
| 144 | // POST /settings/links/edge/unlink | |
| 145 | export async function edgeUnlinkSubmit(ctx: Ctx): Promise<Response> { | |
| 146 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 147 | const form = await readForm(ctx, "admin"); | |
| 148 | if (form instanceof Response) return form; | |
| 149 | const removed = unlinkSignIn(ctx.app, ctx.auth.user, "edge"); | |
| 150 | if (removed.ok) return flashTo(ctx, HERE, { message: "Unlinked." }); | |
| 151 | const message = { | |
| 152 | unauthorized: "Only a person's account has sign-in methods.", | |
| 153 | not_linked: "There was no link to remove.", | |
| 154 | only_way_in: | |
| 155 | "This account has no password, so the link is its only way in and stays.", | |
| 156 | }[removed.error]; | |
| 157 | return flashTo(ctx, HERE, { tone: "danger", message }); | |
| 158 | } |
src/web/pages/origins.ts +7
| @@ -64,6 +64,7 @@ import { | ||
| 64 | 64 | export type AccountSection = |
| 65 | 65 | | "profile" |
| 66 | 66 | | "password" |
| 67 | | "links" | |
| 67 | 68 | | "tokens" |
| 68 | 69 | | "keys" |
| 69 | 70 | | "origins" |
| @@ -130,6 +131,12 @@ export function accountSections( | ||
| 130 | 131 | icon: "lock", |
| 131 | 132 | current: current === "password", |
| 132 | 133 | }, |
| 134 | { | |
| 135 | label: "Sign-in methods", | |
| 136 | href: "/settings/links", | |
| 137 | icon: "link", | |
| 138 | current: current === "links", | |
| 139 | }, | |
| 133 | 140 | { |
| 134 | 141 | label: "Access tokens", |
| 135 | 142 | href: "/settings/tokens", |
src/web/pages/site.ts +38 −28
| @@ -38,7 +38,8 @@ import { isThemeName } from "../../design/themes.ts"; | ||
| 38 | 38 | import { crossOrigin, type Ctx } from "../../http/context.ts"; |
| 39 | 39 | import { askCountFor, askSentence } from "../studio/asks.ts"; |
| 40 | 40 | import { studioDoor, studioHome } from "../studio/home.ts"; |
| 41 | import { edgeLoginUrl } from "../../http/edge.ts"; | |
| 41 | import { edgeIsTheDoor, edgeLoginUrl } from "../../http/edge.ts"; | |
| 42 | import { linkSignIn } from "../../actions/links.ts"; | |
| 42 | 43 | import { sshCommand } from "../../ssh/command.ts"; |
| 43 | 44 | import { |
| 44 | 45 | hostOf, |
| @@ -619,29 +620,35 @@ function authForm( | ||
| 619 | 620 | // of the whole user table — and the sign-in screen paints on every session. |
| 620 | 621 | const firstAccount = kind === "signup" && !hasAnyUser(ctx.app.db); |
| 621 | 622 | |
| 622 | // Behind a platform edge, that is the door: the button first, and the | |
| 623 | // password form kept for the accounts that have one — an account the edge | |
| 624 | // made has none, and the note says so before anyone types. | |
| 625 | const edge = | |
| 626 | ctx.app.config.edgeToken === undefined | |
| 627 | ? undefined | |
| 628 | : (ctx.app.config.edgeName ?? "the platform"); | |
| 629 | const throughEdge = when( | |
| 630 | edge, | |
| 631 | (name) => | |
| 632 | html`<div class="stack mb"> | |
| 633 | ${btn(`Continue with ${name}`, { | |
| 634 | href: edgeLoginUrl(next ?? "/"), | |
| 635 | variant: "primary", | |
| 636 | size: "lg", | |
| 637 | })} | |
| 638 | <p class="muted small"> | |
| 639 | ${kind === "signin" | |
| 640 | ? `Accounts made through ${name} sign in there and have no password here. The form below is for an account that was given one.` | |
| 641 | : `Signing in through ${name} makes your account on first sight; the form below makes one with a password instead.`} | |
| 642 | </p> | |
| 643 | </div>`, | |
| 644 | ); | |
| 623 | // Behind a platform edge, that is the door to sign in by: the button | |
| 624 | // first, and the password form kept for the accounts that have one. The | |
| 625 | // edge makes no account (#668), so signing up is always the form here — | |
| 626 | // and a person the edge already knows, whom nobody has linked, is told | |
| 627 | // what the form will do with that identity. | |
| 628 | const edgeName = ctx.app.config.edgeName ?? "the platform"; | |
| 629 | const known = ctx.auth.via === "edge" ? undefined : ctx.auth.edge; | |
| 630 | const throughEdge = known | |
| 631 | ? html`<p class="muted small mb"> | |
| 632 | ${kind === "signup" | |
| 633 | ? `${edgeName} knows you as ${known}, and no account here is linked to it yet. The account you create below is linked to it, so ${edgeName} signs you in to it from now on.` | |
| 634 | : `${edgeName} knows you as ${known}, and no account here is linked to it yet. Sign in with your password, then link it under Settings → Sign-in methods — or create an account.`} | |
| 635 | </p>` | |
| 636 | : edgeIsTheDoor(ctx) | |
| 637 | ? kind === "signin" | |
| 638 | ? html`<div class="stack mb"> | |
| 639 | ${btn(`Continue with ${edgeName}`, { | |
| 640 | href: edgeLoginUrl(next ?? "/"), | |
| 641 | variant: "primary", | |
| 642 | size: "lg", | |
| 643 | })} | |
| 644 | <p class="muted small"> | |
| 645 | ${`Accounts linked to ${edgeName} sign in there. The form below is for an account with a password.`} | |
| 646 | </p> | |
| 647 | </div>` | |
| 648 | : html`<p class="muted small mb"> | |
| 649 | ${`Signing in through ${edgeName} does not make an account here. Create one below; while ${edgeName} has you signed in, it is linked to it.`} | |
| 650 | </p>` | |
| 651 | : null; | |
| 645 | 652 | |
| 646 | 653 | const body = html`${throughEdge} |
| 647 | 654 | <form method="post" action="${action}"> |
| @@ -788,10 +795,9 @@ export async function signinSubmit(ctx: Ctx): Promise<Response> { | ||
| 788 | 795 | body: authForm(ctx, "signin", { |
| 789 | 796 | values: { username }, |
| 790 | 797 | errors: { |
| 791 | password: | |
| 792 | ctx.app.config.edgeToken === undefined | |
| 793 | ? "That username and password don't match." | |
| 794 | : `That username and password don't match. An account made through ${ctx.app.config.edgeName ?? "the platform"} has no password here — use the button above.`, | |
| 798 | password: edgeIsTheDoor(ctx) | |
| 799 | ? `That username and password don't match. An account linked to ${ctx.app.config.edgeName ?? "the platform"} may have no password here — use the button above.` | |
| 800 | : "That username and password don't match.", | |
| 795 | 801 | }, |
| 796 | 802 | }), |
| 797 | 803 | }, |
| @@ -834,6 +840,10 @@ export async function signupSubmit(ctx: Ctx): Promise<Response> { | ||
| 834 | 840 | 422, |
| 835 | 841 | ); |
| 836 | 842 | } |
| 843 | // A person the edge vouched for on this very request, whom no account had | |
| 844 | // linked, made this account: the identity is theirs, so it is linked. | |
| 845 | if (ctx.auth.via !== "edge" && ctx.auth.edge !== undefined) | |
| 846 | linkSignIn(ctx.app, created.value, "edge", ctx.auth.edge); | |
| 837 | 847 | const session = createSession(ctx.app.db, created.value.id); |
| 838 | 848 | return redirect( |
| 839 | 849 | safeNext(ctx.url.searchParams.get("next")) ?? "/", |
src/web/studio/home.ts +2 −2
| @@ -15,7 +15,7 @@ import { getReposByIds, type Repo } from "../../core/repos.ts"; | ||
| 15 | 15 | import { type User } from "../../core/users.ts"; |
| 16 | 16 | import { isPausedAtCeiling, spentTodayForRepos } from "../../crew/autonomy.ts"; |
| 17 | 17 | import type { Ctx } from "../../http/context.ts"; |
| 18 | import { edgeLoginUrl } from "../../http/edge.ts"; | |
| 18 | import { edgeIsTheDoor, edgeLoginUrl } from "../../http/edge.ts"; | |
| 19 | 19 | import { sshCommand } from "../../ssh/command.ts"; |
| 20 | 20 | import { repoBase } from "../chrome.ts"; |
| 21 | 21 | import { liveRegionsJs } from "../client.ts"; |
| @@ -264,7 +264,7 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 264 | 264 | <h1>Apps, live.</h1> |
| 265 | 265 | <p>A software forge that builds and serves its own apps.</p> |
| 266 | 266 | <div class="bar"> |
| 267 | ${ctx.app.config.edgeToken === undefined | |
| 267 | ${!edgeIsTheDoor(ctx) | |
| 268 | 268 | ? html`${btn("Create an account", { |
| 269 | 269 | href: "/signup", |
| 270 | 270 | variant: "primary", |
new above
offline