@nodii/dev 0.6.79 → 0.6.80
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/dist/core.js +114 -2
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { homedir, platform } from "node:os";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
|
|
7
|
-
export const VERSION = "0.6.
|
|
7
|
+
export const VERSION = "0.6.80";
|
|
8
8
|
export const configPath = () =>
|
|
9
9
|
process.env.NODII_DEV_CONFIG_PATH ??
|
|
10
10
|
join(homedir(), ".config/nodii-dev/config.json");
|
|
@@ -135,7 +135,7 @@ const withSignal = (promise, signal) => {
|
|
|
135
135
|
});
|
|
136
136
|
};
|
|
137
137
|
|
|
138
|
-
async function
|
|
138
|
+
async function devFetchOnce(method, path, bodyText, contentType, accept, extra = {}, signal) {
|
|
139
139
|
const wait = async (promise) => {
|
|
140
140
|
try {
|
|
141
141
|
return await withSignal(promise, signal);
|
|
@@ -175,6 +175,118 @@ async function devFetch(method, path, bodyText, contentType, accept, extra = {},
|
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
// hub-254: an operator-only route refuses the CLI's sign-in (an
|
|
179
|
+
// OAuth agent) with 403 OPERATOR_REQUIRED. When a person is at the terminal,
|
|
180
|
+
// the CLI asks for their TOTP code, Dev verifies it against the signed-in
|
|
181
|
+
// person, and the call is sent again with the ten-minute grant Dev returns.
|
|
182
|
+
// The code is never stored, logged or passed on; the grant lives only in this
|
|
183
|
+
// process's memory. Without a TTY (an agent) the refusal is surfaced as it is.
|
|
184
|
+
export const OPERATOR_STEP_UP_PATH = "/api/v1/operator/step-up";
|
|
185
|
+
export const OPERATOR_STEP_UP_HEADER = "x-operator-step-up";
|
|
186
|
+
let stepUpGrant = null; // { grant, expiresAt } for this process only
|
|
187
|
+
let pendingStepUp = null; // the step-up in flight, shared by concurrent refusals
|
|
188
|
+
let totpPrompt = defaultTotpPrompt;
|
|
189
|
+
|
|
190
|
+
/** Tests replace the TTY prompt; `null` restores the default. Also forgets the grant. */
|
|
191
|
+
export function setTotpPrompt(fn) {
|
|
192
|
+
totpPrompt = fn ?? defaultTotpPrompt;
|
|
193
|
+
stepUpGrant = null;
|
|
194
|
+
pendingStepUp = null;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** Reads one code from the terminal without echoing it, or null when there is no TTY. */
|
|
198
|
+
async function defaultTotpPrompt() {
|
|
199
|
+
if (!process.stdin.isTTY || !process.stderr.isTTY) return null;
|
|
200
|
+
const { createInterface } = await import("node:readline");
|
|
201
|
+
const { Writable } = await import("node:stream");
|
|
202
|
+
let muted = false;
|
|
203
|
+
const output = new Writable({
|
|
204
|
+
write(chunk, encoding, done) {
|
|
205
|
+
if (!muted) process.stderr.write(chunk, encoding);
|
|
206
|
+
done();
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
const rl = createInterface({ input: process.stdin, output, terminal: true });
|
|
210
|
+
try {
|
|
211
|
+
return await new Promise((resolvePromise, rejectPromise) => {
|
|
212
|
+
rl.once("close", () => resolvePromise(null));
|
|
213
|
+
// In raw mode Ctrl-C reaches readline, not the process: without this the
|
|
214
|
+
// prompt would pause and never settle.
|
|
215
|
+
rl.once("SIGINT", () => {
|
|
216
|
+
rejectPromise(new CliError("OPERATOR_STEP_UP_CANCELLED", "Cancelled at the TOTP prompt.", 130));
|
|
217
|
+
rl.close();
|
|
218
|
+
});
|
|
219
|
+
rl.question("Enter your TOTP code: ", (answer) => resolvePromise(answer.trim()));
|
|
220
|
+
muted = true;
|
|
221
|
+
});
|
|
222
|
+
} finally {
|
|
223
|
+
rl.close();
|
|
224
|
+
process.stderr.write("\n");
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const refusalCode = (raw) => {
|
|
229
|
+
try {
|
|
230
|
+
return JSON.parse(raw)?.code ?? null;
|
|
231
|
+
} catch {
|
|
232
|
+
return null;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const liveGrant = () => (stepUpGrant && Date.parse(stepUpGrant.expiresAt) - Date.now() > 5_000 ? stepUpGrant.grant : null);
|
|
237
|
+
|
|
238
|
+
/** Ask for the code once and trade it for a grant; null when there is no TTY and the refusal stands. */
|
|
239
|
+
async function stepUpOnce(refusal, signal) {
|
|
240
|
+
const typed = await totpPrompt();
|
|
241
|
+
if (typed === null || typed === undefined) {
|
|
242
|
+
if (refusal !== "OPERATOR_REQUIRED") return null;
|
|
243
|
+
throw new CliError(
|
|
244
|
+
"OPERATOR_REQUIRED",
|
|
245
|
+
"This is operator-only. Run it yourself in a terminal: the CLI then asks for your TOTP code. An agent cannot answer it.",
|
|
246
|
+
1,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (!/^[0-9]{6}$/.test(typed)) throw new CliError("OPERATOR_STEP_UP_CODE_REJECTED", "A TOTP code is six digits.", 1);
|
|
250
|
+
const stepUp = await devFetchOnce("POST", OPERATOR_STEP_UP_PATH, JSON.stringify({ code: typed }), "application/json", "application/json", {}, signal);
|
|
251
|
+
let payload = {};
|
|
252
|
+
try {
|
|
253
|
+
payload = JSON.parse(stepUp.raw);
|
|
254
|
+
} catch {}
|
|
255
|
+
if (!stepUp.response.ok)
|
|
256
|
+
throw new CliError(payload.code ?? `HTTP_${stepUp.response.status}`, payload.message ?? `Dev returned HTTP ${stepUp.response.status}`, 1, payload.details);
|
|
257
|
+
const grant = payload?.data?.grant;
|
|
258
|
+
const expiresAt = payload?.data?.expiresAt;
|
|
259
|
+
if (typeof grant !== "string" || typeof expiresAt !== "string" || !Number.isFinite(Date.parse(expiresAt)))
|
|
260
|
+
throw new CliError("OPERATOR_STEP_UP_UNAVAILABLE", "Dev's step-up answer carried no usable grant.", 1);
|
|
261
|
+
stepUpGrant = { grant, expiresAt };
|
|
262
|
+
return grant;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async function devFetch(method, path, bodyText, contentType, accept, extra = {}, signal) {
|
|
266
|
+
const send = (grant) =>
|
|
267
|
+
devFetchOnce(method, path, bodyText, contentType, accept, grant ? { ...extra, [OPERATOR_STEP_UP_HEADER]: grant } : extra, signal);
|
|
268
|
+
if (path === OPERATOR_STEP_UP_PATH) return devFetchOnce(method, path, bodyText, contentType, accept, extra, signal);
|
|
269
|
+
const held = liveGrant();
|
|
270
|
+
const first = await send(held);
|
|
271
|
+
const code = refusalCode(first.raw);
|
|
272
|
+
const refused =
|
|
273
|
+
(first.response.status === 403 && code === "OPERATOR_REQUIRED") ||
|
|
274
|
+
(first.response.status === 401 && code === "OPERATOR_STEP_UP_INVALID");
|
|
275
|
+
if (!refused) return first;
|
|
276
|
+
// Drop only the grant this call was refused with; one obtained meanwhile by
|
|
277
|
+
// a concurrent call is used as it is, without a second prompt.
|
|
278
|
+
if (stepUpGrant?.grant === held) stepUpGrant = null;
|
|
279
|
+
const fresh = liveGrant();
|
|
280
|
+
if (fresh && fresh !== held) return send(fresh);
|
|
281
|
+
// Concurrent refusals share one prompt and one grant (one TTY, one code).
|
|
282
|
+
pendingStepUp ??= stepUpOnce(code, signal).finally(() => {
|
|
283
|
+
pendingStepUp = null;
|
|
284
|
+
});
|
|
285
|
+
const grant = await pendingStepUp;
|
|
286
|
+
if (grant === null) return first;
|
|
287
|
+
return send(grant);
|
|
288
|
+
}
|
|
289
|
+
|
|
178
290
|
/** A JSON call that keeps the status: `{ status, payload }`. Errors throw as devRawRequest's do. */
|
|
179
291
|
export async function devExchange(method, path, bodyText, { contentType = "application/json", headers = {}, signal } = {}) {
|
|
180
292
|
const { response, raw } = await devFetch(method, path, bodyText, contentType, "application/json", headers, signal);
|