@basaltkit/deploy-ui 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/dist/cli.d.ts +2 -1
- package/dist/cli.js +15 -2
- package/dist/client/main.js +68 -23
- package/dist/server.d.ts +5 -1
- package/dist/server.js +2 -2
- package/dist/session.d.ts +16 -3
- package/dist/session.js +39 -6
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@ secrets.
|
|
|
38
38
|
| `--targets=<file>` | the targets file (default below) |
|
|
39
39
|
| `--ssh=<absolute path>` | the ssh client (default: `ssh` from `PATH`; on Windows `%SystemRoot%\System32\OpenSSH\ssh.exe`, else the first `ssh.exe` in an absolute `PATH` entry, never the current folder) |
|
|
40
40
|
| `--no-open` | print the URL instead of opening the browser |
|
|
41
|
+
| `--pair` | open the bare URL and type a short code the terminal shows (shared machines) |
|
|
41
42
|
| `--idle-minutes=<n>` | stop after n idle minutes (1–240, default 15) |
|
|
42
43
|
|
|
43
44
|
There is no `--host` and no `--port`: see the security model.
|
|
@@ -158,7 +159,10 @@ every page your browser opens. So:
|
|
|
158
159
|
|
|
159
160
|
Limits: on a machine shared with other users, the launch code is visible in the
|
|
160
161
|
browser opener's arguments for an instant — a claim by anyone else makes your
|
|
161
|
-
tab fail loudly and the terminal say so
|
|
162
|
+
tab fail loudly and the terminal say so. There, start it with `--pair`: the
|
|
163
|
+
browser opens the bare URL and you type an 8-character code the terminal
|
|
164
|
+
shows (no look-alike letters, 2 minutes, 5 tries, then the UI must restart),
|
|
165
|
+
so nothing secret passes through another process's arguments (RFC 0009 Q1). JavaScript strings
|
|
162
166
|
cannot be wiped from memory. The server's proof is checked just before an
|
|
163
167
|
answer is sent, not inside the same request. After a reload, the page you get
|
|
164
168
|
is whatever answers on the port. When the terminal says the UI stopped, close
|
package/dist/cli.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import type { Spawn, SshRunner } from './remote.js';
|
|
2
2
|
import { type UiServer, type UiServerOptions } from './server.js';
|
|
3
|
-
export declare const USAGE = "basalt-deploy-ui \u2014 a local UI for @basaltkit/deploy (RFC 0009)\n\nRuns on YOUR machine, never on the server. Listens on 127.0.0.1 only and drives\neach server over your own ssh (keys, agent, known_hosts), as the app user.\n\n npx @basaltkit/deploy-ui@<exact version> [options]\n\nOptions:\n --targets=<file> the targets file (default: $XDG_CONFIG_HOME/basalt/deploy-targets.json,\n %APPDATA%\\basalt\\deploy-targets.json on Windows)\n --ssh=<absolute path> the ssh client (default: ssh from PATH; on Windows\n %SystemRoot%\\System32\\OpenSSH\\ssh.exe, else the first ssh.exe\n in an absolute PATH entry \u2014 never the current folder)\n --no-open print the URL instead of opening the browser\n --idle-minutes=<n> stop after n idle minutes (1\u2013240, default 15)\n -h, --help\n\nNever add @basaltkit/deploy-ui to an app's package.json: the deploy installs an\napp's devDependencies on the server.\n";
|
|
3
|
+
export declare const USAGE = "basalt-deploy-ui \u2014 a local UI for @basaltkit/deploy (RFC 0009)\n\nRuns on YOUR machine, never on the server. Listens on 127.0.0.1 only and drives\neach server over your own ssh (keys, agent, known_hosts), as the app user.\n\n npx @basaltkit/deploy-ui@<exact version> [options]\n\nOptions:\n --targets=<file> the targets file (default: $XDG_CONFIG_HOME/basalt/deploy-targets.json,\n %APPDATA%\\basalt\\deploy-targets.json on Windows)\n --ssh=<absolute path> the ssh client (default: ssh from PATH; on Windows\n %SystemRoot%\\System32\\OpenSSH\\ssh.exe, else the first ssh.exe\n in an absolute PATH entry \u2014 never the current folder)\n --no-open print the URL instead of opening the browser\n --pair open the bare URL and type a short code this terminal shows:\n nothing secret in the browser's command line, which other\n users of a shared machine can read\n --idle-minutes=<n> stop after n idle minutes (1\u2013240, default 15)\n -h, --help\n\nNever add @basaltkit/deploy-ui to an app's package.json: the deploy installs an\napp's devDependencies on the server.\n";
|
|
4
4
|
export interface CliFlags {
|
|
5
5
|
targets?: string;
|
|
6
6
|
ssh?: string;
|
|
7
7
|
open: boolean;
|
|
8
|
+
pair: boolean;
|
|
8
9
|
idleMinutes: number;
|
|
9
10
|
help: boolean;
|
|
10
11
|
}
|
package/dist/cli.js
CHANGED
|
@@ -19,6 +19,9 @@ Options:
|
|
|
19
19
|
%SystemRoot%\\System32\\OpenSSH\\ssh.exe, else the first ssh.exe
|
|
20
20
|
in an absolute PATH entry — never the current folder)
|
|
21
21
|
--no-open print the URL instead of opening the browser
|
|
22
|
+
--pair open the bare URL and type a short code this terminal shows:
|
|
23
|
+
nothing secret in the browser's command line, which other
|
|
24
|
+
users of a shared machine can read
|
|
22
25
|
--idle-minutes=<n> stop after n idle minutes (1–${MAX_IDLE_MINUTES}, default 15)
|
|
23
26
|
-h, --help
|
|
24
27
|
|
|
@@ -28,7 +31,7 @@ app's devDependencies on the server.
|
|
|
28
31
|
export class UsageError extends Error {
|
|
29
32
|
}
|
|
30
33
|
export function parseFlags(argv, cwd) {
|
|
31
|
-
const flags = { open: true, idleMinutes: 15, help: false };
|
|
34
|
+
const flags = { open: true, pair: false, idleMinutes: 15, help: false };
|
|
32
35
|
for (const arg of argv) {
|
|
33
36
|
if (arg === '-h' || arg === '--help') {
|
|
34
37
|
flags.help = true;
|
|
@@ -38,6 +41,10 @@ export function parseFlags(argv, cwd) {
|
|
|
38
41
|
flags.open = false;
|
|
39
42
|
continue;
|
|
40
43
|
}
|
|
44
|
+
if (arg === '--pair') {
|
|
45
|
+
flags.pair = true;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
41
48
|
const match = /^--(targets|ssh|idle-minutes)=(.+)$/.exec(arg);
|
|
42
49
|
if (!match)
|
|
43
50
|
throw new UsageError(`unknown argument ${JSON.stringify(arg)} (there is no --host or --port: the UI listens on 127.0.0.1 and a port the OS picks)`);
|
|
@@ -101,6 +108,7 @@ export async function main(argv, deps = processDeps()) {
|
|
|
101
108
|
runner: { program: sshProgram(deps.platform, flags.ssh, deps.env), prefix: [], spawn: deps.spawn, env: deps.env, cwd: deps.home, ...deps.runner },
|
|
102
109
|
...(deps.osUser ? { osUser: deps.osUser } : {}),
|
|
103
110
|
idleMinutes: flags.idleMinutes,
|
|
111
|
+
pair: flags.pair,
|
|
104
112
|
...(deps.clientDir === undefined ? {} : { clientDir: deps.clientDir }),
|
|
105
113
|
log: (line) => deps.print(`basalt-deploy-ui: ${line}`),
|
|
106
114
|
onStop: (reason) => {
|
|
@@ -117,7 +125,12 @@ export async function main(argv, deps = processDeps()) {
|
|
|
117
125
|
deps.print(`basalt-deploy-ui: listening on ${server.url} (this machine only)`);
|
|
118
126
|
deps.print(`basalt-deploy-ui: targets in ${targetsPath}`);
|
|
119
127
|
const opened = flags.open && await Promise.race([deps.open(server.launchUrl), new Promise((resolve) => setTimeout(() => resolve(true), 5000).unref())]);
|
|
120
|
-
if (
|
|
128
|
+
if (server.pairCode) {
|
|
129
|
+
if (!opened)
|
|
130
|
+
deps.print(`basalt-deploy-ui: open this URL in your browser:\n ${server.launchUrl}`);
|
|
131
|
+
deps.print(`basalt-deploy-ui: pairing code ${server.pairCode} — type it in the page within 2 minutes (5 tries)`);
|
|
132
|
+
}
|
|
133
|
+
else if (!opened) {
|
|
121
134
|
deps.print(`basalt-deploy-ui: open this URL in your browser within 2 minutes (it works once):\n ${server.launchUrl}`);
|
|
122
135
|
}
|
|
123
136
|
return finished;
|
package/dist/client/main.js
CHANGED
|
@@ -12,7 +12,7 @@ function storageOf(win) {
|
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* The page's start: the one-time launch code from the URL fragment (removed
|
|
15
|
-
* from the address bar at once), exchanged for the session token and the
|
|
15
|
+
* from the address bar at once) — or, with none, the pairing code typed in, exchanged for the session token and the
|
|
16
16
|
* server key, kept in this tab's sessionStorage — never persistent storage,
|
|
17
17
|
* never a cookie. Then the server proves it is the process that printed the
|
|
18
18
|
* URL, before anything else.
|
|
@@ -28,41 +28,32 @@ export async function boot(env) {
|
|
|
28
28
|
const match = /^#launch=([A-Za-z0-9_-]{1,128})$/.exec(env.location.hash);
|
|
29
29
|
if (env.location.hash)
|
|
30
30
|
env.history.replaceState(null, '', env.location.pathname);
|
|
31
|
-
let
|
|
32
|
-
let serverKey;
|
|
31
|
+
let credentials;
|
|
33
32
|
try {
|
|
34
|
-
token = env.storage?.getItem(TOKEN_KEY) ?? undefined;
|
|
35
|
-
serverKey = env.storage?.getItem(SERVER_KEY) ?? undefined;
|
|
33
|
+
const token = env.storage?.getItem(TOKEN_KEY) ?? undefined;
|
|
34
|
+
const serverKey = env.storage?.getItem(SERVER_KEY) ?? undefined;
|
|
35
|
+
if (token && serverKey)
|
|
36
|
+
credentials = { token, serverKey };
|
|
36
37
|
}
|
|
37
38
|
catch {
|
|
38
|
-
|
|
39
|
+
credentials = undefined;
|
|
39
40
|
}
|
|
40
41
|
if (match) {
|
|
41
42
|
try {
|
|
42
|
-
;
|
|
43
|
-
({ token, serverKey } = await exchange(match[1], env.fetch));
|
|
44
|
-
try {
|
|
45
|
-
env.storage?.setItem(TOKEN_KEY, token);
|
|
46
|
-
env.storage?.setItem(SERVER_KEY, serverKey);
|
|
47
|
-
}
|
|
48
|
-
catch {
|
|
49
|
-
// No storage: this tab works until it reloads.
|
|
50
|
-
}
|
|
43
|
+
credentials = keep(env, await exchange(match[1], env.fetch));
|
|
51
44
|
}
|
|
52
45
|
catch (error) {
|
|
53
|
-
|
|
54
|
-
replace(shell.main, el('div', { class: 'banner err', role: 'alert' }, el('strong', {}, taken ? 'This UI already has its browser session.' : 'The launch code was refused.'), el('p', {}, taken
|
|
55
|
-
? 'Another tab or browser claimed it first. If that was not you, stop basalt-deploy-ui in your terminal now (Ctrl-C) and start it again.'
|
|
56
|
-
: `${describe(error)}. Start basalt-deploy-ui again and open the URL it prints.`)));
|
|
46
|
+
replace(shell.main, refusal(error));
|
|
57
47
|
return undefined;
|
|
58
48
|
}
|
|
59
49
|
}
|
|
60
|
-
if (!
|
|
50
|
+
if (!credentials) {
|
|
61
51
|
forgetSession(env.storage);
|
|
62
|
-
|
|
63
|
-
|
|
52
|
+
credentials = await pair(env, shell.main);
|
|
53
|
+
if (!credentials)
|
|
54
|
+
return undefined;
|
|
64
55
|
}
|
|
65
|
-
const app = new App(new Api(token, env.fetch, { serverKey, ...(env.crypto ? { crypto: env.crypto } : {}) }), shell, env.storage);
|
|
56
|
+
const app = new App(new Api(credentials.token, env.fetch, { serverKey: credentials.serverKey, ...(env.crypto ? { crypto: env.crypto } : {}) }), shell, env.storage);
|
|
66
57
|
try {
|
|
67
58
|
await app.api.verifyServer();
|
|
68
59
|
await app.start();
|
|
@@ -76,6 +67,60 @@ export async function boot(env) {
|
|
|
76
67
|
}
|
|
77
68
|
return app;
|
|
78
69
|
}
|
|
70
|
+
/** Credentials kept in this tab's sessionStorage (or only in memory, with no storage). */
|
|
71
|
+
function keep(env, credentials) {
|
|
72
|
+
try {
|
|
73
|
+
env.storage?.setItem(TOKEN_KEY, credentials.token);
|
|
74
|
+
env.storage?.setItem(SERVER_KEY, credentials.serverKey);
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
// No storage: this tab works until it reloads.
|
|
78
|
+
}
|
|
79
|
+
return credentials;
|
|
80
|
+
}
|
|
81
|
+
/** A refused code that typing again cannot fix: the session is taken, the code expired, or the tries ran out. */
|
|
82
|
+
const final = (error) => error instanceof ApiError && (error.status === 409 || error.status === 410 || error.status === 429);
|
|
83
|
+
function refusal(error) {
|
|
84
|
+
const taken = error instanceof ApiError && error.status === 409;
|
|
85
|
+
return el('div', { class: 'banner err', role: 'alert' }, el('strong', {}, taken ? 'This UI already has its browser session.' : 'The code was refused.'), el('p', {}, taken
|
|
86
|
+
? 'Another tab or browser claimed it first. If that was not you, stop basalt-deploy-ui in your terminal now (Ctrl-C) and start it again.'
|
|
87
|
+
: `${describe(error)}. Start basalt-deploy-ui again.`));
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* No session yet: the code the terminal shows, typed here (`--pair`: nothing
|
|
91
|
+
* secret went through the browser's command line). A launch code pasted from
|
|
92
|
+
* the terminal works too. Resolves with the credentials, or undefined once no
|
|
93
|
+
* code can work any more.
|
|
94
|
+
*/
|
|
95
|
+
function pair(env, main) {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
const input = el('input', { id: 'pair-code', name: 'code', type: 'text', autocomplete: 'off', spellcheck: false, required: true, placeholder: 'ABCD-EFGH', aria: { describedby: 'pair-help' } });
|
|
98
|
+
const status = el('span', { class: 'field-error', role: 'alert' });
|
|
99
|
+
const submit = el('button', { type: 'submit', class: 'primary' }, 'Open the session');
|
|
100
|
+
const form = el('form', { class: 'card pair', aria: { label: 'Pair this page' }, on: { submit: (event) => {
|
|
101
|
+
event.preventDefault();
|
|
102
|
+
const code = input.value.trim();
|
|
103
|
+
if (!/^[A-Za-z0-9_\s-]{1,128}$/.test(code)) {
|
|
104
|
+
replace(status, 'Type the code your terminal shows.');
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
submit.disabled = true;
|
|
108
|
+
void exchange(code, env.fetch).then((credentials) => resolve(keep(env, credentials)), (error) => {
|
|
109
|
+
if (final(error)) {
|
|
110
|
+
replace(main, refusal(error));
|
|
111
|
+
resolve(undefined);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
submit.disabled = false;
|
|
115
|
+
input.value = '';
|
|
116
|
+
input.focus();
|
|
117
|
+
replace(status, describe(error));
|
|
118
|
+
});
|
|
119
|
+
} } }, el('div', { class: 'card-head' }, el('h3', {}, 'Pair this page')), el('div', { class: 'card-body' }, el('div', { class: 'field' }, el('label', { for: 'pair-code' }, 'Pairing code'), input, el('span', { id: 'pair-help', class: 'field-help' }, 'Type the pairing code basalt-deploy-ui shows in your terminal (2 minutes, 5 tries). Without --pair, open the URL it printed instead.'), status)), el('div', { class: 'card-foot' }, submit));
|
|
120
|
+
replace(main, form);
|
|
121
|
+
input.focus();
|
|
122
|
+
});
|
|
123
|
+
}
|
|
79
124
|
if (typeof window !== 'undefined' && document.getElementById('app')) {
|
|
80
125
|
void boot({ location: window.location, history: window.history, storage: storageOf(window), fetch: (input, init) => window.fetch(input, init), document, crypto: window.crypto });
|
|
81
126
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -13,6 +13,8 @@ export interface UiServerOptions {
|
|
|
13
13
|
osUser?: string;
|
|
14
14
|
/** Default 15, at most 240. */
|
|
15
15
|
idleMinutes?: number;
|
|
16
|
+
/** `--pair`: the browser opens the bare URL; the person types {@link UiServer.pairCode} (RFC 0009 Q1). */
|
|
17
|
+
pair?: boolean;
|
|
16
18
|
/** Where the compiled client modules are (default: `dist/client` next to this file); null: none. */
|
|
17
19
|
clientDir?: string | null;
|
|
18
20
|
/** One line to the terminal. Never a request body, never an answer. */
|
|
@@ -30,8 +32,10 @@ export interface UiServer {
|
|
|
30
32
|
port: number;
|
|
31
33
|
/** `http://127.0.0.1:<port>/` */
|
|
32
34
|
url: string;
|
|
33
|
-
/** The URL with the one-time launch code in its fragment
|
|
35
|
+
/** The URL with the one-time launch code in its fragment — the bare URL with `pair`. */
|
|
34
36
|
launchUrl: string;
|
|
37
|
+
/** With `pair`: the code the person types in the page (the terminal shows it). */
|
|
38
|
+
pairCode?: string;
|
|
35
39
|
close(): Promise<void>;
|
|
36
40
|
}
|
|
37
41
|
export declare const MAX_SSH_SESSIONS = 4;
|
package/dist/server.js
CHANGED
|
@@ -40,7 +40,7 @@ async function loadClientFiles(dir) {
|
|
|
40
40
|
}
|
|
41
41
|
export async function startUiServer(options) {
|
|
42
42
|
const idleMinutes = Math.min(Math.max(options.idleMinutes ?? 15, 1), MAX_IDLE_MINUTES);
|
|
43
|
-
const session = new Session(options.now, options.random);
|
|
43
|
+
const session = new Session(options.now, options.random, options.pair === true);
|
|
44
44
|
const clientFiles = await loadClientFiles(options.clientDir === undefined ? fileURLToPath(new URL('./client/', import.meta.url)) : options.clientDir);
|
|
45
45
|
const timeouts = { ...DEFAULT_TIMEOUTS, ...options.timeouts };
|
|
46
46
|
const osActor = options.osUser && ACTOR.test(options.osUser) ? options.osUser : undefined;
|
|
@@ -509,5 +509,5 @@ export async function startUiServer(options) {
|
|
|
509
509
|
throw new HttpError(404, 'no such route');
|
|
510
510
|
}
|
|
511
511
|
}
|
|
512
|
-
return { port, url, launchUrl: `${url}#launch=${session.launchCode}`, close };
|
|
512
|
+
return session.pair ? { port, url, launchUrl: url, pairCode: session.launchCode, close } : { port, url, launchUrl: `${url}#launch=${session.launchCode}`, close };
|
|
513
513
|
}
|
package/dist/session.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The one browser session of a UI process (RFC 0009 §4.2): a one-time launch
|
|
3
|
-
* code (in the URL fragment, valid 120 s) exchanged once for a session token
|
|
3
|
+
* code (in the URL fragment, or typed with `--pair`; valid 120 s) exchanged once for a session token
|
|
4
4
|
* sent as `Authorization: Bearer`. The first exchange wins; any later one —
|
|
5
5
|
* right code or not — is refused, and the terminal says so.
|
|
6
6
|
*
|
|
@@ -13,6 +13,17 @@
|
|
|
13
13
|
export declare const PROOF_CONTEXT = "basalt-deploy-ui hello v1\n";
|
|
14
14
|
export declare const NONCE: RegExp;
|
|
15
15
|
export declare const LAUNCH_TTL_MS = 120000;
|
|
16
|
+
/**
|
|
17
|
+
* `--pair` (RFC 0009 Q1): the browser opens the bare URL and the person types
|
|
18
|
+
* a short code the terminal shows. Nothing secret passes through the opener's
|
|
19
|
+
* or the browser's argv, which another local user can read. Eight characters
|
|
20
|
+
* of an alphabet with no look-alikes (~40 bits), five tries, two minutes.
|
|
21
|
+
*/
|
|
22
|
+
export declare const PAIR_ALPHABET = "23456789ABCDEFGHJKMNPQRSTUVWXYZ";
|
|
23
|
+
export declare const PAIR_LENGTH = 8;
|
|
24
|
+
export declare const PAIR_TRIES = 5;
|
|
25
|
+
/** What a person types, as the code is compared: upper case, without the separator or spaces. */
|
|
26
|
+
export declare const normalizePairCode: (code: string) => string;
|
|
16
27
|
/** Constant time for equal lengths; a length mismatch is a plain refusal (the lengths are public). */
|
|
17
28
|
export declare function safeEqual(given: string, expected: string): boolean;
|
|
18
29
|
export type Exchange = {
|
|
@@ -21,17 +32,19 @@ export type Exchange = {
|
|
|
21
32
|
serverKey: string;
|
|
22
33
|
} | {
|
|
23
34
|
ok: false;
|
|
24
|
-
status: 401 | 409;
|
|
35
|
+
status: 401 | 409 | 410 | 429;
|
|
25
36
|
reason: string;
|
|
26
37
|
};
|
|
27
38
|
export declare class Session {
|
|
28
39
|
private readonly now;
|
|
40
|
+
readonly pair: boolean;
|
|
29
41
|
readonly launchCode: string;
|
|
30
42
|
private readonly token;
|
|
31
43
|
private readonly serverKey;
|
|
32
44
|
private readonly createdAt;
|
|
33
45
|
private claimed;
|
|
34
|
-
|
|
46
|
+
private triesLeft;
|
|
47
|
+
constructor(now?: () => number, random?: (size: number) => Buffer, pair?: boolean);
|
|
35
48
|
get isClaimed(): boolean;
|
|
36
49
|
exchange(code: unknown): Exchange;
|
|
37
50
|
/** HMAC-SHA256(serverKey, context + nonce), base64url; undefined for a malformed nonce. */
|
package/dist/session.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createHmac, randomBytes as nodeRandomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
2
|
/**
|
|
3
3
|
* The one browser session of a UI process (RFC 0009 §4.2): a one-time launch
|
|
4
|
-
* code (in the URL fragment, valid 120 s) exchanged once for a session token
|
|
4
|
+
* code (in the URL fragment, or typed with `--pair`; valid 120 s) exchanged once for a session token
|
|
5
5
|
* sent as `Authorization: Bearer`. The first exchange wins; any later one —
|
|
6
6
|
* right code or not — is refused, and the terminal says so.
|
|
7
7
|
*
|
|
@@ -15,6 +15,28 @@ export const PROOF_CONTEXT = 'basalt-deploy-ui hello v1\n';
|
|
|
15
15
|
export const NONCE = /^[A-Za-z0-9_-]{22,128}$/;
|
|
16
16
|
export const LAUNCH_TTL_MS = 120_000;
|
|
17
17
|
const SECRET_BYTES = 32;
|
|
18
|
+
/**
|
|
19
|
+
* `--pair` (RFC 0009 Q1): the browser opens the bare URL and the person types
|
|
20
|
+
* a short code the terminal shows. Nothing secret passes through the opener's
|
|
21
|
+
* or the browser's argv, which another local user can read. Eight characters
|
|
22
|
+
* of an alphabet with no look-alikes (~40 bits), five tries, two minutes.
|
|
23
|
+
*/
|
|
24
|
+
export const PAIR_ALPHABET = '23456789ABCDEFGHJKMNPQRSTUVWXYZ';
|
|
25
|
+
export const PAIR_LENGTH = 8;
|
|
26
|
+
export const PAIR_TRIES = 5;
|
|
27
|
+
/** What a person types, as the code is compared: upper case, without the separator or spaces. */
|
|
28
|
+
export const normalizePairCode = (code) => code.toUpperCase().replace(/[\s-]/g, '');
|
|
29
|
+
function pairCode(random) {
|
|
30
|
+
let code = '';
|
|
31
|
+
// Rejection sampling: 248 = 8 × 31, so every letter is equally likely.
|
|
32
|
+
while (code.length < PAIR_LENGTH) {
|
|
33
|
+
for (const byte of random(16)) {
|
|
34
|
+
if (byte < 248 && code.length < PAIR_LENGTH)
|
|
35
|
+
code += PAIR_ALPHABET[byte % PAIR_ALPHABET.length];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return `${code.slice(0, 4)}-${code.slice(4)}`;
|
|
39
|
+
}
|
|
18
40
|
/** Constant time for equal lengths; a length mismatch is a plain refusal (the lengths are public). */
|
|
19
41
|
export function safeEqual(given, expected) {
|
|
20
42
|
const a = Buffer.from(given, 'utf8');
|
|
@@ -28,14 +50,17 @@ export function safeEqual(given, expected) {
|
|
|
28
50
|
}
|
|
29
51
|
export class Session {
|
|
30
52
|
now;
|
|
53
|
+
pair;
|
|
31
54
|
launchCode;
|
|
32
55
|
token;
|
|
33
56
|
serverKey;
|
|
34
57
|
createdAt;
|
|
35
58
|
claimed = false;
|
|
36
|
-
|
|
59
|
+
triesLeft = PAIR_TRIES;
|
|
60
|
+
constructor(now = Date.now, random = nodeRandomBytes, pair = false) {
|
|
37
61
|
this.now = now;
|
|
38
|
-
this.
|
|
62
|
+
this.pair = pair;
|
|
63
|
+
this.launchCode = pair ? pairCode(random) : random(SECRET_BYTES).toString('base64url');
|
|
39
64
|
this.token = random(SECRET_BYTES).toString('base64url');
|
|
40
65
|
this.serverKey = random(SECRET_BYTES).toString('base64url');
|
|
41
66
|
this.createdAt = now();
|
|
@@ -46,10 +71,18 @@ export class Session {
|
|
|
46
71
|
exchange(code) {
|
|
47
72
|
if (this.claimed)
|
|
48
73
|
return { ok: false, status: 409, reason: 'this UI already has its browser session; restart basalt-deploy-ui for a new one' };
|
|
49
|
-
if (
|
|
50
|
-
return { ok: false, status:
|
|
74
|
+
if (this.pair && this.triesLeft <= 0)
|
|
75
|
+
return { ok: false, status: 429, reason: `${PAIR_TRIES} wrong pairing codes: restart basalt-deploy-ui for a new one` };
|
|
76
|
+
const given = typeof code === 'string' && this.pair ? normalizePairCode(code) : code;
|
|
77
|
+
const expected = this.pair ? normalizePairCode(this.launchCode) : this.launchCode;
|
|
78
|
+
if (typeof given !== 'string' || !safeEqual(given, expected)) {
|
|
79
|
+
if (!this.pair)
|
|
80
|
+
return { ok: false, status: 401, reason: 'wrong launch code' };
|
|
81
|
+
this.triesLeft--;
|
|
82
|
+
return { ok: false, status: this.triesLeft > 0 ? 401 : 429, reason: this.triesLeft > 0 ? `wrong pairing code (${this.triesLeft} ${this.triesLeft === 1 ? 'try' : 'tries'} left)` : `${PAIR_TRIES} wrong pairing codes: restart basalt-deploy-ui for a new one` };
|
|
83
|
+
}
|
|
51
84
|
if (this.now() - this.createdAt > LAUNCH_TTL_MS)
|
|
52
|
-
return { ok: false, status:
|
|
85
|
+
return { ok: false, status: 410, reason: 'the launch code expired (120 s); restart basalt-deploy-ui' };
|
|
53
86
|
this.claimed = true;
|
|
54
87
|
return { ok: true, token: this.token, serverKey: this.serverKey };
|
|
55
88
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basaltkit/deploy-ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=22.5.0"
|
|
6
6
|
},
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dist"
|
|
22
22
|
],
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@basaltkit/deploy": "^0.5.
|
|
24
|
+
"@basaltkit/deploy": "^0.5.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/node": "^26.6.5",
|