create-signet-app 0.4.0 → 0.4.2
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/package.json +6 -3
- package/src/desktop.ts +50 -0
- package/src/index.ts +104 -16
- package/template-cli/README.md +51 -0
- package/template-cli/_github/workflows/ci.yml +42 -0
- package/template-cli/_gitignore +9 -0
- package/template-cli/biome.json +49 -0
- package/template-cli/src/config.ts +23 -0
- package/template-cli/src/index.ts +104 -0
- package/template-cli/src/poll.ts +58 -0
- package/template-cli/src/ui.ts +16 -0
- package/template-cli/test/poll.test.ts +68 -0
- package/template-cli/tsconfig.json +15 -0
- package/template-cli/tsconfig.test.json +7 -0
- package/template-desktop/README.md +39 -0
- package/template-desktop/_github/workflows/ci.yml +48 -0
- package/template-desktop/_gitignore +6 -0
- package/template-desktop/biome.json +49 -0
- package/template-desktop/index.html +16 -0
- package/template-desktop/src/App.tsx +59 -0
- package/template-desktop/src/app.css +9 -0
- package/template-desktop/src/lib/auth.ts +102 -0
- package/template-desktop/src/lib/pkce.ts +21 -0
- package/template-desktop/src/main.tsx +16 -0
- package/template-desktop/src/pkce.test.ts +15 -0
- package/template-desktop/src-tauri/Cargo.toml +13 -0
- package/template-desktop/src-tauri/build.rs +3 -0
- package/template-desktop/src-tauri/capabilities/default.json +18 -0
- package/template-desktop/src-tauri/icons/128x128.png +0 -0
- package/template-desktop/src-tauri/icons/32x32.png +0 -0
- package/template-desktop/src-tauri/icons/icon.icns +0 -0
- package/template-desktop/src-tauri/icons/icon.ico +0 -0
- package/template-desktop/src-tauri/icons/icon.png +0 -0
- package/template-desktop/src-tauri/src/main.rs +124 -0
- package/template-desktop/src-tauri/tauri.conf.json +42 -0
- package/template-desktop/tsconfig.json +22 -0
- package/template-desktop/vite.config.ts +12 -0
- package/template-extension/.env.example +3 -0
- package/template-extension/README.md +64 -0
- package/template-extension/_github/workflows/ci.yml +22 -0
- package/template-extension/_gitignore +8 -0
- package/template-extension/biome.json +50 -0
- package/template-extension/options.html +13 -0
- package/template-extension/popup.html +13 -0
- package/template-extension/public/manifest.json +11 -0
- package/template-extension/public/theme-init.js +9 -0
- package/template-extension/src/app.css +9 -0
- package/template-extension/src/background.ts +23 -0
- package/template-extension/src/chrome.d.ts +21 -0
- package/template-extension/src/config.ts +11 -0
- package/template-extension/src/lib/api.ts +15 -0
- package/template-extension/src/lib/auth.ts +67 -0
- package/template-extension/src/lib/pkce.test.ts +13 -0
- package/template-extension/src/lib/pkce.ts +17 -0
- package/template-extension/src/lib/use-auth.ts +31 -0
- package/template-extension/src/options.tsx +56 -0
- package/template-extension/src/popup.tsx +44 -0
- package/template-extension/tsconfig.json +21 -0
- package/template-extension/vite.config.ts +23 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-signet-app",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Scaffold a Vite + React + Tailwind app wired to Signet.",
|
|
6
6
|
"keywords": [
|
|
@@ -30,11 +30,14 @@
|
|
|
30
30
|
"src",
|
|
31
31
|
"template",
|
|
32
32
|
"template-api",
|
|
33
|
-
"template-full"
|
|
33
|
+
"template-full",
|
|
34
|
+
"template-cli",
|
|
35
|
+
"template-extension",
|
|
36
|
+
"template-desktop"
|
|
34
37
|
],
|
|
35
38
|
"scripts": {
|
|
36
39
|
"typecheck": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
37
|
-
"test": "bun run test/scaffold.ts && bun run test/scaffold-api.ts && bun run test/scaffold-full.ts"
|
|
40
|
+
"test": "bun run test/scaffold.ts && bun run test/scaffold-api.ts && bun run test/scaffold-full.ts && bun run test/scaffold-cli.ts && bun run test/scaffold-extension.ts && bun run test/scaffold-desktop.ts"
|
|
38
41
|
},
|
|
39
42
|
"devDependencies": {
|
|
40
43
|
"@types/bun": "^1.4.2",
|
package/src/desktop.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** The desktop template (Tauri v2): its package.json and the app id derived from the name. */
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Lowercase alphanumerics only, starting with a letter: valid in a reverse-domain
|
|
5
|
+
* identifier segment and as a Cargo crate name. "My App!" -> "myapp".
|
|
6
|
+
*/
|
|
7
|
+
export function appId(name: string): string {
|
|
8
|
+
const id = name.toLowerCase().replace(/[^a-z0-9]/g, "")
|
|
9
|
+
return /^[a-z]/.test(id) ? id : `app${id}`
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const DESKTOP_PKG = {
|
|
13
|
+
scripts: {
|
|
14
|
+
dev: "vite",
|
|
15
|
+
build: "tsc --noEmit && vite build",
|
|
16
|
+
preview: "vite preview",
|
|
17
|
+
tauri: "tauri",
|
|
18
|
+
lint: "biome check .",
|
|
19
|
+
format: "biome check --write .",
|
|
20
|
+
typecheck: "tsc --noEmit",
|
|
21
|
+
test: "bun test src",
|
|
22
|
+
},
|
|
23
|
+
dependencies: {
|
|
24
|
+
"@dapplelight/signet-lib": "^0.4.1",
|
|
25
|
+
"@dapplelight/signet-tokens": "^0.4.0",
|
|
26
|
+
"@dapplelight/signet-ui": "^0.3.0",
|
|
27
|
+
"@tauri-apps/api": "^2.0.0",
|
|
28
|
+
"@tauri-apps/plugin-opener": "^2.0.0",
|
|
29
|
+
"@tauri-apps/plugin-store": "^2.0.0",
|
|
30
|
+
"class-variance-authority": "^0.7.1",
|
|
31
|
+
clsx: "^2.1.1",
|
|
32
|
+
"lucide-react": "^1.47.0",
|
|
33
|
+
react: "^19.3.0",
|
|
34
|
+
"react-dom": "^19.3.0",
|
|
35
|
+
"tailwind-merge": "^3.7.0",
|
|
36
|
+
},
|
|
37
|
+
devDependencies: {
|
|
38
|
+
"@biomejs/biome": "^2.5.14",
|
|
39
|
+
"@tailwindcss/vite": "^4.3.3",
|
|
40
|
+
"@tauri-apps/cli": "^2.0.0",
|
|
41
|
+
"@types/bun": "^1.4.2",
|
|
42
|
+
"@types/node": "^26.6.2",
|
|
43
|
+
"@types/react": "^19.3.0",
|
|
44
|
+
"@types/react-dom": "^19.3.0",
|
|
45
|
+
"@vitejs/plugin-react": "^6.1.1",
|
|
46
|
+
tailwindcss: "^4.3.3",
|
|
47
|
+
typescript: "^7.0.2",
|
|
48
|
+
vite: "^8.3.0",
|
|
49
|
+
},
|
|
50
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -14,10 +14,11 @@ import { existsSync } from "node:fs"
|
|
|
14
14
|
import { cp, mkdir, readdir, readFile, rename, writeFile } from "node:fs/promises"
|
|
15
15
|
import { basename, join, resolve } from "node:path"
|
|
16
16
|
import { createInterface } from "node:readline/promises"
|
|
17
|
+
import { appId, DESKTOP_PKG } from "./desktop.js"
|
|
17
18
|
|
|
18
19
|
const THEMES = ["editorial", "cinematic", "finance"] as const
|
|
19
20
|
type Theme = (typeof THEMES)[number]
|
|
20
|
-
const TEMPLATES = ["web", "api", "full"] as const
|
|
21
|
+
const TEMPLATES = ["web", "api", "full", "cli", "extension", "desktop"] as const
|
|
21
22
|
type Template = (typeof TEMPLATES)[number]
|
|
22
23
|
const TARGETS = ["cloudflare", "bun", "node", "vercel"] as const
|
|
23
24
|
type Target = (typeof TARGETS)[number]
|
|
@@ -73,12 +74,12 @@ function applyPm(ci: string, pm: Pm): string {
|
|
|
73
74
|
" - uses: oven-sh/setup-bun@v2",
|
|
74
75
|
].join("\n")
|
|
75
76
|
return ci
|
|
76
|
-
.replace(/^ {6}- uses: oven-sh\/setup-bun@v2$/
|
|
77
|
+
.replace(/^ {6}- uses: oven-sh\/setup-bun@v2$/gm, setup)
|
|
77
78
|
.replace(
|
|
78
79
|
/Commit bun\.lock: --frozen-lockfile fails/,
|
|
79
80
|
`Commit ${p.lock}: a frozen install fails`,
|
|
80
81
|
)
|
|
81
|
-
.
|
|
82
|
+
.replaceAll("bun install --frozen-lockfile", p.ci)
|
|
82
83
|
.replaceAll("bun run ", `${p.run} `)
|
|
83
84
|
.replaceAll("bunx ", `${p.x} `)
|
|
84
85
|
}
|
|
@@ -87,12 +88,15 @@ const TEMPLATE_DIRS: Record<Template, string> = {
|
|
|
87
88
|
web: "template",
|
|
88
89
|
api: "template-api",
|
|
89
90
|
full: "template-full",
|
|
91
|
+
cli: "template-cli",
|
|
92
|
+
extension: "template-extension",
|
|
93
|
+
desktop: "template-desktop",
|
|
90
94
|
}
|
|
91
95
|
|
|
92
96
|
// The api template: a Hono Worker with Signet's request id, error envelope,
|
|
93
97
|
// validation and exact Durable Object rate limiting already wired.
|
|
94
98
|
const API_DEPS = {
|
|
95
|
-
"@dapplelight/signet-server": "^0.
|
|
99
|
+
"@dapplelight/signet-server": "^0.5.0",
|
|
96
100
|
hono: "^4.13.8",
|
|
97
101
|
zod: "^4.6.5",
|
|
98
102
|
}
|
|
@@ -157,9 +161,9 @@ const DEPS = {
|
|
|
157
161
|
// Real ranges, not workspace:* — the packages are published, so a scaffolded
|
|
158
162
|
// app installs from npm and works anywhere. Inside this monorepo bun still
|
|
159
163
|
// links the local copies, because the range matches the workspace version.
|
|
160
|
-
"@dapplelight/signet-lib": "^0.
|
|
161
|
-
"@dapplelight/signet-tokens": "^0.
|
|
162
|
-
"@dapplelight/signet-ui": "^0.
|
|
164
|
+
"@dapplelight/signet-lib": "^0.4.0",
|
|
165
|
+
"@dapplelight/signet-tokens": "^0.4.0",
|
|
166
|
+
"@dapplelight/signet-ui": "^0.3.0",
|
|
163
167
|
"@tanstack/react-query": "^5.103.2",
|
|
164
168
|
"@tanstack/react-router": "^1.170.39",
|
|
165
169
|
"class-variance-authority": "^0.7.1",
|
|
@@ -191,7 +195,7 @@ const FULL_DEPS = {
|
|
|
191
195
|
...DEPS,
|
|
192
196
|
...API_DEPS,
|
|
193
197
|
"@base-ui/react": "^1.3.0",
|
|
194
|
-
"@dapplelight/signet-db": "^0.
|
|
198
|
+
"@dapplelight/signet-db": "^0.2.0",
|
|
195
199
|
"@dapplelight/signet-payments": "^0.1.0",
|
|
196
200
|
"drizzle-orm": "^0.45.4",
|
|
197
201
|
"input-otp": "^1.4.2",
|
|
@@ -203,6 +207,38 @@ const FULL_DEV_DEPS = {
|
|
|
203
207
|
"drizzle-kit": "^0.31.11",
|
|
204
208
|
}
|
|
205
209
|
|
|
210
|
+
// The cli template: a Bun CLI that signs in by device code, compiled to one binary.
|
|
211
|
+
const CLI_PKG = {
|
|
212
|
+
scripts: {
|
|
213
|
+
dev: "bun src/index.ts",
|
|
214
|
+
build: "bun build --compile src/index.ts --outfile dist/__APP_NAME__",
|
|
215
|
+
lint: "biome check .",
|
|
216
|
+
format: "biome check --write .",
|
|
217
|
+
typecheck: "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
|
|
218
|
+
test: "bun test",
|
|
219
|
+
},
|
|
220
|
+
dependencies: {
|
|
221
|
+
"@dapplelight/signet-lib": "^0.4.0",
|
|
222
|
+
"@dapplelight/signet-tokens": "^0.4.0",
|
|
223
|
+
},
|
|
224
|
+
devDependencies: API_DEV_DEPS,
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// The extension template: the web app's deps without the router, query and
|
|
228
|
+
// toasts, built by plain Vite into popup, options and service worker.
|
|
229
|
+
const EXT_OMIT = ["@tanstack/react-query", "@tanstack/react-router", "sonner"]
|
|
230
|
+
const EXT_DEV_OMIT = ["@tanstack/router-plugin"]
|
|
231
|
+
const omit = (o: Record<string, string>, keys: string[]) =>
|
|
232
|
+
Object.fromEntries(Object.entries(o).filter(([k]) => !keys.includes(k)))
|
|
233
|
+
const EXT_SCRIPTS = {
|
|
234
|
+
dev: "vite build --watch",
|
|
235
|
+
build: "tsc --noEmit && vite build",
|
|
236
|
+
lint: "biome check .",
|
|
237
|
+
format: "biome check --write .",
|
|
238
|
+
typecheck: "tsc --noEmit",
|
|
239
|
+
test: "bun test",
|
|
240
|
+
}
|
|
241
|
+
|
|
206
242
|
const FULL_SCRIPTS = {
|
|
207
243
|
// Both halves: the API (restarting on change) and Vite, which proxies /api to it.
|
|
208
244
|
dev: "bun scripts/dev.ts",
|
|
@@ -264,13 +300,20 @@ function parseArgs(argv: string[]) {
|
|
|
264
300
|
create-signet-app <directory> [options]
|
|
265
301
|
|
|
266
302
|
Options:
|
|
267
|
-
--template <kind> web | api | full (default: web)
|
|
303
|
+
--template <kind> web | api | full | cli | extension | desktop (default: web)
|
|
268
304
|
web: Vite + React + Signet UI
|
|
269
305
|
api: Hono Worker with the error envelope, request ids,
|
|
270
306
|
validation and Durable Object rate limiting
|
|
271
307
|
full: web + API in one project, typed client between
|
|
272
308
|
them, SQLite with migrations, phone sign-in,
|
|
273
309
|
account, inbox, privacy, billing and admin
|
|
310
|
+
cli: Bun CLI compiled to one binary; login by device
|
|
311
|
+
code, logout, whoami
|
|
312
|
+
extension: Chrome MV3 extension: popup, options page and
|
|
313
|
+
service worker, signed in over PKCE with
|
|
314
|
+
chrome.identity (plain Vite, no extension plugin)
|
|
315
|
+
desktop: Tauri v2 desktop + menu-bar app, browser sign-in
|
|
316
|
+
(PKCE, loopback redirect), CI on macOS/Windows/Linux
|
|
274
317
|
--target <runtime> ${TARGETS.join(" | ")} (default: cloudflare; api only)
|
|
275
318
|
cloudflare: Worker entry + wrangler.jsonc
|
|
276
319
|
bun: Bun.serve entry + Dockerfile
|
|
@@ -288,6 +331,7 @@ Examples:
|
|
|
288
331
|
bun create-signet-app my-api --template api
|
|
289
332
|
bun create-signet-app my-api --template api --target bun
|
|
290
333
|
bun create-signet-app my-app --template full --theme finance
|
|
334
|
+
bun create-signet-app my-cli --template cli
|
|
291
335
|
npm create signet-app my-app -- --template full --pm npm
|
|
292
336
|
`)
|
|
293
337
|
process.exit(0)
|
|
@@ -300,7 +344,10 @@ Examples:
|
|
|
300
344
|
|
|
301
345
|
if (!name) throw new Error("Missing target directory.\nUsage: create-signet-app <directory>")
|
|
302
346
|
if (target && template !== "api") throw new Error("--target applies to --template api only.")
|
|
303
|
-
if (
|
|
347
|
+
if (
|
|
348
|
+
mcp !== undefined &&
|
|
349
|
+
(template === "web" || template === "cli" || template === "extension" || template === "desktop")
|
|
350
|
+
)
|
|
304
351
|
throw new Error("--mcp / --no-mcp apply to --template api and full only.")
|
|
305
352
|
if (theme !== "editorial" && template === "api")
|
|
306
353
|
throw new Error("--theme applies to --template web and full only.")
|
|
@@ -348,7 +395,7 @@ async function main() {
|
|
|
348
395
|
throw new Error(`${dest} already exists and is not empty.`)
|
|
349
396
|
}
|
|
350
397
|
|
|
351
|
-
const mcp = template
|
|
398
|
+
const mcp = (template === "api" || template === "full") && (flags.mcp ?? (await askMcp()))
|
|
352
399
|
const templateDir = resolve(import.meta.dirname, `../${TEMPLATE_DIRS[template]}`)
|
|
353
400
|
await mkdir(dest, { recursive: true })
|
|
354
401
|
const targetsDir = join(templateDir, "_targets")
|
|
@@ -362,8 +409,11 @@ async function main() {
|
|
|
362
409
|
if (mcp && existsSync(mcpDir)) await cp(mcpDir, dest, { recursive: true })
|
|
363
410
|
const mcpFiles: Record<Template, string[]> = {
|
|
364
411
|
web: [],
|
|
412
|
+
extension: [],
|
|
413
|
+
desktop: [],
|
|
365
414
|
api: ["src/app.ts", "src/env.ts"],
|
|
366
415
|
full: ["api/app.ts", "api/env.ts"],
|
|
416
|
+
cli: [],
|
|
367
417
|
}
|
|
368
418
|
for (const f of mcpFiles[template]) {
|
|
369
419
|
const path = join(dest, f)
|
|
@@ -391,6 +441,7 @@ async function main() {
|
|
|
391
441
|
|
|
392
442
|
await substitute(dest, {
|
|
393
443
|
__APP_NAME__: appName,
|
|
444
|
+
__APP_ID__: appId(appName),
|
|
394
445
|
__THEME__: theme,
|
|
395
446
|
__PM_INSTALL__: PM[pm].install,
|
|
396
447
|
__PM_RUN__: PM[pm].run,
|
|
@@ -453,24 +504,61 @@ async function main() {
|
|
|
453
504
|
? { engines: API_TARGETS[target].engines }
|
|
454
505
|
: {}),
|
|
455
506
|
}
|
|
507
|
+
if (template === "extension")
|
|
508
|
+
Object.assign(pkg, {
|
|
509
|
+
scripts: EXT_SCRIPTS,
|
|
510
|
+
// ^0.4.1: chromeStorage().watch, so the pages see the service worker sign in.
|
|
511
|
+
dependencies: { ...omit(DEPS, EXT_OMIT), "@dapplelight/signet-lib": "^0.4.1" },
|
|
512
|
+
devDependencies: omit(DEV_DEPS, EXT_DEV_OMIT),
|
|
513
|
+
})
|
|
514
|
+
const final =
|
|
515
|
+
template === "cli"
|
|
516
|
+
? JSON.parse(JSON.stringify(CLI_PKG).replaceAll("__APP_NAME__", appName))
|
|
517
|
+
: pkg
|
|
456
518
|
await writeFile(
|
|
457
519
|
join(dest, "package.json"),
|
|
458
|
-
`${JSON.stringify({ name: appName, private: true, version: "0.1.0", type: "module", ...
|
|
520
|
+
`${JSON.stringify({ name: appName, private: true, version: "0.1.0", type: "module", ...(template === "desktop" ? DESKTOP_PKG : final) }, null, 2)}\n`,
|
|
459
521
|
)
|
|
460
522
|
|
|
461
523
|
const { install, run } = PM[pm]
|
|
462
524
|
const steps = `cd ${name}\n ${install}\n ${run} dev`
|
|
463
525
|
console.log(
|
|
464
|
-
template === "
|
|
526
|
+
template === "cli"
|
|
465
527
|
? `
|
|
528
|
+
Created ${appName}, a CLI that compiles to one binary.
|
|
529
|
+
|
|
530
|
+
${steps.replace(`${run} dev`, `${run} build`)}
|
|
531
|
+
|
|
532
|
+
Then ./dist/${appName} login. README.md covers the server side.
|
|
533
|
+
`
|
|
534
|
+
: template === "extension"
|
|
535
|
+
? `
|
|
536
|
+
Created ${appName}, a Chrome extension with the ${theme} theme.
|
|
537
|
+
|
|
538
|
+
${steps.replace(`${run} dev`, `${run} build`)}
|
|
539
|
+
|
|
540
|
+
Then load dist/ at chrome://extensions (Developer mode, Load unpacked), and
|
|
541
|
+
register its redirect URI on your server. README.md has both steps.
|
|
542
|
+
`
|
|
543
|
+
: template === "api"
|
|
544
|
+
? `
|
|
466
545
|
Created ${appName}, an API for ${target}${mcp ? ", with MCP at /mcp (MCP=off turns it off)" : ""}.
|
|
467
546
|
|
|
468
547
|
${steps}
|
|
469
548
|
|
|
470
549
|
${API_NEXT_STEPS[target]}
|
|
471
550
|
`
|
|
472
|
-
|
|
473
|
-
|
|
551
|
+
: template === "desktop"
|
|
552
|
+
? `
|
|
553
|
+
Created ${appName}, a Tauri desktop + menu-bar app with the ${theme} theme.
|
|
554
|
+
|
|
555
|
+
${steps.replace(`${run} dev`, `${PM[pm].x} tauri dev`)}
|
|
556
|
+
|
|
557
|
+
Needs Rust and Tauri's system dependencies. README.md covers sign-in (register
|
|
558
|
+
the client on signet-server) and the identifier in src-tauri/tauri.conf.json.
|
|
559
|
+
`
|
|
560
|
+
: template === "full"
|
|
561
|
+
? `
|
|
474
562
|
Created ${appName}: web app + API with the ${theme} theme${mcp ? ", MCP at /mcp" : ""}.
|
|
475
563
|
|
|
476
564
|
${steps}
|
|
@@ -480,7 +568,7 @@ async function main() {
|
|
|
480
568
|
to see /admin. README.md walks through the first day. The API runs on Bun,
|
|
481
569
|
whichever package manager installs it.
|
|
482
570
|
`
|
|
483
|
-
|
|
571
|
+
: `
|
|
484
572
|
Created ${appName} with the ${theme} theme.
|
|
485
573
|
|
|
486
574
|
${steps}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# `__APP_NAME__`
|
|
2
|
+
|
|
3
|
+
A CLI on Bun, compiled to one standalone binary, that signs in through
|
|
4
|
+
Signet's device code flow.
|
|
5
|
+
|
|
6
|
+
```
|
|
7
|
+
__PM_INSTALL__
|
|
8
|
+
__PM_RUN__ build # dist/__APP_NAME__ for this platform
|
|
9
|
+
./dist/__APP_NAME__ login
|
|
10
|
+
./dist/__APP_NAME__ whoami
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Without compiling: `bun src/index.ts login`.
|
|
14
|
+
|
|
15
|
+
## Server side
|
|
16
|
+
|
|
17
|
+
Mount `clientLoginRoutes` from `@dapplelight/signet-server/auth/hono` at
|
|
18
|
+
`/auth/client-login` and register this CLI as a client:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
app.route(
|
|
22
|
+
"/auth/client-login",
|
|
23
|
+
clientLoginRoutes({
|
|
24
|
+
trust: "cloudflare",
|
|
25
|
+
clients: { __APP_NAME__: {} },
|
|
26
|
+
verificationUri: "https://your.app/device",
|
|
27
|
+
}),
|
|
28
|
+
)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Also pass `sessions`, `kv` and `rateLimit` (see signet-server's
|
|
32
|
+
`ClientLoginOptions`). `verificationUri` is the page where a signed-in user
|
|
33
|
+
types the code.
|
|
34
|
+
|
|
35
|
+
`src/config.ts` holds the client id, the mount path, the default API URL
|
|
36
|
+
(`API_URL` or `--api-url` override it) and the route `whoami` calls.
|
|
37
|
+
|
|
38
|
+
## How it works
|
|
39
|
+
|
|
40
|
+
- `login` asks `POST /device/code`, prints the code and link, then
|
|
41
|
+
`src/poll.ts` polls `POST /token`, honouring `interval`, `slow_down`,
|
|
42
|
+
`authorization_pending` and `expired_token`.
|
|
43
|
+
- Tokens live in a 0600 file in the OS config dir via `createSession` and
|
|
44
|
+
`fileStorage`; the API client refreshes on a 401 through `tokenEndpointRefresh`.
|
|
45
|
+
- `logout` calls `POST /revoke` and deletes the file.
|
|
46
|
+
- Colour comes from the Signet tokens and is off under `NO_COLOR` or a pipe.
|
|
47
|
+
|
|
48
|
+
## Releasing
|
|
49
|
+
|
|
50
|
+
`.github/workflows/ci.yml` builds a binary per OS on `v*` tags. Add
|
|
51
|
+
`--target` to `bun build --compile` to cross-compile instead.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
tags: ["v*"]
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
verify:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: oven-sh/setup-bun@v2
|
|
18
|
+
# Commit bun.lock: --frozen-lockfile fails the build if it is missing or stale.
|
|
19
|
+
- run: bun install --frozen-lockfile
|
|
20
|
+
- run: bun run lint
|
|
21
|
+
- run: bun run typecheck
|
|
22
|
+
- run: bun run test
|
|
23
|
+
|
|
24
|
+
# One standalone binary per platform, built on that platform (a compiled
|
|
25
|
+
# Bun binary embeds the runtime of the machine that built it). Attached to
|
|
26
|
+
# the run; publish them as release assets however you ship.
|
|
27
|
+
binaries:
|
|
28
|
+
needs: verify
|
|
29
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
30
|
+
strategy:
|
|
31
|
+
matrix:
|
|
32
|
+
os: [ubuntu-latest, macos-latest, windows-latest]
|
|
33
|
+
runs-on: ${{ matrix.os }}
|
|
34
|
+
steps:
|
|
35
|
+
- uses: actions/checkout@v4
|
|
36
|
+
- uses: oven-sh/setup-bun@v2
|
|
37
|
+
- run: bun install --frozen-lockfile
|
|
38
|
+
- run: bun run build
|
|
39
|
+
- uses: actions/upload-artifact@v4
|
|
40
|
+
with:
|
|
41
|
+
name: __APP_NAME__-${{ matrix.os }}
|
|
42
|
+
path: dist/
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/2.5.14/schema.json",
|
|
3
|
+
"vcs": {
|
|
4
|
+
"enabled": true,
|
|
5
|
+
"clientKind": "git",
|
|
6
|
+
"useIgnoreFile": true
|
|
7
|
+
},
|
|
8
|
+
"files": {
|
|
9
|
+
"ignoreUnknown": true
|
|
10
|
+
},
|
|
11
|
+
"formatter": {
|
|
12
|
+
"enabled": true,
|
|
13
|
+
"indentStyle": "space",
|
|
14
|
+
"indentWidth": 2,
|
|
15
|
+
"lineWidth": 100
|
|
16
|
+
},
|
|
17
|
+
"javascript": {
|
|
18
|
+
"formatter": {
|
|
19
|
+
"quoteStyle": "double",
|
|
20
|
+
"semicolons": "asNeeded"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"linter": {
|
|
24
|
+
"enabled": true,
|
|
25
|
+
"rules": {
|
|
26
|
+
"correctness": {
|
|
27
|
+
"useExhaustiveDependencies": "warn",
|
|
28
|
+
"useHookAtTopLevel": "error"
|
|
29
|
+
},
|
|
30
|
+
"suspicious": {
|
|
31
|
+
"noExplicitAny": "warn"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"assist": {
|
|
36
|
+
"actions": {
|
|
37
|
+
"source": {
|
|
38
|
+
"organizeImports": "on"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"css": {
|
|
43
|
+
"parser": {
|
|
44
|
+
"cssModules": false,
|
|
45
|
+
"allowWrongLineComments": false,
|
|
46
|
+
"tailwindDirectives": true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { homedir } from "node:os"
|
|
2
|
+
import { join } from "node:path"
|
|
3
|
+
|
|
4
|
+
/** Where the API lives; `API_URL` overrides it, as does `--api-url`. */
|
|
5
|
+
export const DEFAULT_API_URL = "http://localhost:8787"
|
|
6
|
+
/** Must be a key in the server's `clientLoginRoutes({ clients })`. */
|
|
7
|
+
export const CLIENT_ID = "__APP_NAME__"
|
|
8
|
+
/** Where `clientLoginRoutes` is mounted, e.g. `app.route("/auth/client-login", ...)`. */
|
|
9
|
+
export const AUTH_PATH = "/auth/client-login"
|
|
10
|
+
/** An authenticated route that answers with the signed-in user; replace with yours. */
|
|
11
|
+
export const WHOAMI_PATH = "/api/me"
|
|
12
|
+
|
|
13
|
+
/** The OS config directory for this CLI: ~/Library/Application Support, %APPDATA% or $XDG_CONFIG_HOME. */
|
|
14
|
+
export function sessionPath(): string {
|
|
15
|
+
const home = homedir()
|
|
16
|
+
const base =
|
|
17
|
+
process.platform === "darwin"
|
|
18
|
+
? join(home, "Library", "Application Support")
|
|
19
|
+
: process.platform === "win32"
|
|
20
|
+
? (process.env.APPDATA ?? join(home, "AppData", "Roaming"))
|
|
21
|
+
: (process.env.XDG_CONFIG_HOME ?? join(home, ".config"))
|
|
22
|
+
return join(base, CLIENT_ID, "session.json")
|
|
23
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
import { parseArgs } from "node:util"
|
|
4
|
+
import { createApiClient } from "@dapplelight/signet-lib/api"
|
|
5
|
+
import {
|
|
6
|
+
createSession,
|
|
7
|
+
fromTokenResponse,
|
|
8
|
+
tokenEndpointRefresh,
|
|
9
|
+
} from "@dapplelight/signet-lib/session"
|
|
10
|
+
import { fileStorage } from "@dapplelight/signet-lib/storage/file"
|
|
11
|
+
import { AUTH_PATH, CLIENT_ID, DEFAULT_API_URL, sessionPath, WHOAMI_PATH } from "./config.ts"
|
|
12
|
+
import { pollDeviceToken } from "./poll.ts"
|
|
13
|
+
import { dim, fail, link, ok } from "./ui.ts"
|
|
14
|
+
|
|
15
|
+
const HELP = `__APP_NAME__ <command>
|
|
16
|
+
|
|
17
|
+
Commands:
|
|
18
|
+
login Sign in: shows a code to approve in your browser
|
|
19
|
+
logout Revoke this device's session and forget it
|
|
20
|
+
whoami Show the signed-in user
|
|
21
|
+
|
|
22
|
+
Options:
|
|
23
|
+
--api-url <url> API base URL (default: $API_URL or ${DEFAULT_API_URL})
|
|
24
|
+
-h, --help
|
|
25
|
+
`
|
|
26
|
+
|
|
27
|
+
async function main() {
|
|
28
|
+
const { values, positionals } = parseArgs({
|
|
29
|
+
allowPositionals: true,
|
|
30
|
+
options: {
|
|
31
|
+
"api-url": { type: "string" },
|
|
32
|
+
help: { type: "boolean", short: "h" },
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
const command = positionals[0]
|
|
36
|
+
if (values.help || !command) {
|
|
37
|
+
console.log(HELP)
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const apiUrl = (values["api-url"] ?? process.env.API_URL ?? DEFAULT_API_URL).replace(/\/$/, "")
|
|
42
|
+
const authUrl = `${apiUrl}${AUTH_PATH}`
|
|
43
|
+
const session = createSession({
|
|
44
|
+
storage: fileStorage(sessionPath()),
|
|
45
|
+
refresh: tokenEndpointRefresh(`${authUrl}/token`, CLIENT_ID),
|
|
46
|
+
})
|
|
47
|
+
const api = createApiClient({
|
|
48
|
+
baseUrl: apiUrl,
|
|
49
|
+
token: session.token,
|
|
50
|
+
refresh: session.refresh,
|
|
51
|
+
credentials: "omit",
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (command === "login") {
|
|
55
|
+
const res = await fetch(`${authUrl}/device/code`, {
|
|
56
|
+
method: "POST",
|
|
57
|
+
headers: { "Content-Type": "application/json" },
|
|
58
|
+
body: JSON.stringify({ client_id: CLIENT_ID }),
|
|
59
|
+
})
|
|
60
|
+
if (!res.ok) throw new Error(`Could not start sign-in (HTTP ${res.status})`)
|
|
61
|
+
const d = (await res.json()) as {
|
|
62
|
+
device_code: string
|
|
63
|
+
user_code: string
|
|
64
|
+
verification_uri_complete: string
|
|
65
|
+
expires_in: number
|
|
66
|
+
interval: number
|
|
67
|
+
}
|
|
68
|
+
console.log(
|
|
69
|
+
`Your code is ${ok(d.user_code)}\nApprove it at ${link(d.verification_uri_complete)}`,
|
|
70
|
+
)
|
|
71
|
+
console.log(dim("Waiting for approval..."))
|
|
72
|
+
const tokens = await pollDeviceToken({
|
|
73
|
+
tokenUrl: `${authUrl}/token`,
|
|
74
|
+
clientId: CLIENT_ID,
|
|
75
|
+
deviceCode: d.device_code,
|
|
76
|
+
interval: d.interval,
|
|
77
|
+
expiresIn: d.expires_in,
|
|
78
|
+
})
|
|
79
|
+
await session.set(fromTokenResponse(tokens))
|
|
80
|
+
console.log(ok("Signed in."))
|
|
81
|
+
} else if (command === "logout") {
|
|
82
|
+
const t = await session.get()
|
|
83
|
+
// The server revokes either token and always answers 200; the local copy goes regardless.
|
|
84
|
+
for (const token of [t?.refreshToken, t?.accessToken])
|
|
85
|
+
if (token)
|
|
86
|
+
await fetch(`${authUrl}/revoke`, {
|
|
87
|
+
method: "POST",
|
|
88
|
+
headers: { "Content-Type": "application/json" },
|
|
89
|
+
body: JSON.stringify({ token }),
|
|
90
|
+
}).catch(() => {})
|
|
91
|
+
await session.clear()
|
|
92
|
+
console.log(ok("Signed out."))
|
|
93
|
+
} else if (command === "whoami") {
|
|
94
|
+
if (!(await session.get())) throw new Error("Not signed in. Run login.")
|
|
95
|
+
console.log(JSON.stringify(await api.get(WHOAMI_PATH), null, 2))
|
|
96
|
+
} else {
|
|
97
|
+
throw new Error(`Unknown command "${command}".\n\n${HELP}`)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
main().catch((err: unknown) => {
|
|
102
|
+
console.error(fail(err instanceof Error ? err.message : String(err)))
|
|
103
|
+
process.exit(1)
|
|
104
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device flow's polling half (RFC 8628 section 3.5), apart from I/O so it
|
|
3
|
+
* tests against a fake fetch: wait `interval`, POST /token, and on
|
|
4
|
+
* `authorization_pending` wait again, on `slow_down` wait 5 seconds longer from
|
|
5
|
+
* then on, on `expired_token` or any other error stop.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code"
|
|
9
|
+
|
|
10
|
+
export type TokenBody = { access_token: string; refresh_token?: string; expires_in?: number }
|
|
11
|
+
|
|
12
|
+
export type PollOptions = {
|
|
13
|
+
tokenUrl: string
|
|
14
|
+
clientId: string
|
|
15
|
+
deviceCode: string
|
|
16
|
+
/** Seconds between polls, from the device-code response. */
|
|
17
|
+
interval: number
|
|
18
|
+
/** Seconds until the device code dies, from the same response. */
|
|
19
|
+
expiresIn: number
|
|
20
|
+
fetch?: typeof fetch
|
|
21
|
+
sleep?: (ms: number) => Promise<void>
|
|
22
|
+
now?: () => number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function pollDeviceToken(o: PollOptions): Promise<TokenBody> {
|
|
26
|
+
const doFetch = o.fetch ?? fetch
|
|
27
|
+
const sleep = o.sleep ?? ((ms) => new Promise((r) => setTimeout(r, ms)))
|
|
28
|
+
const now = o.now ?? Date.now
|
|
29
|
+
const deadline = now() + o.expiresIn * 1000
|
|
30
|
+
let interval = o.interval
|
|
31
|
+
|
|
32
|
+
for (;;) {
|
|
33
|
+
await sleep(interval * 1000)
|
|
34
|
+
if (now() >= deadline) throw new Error("The sign-in code expired. Run login again.")
|
|
35
|
+
const res = await doFetch(o.tokenUrl, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
38
|
+
body: new URLSearchParams({
|
|
39
|
+
grant_type: DEVICE_GRANT,
|
|
40
|
+
device_code: o.deviceCode,
|
|
41
|
+
client_id: o.clientId,
|
|
42
|
+
}),
|
|
43
|
+
})
|
|
44
|
+
if (res.ok) return (await res.json()) as TokenBody
|
|
45
|
+
const body = (await res.json().catch(() => ({}))) as {
|
|
46
|
+
error?: string
|
|
47
|
+
error_description?: string
|
|
48
|
+
}
|
|
49
|
+
if (body.error === "authorization_pending") continue
|
|
50
|
+
if (body.error === "slow_down") {
|
|
51
|
+
interval += 5
|
|
52
|
+
continue
|
|
53
|
+
}
|
|
54
|
+
if (body.error === "expired_token")
|
|
55
|
+
throw new Error("The sign-in code expired. Run login again.")
|
|
56
|
+
throw new Error(body.error_description ?? body.error ?? `Sign-in failed (HTTP ${res.status})`)
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ansi } from "@dapplelight/signet-tokens/ansi"
|
|
2
|
+
|
|
3
|
+
/** Colour only on a terminal, and never when NO_COLOR is set (https://no-color.org). */
|
|
4
|
+
const on = process.stdout.isTTY === true && !process.env.NO_COLOR
|
|
5
|
+
const theme = on
|
|
6
|
+
? ansi("editorial", {
|
|
7
|
+
mode: "dark",
|
|
8
|
+
depth: /truecolor|24bit/.test(process.env.COLORTERM ?? "") ? "truecolor" : "256",
|
|
9
|
+
})
|
|
10
|
+
: undefined
|
|
11
|
+
const paint = (token: string) => (s: string) => theme?.fg[token]?.(s) ?? s
|
|
12
|
+
|
|
13
|
+
export const ok = paint("accentGreen")
|
|
14
|
+
export const fail = paint("accentRed")
|
|
15
|
+
export const link = paint("accentBlue")
|
|
16
|
+
export const dim = paint("textMuted")
|