@n6k.io/serve 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -1
- package/package.json +7 -6
- package/src/assets.ts +4 -20
- package/src/bundle.ts +6 -6
- package/src/cli.ts +70 -9
- package/src/logout.test.ts +65 -0
- package/src/logout.ts +35 -0
- package/src/mcp-instructions.ts +1 -1
- package/src/oidc-grants.ts +220 -0
- package/src/oidc.test.ts +303 -0
- package/src/oidc.ts +120 -0
- package/src/paths.ts +14 -4
- package/src/root.tsx +26 -5
- package/src/saved-logins.ts +114 -0
- package/src/server.ts +66 -1
- package/src/token-route.test.ts +87 -0
- package/src/virtual-modules.d.ts +0 -6
- package/tsconfig.json +29 -0
package/src/server.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { timingSafeEqual } from "node:crypto";
|
|
1
2
|
import type { Bundle } from "./bundle";
|
|
2
3
|
import { serveAsset } from "./assets";
|
|
3
4
|
|
|
@@ -26,6 +27,8 @@ export type ServeOptions = {
|
|
|
26
27
|
hostname: string;
|
|
27
28
|
port: number;
|
|
28
29
|
origins: string[];
|
|
30
|
+
token?: (alias: string) => Promise<string> | undefined;
|
|
31
|
+
secret?: string;
|
|
29
32
|
};
|
|
30
33
|
|
|
31
34
|
type SocketData = { key: string };
|
|
@@ -36,6 +39,20 @@ const ISOLATION = {
|
|
|
36
39
|
};
|
|
37
40
|
|
|
38
41
|
const RELOAD_PREFIX = "/_n6k/reload/";
|
|
42
|
+
export const TOKEN_PREFIX = "/_n6k/token/";
|
|
43
|
+
export const SECRET_PARAM = "secret";
|
|
44
|
+
|
|
45
|
+
// Cookies are shared across ports on one host, so the name carries the port.
|
|
46
|
+
export function secretCookie(port: number): string {
|
|
47
|
+
return `n6k_secret_${port}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function sameSecret(a: string | null, b: string): boolean {
|
|
51
|
+
if (a === null) return false;
|
|
52
|
+
const x = Buffer.from(a);
|
|
53
|
+
const y = Buffer.from(b);
|
|
54
|
+
return x.length === y.length && timingSafeEqual(x, y);
|
|
55
|
+
}
|
|
39
56
|
|
|
40
57
|
function topicFor(key: string): string {
|
|
41
58
|
return `reload:${key}`;
|
|
@@ -113,6 +130,23 @@ async function servePage(
|
|
|
113
130
|
);
|
|
114
131
|
}
|
|
115
132
|
|
|
133
|
+
async function serveToken(
|
|
134
|
+
options: ServeOptions,
|
|
135
|
+
alias: string,
|
|
136
|
+
): Promise<Response> {
|
|
137
|
+
const pending = options.token?.(alias);
|
|
138
|
+
if (!pending) return new Response(`no token for ${alias}`, { status: 404 });
|
|
139
|
+
try {
|
|
140
|
+
return new Response(await pending, {
|
|
141
|
+
headers: { "Content-Type": "text/plain", "Cache-Control": "no-store" },
|
|
142
|
+
});
|
|
143
|
+
} catch (err) {
|
|
144
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
145
|
+
console.error(`token for ${alias}: ${message}`);
|
|
146
|
+
return new Response(message, { status: 502 });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
116
150
|
export function startServer(site: Site, options: ServeOptions) {
|
|
117
151
|
const originAllowed = (req: Request): boolean =>
|
|
118
152
|
options.origins.length === 0 ||
|
|
@@ -122,7 +156,23 @@ export function startServer(site: Site, options: ServeOptions) {
|
|
|
122
156
|
hostname: options.hostname,
|
|
123
157
|
port: options.port,
|
|
124
158
|
async fetch(req, server) {
|
|
125
|
-
const
|
|
159
|
+
const url = new URL(req.url);
|
|
160
|
+
const { pathname } = url;
|
|
161
|
+
const { secret } = options;
|
|
162
|
+
const cookie = secretCookie(server.port!);
|
|
163
|
+
|
|
164
|
+
if (secret !== undefined && url.searchParams.has(SECRET_PARAM)) {
|
|
165
|
+
if (!sameSecret(url.searchParams.get(SECRET_PARAM), secret))
|
|
166
|
+
return new Response("wrong secret", { status: 403 });
|
|
167
|
+
url.searchParams.delete(SECRET_PARAM);
|
|
168
|
+
return new Response(null, {
|
|
169
|
+
status: 303,
|
|
170
|
+
headers: {
|
|
171
|
+
Location: url.pathname + url.search,
|
|
172
|
+
"Set-Cookie": `${cookie}=${secret}; Path=/; HttpOnly; SameSite=Strict`,
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
}
|
|
126
176
|
|
|
127
177
|
if (pathname.startsWith(RELOAD_PREFIX)) {
|
|
128
178
|
if (!originAllowed(req))
|
|
@@ -134,6 +184,21 @@ export function startServer(site: Site, options: ServeOptions) {
|
|
|
134
184
|
return new Response("upgrade required", { status: 426 });
|
|
135
185
|
}
|
|
136
186
|
|
|
187
|
+
if (pathname.startsWith(TOKEN_PREFIX)) {
|
|
188
|
+
if (
|
|
189
|
+
secret !== undefined &&
|
|
190
|
+
!sameSecret(
|
|
191
|
+
new Bun.CookieMap(req.headers.get("cookie") ?? "").get(cookie),
|
|
192
|
+
secret,
|
|
193
|
+
)
|
|
194
|
+
)
|
|
195
|
+
return new Response(
|
|
196
|
+
"missing secret: open the URL n6k-serve printed",
|
|
197
|
+
{ status: 403 },
|
|
198
|
+
);
|
|
199
|
+
return serveToken(options, pathname.slice(TOKEN_PREFIX.length));
|
|
200
|
+
}
|
|
201
|
+
|
|
137
202
|
const resolved = site.resolve(pathname);
|
|
138
203
|
if (resolved?.kind === "redirect")
|
|
139
204
|
return Response.redirect(resolved.to, 308);
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { afterEach, expect, test } from "bun:test";
|
|
2
|
+
import { secretCookie, startServer, type Site } from "./server";
|
|
3
|
+
|
|
4
|
+
const servers: { stop: (force?: boolean) => unknown }[] = [];
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
for (const s of servers.splice(0)) s.stop(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
const status = (res: Promise<Response>) => res.then((r) => r.status);
|
|
10
|
+
|
|
11
|
+
test("token route serves tokens by alias", async () => {
|
|
12
|
+
const site: Site = { resolve: () => {}, page: () => {} };
|
|
13
|
+
const server = startServer(site, {
|
|
14
|
+
hostname: "127.0.0.1",
|
|
15
|
+
port: 0,
|
|
16
|
+
origins: ["http://ok"],
|
|
17
|
+
token: (alias) =>
|
|
18
|
+
alias === "nyc"
|
|
19
|
+
? Promise.resolve("tok")
|
|
20
|
+
: alias === "bad"
|
|
21
|
+
? Promise.reject(new Error("refresh failed"))
|
|
22
|
+
: undefined,
|
|
23
|
+
});
|
|
24
|
+
servers.push(server);
|
|
25
|
+
const get = (alias: string) => fetch(`${server.url}_n6k/token/${alias}`);
|
|
26
|
+
|
|
27
|
+
const ok = await get("nyc");
|
|
28
|
+
expect(ok.status).toBe(200);
|
|
29
|
+
expect(ok.headers.get("cache-control")).toBe("no-store");
|
|
30
|
+
expect(await ok.text()).toBe("tok");
|
|
31
|
+
const other = await get("other");
|
|
32
|
+
expect(other.status).toBe(404);
|
|
33
|
+
const failed = await get("bad");
|
|
34
|
+
expect(failed.status).toBe(502);
|
|
35
|
+
expect(await failed.text()).toBe("refresh failed");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("secret URL sets the cookie the token route requires", async () => {
|
|
39
|
+
const site: Site = { resolve: () => {}, page: () => {} };
|
|
40
|
+
const server = startServer(site, {
|
|
41
|
+
hostname: "127.0.0.1",
|
|
42
|
+
port: 0,
|
|
43
|
+
origins: [],
|
|
44
|
+
token: () => Promise.resolve("tok"),
|
|
45
|
+
secret: "s3cret",
|
|
46
|
+
});
|
|
47
|
+
servers.push(server);
|
|
48
|
+
const name = secretCookie(server.port!);
|
|
49
|
+
const token = (cookie?: string) =>
|
|
50
|
+
fetch(`${server.url}_n6k/token/nyc`, {
|
|
51
|
+
headers: cookie ? { cookie } : {},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(await status(token())).toBe(403);
|
|
55
|
+
expect(await status(token(`${name}=wrong`))).toBe(403);
|
|
56
|
+
expect(await status(token(`n6k_secret_1=s3cret`))).toBe(403);
|
|
57
|
+
|
|
58
|
+
const wrong = await fetch(`${server.url}x?secret=nope`, {
|
|
59
|
+
redirect: "manual",
|
|
60
|
+
});
|
|
61
|
+
expect(wrong.status).toBe(403);
|
|
62
|
+
|
|
63
|
+
const visit = await fetch(`${server.url}x?a=1&secret=s3cret`, {
|
|
64
|
+
redirect: "manual",
|
|
65
|
+
});
|
|
66
|
+
expect(visit.status).toBe(303);
|
|
67
|
+
expect(visit.headers.get("location")).toBe("/x?a=1");
|
|
68
|
+
const set = visit.headers.get("set-cookie")!;
|
|
69
|
+
expect(set).toContain(`${name}=s3cret`);
|
|
70
|
+
expect(set).toContain("HttpOnly");
|
|
71
|
+
expect(set).toContain("SameSite=Strict");
|
|
72
|
+
|
|
73
|
+
const ok = await token(`${name}=s3cret`);
|
|
74
|
+
expect(ok.status).toBe(200);
|
|
75
|
+
expect(await ok.text()).toBe("tok");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("--oidc-login is rejected with --mcp and --pages", async () => {
|
|
79
|
+
for (const args of [["--mcp"], ["--pages", "x.yml"]]) {
|
|
80
|
+
const proc = Bun.spawn(
|
|
81
|
+
["bun", `${import.meta.dir}/cli.ts`, ...args, "--oidc-login"],
|
|
82
|
+
{ stdin: "ignore", stdout: "pipe", stderr: "pipe" },
|
|
83
|
+
);
|
|
84
|
+
expect(await proc.exited).toBe(1);
|
|
85
|
+
expect(await new Response(proc.stderr).text()).toContain("usage:");
|
|
86
|
+
}
|
|
87
|
+
});
|
package/src/virtual-modules.d.ts
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
// Provided by @n6k.io/build's n6kWorkersPlugin.
|
|
2
|
-
declare module "virtual:n6k-workers" {
|
|
3
|
-
export const duckdbWorkerUrl: string;
|
|
4
|
-
export const wsWorkerUrl: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
1
|
// Provided by @n6k.io/build's n6kDuckDBPlugin.
|
|
8
2
|
declare module "virtual:n6k-duckdb-bundle" {
|
|
9
3
|
export const duckdbBundle: {
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
// Environment setup & latest features
|
|
4
|
+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"module": "Preserve",
|
|
7
|
+
"moduleDetection": "force",
|
|
8
|
+
"jsx": "react-jsx",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"types": ["bun-types"],
|
|
11
|
+
|
|
12
|
+
// Bundler mode
|
|
13
|
+
"moduleResolution": "bundler",
|
|
14
|
+
"allowImportingTsExtensions": true,
|
|
15
|
+
"verbatimModuleSyntax": true,
|
|
16
|
+
"noEmit": true,
|
|
17
|
+
|
|
18
|
+
// Best practices
|
|
19
|
+
"strict": true,
|
|
20
|
+
"skipLibCheck": true,
|
|
21
|
+
"noFallthroughCasesInSwitch": true,
|
|
22
|
+
"noImplicitOverride": true,
|
|
23
|
+
|
|
24
|
+
// Some stricter flags (disabled by default)
|
|
25
|
+
"noUnusedLocals": false,
|
|
26
|
+
"noUnusedParameters": false,
|
|
27
|
+
"noPropertyAccessFromIndexSignature": false
|
|
28
|
+
}
|
|
29
|
+
}
|