Public: anyone can read this; the crew can change it.
The caller's own lists answer `{ items, next_cursor }`: tokens, origins and origin apps #5142
scout/forge:scout/4841-the-caller-s-own-lists-answer-items-next → main 3 commits fork
7 files
changed +165
−23
docs/ORIGINS.md +2 −2
| @@ -569,7 +569,7 @@ to admins only. API, all admin-only (anyone else gets 404): | ||
| 569 | 569 | |
| 570 | 570 | GET /go/:code anonymous: 303 to a live flow's |
| 571 | 571 | verification/authorize URL |
| 572 | GET /api/v1/user/origins list (no tokens, ever) | |
| 572 | GET /api/v1/user/origins list, `{ items, next_cursor }` (no tokens, ever) | |
| 573 | 573 | POST /api/v1/user/origins {host|kind, token} → discovers, validates |
| 574 | 574 | → 201 origin + reattached [{owner,name, |
| 575 | 575 | fullName,refused}]; renews in place |
| @@ -598,7 +598,7 @@ POST /api/v1/repos/:o/:n/contribute the fork flow, from a mirror | ||
| 598 | 598 | POST /api/v1/user/origins/:id/mirror {fullName, name?} → 202 {job} |
| 599 | 599 | GET /api/v1/user/origins/:id/mirror/:job poll one job |
| 600 | 600 | GET /api/v1/user/origins/:id/mirror/:job/stream SSE: mirror.<stage>, ends on done |
| 601 | GET /api/v1/user/origin-apps registered OAuth apps (no secrets) | |
| 601 | GET /api/v1/user/origin-apps registered OAuth apps, `{ items, next_cursor }` (no secrets) | |
| 602 | 602 | POST /api/v1/user/origin-apps {host, clientId, clientSecret?} |
| 603 | 603 | DELETE /api/v1/user/origin-apps/:id |
| 604 | 604 | POST /api/v1/repos/:o/:n/sync sync this mirror now; on a fork, take the |
src/http/api.ts +32 −6
| @@ -1044,6 +1044,32 @@ function listQuery(ctx: Ctx) { | ||
| 1044 | 1044 | }; |
| 1045 | 1045 | } |
| 1046 | 1046 | |
| 1047 | /** | |
| 1048 | * `?limit=&cursor=` over a list the caller owns, already in order: limit is a | |
| 1049 | * whole number from 1 to 200 (default 50), the cursor is the id of the last | |
| 1050 | * item of the page before. Anything else is a 422. | |
| 1051 | */ | |
| 1052 | function ownPage<T extends { id: string }>(ctx: Ctx, all: T[]): Response { | |
| 1053 | const q = ctx.url.searchParams; | |
| 1054 | const rawLimit = q.get("limit"); | |
| 1055 | const limit = | |
| 1056 | rawLimit === null ? LIST_LIMIT : /^\d{1,4}$/.test(rawLimit) ? Number(rawLimit) : 0; | |
| 1057 | if (limit < 1 || limit > LIST_LIMIT_MAX) | |
| 1058 | return apiError("invalid_limit", 422, `limit is a whole number from 1 to ${LIST_LIMIT_MAX}`); | |
| 1059 | const cursor = q.get("cursor"); | |
| 1060 | let start = 0; | |
| 1061 | if (cursor !== null) { | |
| 1062 | const at = /^[A-Za-z0-9_-]{1,64}$/.test(cursor) ? all.findIndex((i) => i.id === cursor) : -1; | |
| 1063 | if (at < 0) return apiError("invalid_cursor", 422, "cursor is the next_cursor of a previous page"); | |
| 1064 | start = at + 1; | |
| 1065 | } | |
| 1066 | const items = all.slice(start, start + limit); | |
| 1067 | return json({ | |
| 1068 | items, | |
| 1069 | next_cursor: start + limit < all.length ? items[items.length - 1]!.id : null, | |
| 1070 | }); | |
| 1071 | } | |
| 1072 | ||
| 1047 | 1073 | /** |
| 1048 | 1074 | * `?limit=&cursor=` for a list paged by a name: limit 1..200 (default 50), |
| 1049 | 1075 | * the cursor the last name of the page before. A bad one is a 422. |
| @@ -2158,8 +2184,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 2158 | 2184 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 2159 | 2185 | const denied = requireAccount(ctx); |
| 2160 | 2186 | if (denied) return denied; |
| 2161 | const tokens = timePhase(ctx.request, "tokens", () => listTokens(ctx.app.db, ctx.auth.user!.id)); | |
| 2162 | return timePhase(ctx.request, "render", () => json(tokens)); | |
| 2187 | const all = timePhase(ctx.request, "tokens", () => listTokens(ctx.app.db, ctx.auth.user!.id)); | |
| 2188 | return timePhase(ctx.request, "render", () => ownPage(ctx, all)); | |
| 2163 | 2189 | }); |
| 2164 | 2190 | |
| 2165 | 2191 | router.add("DELETE", "/api/v1/tokens/:id", (ctx) => { |
| @@ -4621,8 +4647,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4621 | 4647 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 4622 | 4648 | const denied = requireAccount(ctx, { adminToken: true }); |
| 4623 | 4649 | if (denied) return denied; |
| 4624 | const origins = timePhase(ctx.request, "origins", () => listUserOrigins(ctx.app, ctx.auth.user!).map(originJson)); | |
| 4625 | return timePhase(ctx.request, "render", () => json(origins)); | |
| 4650 | const all = timePhase(ctx.request, "origins", () => listUserOrigins(ctx.app, ctx.auth.user!).map(originJson)); | |
| 4651 | return timePhase(ctx.request, "render", () => ownPage(ctx, all)); | |
| 4626 | 4652 | }); |
| 4627 | 4653 | |
| 4628 | 4654 | router.add("POST", "/api/v1/user/origins", async (ctx) => { |
| @@ -4697,8 +4723,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4697 | 4723 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 4698 | 4724 | const denied = requireAccount(ctx, { adminToken: true }); |
| 4699 | 4725 | if (denied) return denied; |
| 4700 | const apps = timePhase(ctx.request, "apps", () => listOriginApps(ctx.app, ctx.auth.user!)); | |
| 4701 | return timePhase(ctx.request, "render", () => json(apps)); | |
| 4726 | const all = timePhase(ctx.request, "apps", () => listOriginApps(ctx.app, ctx.auth.user!)); | |
| 4727 | return timePhase(ctx.request, "render", () => ownPage(ctx, all)); | |
| 4702 | 4728 | }); |
| 4703 | 4729 | |
| 4704 | 4730 | router.add("POST", "/api/v1/user/origin-apps", async (ctx) => { |
test/origins-oauth.test.ts +5 −3
| @@ -375,9 +375,11 @@ describe("Forgejo's code flow: PKCE, state, and a browser anywhere", () => { | ||
| 375 | 375 | expect(pkce().challenge).not.toBe(challenge); // sanity: challenges differ |
| 376 | 376 | expect(verifier.length).toBeGreaterThan(42); |
| 377 | 377 | |
| 378 | const origins = await read<{ authKind: string; login: string }[]>( | |
| 379 | await api("/api/v1/user/origins"), | |
| 380 | ); | |
| 378 | const origins = ( | |
| 379 | await read<{ items: { authKind: string; login: string }[] }>( | |
| 380 | await api("/api/v1/user/origins"), | |
| 381 | ) | |
| 382 | ).items; | |
| 381 | 383 | expect(origins).toHaveLength(1); |
| 382 | 384 | expect(origins[0]).toMatchObject({ authKind: "oauth", login: LOGIN }); |
| 383 | 385 | }); |
test/origins-security-auth.test.ts +9 −7
| @@ -993,11 +993,11 @@ describe("authorization across origins, mirrors, pins and disconnect", () => { | ||
| 993 | 993 | ); |
| 994 | 994 | } |
| 995 | 995 | expect( |
| 996 | await read<unknown[]>(await api("/api/v1/user/origins", { as })), | |
| 997 | ).toEqual([]); | |
| 996 | await read<{ items: unknown[]; next_cursor: null }>(await api("/api/v1/user/origins", { as })), | |
| 997 | ).toEqual({ items: [], next_cursor: null }); | |
| 998 | 998 | expect( |
| 999 | await read<unknown[]>(await api("/api/v1/user/origin-apps", { as })), | |
| 1000 | ).toEqual([]); | |
| 999 | await read<{ items: unknown[]; next_cursor: null }>(await api("/api/v1/user/origin-apps", { as })), | |
| 1000 | ).toEqual({ items: [], next_cursor: null }); | |
| 1001 | 1001 | } |
| 1002 | 1002 | // Nothing was deleted or spent by any of that. |
| 1003 | 1003 | expect(getOriginById(app.db, originId)).toBeDefined(); |
| @@ -1220,9 +1220,11 @@ describe("authorization across origins, mirrors, pins and disconnect", () => { | ||
| 1220 | 1220 | ); |
| 1221 | 1221 | } |
| 1222 | 1222 | // The apps list says only whether a secret exists. |
| 1223 | const apps = await read<Record<string, unknown>[]>( | |
| 1224 | await api("/api/v1/user/origin-apps"), | |
| 1225 | ); | |
| 1223 | const apps = ( | |
| 1224 | await read<{ items: Record<string, unknown>[] }>( | |
| 1225 | await api("/api/v1/user/origin-apps"), | |
| 1226 | ) | |
| 1227 | ).items; | |
| 1226 | 1228 | expect(apps).toHaveLength(1); |
| 1227 | 1229 | expect(apps[0]!.hasSecret).toBe(true); |
| 1228 | 1230 | expect(Object.keys(apps[0]!)).not.toContain("clientSecret"); |
test/origins.test.ts +4 −4
| @@ -302,9 +302,9 @@ describe("origins: connecting, mirroring, writing through", () => { | ||
| 302 | 302 | }); |
| 303 | 303 | |
| 304 | 304 | test("listing origins never returns a credential", async () => { |
| 305 | const listed = (await ( | |
| 305 | const listed = ((await ( | |
| 306 | 306 | await api("/api/v1/user/origins") |
| 307 | ).json()) as unknown[]; | |
| 307 | ).json()) as { items: unknown[] }).items; | |
| 308 | 308 | expect(listed).toHaveLength(1); |
| 309 | 309 | expect(JSON.stringify(listed)).not.toContain(ORIGIN_TOKEN); |
| 310 | 310 | }); |
| @@ -353,9 +353,9 @@ describe("origins: connecting, mirroring, writing through", () => { | ||
| 353 | 353 | }) |
| 354 | 354 | ).status, |
| 355 | 355 | ).toBe(404); |
| 356 | const bobsOrigins = (await ( | |
| 356 | const bobsOrigins = ((await ( | |
| 357 | 357 | await api("/api/v1/user/origins", { as: "bob" }) |
| 358 | ).json()) as unknown[]; | |
| 358 | ).json()) as { items: unknown[] }).items; | |
| 359 | 359 | expect(bobsOrigins).toHaveLength(0); |
| 360 | 360 | }); |
| 361 | 361 |
test/own-lists.test.ts added +108
| @@ -0,0 +1,108 @@ | ||
| 1 | /** | |
| 2 | * #4841: the caller's own lists — tokens, origins, origin apps — answer | |
| 3 | * `{ items, next_cursor }`, page with `limit` and `cursor`, and refuse a bad | |
| 4 | * limit or cursor with a 422. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createOrigin } from "../src/core/origins.ts"; | |
| 13 | import { createToken } from "../src/core/tokens.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { saveApp } from "../src/origins/oauth.ts"; | |
| 16 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | const PASS = "a-long-member-passphrase"; | |
| 22 | const COUNT = 5; | |
| 23 | ||
| 24 | type Page = { items: { id: string }[]; next_cursor: string | null }; | |
| 25 | ||
| 26 | describe("the caller's own lists", () => { | |
| 27 | let dataDir = ""; | |
| 28 | let app: App; | |
| 29 | let servers: Servers; | |
| 30 | ||
| 31 | const get = (path: string) => | |
| 32 | fetch(`${servers.url}${path}`, { | |
| 33 | headers: { authorization: `Basic ${btoa(`olive:${PASS}`)}` }, | |
| 34 | }); | |
| 35 | ||
| 36 | beforeAll(async () => { | |
| 37 | dataDir = await mkdtemp(join(tmpdir(), "forge-own-lists-")); | |
| 38 | app = createApp({ dataDir, port: 0, url: "https://forge.example" }); | |
| 39 | servers = await serve(app); | |
| 40 | const olive = await seedAdmin(app, { username: "olive", email: "[email protected]", password: PASS }); | |
| 41 | if (!olive.ok) throw new Error(olive.error); | |
| 42 | const userId = olive.value.id; | |
| 43 | for (let i = 0; i < COUNT; i++) { | |
| 44 | createToken(app.db, userId, `t${i}`, "ro"); | |
| 45 | const host = `https://git${i}.example`; | |
| 46 | const made = createOrigin(app.db, dataDir, { | |
| 47 | userId, | |
| 48 | kind: "forgejo", | |
| 49 | apiUrl: `${host}/api/v1`, | |
| 50 | webUrl: host, | |
| 51 | login: "olive", | |
| 52 | token: `secret-${i}`, | |
| 53 | }); | |
| 54 | if (!made.ok) throw new Error(made.error); | |
| 55 | saveApp(app.db, dataDir, { | |
| 56 | userId, | |
| 57 | kind: "forgejo", | |
| 58 | apiUrl: `${host}/api/v1`, | |
| 59 | webUrl: host, | |
| 60 | clientId: `client-${i}`, | |
| 61 | }); | |
| 62 | } | |
| 63 | }); | |
| 64 | ||
| 65 | afterAll(async () => { | |
| 66 | await servers.stop(); | |
| 67 | await rm(dataDir, { recursive: true, force: true }); | |
| 68 | }); | |
| 69 | ||
| 70 | for (const path of ["/api/v1/tokens", "/api/v1/user/origins", "/api/v1/user/origin-apps"]) { | |
| 71 | describe(path, () => { | |
| 72 | test("answers { items, next_cursor }", async () => { | |
| 73 | const res = await get(path); | |
| 74 | expect(res.status).toBe(200); | |
| 75 | const body = (await res.json()) as Page; | |
| 76 | expect(Array.isArray(body)).toBe(false); | |
| 77 | expect(body.items.length).toBeGreaterThanOrEqual(COUNT); | |
| 78 | expect(body.next_cursor).toBeNull(); | |
| 79 | }); | |
| 80 | ||
| 81 | test("pages with limit and next_cursor to the end", async () => { | |
| 82 | const all = ((await (await get(path)).json()) as Page).items.map((i) => i.id); | |
| 83 | const seen: string[] = []; | |
| 84 | let cursor: string | null = null; | |
| 85 | let pages = 0; | |
| 86 | do { | |
| 87 | const res = await get(`${path}?limit=2${cursor ? `&cursor=${cursor}` : ""}`); | |
| 88 | expect(res.status).toBe(200); | |
| 89 | const page = (await res.json()) as Page; | |
| 90 | expect(page.items.length).toBeLessThanOrEqual(2); | |
| 91 | seen.push(...page.items.map((i) => i.id)); | |
| 92 | cursor = page.next_cursor; | |
| 93 | pages++; | |
| 94 | } while (cursor !== null && pages < 20); | |
| 95 | expect(seen).toEqual(all); | |
| 96 | expect(pages).toBe(Math.ceil(all.length / 2)); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a bad limit or cursor is a 422", async () => { | |
| 100 | for (const q of ["limit=0", "limit=201", "limit=-1", "limit=abc", "limit=1.5", "cursor=nope", "cursor=%20"]) { | |
| 101 | const res = await get(`${path}?${q}`); | |
| 102 | expect(`${q} ${res.status}`).toBe(`${q} 422`); | |
| 103 | } | |
| 104 | expect((await get(`${path}?limit=200`)).status).toBe(200); | |
| 105 | }); | |
| 106 | }); | |
| 107 | } | |
| 108 | }); |
test/parity.test.ts +5 −1
| @@ -95,7 +95,11 @@ describe("parity: full control through actions and the API", () => { | ||
| 95 | 95 | return { |
| 96 | 96 | status: res.status, |
| 97 | 97 | json: (Array.isArray(parsed) ? {} : parsed) as Json, |
| 98 | list: (Array.isArray(parsed) ? parsed : []) as Json[], | |
| 98 | list: (Array.isArray(parsed) | |
| 99 | ? parsed | |
| 100 | : Array.isArray((parsed as { items?: unknown }).items) | |
| 101 | ? (parsed as { items: unknown[] }).items | |
| 102 | : []) as Json[], | |
| 99 | 103 | }; |
| 100 | 104 | } |
| 101 | 105 |
new above
offline