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
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { Button } from "@dapplelight/signet-ui/components/button"
|
|
2
|
+
import { Card, CardContent, CardHeader, CardTitle } from "@dapplelight/signet-ui/components/card"
|
|
3
|
+
import { ThemeToggle } from "@dapplelight/signet-ui/components/theme-toggle"
|
|
4
|
+
import { getCurrentWindow } from "@tauri-apps/api/window"
|
|
5
|
+
import { useEffect, useState } from "react"
|
|
6
|
+
import { session, signIn, signOut } from "./lib/auth"
|
|
7
|
+
|
|
8
|
+
/** null while the stored session loads. */
|
|
9
|
+
function useSignedIn(): boolean | null {
|
|
10
|
+
const [signedIn, setSignedIn] = useState<boolean | null>(null)
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
session.get().then((t) => setSignedIn(t !== null))
|
|
13
|
+
return session.subscribe((t) => setSignedIn(t !== null))
|
|
14
|
+
}, [])
|
|
15
|
+
return signedIn
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function App() {
|
|
19
|
+
const signedIn = useSignedIn()
|
|
20
|
+
const [error, setError] = useState<string>()
|
|
21
|
+
// One flow at a time: a second click would start a second PKCE flow.
|
|
22
|
+
const [busy, setBusy] = useState(false)
|
|
23
|
+
// One webview app, two windows: the small tray window and the main window.
|
|
24
|
+
const compact = getCurrentWindow().label === "tray"
|
|
25
|
+
|
|
26
|
+
const run = (fn: () => Promise<void>) => {
|
|
27
|
+
setError(undefined)
|
|
28
|
+
setBusy(true)
|
|
29
|
+
fn()
|
|
30
|
+
.catch((e: unknown) => setError(e instanceof Error ? e.message : String(e)))
|
|
31
|
+
.finally(() => setBusy(false))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<main className={compact ? "p-3" : "mx-auto max-w-md p-8"}>
|
|
36
|
+
<Card>
|
|
37
|
+
<CardHeader className="flex-row items-center justify-between">
|
|
38
|
+
<CardTitle>__APP_NAME__</CardTitle>
|
|
39
|
+
<ThemeToggle />
|
|
40
|
+
</CardHeader>
|
|
41
|
+
<CardContent className="space-y-3">
|
|
42
|
+
<p className="text-sm text-muted-foreground">
|
|
43
|
+
{signedIn === null ? "Loading..." : signedIn ? "Signed in." : "Signed out."}
|
|
44
|
+
</p>
|
|
45
|
+
{signedIn ? (
|
|
46
|
+
<Button variant="outline" disabled={busy} onClick={() => run(signOut)}>
|
|
47
|
+
Sign out
|
|
48
|
+
</Button>
|
|
49
|
+
) : (
|
|
50
|
+
<Button disabled={signedIn === null || busy} onClick={() => run(signIn)}>
|
|
51
|
+
Sign in with your browser
|
|
52
|
+
</Button>
|
|
53
|
+
)}
|
|
54
|
+
{error && <p className="text-sm text-destructive">{error}</p>}
|
|
55
|
+
</CardContent>
|
|
56
|
+
</Card>
|
|
57
|
+
</main>
|
|
58
|
+
)
|
|
59
|
+
}
|
|
@@ -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,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign-in against signet-server's `clientLoginRoutes` (mounted at `<API>/auth`):
|
|
3
|
+
* authorization code + PKCE through the system browser, with the code returned
|
|
4
|
+
* to a one-shot loopback listener in Rust (`listen_for_callback`). The server
|
|
5
|
+
* matches a port-less registered `http://127.0.0.1/callback` on any port.
|
|
6
|
+
*
|
|
7
|
+
* Tokens live in the Tauri store (`auth.json`) behind `createSession`.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { createApiClient } from "@dapplelight/signet-lib/api"
|
|
11
|
+
import {
|
|
12
|
+
createSession,
|
|
13
|
+
fromTokenResponse,
|
|
14
|
+
tokenEndpointRefresh,
|
|
15
|
+
} from "@dapplelight/signet-lib/session"
|
|
16
|
+
import { tauriStorage } from "@dapplelight/signet-lib/storage"
|
|
17
|
+
import { invoke } from "@tauri-apps/api/core"
|
|
18
|
+
import { listen } from "@tauri-apps/api/event"
|
|
19
|
+
import { openUrl } from "@tauri-apps/plugin-opener"
|
|
20
|
+
import { LazyStore } from "@tauri-apps/plugin-store"
|
|
21
|
+
import { createPkce } from "./pkce"
|
|
22
|
+
|
|
23
|
+
export const API_BASE: string = import.meta.env.VITE_API_BASE ?? "http://localhost:8787/api"
|
|
24
|
+
const AUTH = `${API_BASE}/auth`
|
|
25
|
+
/** Register this id on the server with redirectUris ["http://127.0.0.1/callback"]. */
|
|
26
|
+
export const CLIENT_ID = "__APP_ID__-desktop"
|
|
27
|
+
const TIMEOUT_MS = 5 * 60_000
|
|
28
|
+
|
|
29
|
+
export const session = createSession({
|
|
30
|
+
storage: tauriStorage(new LazyStore("auth.json")),
|
|
31
|
+
refresh: tokenEndpointRefresh(`${AUTH}/token`, CLIENT_ID),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
export const api = createApiClient({
|
|
35
|
+
baseUrl: API_BASE,
|
|
36
|
+
token: session.token,
|
|
37
|
+
refresh: session.refresh,
|
|
38
|
+
credentials: "omit",
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
export async function signIn(): Promise<void> {
|
|
42
|
+
const { verifier, challenge, state } = await createPkce()
|
|
43
|
+
let onCallback: (query: string) => void = () => {}
|
|
44
|
+
const callback = new Promise<string>((resolve) => {
|
|
45
|
+
onCallback = resolve
|
|
46
|
+
})
|
|
47
|
+
const unlisten = await listen<string>("oauth-callback", (e) => onCallback(e.payload))
|
|
48
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
49
|
+
try {
|
|
50
|
+
const port = await invoke<number>("listen_for_callback")
|
|
51
|
+
const redirectUri = `http://127.0.0.1:${port}/callback`
|
|
52
|
+
const url = new URL(`${AUTH}/authorize`)
|
|
53
|
+
url.search = new URLSearchParams({
|
|
54
|
+
response_type: "code",
|
|
55
|
+
client_id: CLIENT_ID,
|
|
56
|
+
redirect_uri: redirectUri,
|
|
57
|
+
code_challenge: challenge,
|
|
58
|
+
code_challenge_method: "S256",
|
|
59
|
+
state,
|
|
60
|
+
}).toString()
|
|
61
|
+
await openUrl(url.toString())
|
|
62
|
+
|
|
63
|
+
const timeout = new Promise<never>((_, reject) => {
|
|
64
|
+
timer = setTimeout(() => reject(new Error("Sign-in timed out")), TIMEOUT_MS)
|
|
65
|
+
})
|
|
66
|
+
const query = new URLSearchParams(await Promise.race([callback, timeout]))
|
|
67
|
+
if (query.get("state") !== state) throw new Error("Sign-in state mismatch")
|
|
68
|
+
const code = query.get("code")
|
|
69
|
+
if (!code)
|
|
70
|
+
throw new Error(query.get("error_description") ?? query.get("error") ?? "Sign-in failed")
|
|
71
|
+
|
|
72
|
+
const res = await fetch(`${AUTH}/token`, {
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
75
|
+
body: new URLSearchParams({
|
|
76
|
+
grant_type: "authorization_code",
|
|
77
|
+
code,
|
|
78
|
+
code_verifier: verifier,
|
|
79
|
+
redirect_uri: redirectUri,
|
|
80
|
+
client_id: CLIENT_ID,
|
|
81
|
+
}),
|
|
82
|
+
})
|
|
83
|
+
if (!res.ok) throw new Error(`Sign-in failed (HTTP ${res.status})`)
|
|
84
|
+
await session.set(fromTokenResponse(await res.json()))
|
|
85
|
+
} finally {
|
|
86
|
+
clearTimeout(timer)
|
|
87
|
+
unlisten()
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Revokes the session on the server (best effort), then forgets the tokens. */
|
|
92
|
+
export async function signOut(): Promise<void> {
|
|
93
|
+
const refreshToken = (await session.get())?.refreshToken
|
|
94
|
+
if (refreshToken) {
|
|
95
|
+
await fetch(`${AUTH}/revoke`, {
|
|
96
|
+
method: "POST",
|
|
97
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
98
|
+
body: new URLSearchParams({ token: refreshToken }),
|
|
99
|
+
}).catch(() => {})
|
|
100
|
+
}
|
|
101
|
+
await session.clear()
|
|
102
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** PKCE (RFC 7636, S256) and a state value, from the webview's WebCrypto. */
|
|
2
|
+
|
|
3
|
+
const b64url = (bytes: Uint8Array) =>
|
|
4
|
+
btoa(String.fromCharCode(...bytes))
|
|
5
|
+
.replaceAll("+", "-")
|
|
6
|
+
.replaceAll("/", "_")
|
|
7
|
+
.replace(/=+$/, "")
|
|
8
|
+
|
|
9
|
+
/** 32 random bytes, base64url: 43 characters, inside the verifier's 43-128. */
|
|
10
|
+
export const randomToken = () => b64url(crypto.getRandomValues(new Uint8Array(32)))
|
|
11
|
+
|
|
12
|
+
/** base64url(SHA-256(verifier)), the `code_challenge` for `code_challenge_method=S256`. */
|
|
13
|
+
export async function pkceChallenge(verifier: string): Promise<string> {
|
|
14
|
+
const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(verifier))
|
|
15
|
+
return b64url(new Uint8Array(digest))
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function createPkce() {
|
|
19
|
+
const verifier = randomToken()
|
|
20
|
+
return { verifier, challenge: await pkceChallenge(verifier), state: randomToken() }
|
|
21
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ThemeProvider } from "@dapplelight/signet-ui/components/theme-provider"
|
|
2
|
+
import { StrictMode } from "react"
|
|
3
|
+
import { createRoot } from "react-dom/client"
|
|
4
|
+
import { App } from "./App"
|
|
5
|
+
import "./app.css"
|
|
6
|
+
|
|
7
|
+
const rootElement = document.getElementById("root")
|
|
8
|
+
if (!rootElement) throw new Error("Root element #root not found in index.html")
|
|
9
|
+
|
|
10
|
+
createRoot(rootElement).render(
|
|
11
|
+
<StrictMode>
|
|
12
|
+
<ThemeProvider>
|
|
13
|
+
<App />
|
|
14
|
+
</ThemeProvider>
|
|
15
|
+
</StrictMode>,
|
|
16
|
+
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { expect, test } from "bun:test"
|
|
2
|
+
import { createPkce, pkceChallenge } from "./lib/pkce"
|
|
3
|
+
|
|
4
|
+
test("S256 challenge matches the RFC 7636 appendix B vector", async () => {
|
|
5
|
+
expect(await pkceChallenge("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk")).toBe(
|
|
6
|
+
"E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
|
|
7
|
+
)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test("verifier is 43 unreserved characters and state differs from it", async () => {
|
|
11
|
+
const { verifier, challenge, state } = await createPkce()
|
|
12
|
+
expect(verifier).toMatch(/^[A-Za-z0-9_-]{43}$/)
|
|
13
|
+
expect(challenge).toMatch(/^[A-Za-z0-9_-]{43}$/)
|
|
14
|
+
expect(state).not.toBe(verifier)
|
|
15
|
+
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "__APP_ID__"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "__APP_NAME__"
|
|
5
|
+
edition = "2021"
|
|
6
|
+
|
|
7
|
+
[build-dependencies]
|
|
8
|
+
tauri-build = { version = "2", features = [] }
|
|
9
|
+
|
|
10
|
+
[dependencies]
|
|
11
|
+
tauri = { version = "2", features = ["tray-icon"] }
|
|
12
|
+
tauri-plugin-opener = "2"
|
|
13
|
+
tauri-plugin-store = "2"
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../gen/schemas/desktop-schema.json",
|
|
3
|
+
"identifier": "default",
|
|
4
|
+
"description": "What the main and tray windows may do. Nothing else is allowed.",
|
|
5
|
+
"windows": ["main", "tray"],
|
|
6
|
+
"permissions": [
|
|
7
|
+
"core:default",
|
|
8
|
+
{
|
|
9
|
+
"identifier": "opener:allow-open-url",
|
|
10
|
+
"allow": [{ "url": "https://*" }, { "url": "http://localhost:*" }]
|
|
11
|
+
},
|
|
12
|
+
"store:allow-load",
|
|
13
|
+
"store:allow-get",
|
|
14
|
+
"store:allow-set",
|
|
15
|
+
"store:allow-delete",
|
|
16
|
+
"store:allow-save"
|
|
17
|
+
]
|
|
18
|
+
}
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// No console window on Windows release builds.
|
|
2
|
+
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
3
|
+
|
|
4
|
+
use std::{
|
|
5
|
+
io::{ErrorKind, Read, Write},
|
|
6
|
+
net::TcpListener,
|
|
7
|
+
thread,
|
|
8
|
+
time::{Duration, Instant},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
use tauri::{
|
|
12
|
+
menu::{Menu, MenuItem},
|
|
13
|
+
tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
|
|
14
|
+
Emitter, Manager, WindowEvent,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const CALLBACK_PAGE: &str = "<!doctype html><meta charset=utf-8><title>Signed in</title>\
|
|
18
|
+
<p style=\"font-family:system-ui;margin:3rem\">Signed in. You can close this tab.</p>";
|
|
19
|
+
|
|
20
|
+
fn reply(stream: &mut std::net::TcpStream, status: &str, body: &str) {
|
|
21
|
+
let _ = write!(
|
|
22
|
+
stream,
|
|
23
|
+
"HTTP/1.1 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
|
24
|
+
body.len()
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// Sign-in step: bind a loopback port for the OAuth redirect and return it. The
|
|
29
|
+
/// first `GET /callback?...` is emitted to the webview as `oauth-callback`
|
|
30
|
+
/// (the raw query string), then the listener closes; so does it after 5 minutes.
|
|
31
|
+
#[tauri::command]
|
|
32
|
+
fn listen_for_callback(app: tauri::AppHandle) -> Result<u16, String> {
|
|
33
|
+
let listener = TcpListener::bind("127.0.0.1:0").map_err(|e| e.to_string())?;
|
|
34
|
+
listener.set_nonblocking(true).map_err(|e| e.to_string())?;
|
|
35
|
+
let port = listener.local_addr().map_err(|e| e.to_string())?.port();
|
|
36
|
+
thread::spawn(move || {
|
|
37
|
+
let deadline = Instant::now() + Duration::from_secs(300);
|
|
38
|
+
while Instant::now() < deadline {
|
|
39
|
+
match listener.accept() {
|
|
40
|
+
Ok((mut stream, _)) => {
|
|
41
|
+
let _ = stream.set_nonblocking(false);
|
|
42
|
+
let _ = stream.set_read_timeout(Some(Duration::from_secs(5)));
|
|
43
|
+
let mut buf = [0u8; 4096];
|
|
44
|
+
let n = stream.read(&mut buf).unwrap_or(0);
|
|
45
|
+
let request = String::from_utf8_lossy(&buf[..n]);
|
|
46
|
+
let target = request.split_whitespace().nth(1).unwrap_or("");
|
|
47
|
+
if let Some(query) = target.strip_prefix("/callback?") {
|
|
48
|
+
let _ = app.emit("oauth-callback", query.to_string());
|
|
49
|
+
reply(&mut stream, "200 OK", CALLBACK_PAGE);
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
reply(&mut stream, "404 Not Found", "");
|
|
53
|
+
}
|
|
54
|
+
Err(e) if e.kind() == ErrorKind::WouldBlock => {
|
|
55
|
+
thread::sleep(Duration::from_millis(100))
|
|
56
|
+
}
|
|
57
|
+
Err(_) => break,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
Ok(port)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fn show(app: &tauri::AppHandle, label: &str) {
|
|
65
|
+
if let Some(window) = app.get_webview_window(label) {
|
|
66
|
+
let _ = window.show();
|
|
67
|
+
let _ = window.set_focus();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
fn main() {
|
|
72
|
+
tauri::Builder::default()
|
|
73
|
+
.plugin(tauri_plugin_opener::init())
|
|
74
|
+
.plugin(tauri_plugin_store::Builder::new().build())
|
|
75
|
+
.invoke_handler(tauri::generate_handler![listen_for_callback])
|
|
76
|
+
.setup(|app| {
|
|
77
|
+
let open = MenuItem::with_id(app, "open", "Open", true, None::<&str>)?;
|
|
78
|
+
let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
|
|
79
|
+
let menu = Menu::with_items(app, &[&open, &quit])?;
|
|
80
|
+
TrayIconBuilder::new()
|
|
81
|
+
.icon(app.default_window_icon().expect("bundle icon").clone())
|
|
82
|
+
.menu(&menu)
|
|
83
|
+
.show_menu_on_left_click(false)
|
|
84
|
+
.on_menu_event(|app, event| match event.id.as_ref() {
|
|
85
|
+
"open" => show(app, "main"),
|
|
86
|
+
"quit" => app.exit(0),
|
|
87
|
+
_ => {}
|
|
88
|
+
})
|
|
89
|
+
// Left click toggles the small window; right click opens the menu.
|
|
90
|
+
// ponytail: not positioned under the icon; add a positioner when it matters.
|
|
91
|
+
.on_tray_icon_event(|tray, event| {
|
|
92
|
+
if let TrayIconEvent::Click {
|
|
93
|
+
button: MouseButton::Left,
|
|
94
|
+
button_state: MouseButtonState::Up,
|
|
95
|
+
..
|
|
96
|
+
} = event
|
|
97
|
+
{
|
|
98
|
+
let app = tray.app_handle();
|
|
99
|
+
match app.get_webview_window("tray") {
|
|
100
|
+
Some(w) if w.is_visible().unwrap_or(false) => {
|
|
101
|
+
let _ = w.hide();
|
|
102
|
+
}
|
|
103
|
+
_ => show(app, "tray"),
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
})
|
|
107
|
+
.build(app)?;
|
|
108
|
+
Ok(())
|
|
109
|
+
})
|
|
110
|
+
.on_window_event(|window, event| match (window.label(), event) {
|
|
111
|
+
// The tray window hides when it loses focus; closing the main window
|
|
112
|
+
// keeps the app running in the tray (Quit is in the tray menu).
|
|
113
|
+
("tray", WindowEvent::Focused(false)) => {
|
|
114
|
+
let _ = window.hide();
|
|
115
|
+
}
|
|
116
|
+
("main", WindowEvent::CloseRequested { api, .. }) => {
|
|
117
|
+
api.prevent_close();
|
|
118
|
+
let _ = window.hide();
|
|
119
|
+
}
|
|
120
|
+
_ => {}
|
|
121
|
+
})
|
|
122
|
+
.run(tauri::generate_context!())
|
|
123
|
+
.expect("error while running the application");
|
|
124
|
+
}
|
|
@@ -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>
|