create-signet-app 0.4.1 → 0.4.5
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 +116 -14
- package/template/public/offline.html +20 -0
- package/template/src/lib/i18n.test.ts +11 -0
- package/template/src/lib/i18n.ts +31 -0
- package/template/src/lib/pwa.ts +59 -0
- package/template/src/locales/en.json +18 -0
- package/template/src/main.tsx +4 -0
- package/template/src/routes/index.tsx +7 -8
- package/template/src/routes/login.tsx +4 -4
- package/template/src/sw.ts +79 -0
- package/template/tsconfig.app.json +1 -1
- package/template/vite.config.ts +18 -0
- 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/template/vite.config.ts
CHANGED
|
@@ -3,6 +3,7 @@ import tailwindcss from "@tailwindcss/vite"
|
|
|
3
3
|
import { tanstackRouter } from "@tanstack/router-plugin/vite"
|
|
4
4
|
import react from "@vitejs/plugin-react"
|
|
5
5
|
import { defineConfig } from "vite"
|
|
6
|
+
import { VitePWA } from "vite-plugin-pwa"
|
|
6
7
|
|
|
7
8
|
export default defineConfig({
|
|
8
9
|
plugins: [
|
|
@@ -11,6 +12,23 @@ export default defineConfig({
|
|
|
11
12
|
tanstackRouter({ target: "react", autoCodeSplitting: true, quoteStyle: "double" }),
|
|
12
13
|
react(),
|
|
13
14
|
tailwindcss(),
|
|
15
|
+
// Installable app + offline: src/sw.ts is the service worker (precache,
|
|
16
|
+
// public/offline.html fallback, update on "Reload", web push). Add icons
|
|
17
|
+
// (192 and 512 px PNGs in public/) to the manifest for the install prompt.
|
|
18
|
+
VitePWA({
|
|
19
|
+
registerType: "prompt",
|
|
20
|
+
strategies: "injectManifest",
|
|
21
|
+
srcDir: "src",
|
|
22
|
+
filename: "sw.ts",
|
|
23
|
+
manifest: {
|
|
24
|
+
name: "__APP_NAME__",
|
|
25
|
+
short_name: "__APP_NAME__",
|
|
26
|
+
display: "standalone",
|
|
27
|
+
start_url: "/",
|
|
28
|
+
scope: "/",
|
|
29
|
+
icons: [],
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
14
32
|
],
|
|
15
33
|
resolve: {
|
|
16
34
|
alias: { "@": path.resolve(import.meta.dirname, "./src") },
|
|
@@ -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")
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { pollDeviceToken } from "../src/poll.ts"
|
|
3
|
+
|
|
4
|
+
/** A fake token endpoint that answers from a script and records each call. */
|
|
5
|
+
function fake(script: Array<{ status: number; body: object }>) {
|
|
6
|
+
const calls: URLSearchParams[] = []
|
|
7
|
+
const f = (async (_url: string, init: RequestInit) => {
|
|
8
|
+
calls.push(init.body as URLSearchParams)
|
|
9
|
+
const step = script[Math.min(calls.length, script.length) - 1] as (typeof script)[number]
|
|
10
|
+
return new Response(JSON.stringify(step.body), { status: step.status })
|
|
11
|
+
}) as unknown as typeof fetch
|
|
12
|
+
return { f, calls }
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function run(script: Parameters<typeof fake>[0], expiresIn = 600) {
|
|
16
|
+
const { f, calls } = fake(script)
|
|
17
|
+
const waits: number[] = []
|
|
18
|
+
let t = 0
|
|
19
|
+
const p = pollDeviceToken({
|
|
20
|
+
tokenUrl: "http://x/token",
|
|
21
|
+
clientId: "cli",
|
|
22
|
+
deviceCode: "dc",
|
|
23
|
+
interval: 5,
|
|
24
|
+
expiresIn,
|
|
25
|
+
fetch: f,
|
|
26
|
+
sleep: async (ms) => {
|
|
27
|
+
waits.push(ms / 1000)
|
|
28
|
+
t += ms
|
|
29
|
+
},
|
|
30
|
+
now: () => t,
|
|
31
|
+
})
|
|
32
|
+
return { p, calls, waits }
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const pending = { status: 400, body: { error: "authorization_pending" } }
|
|
36
|
+
const ok = { status: 200, body: { access_token: "a", refresh_token: "r", expires_in: 900 } }
|
|
37
|
+
|
|
38
|
+
describe("pollDeviceToken", () => {
|
|
39
|
+
test("pending then success, sending the device grant", async () => {
|
|
40
|
+
const { p, calls, waits } = run([pending, pending, ok])
|
|
41
|
+
expect((await p).access_token).toBe("a")
|
|
42
|
+
expect(waits).toEqual([5, 5, 5])
|
|
43
|
+
expect(calls[0]?.get("grant_type")).toBe("urn:ietf:params:oauth:grant-type:device_code")
|
|
44
|
+
expect(calls[0]?.get("device_code")).toBe("dc")
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test("slow_down adds 5 seconds to every later wait", async () => {
|
|
48
|
+
const { p, waits } = run([pending, { status: 400, body: { error: "slow_down" } }, pending, ok])
|
|
49
|
+
await p
|
|
50
|
+
expect(waits).toEqual([5, 5, 10, 10])
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test("expired_token stops", async () => {
|
|
54
|
+
const { p } = run([pending, { status: 400, body: { error: "expired_token" } }])
|
|
55
|
+
await expect(p).rejects.toThrow(/expired/)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test("gives up at expires_in even if the server keeps saying pending", async () => {
|
|
59
|
+
const { p, calls } = run([pending], 12)
|
|
60
|
+
await expect(p).rejects.toThrow(/expired/)
|
|
61
|
+
expect(calls.length).toBe(2)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
test("any other error surfaces its message", async () => {
|
|
65
|
+
const { p } = run([{ status: 400, body: { error: "access_denied", error_description: "No." } }])
|
|
66
|
+
await expect(p).rejects.toThrow("No.")
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"types": ["bun"],
|
|
8
|
+
"strict": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"isolatedModules": true,
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"noEmit": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src", "*.ts"]
|
|
15
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# __APP_NAME__
|
|
2
|
+
|
|
3
|
+
A Tauri v2 desktop and menu-bar app: React + Vite in the webview, styled with Signet's tokens and UI, signed in through the system browser.
|
|
4
|
+
|
|
5
|
+
## Run
|
|
6
|
+
|
|
7
|
+
You need [Rust](https://rustup.rs) and [Tauri's system dependencies](https://tauri.app/start/prerequisites/).
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
__PM_INSTALL__
|
|
11
|
+
__PM_X__ tauri dev
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`__PM_RUN__ dev` alone runs just the webview (no sign-in or tray; those need Tauri).
|
|
15
|
+
|
|
16
|
+
## What is here
|
|
17
|
+
|
|
18
|
+
- `src/` the webview. `src/lib/auth.ts` is sign-in; `src/lib/pkce.ts` the PKCE helper.
|
|
19
|
+
- `src-tauri/src/main.rs` the tray icon (left click toggles the small `tray` window, right click opens the menu), the two windows and the loopback listener.
|
|
20
|
+
- `src-tauri/tauri.conf.json` windows, CSP and bundling. The identifier `com.example.__APP_ID__` is a placeholder: set your own before you ship.
|
|
21
|
+
- `src-tauri/capabilities/default.json` what the windows may call. Narrow the `opener:allow-open-url` scope to your API's origin.
|
|
22
|
+
- `src-tauri/icons/` placeholder icons. Replace them: `__PM_X__ tauri icon path/to/logo.png`.
|
|
23
|
+
- `.github/workflows/ci.yml` lint, typecheck and test, then an installer build on macOS, Windows and Linux.
|
|
24
|
+
|
|
25
|
+
## Sign-in
|
|
26
|
+
|
|
27
|
+
Authorization code + PKCE (S256) against signet-server's `clientLoginRoutes`, mounted at `<API>/auth`. The app opens `/authorize` in your browser (you must be signed in to the web app there), the server redirects to `http://127.0.0.1:<port>/callback`, a one-shot listener in Rust hands the code to the webview, and the webview swaps it for tokens. Tokens are kept in the Tauri store (`auth.json`) through `createSession`.
|
|
28
|
+
|
|
29
|
+
Register the client on the server:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
clients: { "__APP_ID__-desktop": { redirectUris: ["http://127.0.0.1/callback"] } }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The server matches a port-less loopback URI on any port. Set `VITE_API_BASE` (default `http://localhost:8787/api`) to your API. The webview calls the API cross-origin, so the server's CORS must allow the webview's origin (`tauri://localhost` on macOS and Linux, `http://tauri.localhost` on Windows). In `tauri.conf.json`, narrow the CSP's `connect-src https:` to your API's origin.
|
|
36
|
+
|
|
37
|
+
## Release
|
|
38
|
+
|
|
39
|
+
CI builds unsigned installers. Code signing and notarization (Apple, Windows) are yours to add; see [Tauri's distribution guide](https://tauri.app/distribute/).
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
verify:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: oven-sh/setup-bun@v2
|
|
17
|
+
# Commit bun.lock: --frozen-lockfile fails the build if it is missing or stale.
|
|
18
|
+
- run: bun install --frozen-lockfile
|
|
19
|
+
- run: bun run lint
|
|
20
|
+
- run: bun run typecheck
|
|
21
|
+
- run: bun run test
|
|
22
|
+
|
|
23
|
+
# The Rust half: builds the installer on each OS (no signing, no release).
|
|
24
|
+
build:
|
|
25
|
+
needs: verify
|
|
26
|
+
strategy:
|
|
27
|
+
fail-fast: false
|
|
28
|
+
matrix:
|
|
29
|
+
os: [macos-latest, windows-latest, ubuntu-latest]
|
|
30
|
+
runs-on: ${{ matrix.os }}
|
|
31
|
+
steps:
|
|
32
|
+
- uses: actions/checkout@v4
|
|
33
|
+
- uses: oven-sh/setup-bun@v2
|
|
34
|
+
- uses: dtolnay/rust-toolchain@stable
|
|
35
|
+
- uses: Swatinem/rust-cache@v2
|
|
36
|
+
with:
|
|
37
|
+
workspaces: src-tauri
|
|
38
|
+
- name: Linux system libraries
|
|
39
|
+
if: matrix.os == 'ubuntu-latest'
|
|
40
|
+
run: |
|
|
41
|
+
sudo apt-get update
|
|
42
|
+
sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
|
|
43
|
+
- run: bun install --frozen-lockfile
|
|
44
|
+
- run: bunx tauri build
|
|
45
|
+
- uses: actions/upload-artifact@v4
|
|
46
|
+
with:
|
|
47
|
+
name: __APP_ID__-${{ matrix.os }}
|
|
48
|
+
path: src-tauri/target/release/bundle/**
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"vcs": {
|
|
3
|
+
"enabled": true,
|
|
4
|
+
"clientKind": "git",
|
|
5
|
+
"useIgnoreFile": true
|
|
6
|
+
},
|
|
7
|
+
"files": {
|
|
8
|
+
"ignoreUnknown": true,
|
|
9
|
+
"includes": ["**", "!**/routeTree.gen.ts"]
|
|
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,16 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>__APP_NAME__</title>
|
|
7
|
+
<!-- Applies the stored theme before first paint; React mounts too late. -->
|
|
8
|
+
<script>
|
|
9
|
+
(() => { try { const t = localStorage.getItem("signet-theme"); const d = t === "dark" || ((!t || t === "system") && matchMedia("(prefers-color-scheme: dark)").matches); document.documentElement.classList.toggle("dark", d); document.documentElement.style.colorScheme = d ? "dark" : "light" } catch {} })()
|
|
10
|
+
</script>
|
|
11
|
+
</head>
|
|
12
|
+
<body>
|
|
13
|
+
<div id="root"></div>
|
|
14
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
15
|
+
</body>
|
|
16
|
+
</html>
|