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

The caller's own lists answer `{ items, next_cursor }`: tokens, origins and origin apps #4861

Parked opened this
Closed without merging

scout/forge:scout/4841-the-caller-s-own-lists-answer-items-next → main 2 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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):
569569 ```
570570 GET /go/:code anonymous: 303 to a live flow's
571571 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)
573573 POST /api/v1/user/origins {host|kind, token} → discovers, validates
574574 → 201 origin + reattached [{owner,name,
575575 fullName,refused}]; renews in place
@@ -598,7 +598,7 @@ POST /api/v1/repos/:o/:n/contribute the fork flow, from a mirror
598598 POST /api/v1/user/origins/:id/mirror {fullName, name?} → 202 {job}
599599 GET /api/v1/user/origins/:id/mirror/:job poll one job
600600 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)
602602 POST /api/v1/user/origin-apps {host, clientId, clientSecret?}
603603 DELETE /api/v1/user/origin-apps/:id
604604 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) {
10441044 };
10451045 }
10461046
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
10471073 /**
10481074 * `?limit=&cursor=` for a list paged by a name: limit 1..200 (default 50),
10491075 * the cursor the last name of the page before. A bad one is a 422.
@@ -2154,8 +2180,8 @@ export function apiRoutes(router: Router<Handler>): void {
21542180 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
21552181 const denied = requireAccount(ctx);
21562182 if (denied) return denied;
2157 const tokens = timePhase(ctx.request, "tokens", () => listTokens(ctx.app.db, ctx.auth.user!.id));
2158 return timePhase(ctx.request, "render", () => json(tokens));
2183 const all = timePhase(ctx.request, "tokens", () => listTokens(ctx.app.db, ctx.auth.user!.id));
2184 return timePhase(ctx.request, "render", () => ownPage(ctx, all));
21592185 });
21602186
21612187 router.add("DELETE", "/api/v1/tokens/:id", (ctx) => {
@@ -4617,8 +4643,8 @@ export function apiRoutes(router: Router<Handler>): void {
46174643 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
46184644 const denied = requireAccount(ctx, { adminToken: true });
46194645 if (denied) return denied;
4620 const origins = timePhase(ctx.request, "origins", () => listUserOrigins(ctx.app, ctx.auth.user!).map(originJson));
4621 return timePhase(ctx.request, "render", () => json(origins));
4646 const all = timePhase(ctx.request, "origins", () => listUserOrigins(ctx.app, ctx.auth.user!).map(originJson));
4647 return timePhase(ctx.request, "render", () => ownPage(ctx, all));
46224648 });
46234649
46244650 router.add("POST", "/api/v1/user/origins", async (ctx) => {
@@ -4693,8 +4719,8 @@ export function apiRoutes(router: Router<Handler>): void {
46934719 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
46944720 const denied = requireAccount(ctx, { adminToken: true });
46954721 if (denied) return denied;
4696 const apps = timePhase(ctx.request, "apps", () => listOriginApps(ctx.app, ctx.auth.user!));
4697 return timePhase(ctx.request, "render", () => json(apps));
4722 const all = timePhase(ctx.request, "apps", () => listOriginApps(ctx.app, ctx.auth.user!));
4723 return timePhase(ctx.request, "render", () => ownPage(ctx, all));
46984724 });
46994725
47004726 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", () => {
375375 expect(pkce().challenge).not.toBe(challenge); // sanity: challenges differ
376376 expect(verifier.length).toBeGreaterThan(42);
377377
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;
381383 expect(origins).toHaveLength(1);
382384 expect(origins[0]).toMatchObject({ authKind: "oauth", login: LOGIN });
383385 });
test/origins-security-auth.test.ts +9 −7
@@ -993,11 +993,11 @@ describe("authorization across origins, mirrors, pins and disconnect", () => {
993993 );
994994 }
995995 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 });
998998 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 });
10011001 }
10021002 // Nothing was deleted or spent by any of that.
10031003 expect(getOriginById(app.db, originId)).toBeDefined();
@@ -1220,9 +1220,11 @@ describe("authorization across origins, mirrors, pins and disconnect", () => {
12201220 );
12211221 }
12221222 // 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;
12261228 expect(apps).toHaveLength(1);
12271229 expect(apps[0]!.hasSecret).toBe(true);
12281230 expect(Object.keys(apps[0]!)).not.toContain("clientSecret");
test/origins.test.ts +4 −4
@@ -302,9 +302,9 @@ describe("origins: connecting, mirroring, writing through", () => {
302302 });
303303
304304 test("listing origins never returns a credential", async () => {
305 const listed = (await (
305 const listed = ((await (
306306 await api("/api/v1/user/origins")
307 ).json()) as unknown[];
307 ).json()) as { items: unknown[] }).items;
308308 expect(listed).toHaveLength(1);
309309 expect(JSON.stringify(listed)).not.toContain(ORIGIN_TOKEN);
310310 });
@@ -353,9 +353,9 @@ describe("origins: connecting, mirroring, writing through", () => {
353353 })
354354 ).status,
355355 ).toBe(404);
356 const bobsOrigins = (await (
356 const bobsOrigins = ((await (
357357 await api("/api/v1/user/origins", { as: "bob" })
358 ).json()) as unknown[];
358 ).json()) as { items: unknown[] }).items;
359359 expect(bobsOrigins).toHaveLength(0);
360360 });
361361
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", () => {
9595 return {
9696 status: res.status,
9797 json: (Array.isArray(parsed) ? {} : parsed) as Json,
98 list: (Array.isArray(parsed) ? parsed : []) as Json[],
98 list: (Array.isArray(parsed)

Non-blocking: falling back to items here means a route this suite reads could switch between a bare array and { items } without failing it. Once the parallel list issues land, consider making list read items only, so the parity suite also checks that the shape is the same everywhere.

99 ? parsed
100 : Array.isArray((parsed as { items?: unknown }).items)
101 ? (parsed as { items: unknown[] }).items
102 : []) as Json[],
99103 };
100104 }
101105

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs