create-signet-app 0.4.1 → 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 +100 -12
- 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
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://schema.tauri.app/config/2",
|
|
3
|
+
"productName": "__APP_NAME__",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"identifier": "com.example.__APP_ID__",
|
|
6
|
+
"build": {
|
|
7
|
+
"beforeDevCommand": "__PM_RUN__ dev",
|
|
8
|
+
"devUrl": "http://localhost:1420",
|
|
9
|
+
"beforeBuildCommand": "__PM_RUN__ build",
|
|
10
|
+
"frontendDist": "../dist"
|
|
11
|
+
},
|
|
12
|
+
"app": {
|
|
13
|
+
"withGlobalTauri": false,
|
|
14
|
+
"windows": [
|
|
15
|
+
{
|
|
16
|
+
"label": "main",
|
|
17
|
+
"title": "__APP_NAME__",
|
|
18
|
+
"width": 900,
|
|
19
|
+
"height": 640
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"label": "tray",
|
|
23
|
+
"title": "__APP_NAME__",
|
|
24
|
+
"width": 320,
|
|
25
|
+
"height": 260,
|
|
26
|
+
"visible": false,
|
|
27
|
+
"decorations": false,
|
|
28
|
+
"resizable": false,
|
|
29
|
+
"alwaysOnTop": true,
|
|
30
|
+
"skipTaskbar": true
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"security": {
|
|
34
|
+
"csp": "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self' ipc: http://ipc.localhost http://localhost:* https:"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"bundle": {
|
|
38
|
+
"active": true,
|
|
39
|
+
"targets": "all",
|
|
40
|
+
"icon": ["icons/32x32.png", "icons/128x128.png", "icons/icon.icns", "icons/icon.ico"]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2023", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noUnusedLocals": true,
|
|
10
|
+
"noUnusedParameters": true,
|
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"moduleDetection": "force",
|
|
17
|
+
"noEmit": true,
|
|
18
|
+
"paths": { "@/*": ["./src/*"] },
|
|
19
|
+
"types": ["vite/client", "bun", "node"]
|
|
20
|
+
},
|
|
21
|
+
"include": ["src", "vite.config.ts"]
|
|
22
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import path from "node:path"
|
|
2
|
+
import tailwindcss from "@tailwindcss/vite"
|
|
3
|
+
import react from "@vitejs/plugin-react"
|
|
4
|
+
import { defineConfig } from "vite"
|
|
5
|
+
|
|
6
|
+
// Tauri loads devUrl from tauri.conf.json, so the port is fixed.
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
plugins: [react(), tailwindcss()],
|
|
9
|
+
resolve: { alias: { "@": path.resolve(import.meta.dirname, "./src") } },
|
|
10
|
+
clearScreen: false,
|
|
11
|
+
server: { port: 1420, strictPort: true, watch: { ignored: ["**/src-tauri/**"] } },
|
|
12
|
+
})
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# __APP_NAME__
|
|
2
|
+
|
|
3
|
+
A Chrome (Manifest V3) extension: a popup, an options page and a background
|
|
4
|
+
service worker, built with plain Vite. The UI is `@dapplelight/signet-ui` on a
|
|
5
|
+
`@dapplelight/signet-tokens` theme, with dark mode. Sign-in is authorization
|
|
6
|
+
code + PKCE through `chrome.identity.launchWebAuthFlow`, against the routes
|
|
7
|
+
`clientLoginRoutes` mounts in `@dapplelight/signet-server`.
|
|
8
|
+
|
|
9
|
+
## Load it
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
__PM_INSTALL__
|
|
13
|
+
__PM_RUN__ build
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
1. Open `chrome://extensions` and turn on Developer mode.
|
|
17
|
+
2. Load unpacked, and pick this project's `dist/` folder.
|
|
18
|
+
3. Copy the extension's ID from its card.
|
|
19
|
+
|
|
20
|
+
`__PM_RUN__ dev` rebuilds on every change; reload the extension on its card to
|
|
21
|
+
pick the new build up. Loading `dist/` from the same folder keeps the ID stable.
|
|
22
|
+
|
|
23
|
+
## Register it on the server
|
|
24
|
+
|
|
25
|
+
The server must know this extension before it will send it a code. In the
|
|
26
|
+
server that mounts `clientLoginRoutes` (see `@dapplelight/signet-server/auth/client-login`):
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
clients: {
|
|
30
|
+
"__APP_NAME__": {
|
|
31
|
+
redirectUris: ["https://<extension-id>.chromiumapp.org/"],
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
- The client id is `VITE_CLIENT_ID` (default: this project's name).
|
|
37
|
+
- The redirect URI is `https://<extension-id>.chromiumapp.org/`, with the ID
|
|
38
|
+
from step 3 and the trailing slash. It is matched exactly.
|
|
39
|
+
- `VITE_API_BASE` is where the server mounts those routes without `/auth`:
|
|
40
|
+
`https://api.example.com/api` for `app.route("/api/auth", clientLoginRoutes(...))`.
|
|
41
|
+
Copy `.env.example` to `.env` to change either variable.
|
|
42
|
+
- Put the same origin in `host_permissions` in `public/manifest.json`, or the
|
|
43
|
+
extension's requests to it are blocked.
|
|
44
|
+
|
|
45
|
+
The user signs in on your web app in the window the extension opens. If they
|
|
46
|
+
are not signed in there yet, set `loginUrl` on `clientLoginRoutes` so the
|
|
47
|
+
server sends them to your login page and back.
|
|
48
|
+
|
|
49
|
+
## Layout
|
|
50
|
+
|
|
51
|
+
- `public/manifest.json`: Permissions `storage` and `identity`; copied to `dist/` as is
|
|
52
|
+
- `popup.html`, `src/popup.tsx`: The toolbar popup
|
|
53
|
+
- `options.html`, `src/options.tsx`: The options page (theme, account)
|
|
54
|
+
- `src/background.ts`: The service worker. It runs sign-in, because the popup closes when the sign-in window opens
|
|
55
|
+
- `src/lib/auth.ts`: `signIn`, `signOut` and the session in `chrome.storage.local`
|
|
56
|
+
- `src/lib/api.ts`: `api`: a bearer API client that refreshes the token on a 401
|
|
57
|
+
- `src/lib/pkce.ts`: The S256 verifier and challenge, with the RFC 7636 test vector
|
|
58
|
+
|
|
59
|
+
Extension pages cannot run inline scripts, so the theme is applied before
|
|
60
|
+
first paint by `public/theme-init.js`, not a `<script>` in the page.
|
|
61
|
+
|
|
62
|
+
## Scripts
|
|
63
|
+
|
|
64
|
+
`__PM_RUN__ build`, `__PM_RUN__ dev`, `__PM_RUN__ lint`, `__PM_RUN__ typecheck`, `__PM_RUN__ test`.
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
- run: bun run build
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
"includes": ["**", "!**/routeTree.gen.ts"]
|
|
11
|
+
},
|
|
12
|
+
"formatter": {
|
|
13
|
+
"enabled": true,
|
|
14
|
+
"indentStyle": "space",
|
|
15
|
+
"indentWidth": 2,
|
|
16
|
+
"lineWidth": 100
|
|
17
|
+
},
|
|
18
|
+
"javascript": {
|
|
19
|
+
"formatter": {
|
|
20
|
+
"quoteStyle": "double",
|
|
21
|
+
"semicolons": "asNeeded"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"linter": {
|
|
25
|
+
"enabled": true,
|
|
26
|
+
"rules": {
|
|
27
|
+
"correctness": {
|
|
28
|
+
"useExhaustiveDependencies": "warn",
|
|
29
|
+
"useHookAtTopLevel": "error"
|
|
30
|
+
},
|
|
31
|
+
"suspicious": {
|
|
32
|
+
"noExplicitAny": "warn"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"assist": {
|
|
37
|
+
"actions": {
|
|
38
|
+
"source": {
|
|
39
|
+
"organizeImports": "on"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"css": {
|
|
44
|
+
"parser": {
|
|
45
|
+
"cssModules": false,
|
|
46
|
+
"allowWrongLineComments": false,
|
|
47
|
+
"tailwindDirectives": true
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
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__ options</title>
|
|
7
|
+
<script src="/theme-init.js"></script>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="root"></div>
|
|
11
|
+
<script type="module" src="/src/options.tsx"></script>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
<script src="/theme-init.js"></script>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<div id="root"></div>
|
|
11
|
+
<script type="module" src="/src/popup.tsx"></script>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"manifest_version": 3,
|
|
3
|
+
"name": "__APP_NAME__",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"description": "A browser extension built with Signet.",
|
|
6
|
+
"permissions": ["storage", "identity"],
|
|
7
|
+
"host_permissions": ["http://localhost:8787/*"],
|
|
8
|
+
"action": { "default_popup": "popup.html", "default_title": "__APP_NAME__" },
|
|
9
|
+
"options_ui": { "page": "options.html", "open_in_tab": true },
|
|
10
|
+
"background": { "service_worker": "background.js", "type": "module" }
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Applies the stored theme before first paint. MV3 forbids inline scripts, so
|
|
2
|
+
// this is a file, loaded as a classic script that runs before the page paints.
|
|
3
|
+
try {
|
|
4
|
+
const t = localStorage.getItem("signet-theme")
|
|
5
|
+
const d =
|
|
6
|
+
t === "dark" || ((!t || t === "system") && matchMedia("(prefers-color-scheme: dark)").matches)
|
|
7
|
+
document.documentElement.classList.toggle("dark", d)
|
|
8
|
+
document.documentElement.style.colorScheme = d ? "dark" : "light"
|
|
9
|
+
} catch {}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
@import "tailwindcss";
|
|
2
|
+
@import "@dapplelight/signet-tokens/__THEME__.css";
|
|
3
|
+
@import "@dapplelight/signet-tokens/base.css";
|
|
4
|
+
|
|
5
|
+
/* Tailwind does not scan node_modules, and @dapplelight/signet-ui ships compiled JS
|
|
6
|
+
whose class names must be seen or every component renders unstyled. The theme
|
|
7
|
+
cannot declare this — a package does not know where it is installed.
|
|
8
|
+
Must come after the @import rules: CSS requires imports first. */
|
|
9
|
+
@source "../node_modules/@dapplelight/signet-ui/dist";
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The service worker: no DOM here, so no window or document. It owns sign-in
|
|
2
|
+
// because the popup closes the moment the sign-in window takes focus, and a
|
|
3
|
+
// closed popup cannot finish the token exchange. Pages send a message and
|
|
4
|
+
// follow the result through session.subscribe().
|
|
5
|
+
import { signIn, signOut } from "./lib/auth"
|
|
6
|
+
|
|
7
|
+
type Message = { type: "signin" } | { type: "signout" }
|
|
8
|
+
export type Reply = { ok: true } | { ok: false; error: string }
|
|
9
|
+
|
|
10
|
+
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|
11
|
+
const m = message as Message
|
|
12
|
+
const run = m.type === "signin" ? signIn : m.type === "signout" ? signOut : null
|
|
13
|
+
if (!run) return undefined
|
|
14
|
+
run().then(
|
|
15
|
+
() => sendResponse({ ok: true } satisfies Reply),
|
|
16
|
+
(e: unknown) =>
|
|
17
|
+
sendResponse({
|
|
18
|
+
ok: false,
|
|
19
|
+
error: e instanceof Error ? e.message : String(e),
|
|
20
|
+
} satisfies Reply),
|
|
21
|
+
)
|
|
22
|
+
return true // the reply is async
|
|
23
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The few chrome.* calls this template makes, typed by shape so the template
|
|
2
|
+
// needs no @types/chrome. Swap in @types/chrome when you use more of the API.
|
|
3
|
+
declare const chrome: {
|
|
4
|
+
identity: {
|
|
5
|
+
getRedirectURL(path?: string): string
|
|
6
|
+
launchWebAuthFlow(details: { url: string; interactive: boolean }): Promise<string | undefined>
|
|
7
|
+
}
|
|
8
|
+
runtime: {
|
|
9
|
+
sendMessage(message: unknown): Promise<unknown>
|
|
10
|
+
openOptionsPage(): Promise<void>
|
|
11
|
+
onMessage: {
|
|
12
|
+
addListener(
|
|
13
|
+
listener: (
|
|
14
|
+
message: unknown,
|
|
15
|
+
sender: unknown,
|
|
16
|
+
sendResponse: (response: unknown) => void,
|
|
17
|
+
) => boolean | undefined,
|
|
18
|
+
): void
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the server is and who this extension is to it. Vite reads VITE_API_BASE
|
|
3
|
+
* and VITE_CLIENT_ID at build time (they end up in the bundle: nothing secret).
|
|
4
|
+
*
|
|
5
|
+
* API_BASE is the prefix `clientLoginRoutes` is mounted under, minus `/auth`,
|
|
6
|
+
* e.g. https://api.example.com/api for `app.route("/api/auth", ...)`.
|
|
7
|
+
* CLIENT_ID is this extension's key in the server's `clients` map. Put the
|
|
8
|
+
* server's origin in host_permissions in public/manifest.json.
|
|
9
|
+
*/
|
|
10
|
+
export const API_BASE: string = import.meta.env.VITE_API_BASE ?? "http://localhost:8787/api"
|
|
11
|
+
export const CLIENT_ID: string = import.meta.env.VITE_CLIENT_ID ?? "__APP_NAME__"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createApiClient } from "@dapplelight/signet-lib/api"
|
|
2
|
+
import { API_BASE } from "../config"
|
|
3
|
+
import { session } from "./auth"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Bearer calls to your API from the popup or options page: no cookies, and one
|
|
7
|
+
* retry with a refreshed token on a 401. Kept apart from auth.ts so the service
|
|
8
|
+
* worker, which only signs in and out, does not bundle the client.
|
|
9
|
+
*/
|
|
10
|
+
export const api = createApiClient({
|
|
11
|
+
baseUrl: API_BASE,
|
|
12
|
+
credentials: "omit",
|
|
13
|
+
token: session.token,
|
|
14
|
+
refresh: session.refresh,
|
|
15
|
+
})
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSession,
|
|
3
|
+
fromTokenResponse,
|
|
4
|
+
tokenEndpointRefresh,
|
|
5
|
+
} from "@dapplelight/signet-lib/session"
|
|
6
|
+
import { chromeStorage } from "@dapplelight/signet-lib/storage"
|
|
7
|
+
import { API_BASE, CLIENT_ID } from "../config"
|
|
8
|
+
import { pkceChallenge, randomString } from "./pkce"
|
|
9
|
+
|
|
10
|
+
// Tokens live in chrome.storage.local, shared by the popup, the options page
|
|
11
|
+
// and the service worker. A refresh the server refuses signs out.
|
|
12
|
+
export const session = createSession({
|
|
13
|
+
storage: chromeStorage(),
|
|
14
|
+
refresh: tokenEndpointRefresh(`${API_BASE}/auth/token`, CLIENT_ID),
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Authorization code + PKCE in a browser window. The redirect URI is
|
|
19
|
+
* https://<extension-id>.chromiumapp.org/: register it for CLIENT_ID in the
|
|
20
|
+
* server's `clients`. The user signs in on the web app first if they are not.
|
|
21
|
+
*/
|
|
22
|
+
export async function signIn(): Promise<void> {
|
|
23
|
+
const redirectUri = chrome.identity.getRedirectURL()
|
|
24
|
+
const verifier = randomString()
|
|
25
|
+
const state = randomString()
|
|
26
|
+
const url = new URL(`${API_BASE}/auth/authorize`)
|
|
27
|
+
url.search = new URLSearchParams({
|
|
28
|
+
client_id: CLIENT_ID,
|
|
29
|
+
redirect_uri: redirectUri,
|
|
30
|
+
response_type: "code",
|
|
31
|
+
code_challenge: await pkceChallenge(verifier),
|
|
32
|
+
code_challenge_method: "S256",
|
|
33
|
+
state,
|
|
34
|
+
}).toString()
|
|
35
|
+
|
|
36
|
+
const result = await chrome.identity.launchWebAuthFlow({ url: url.toString(), interactive: true })
|
|
37
|
+
const back = new URL(result ?? "")
|
|
38
|
+
if (back.searchParams.get("state") !== state) throw new Error("Sign-in state did not match")
|
|
39
|
+
const code = back.searchParams.get("code")
|
|
40
|
+
if (!code) throw new Error(back.searchParams.get("error") ?? "Sign-in was not completed")
|
|
41
|
+
|
|
42
|
+
const res = await fetch(`${API_BASE}/auth/token`, {
|
|
43
|
+
method: "POST",
|
|
44
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
45
|
+
body: new URLSearchParams({
|
|
46
|
+
grant_type: "authorization_code",
|
|
47
|
+
code,
|
|
48
|
+
code_verifier: verifier,
|
|
49
|
+
client_id: CLIENT_ID,
|
|
50
|
+
redirect_uri: redirectUri,
|
|
51
|
+
}),
|
|
52
|
+
})
|
|
53
|
+
if (!res.ok) throw new Error(`Token exchange failed (HTTP ${res.status})`)
|
|
54
|
+
await session.set(fromTokenResponse(await res.json()))
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Forgets the session, and asks the server to revoke it (always answers 200). */
|
|
58
|
+
export async function signOut(): Promise<void> {
|
|
59
|
+
const tokens = await session.get()
|
|
60
|
+
await session.clear()
|
|
61
|
+
if (tokens?.refreshToken)
|
|
62
|
+
await fetch(`${API_BASE}/auth/revoke`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
65
|
+
body: new URLSearchParams({ token: tokens.refreshToken }),
|
|
66
|
+
}).catch(() => {})
|
|
67
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { pkceChallenge, randomString } from "./pkce"
|
|
3
|
+
|
|
4
|
+
test("matches the RFC 7636 appendix B vector", async () => {
|
|
5
|
+
expect(await pkceChallenge("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk")).toBe(
|
|
6
|
+
"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
|
7
|
+
)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test("random strings are valid verifiers", () => {
|
|
11
|
+
expect(randomString()).toMatch(/^[A-Za-z0-9_-]{43}$/)
|
|
12
|
+
expect(randomString()).not.toBe(randomString())
|
|
13
|
+
})
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** PKCE (RFC 7636), S256 only: the one method signet-server's /authorize accepts. */
|
|
2
|
+
|
|
3
|
+
const b64url = (bytes: Uint8Array) =>
|
|
4
|
+
btoa(String.fromCharCode(...bytes))
|
|
5
|
+
.replaceAll("+", "-")
|
|
6
|
+
.replaceAll("/", "_")
|
|
7
|
+
.replace(/=+$/, "")
|
|
8
|
+
|
|
9
|
+
/** 32 random bytes as 43 unreserved characters (the spec asks for 43 to 128). */
|
|
10
|
+
export const randomString = () => b64url(crypto.getRandomValues(new Uint8Array(32)))
|
|
11
|
+
|
|
12
|
+
/** base64url(SHA-256(verifier)). */
|
|
13
|
+
export async function pkceChallenge(verifier: string): Promise<string> {
|
|
14
|
+
return b64url(
|
|
15
|
+
new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier))),
|
|
16
|
+
)
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react"
|
|
2
|
+
import type { Reply } from "../background"
|
|
3
|
+
import { session } from "./auth"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Signed in or not, for the popup and options page. Sign-in runs in the service
|
|
7
|
+
* worker (the popup closes when the sign-in window opens); the session watches
|
|
8
|
+
* chrome.storage, so its result reaches every open page through subscribe().
|
|
9
|
+
*/
|
|
10
|
+
export function useAuth() {
|
|
11
|
+
const [signedIn, setSignedIn] = useState<boolean | null>(null)
|
|
12
|
+
const [error, setError] = useState<string | null>(null)
|
|
13
|
+
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
session.get().then((t) => setSignedIn(t !== null))
|
|
16
|
+
return session.subscribe((t) => setSignedIn(t !== null))
|
|
17
|
+
}, [])
|
|
18
|
+
|
|
19
|
+
const ask = useCallback(async (type: "signin" | "signout") => {
|
|
20
|
+
setError(null)
|
|
21
|
+
const reply = (await chrome.runtime.sendMessage({ type })) as Reply | undefined
|
|
22
|
+
if (reply && !reply.ok) setError(reply.error)
|
|
23
|
+
}, [])
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
signedIn,
|
|
27
|
+
error,
|
|
28
|
+
signIn: () => ask("signin"),
|
|
29
|
+
signOut: () => ask("signout"),
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Button } from "@dapplelight/signet-ui/components/button"
|
|
2
|
+
import {
|
|
3
|
+
Card,
|
|
4
|
+
CardContent,
|
|
5
|
+
CardDescription,
|
|
6
|
+
CardHeader,
|
|
7
|
+
CardTitle,
|
|
8
|
+
} from "@dapplelight/signet-ui/components/card"
|
|
9
|
+
import { ThemeProvider } from "@dapplelight/signet-ui/components/theme-provider"
|
|
10
|
+
import { ThemeToggle } from "@dapplelight/signet-ui/components/theme-toggle"
|
|
11
|
+
import { StrictMode } from "react"
|
|
12
|
+
import { createRoot } from "react-dom/client"
|
|
13
|
+
import { useAuth } from "./lib/use-auth"
|
|
14
|
+
import "./app.css"
|
|
15
|
+
|
|
16
|
+
function Options() {
|
|
17
|
+
const { signedIn, error, signIn, signOut } = useAuth()
|
|
18
|
+
return (
|
|
19
|
+
<main className="mx-auto flex max-w-xl flex-col gap-4 p-6">
|
|
20
|
+
<Card>
|
|
21
|
+
<CardHeader>
|
|
22
|
+
<CardTitle>Appearance</CardTitle>
|
|
23
|
+
<CardDescription>Follows your system unless you pick one.</CardDescription>
|
|
24
|
+
</CardHeader>
|
|
25
|
+
<CardContent>
|
|
26
|
+
<ThemeToggle showLabels />
|
|
27
|
+
</CardContent>
|
|
28
|
+
</Card>
|
|
29
|
+
<Card>
|
|
30
|
+
<CardHeader>
|
|
31
|
+
<CardTitle>Account</CardTitle>
|
|
32
|
+
</CardHeader>
|
|
33
|
+
<CardContent className="flex flex-col items-start gap-3">
|
|
34
|
+
{signedIn === null ? null : signedIn ? (
|
|
35
|
+
<Button variant="outline" onClick={signOut}>
|
|
36
|
+
Sign out
|
|
37
|
+
</Button>
|
|
38
|
+
) : (
|
|
39
|
+
<Button onClick={signIn}>Sign in</Button>
|
|
40
|
+
)}
|
|
41
|
+
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
|
42
|
+
</CardContent>
|
|
43
|
+
</Card>
|
|
44
|
+
</main>
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const root = document.getElementById("root")
|
|
49
|
+
if (!root) throw new Error("#root not found in options.html")
|
|
50
|
+
createRoot(root).render(
|
|
51
|
+
<StrictMode>
|
|
52
|
+
<ThemeProvider>
|
|
53
|
+
<Options />
|
|
54
|
+
</ThemeProvider>
|
|
55
|
+
</StrictMode>,
|
|
56
|
+
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Button } from "@dapplelight/signet-ui/components/button"
|
|
2
|
+
import { Card, CardContent, CardHeader, CardTitle } from "@dapplelight/signet-ui/components/card"
|
|
3
|
+
import { ThemeProvider } from "@dapplelight/signet-ui/components/theme-provider"
|
|
4
|
+
import { StrictMode } from "react"
|
|
5
|
+
import { createRoot } from "react-dom/client"
|
|
6
|
+
import { useAuth } from "./lib/use-auth"
|
|
7
|
+
import "./app.css"
|
|
8
|
+
|
|
9
|
+
function Popup() {
|
|
10
|
+
const { signedIn, error, signIn, signOut } = useAuth()
|
|
11
|
+
return (
|
|
12
|
+
<Card className="w-72 rounded-none border-0">
|
|
13
|
+
<CardHeader>
|
|
14
|
+
<CardTitle>__APP_NAME__</CardTitle>
|
|
15
|
+
</CardHeader>
|
|
16
|
+
<CardContent className="flex flex-col gap-3">
|
|
17
|
+
{signedIn === null ? null : signedIn ? (
|
|
18
|
+
<>
|
|
19
|
+
<p className="text-sm text-muted-foreground">You are signed in.</p>
|
|
20
|
+
<Button variant="outline" onClick={() => chrome.runtime.openOptionsPage()}>
|
|
21
|
+
Options
|
|
22
|
+
</Button>
|
|
23
|
+
<Button variant="outline" onClick={signOut}>
|
|
24
|
+
Sign out
|
|
25
|
+
</Button>
|
|
26
|
+
</>
|
|
27
|
+
) : (
|
|
28
|
+
<Button onClick={signIn}>Sign in</Button>
|
|
29
|
+
)}
|
|
30
|
+
{error ? <p className="text-sm text-destructive">{error}</p> : null}
|
|
31
|
+
</CardContent>
|
|
32
|
+
</Card>
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const root = document.getElementById("root")
|
|
37
|
+
if (!root) throw new Error("#root not found in popup.html")
|
|
38
|
+
createRoot(root).render(
|
|
39
|
+
<StrictMode>
|
|
40
|
+
<ThemeProvider>
|
|
41
|
+
<Popup />
|
|
42
|
+
</ThemeProvider>
|
|
43
|
+
</StrictMode>,
|
|
44
|
+
)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"moduleResolution": "bundler",
|
|
7
|
+
"jsx": "react-jsx",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"noUnusedLocals": true,
|
|
10
|
+
"noUnusedParameters": true,
|
|
11
|
+
"noFallthroughCasesInSwitch": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
"allowImportingTsExtensions": true,
|
|
14
|
+
"verbatimModuleSyntax": true,
|
|
15
|
+
"isolatedModules": true,
|
|
16
|
+
"moduleDetection": "force",
|
|
17
|
+
"noEmit": true,
|
|
18
|
+
"types": ["vite/client", "bun"]
|
|
19
|
+
},
|
|
20
|
+
"include": ["src", "vite.config.ts"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import tailwindcss from "@tailwindcss/vite"
|
|
2
|
+
import react from "@vitejs/plugin-react"
|
|
3
|
+
import { defineConfig } from "vite"
|
|
4
|
+
|
|
5
|
+
// Plain multi-entry build, no extension plugin: two HTML pages and the service
|
|
6
|
+
// worker. public/ (manifest.json, theme-init.js) is copied to dist/ as is.
|
|
7
|
+
export default defineConfig({
|
|
8
|
+
plugins: [react(), tailwindcss()],
|
|
9
|
+
build: {
|
|
10
|
+
rollupOptions: {
|
|
11
|
+
input: {
|
|
12
|
+
popup: "popup.html",
|
|
13
|
+
options: "options.html",
|
|
14
|
+
background: "src/background.ts",
|
|
15
|
+
},
|
|
16
|
+
output: {
|
|
17
|
+
// manifest.json names the service worker, so it cannot carry a hash.
|
|
18
|
+
entryFileNames: (chunk) =>
|
|
19
|
+
chunk.name === "background" ? "background.js" : "assets/[name]-[hash].js",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
})
|