@tokenoftrust/cli 7.5.0 → 7.5.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/bin/tot.mjs +5 -3
- package/package.json +1 -1
- package/src/commands/app/conformance.mjs +143 -9
- package/src/commands/app/index.mjs +1 -0
package/bin/tot.mjs
CHANGED
|
@@ -447,13 +447,15 @@ async function main() {
|
|
|
447
447
|
const diagnostics = currentInvocationDiagnostics();
|
|
448
448
|
const diagnosticIds = diagnostics.map((entry) => entry.diagnosticId).filter(Boolean);
|
|
449
449
|
const session = currentInvocationSession();
|
|
450
|
-
//
|
|
451
|
-
|
|
450
|
+
// Keep feedback text and conformance certification inputs out of activity attachments.
|
|
451
|
+
let activityArgs = ["«omitted»"];
|
|
452
|
+
if (cmd === "app" && rest[0] === "conformance") activityArgs = ["conformance"];
|
|
453
|
+
else if (cmd !== "feedback") activityArgs = redactArgs(rest);
|
|
452
454
|
recordActivity({
|
|
453
455
|
ts: new Date().toISOString(),
|
|
454
456
|
v: VERSION,
|
|
455
457
|
cmd: command,
|
|
456
|
-
args:
|
|
458
|
+
args: activityArgs,
|
|
457
459
|
code: exitCode,
|
|
458
460
|
ms: durationMs,
|
|
459
461
|
...(errMsg ? { err: String(errMsg).slice(0, 200) } : {}),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tokenoftrust/cli",
|
|
3
|
-
"version": "7.5.
|
|
3
|
+
"version": "7.5.2",
|
|
4
4
|
"description": "Token of Trust developer CLI — clone a tenant store, run it locally with save→reload, and submit it for preview. Installs the `tot` command.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Token of Trust",
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { generateKeyPairSync, randomUUID, sign } from "node:crypto";
|
|
1
|
+
import { createHash, generateKeyPairSync, randomUUID, sign } from "node:crypto";
|
|
2
|
+
import { mkdtemp, rename, rm, writeFile } from "node:fs/promises";
|
|
2
3
|
import { createServer, request as requestHttp } from "node:http";
|
|
3
4
|
import { request as requestHttps } from "node:https";
|
|
4
5
|
import { isIP } from "node:net";
|
|
6
|
+
import { dirname, join, resolve } from "node:path";
|
|
5
7
|
import { parseArgs } from "node:util";
|
|
8
|
+
import { resolveDeveloperSession } from "../../auth.mjs";
|
|
6
9
|
import { fail } from "../../errors.mjs";
|
|
10
|
+
import { selectedAuthority } from "../../token-store.mjs";
|
|
11
|
+
import { DEFAULT_STOREFRONT_ORIGIN, TOKEN_OF_TRUST_MCP_ORIGINS } from "../../viewer-session.mjs";
|
|
7
12
|
import {
|
|
8
13
|
SIGNATURE_LABEL,
|
|
9
14
|
WEBHOOK_SIGNATURE_COVERED_COMPONENTS,
|
|
@@ -18,7 +23,13 @@ const METADATA_PATH = "/.well-known/tot-app-authorization-metadata";
|
|
|
18
23
|
const METADATA_URL = `http://${METADATA_HOST}:${METADATA_PORT}${METADATA_PATH}`;
|
|
19
24
|
const ARCHETYPES = ["label-station", "dropship", "inventory-sync"];
|
|
20
25
|
const REQUEST_TIMEOUT_MS = 5000;
|
|
26
|
+
const STAFF_SESSION_TIMEOUT_MS = 30_000;
|
|
21
27
|
const MAX_ACK_BYTES = 4096;
|
|
28
|
+
const CERTIFICATION_SCHEMA = "tot-connector-certification.v1";
|
|
29
|
+
const APP_ID = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$/;
|
|
30
|
+
const VERIFIED_ACTOR = /^(?:staff|developer):[a-f0-9]{64}$/;
|
|
31
|
+
const OFFLINE_ACTOR = /^[A-Za-z][A-Za-z0-9._-]{0,159}$/;
|
|
32
|
+
const DEFAULT_LOCAL_ACTOR = "local-conformance-runner";
|
|
22
33
|
|
|
23
34
|
const USAGE = `tot app conformance --url <webhookUrl> [--archetype ${ARCHETYPES.join("|")}]
|
|
24
35
|
|
|
@@ -36,7 +47,16 @@ for the duration of the run. The bundled RSA verifier works with tot app dev emi
|
|
|
36
47
|
|
|
37
48
|
For replay, return a 2xx JSON response with {"accepted":true,"duplicate":true}
|
|
38
49
|
when the same CloudEvent ID is received again. Check connector state separately
|
|
39
|
-
to confirm idempotent side effects and handling of stale inventory
|
|
50
|
+
to confirm idempotent side effects and handling of stale inventory.
|
|
51
|
+
|
|
52
|
+
Conformance certification:
|
|
53
|
+
tot app conformance --url <webhookUrl> --archetype <name> --certify --app-id <id>
|
|
54
|
+
[--receipt <path>] [--actor <id>] [--api-base ${DEFAULT_STOREFRONT_ORIGIN}]
|
|
55
|
+
|
|
56
|
+
Emits a deterministic SHA-256 conformance receipt when every check in the
|
|
57
|
+
archetype checklist passes. Pass --actor (or --staff-actor) to stamp a custom
|
|
58
|
+
certifier ID, or --api-base with your signed-in ToT session to verify the actor
|
|
59
|
+
online. The receipt goes to stdout and, when requested, to --receipt.`;
|
|
40
60
|
|
|
41
61
|
/** @param {string} input */
|
|
42
62
|
function loopbackUrl(input) {
|
|
@@ -175,9 +195,67 @@ function post(target, body, headers, checkDuplicate, request) {
|
|
|
175
195
|
});
|
|
176
196
|
}
|
|
177
197
|
|
|
198
|
+
/** @param {unknown} actor @param {RegExp} pattern @returns {actor is string} */
|
|
199
|
+
function isValidActor(actor, pattern) {
|
|
200
|
+
return typeof actor === "string" && actor === actor.trim() && pattern.test(actor);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** @param {string | undefined} apiBase @param {string | undefined} actor @param {NodeJS.ProcessEnv} env
|
|
204
|
+
* @param {typeof resolveDeveloperSession} resolveDev @param {typeof fetch} fetchImpl
|
|
205
|
+
* @returns {Promise<{ kind: "admitted", actor: string } | { kind: "refused" | "unavailable" }>}
|
|
206
|
+
*/
|
|
207
|
+
async function certificationAdmission(apiBase, actor, env, resolveDev, fetchImpl) {
|
|
208
|
+
const refused = /** @type {const} */ ({ kind: "refused" });
|
|
209
|
+
const unavailable = /** @type {const} */ ({ kind: "unavailable" });
|
|
210
|
+
if (apiBase === undefined) {
|
|
211
|
+
if (actor === undefined) return { kind: "admitted", actor: DEFAULT_LOCAL_ACTOR };
|
|
212
|
+
const admitted = isValidActor(actor, OFFLINE_ACTOR) || isValidActor(actor, VERIFIED_ACTOR);
|
|
213
|
+
return admitted ? { kind: "admitted", actor } : refused;
|
|
214
|
+
}
|
|
215
|
+
if (actor !== undefined && !isValidActor(actor, VERIFIED_ACTOR)) return refused;
|
|
216
|
+
|
|
217
|
+
// An operator-supplied URL must never choose where the cached bearer goes.
|
|
218
|
+
const base = URL.parse(apiBase);
|
|
219
|
+
if (!base || base.href !== `${DEFAULT_STOREFRONT_ORIGIN}/`) return refused;
|
|
220
|
+
try {
|
|
221
|
+
const mcpOrigin = selectedAuthority(env);
|
|
222
|
+
if (!TOKEN_OF_TRUST_MCP_ORIGINS.has(mcpOrigin)) return refused;
|
|
223
|
+
const session = await resolveDev(null, env, { fetchImpl });
|
|
224
|
+
if (session.identity !== "developer" || !session.token || !session.mcpUrl || URL.parse(session.mcpUrl)?.origin !== mcpOrigin) return refused;
|
|
225
|
+
const response = await fetchImpl(new URL("api/dev/cli-session", base).href, {
|
|
226
|
+
method: "POST",
|
|
227
|
+
headers: { authorization: `Bearer ${session.token}`, "content-type": "application/json" },
|
|
228
|
+
body: JSON.stringify({ mcpOrigin, principal: "certifier" }),
|
|
229
|
+
redirect: "error",
|
|
230
|
+
signal: AbortSignal.timeout(STAFF_SESSION_TIMEOUT_MS),
|
|
231
|
+
});
|
|
232
|
+
if (response.status >= 500) return unavailable;
|
|
233
|
+
if (response.status !== 200) return refused;
|
|
234
|
+
const body = await response.json();
|
|
235
|
+
const verifiedActor = body?.certifierActor ?? body?.staffActor;
|
|
236
|
+
if (body?.ok !== true || !isValidActor(verifiedActor, VERIFIED_ACTOR) || (actor && actor !== verifiedActor)) return refused;
|
|
237
|
+
return { kind: "admitted", actor: verifiedActor };
|
|
238
|
+
} catch {
|
|
239
|
+
return unavailable;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/** @param {string} path @param {string} receipt */
|
|
244
|
+
async function writeReceipt(path, receipt) {
|
|
245
|
+
const destination = resolve(path);
|
|
246
|
+
const directory = await mkdtemp(join(dirname(destination), ".tot-certification-"));
|
|
247
|
+
const staged = join(directory, "receipt.json");
|
|
248
|
+
try {
|
|
249
|
+
await writeFile(staged, `${receipt}\n`, { mode: 0o600, flag: "wx" });
|
|
250
|
+
await rename(staged, destination);
|
|
251
|
+
} finally {
|
|
252
|
+
await rm(directory, { recursive: true, force: true });
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
178
256
|
/**
|
|
179
257
|
* @param {string[]} argv
|
|
180
|
-
* @param {{ makeServer?: typeof createServer, request?: typeof directRequest, now?: () => number, log?: (line: string) => void, error?: (line: string) => void }} [deps]
|
|
258
|
+
* @param {{ makeServer?: typeof createServer, request?: typeof directRequest, now?: () => number, log?: (line: string) => void, error?: (line: string) => void, env?: NodeJS.ProcessEnv, resolveDev?: typeof resolveDeveloperSession, fetchImpl?: typeof fetch }} [deps]
|
|
181
259
|
*/
|
|
182
260
|
export async function runAppConformance(argv, {
|
|
183
261
|
makeServer = createServer,
|
|
@@ -185,11 +263,16 @@ export async function runAppConformance(argv, {
|
|
|
185
263
|
now = Date.now,
|
|
186
264
|
log = console.log,
|
|
187
265
|
error = console.error,
|
|
266
|
+
env = process.env,
|
|
267
|
+
resolveDev = resolveDeveloperSession,
|
|
268
|
+
fetchImpl = fetch,
|
|
188
269
|
} = {}) {
|
|
189
270
|
let values;
|
|
190
271
|
try {
|
|
191
272
|
({ values } = parseArgs({ args: argv, options: {
|
|
192
273
|
url: { type: "string" }, archetype: { type: "string" }, help: { type: "boolean", short: "h" },
|
|
274
|
+
certify: { type: "boolean" }, receipt: { type: "string" }, actor: { type: "string" }, "staff-actor": { type: "string" },
|
|
275
|
+
"api-base": { type: "string" }, "app-id": { type: "string" },
|
|
193
276
|
} }));
|
|
194
277
|
} catch {
|
|
195
278
|
error(fail("Invalid conformance options", "tot app conformance --help"));
|
|
@@ -199,6 +282,32 @@ export async function runAppConformance(argv, {
|
|
|
199
282
|
log(USAGE);
|
|
200
283
|
return 0;
|
|
201
284
|
}
|
|
285
|
+
const { certify, archetype, receipt, "app-id": appId, actor: directActor, "staff-actor": staffActor, "api-base": apiBase } = values;
|
|
286
|
+
if (directActor !== undefined && staffActor !== undefined && directActor !== staffActor) {
|
|
287
|
+
error(fail("Invalid conformance options", "tot app conformance --help"));
|
|
288
|
+
return 2;
|
|
289
|
+
}
|
|
290
|
+
const actor = directActor ?? staffActor;
|
|
291
|
+
if (!certify && [receipt, appId, actor, apiBase].some((option) => option !== undefined)) {
|
|
292
|
+
error(fail("Certification options require --certify", "tot app conformance --help"));
|
|
293
|
+
return 2;
|
|
294
|
+
}
|
|
295
|
+
let certifiedBy;
|
|
296
|
+
if (certify) {
|
|
297
|
+
const admission = await certificationAdmission(apiBase, actor, env, resolveDev, fetchImpl);
|
|
298
|
+
if (admission.kind !== "admitted") {
|
|
299
|
+
const message = admission.kind === "unavailable"
|
|
300
|
+
? "Unable to verify certification session"
|
|
301
|
+
: "Invalid certification actor or session";
|
|
302
|
+
error(fail(message, "tot app conformance --help"));
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
certifiedBy = admission.actor;
|
|
306
|
+
if (!appId || appId.length > 128 || appId !== appId.trim() || !APP_ID.test(appId) || !archetype || (receipt !== undefined && !receipt.trim())) {
|
|
307
|
+
error(fail("Certification requires --app-id and --archetype, and --receipt must name a file when supplied", "tot app conformance --help"));
|
|
308
|
+
return 2;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
202
311
|
const target = values.url && loopbackUrl(values.url);
|
|
203
312
|
if (!target) {
|
|
204
313
|
error(fail("Conformance requires --url with a loopback HTTP(S) webhook URL", "tot app conformance --help"));
|
|
@@ -229,10 +338,11 @@ export async function runAppConformance(argv, {
|
|
|
229
338
|
}
|
|
230
339
|
|
|
231
340
|
try {
|
|
232
|
-
log(`Webhook conformance${
|
|
341
|
+
if (!certify) log(`Webhook conformance${archetype ? ` (${archetype})` : ""}`);
|
|
342
|
+
const checks = [];
|
|
233
343
|
let passed = true;
|
|
234
344
|
let initialAccepted = false;
|
|
235
|
-
for (const { label, acknowledgment, body } of fixtures(
|
|
345
|
+
for (const { label, acknowledgment, body } of fixtures(archetype)) {
|
|
236
346
|
const replay = acknowledgment === "replay";
|
|
237
347
|
const digest = await computeContentDigestSha256(body);
|
|
238
348
|
const params = buildSignatureParamsValue(WEBHOOK_SIGNATURE_COVERED_COMPONENTS, {
|
|
@@ -255,17 +365,41 @@ export async function runAppConformance(argv, {
|
|
|
255
365
|
else if (replay && !initialAccepted) finding = `HTTP ${status} duplicate reported without a successful initial delivery`;
|
|
256
366
|
else {
|
|
257
367
|
if (acknowledgment === "initial") initialAccepted = true;
|
|
258
|
-
|
|
368
|
+
// Sorted keys and fixed fixture order give this flat evidence a canonical JSON encoding.
|
|
369
|
+
checks.push({ label, passed: true, status });
|
|
370
|
+
if (!certify) log(`PASS ${label} — HTTP ${status} ${replay ? "duplicate acknowledged" : "delivery acknowledged"}`);
|
|
259
371
|
continue;
|
|
260
372
|
}
|
|
261
373
|
} catch {
|
|
262
374
|
finding = "request failed or timed out";
|
|
263
375
|
}
|
|
264
|
-
log(`FAIL ${label} — ${finding}`);
|
|
376
|
+
(certify ? error : log)(`FAIL ${label} — ${finding}`);
|
|
265
377
|
passed = false;
|
|
266
378
|
}
|
|
267
|
-
log("Results reflect webhook responses. Inspect connector state to assess side effects.");
|
|
268
|
-
|
|
379
|
+
if (!certify) log("Results reflect webhook responses. Inspect connector state to assess side effects.");
|
|
380
|
+
if (!passed) return 1;
|
|
381
|
+
if (certify) {
|
|
382
|
+
const output = JSON.stringify({
|
|
383
|
+
schema: CERTIFICATION_SCHEMA,
|
|
384
|
+
appId,
|
|
385
|
+
archetype,
|
|
386
|
+
certifiedBy,
|
|
387
|
+
certifiedAt: new Date(now()).toISOString(),
|
|
388
|
+
passed: true,
|
|
389
|
+
checks,
|
|
390
|
+
evidenceSha256: createHash("sha256").update(JSON.stringify(checks)).digest("hex"),
|
|
391
|
+
});
|
|
392
|
+
if (receipt !== undefined) {
|
|
393
|
+
try {
|
|
394
|
+
await writeReceipt(receipt, output);
|
|
395
|
+
} catch {
|
|
396
|
+
error(fail("Unable to write the certification receipt"));
|
|
397
|
+
return 1;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
log(output);
|
|
401
|
+
}
|
|
402
|
+
return 0;
|
|
269
403
|
} finally {
|
|
270
404
|
await close(server);
|
|
271
405
|
}
|
|
@@ -15,6 +15,7 @@ const USAGE = `tot app — build and install a Storefront Private App
|
|
|
15
15
|
tot app scaffold <name> scaffold a runnable Private App skeleton
|
|
16
16
|
tot app dev ... local, offline webhook/manifest/JWT harness (see \`tot app dev --help\`)
|
|
17
17
|
tot app conformance --url <webhookUrl> check a running local connector's webhook contract
|
|
18
|
+
tot app conformance --certify issue a conformance receipt (see \`tot app conformance --help\`)
|
|
18
19
|
tot app install install your app on your store (env defaults to test); prints the client secret once
|
|
19
20
|
tot app update <installId> push your app's current version, scopes and manifest to an install
|
|
20
21
|
tot app rotate <installId> issue a new client secret (printed once)
|