@clavis.nexus/setup 2.0.0 → 3.0.1

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/mcp.js CHANGED
@@ -10,7 +10,7 @@ import { promisify } from 'util';
10
10
  import { readFile, chmod, unlink, mkdir, writeFile, lstat } from 'fs/promises';
11
11
  import path, { dirname, join, resolve, relative, isAbsolute } from 'path';
12
12
  import { homedir } from 'os';
13
- import { generateKeyPair, exportJWK, createRemoteJWKSet, jwtVerify } from 'jose';
13
+ import { generateKeyPair, exportJWK, jwtVerify, importJWK } from 'jose';
14
14
  import { createInterface } from 'readline/promises';
15
15
  import { stdin, stdout } from 'process';
16
16
  import { randomBytes } from 'crypto';
@@ -180,19 +180,10 @@ async function postCardCheckout(apiOrigin, bearer, orgId, args) {
180
180
  }
181
181
  });
182
182
  }
183
- async function fetchJwksDocument(apiOrigin) {
184
- const res = await fetch(`${apiOrigin.replace(/\/+$/, "")}/.well-known/jwks.json`, {
185
- redirect: "error",
186
- headers: { Accept: "application/json" }
187
- });
188
- if (!res.ok) throw new SetupApiError(res.status, "JWKS_FETCH", `jwks ${res.status}`);
189
- return res.json();
190
- }
191
- async function getMasterJwkJson(apiOrigin, masterPinJson) {
192
- const { verifySignedJwksDocument } = await import('@clavis.nexus/app');
193
- const doc = await fetchJwksDocument(apiOrigin);
194
- await verifySignedJwksDocument(doc, masterPinJson);
195
- return JSON.stringify(doc.master);
183
+ async function checkTrustRoot(apiOrigin, trustRootJson) {
184
+ const { createVerifier } = await import('@clavis.nexus/app');
185
+ await createVerifier({ trustRoot: trustRootJson, apiOrigin }).trust();
186
+ return JSON.stringify(JSON.parse(trustRootJson));
196
187
  }
197
188
 
198
189
  // src/app-regions.ts
@@ -514,6 +505,17 @@ async function digestOf(value) {
514
505
  const bytes = new TextEncoder().encode(canonicalStringify(value));
515
506
  return `sha256:${bytesToBase64Url(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)))}`;
516
507
  }
508
+ async function valueDigest(type, value) {
509
+ return digestOf({ type, value });
510
+ }
511
+ async function orgPayloadDigest(payload) {
512
+ let value = payload;
513
+ if (payload && typeof payload === "object" && !Array.isArray(payload)) {
514
+ const { proposer_receipt: _receipt, ...rest } = payload;
515
+ value = rest;
516
+ }
517
+ return valueDigest("org.payload@1", value);
518
+ }
517
519
  function orgAnchorPublishStatement(input2) {
518
520
  return { type: "org.anchor.publish@1", domain: input2.domain.trim().toLowerCase(), nonce: input2.nonce, exp: input2.exp };
519
521
  }
@@ -521,7 +523,7 @@ function orgPolicyBootstrapStatement(input2) {
521
523
  return { type: "org.policy.bootstrap@1", org: input2.org.trim(), domain: input2.domain.trim().toLowerCase() };
522
524
  }
523
525
  function orgAppRegisterStatement(input2) {
524
- return { type: "org.app.register@1", org: input2.org.trim(), payload: input2.payload.trim().toLowerCase() };
526
+ return { type: "org.app.register@1", org: input2.org.trim(), payload: input2.payload.trim() };
525
527
  }
526
528
  function orgAppVerifyHostStatement(input2) {
527
529
  return {
@@ -537,7 +539,7 @@ async function orgAppDeclarationStatement(input2) {
537
539
  type: "org.app.declaration@1",
538
540
  org: input2.org.trim(),
539
541
  app: input2.app.trim(),
540
- declaration: input2.declaration === null ? null : await digestOf({ endUsers: input2.declaration.endUsers, uses: input2.declaration.uses })
542
+ declaration: input2.declaration === null ? null : await valueDigest("app.declaration@1", { endUsers: input2.declaration.endUsers, uses: input2.declaration.uses })
541
543
  };
542
544
  }
543
545
  function orgAppRegionsStatement(input2) {
@@ -574,13 +576,6 @@ function sameStatement(a, b) {
574
576
  return false;
575
577
  }
576
578
  }
577
- async function computePayloadCommitmentHex(payload) {
578
- const o = payload && typeof payload === "object" ? { ...payload } : payload;
579
- if (o && typeof o === "object" && !Array.isArray(o)) {
580
- delete o.proposer_receipt;
581
- }
582
- return sha256HexUtf8(canonicalStringify(o));
583
- }
584
579
  async function hashManifestBody(body) {
585
580
  return sha256HexUtf8(canonicalStringify(body));
586
581
  }
@@ -590,14 +585,10 @@ function buildAnchorTxtRecord(args) {
590
585
 
591
586
  // src/ceremony.ts
592
587
  function receiptFromCeremonyPayload(payload) {
593
- if (payload === null || typeof payload !== "object" || Array.isArray(payload)) {
588
+ if (typeof payload !== "string" || payload.length === 0) {
594
589
  throw new Error("Passkey confirmation did not return a receipt.");
595
590
  }
596
- const o = payload;
597
- if (o.type !== "receipt@1" || o.statement === null || typeof o.statement !== "object") {
598
- throw new Error("Passkey confirmation did not return a receipt.");
599
- }
600
- return o;
591
+ return payload;
601
592
  }
602
593
  async function startCeremony(args) {
603
594
  const res = await fetch(`${args.apiOrigin.replace(/\/+$/, "")}/webauthn/authentication/ceremony/start`, {
@@ -729,19 +720,17 @@ async function writeAppEnvFile(appId, contents) {
729
720
 
730
721
  // src/emit-env.ts
731
722
  async function emitAppEnv(args) {
732
- const pin = args.masterPublicJwkJson?.trim() || process.env.CLAVIS_MASTER_PUBLIC_JWK_JSON?.trim() || "";
723
+ const pin = args.trustRoot?.trim() || process.env.CLAVIS_TRUST_ROOT?.trim() || "";
733
724
  if (!pin) {
734
- throw new Error(
735
- "CLAVIS_MASTER_PUBLIC_JWK_JSON is required. Set it to the master public JWK from GET {apiOrigin}/.well-known/jwks.json."
736
- );
725
+ throw new Error("CLAVIS_TRUST_ROOT is required. Set it to the Clavis Master public JWK (docs.clavis.nexus).");
737
726
  }
738
- const master = await getMasterJwkJson(args.origins.apiOrigin, pin);
727
+ const trustRoot = await checkTrustRoot(args.origins.apiOrigin, pin);
739
728
  const operatePath = await saveOperatePrivateJwk(args.appId, args.operatePrivateJwkJson);
740
729
  const envRecord = {
741
730
  CLAVIS_API_ORIGIN: args.origins.apiOrigin,
742
731
  CLAVIS_APP_ID: args.appId,
743
732
  OPERATE_PRIVATE_JWK: args.operatePrivateJwkJson,
744
- MASTER_PUBLIC_JWK_JSON: master
733
+ CLAVIS_TRUST_ROOT: trustRoot
745
734
  };
746
735
  const envFile = await writeAppEnvFile(args.appId, formatEnvExport(envRecord));
747
736
  return {
@@ -777,18 +766,39 @@ async function generateOperateKeyPair() {
777
766
  privateJwkJson: JSON.stringify(priv)
778
767
  };
779
768
  }
780
- var SESSION_SLOT_RE = /^[a-z][a-z0-9]{1,31}-wauthn-auth-jwt(?:-\d{6})?$/;
769
+ var SESSION_SLOT = "wauthn-auth-jwt";
770
+ var SLOT_FIELD = /^[a-z][a-z0-9]{1,31}-slot$/;
771
+ var JWK_MEMBERS = ["kty", "crv", "x", "y", "kid", "alg"];
772
+ async function sessionKey(origin, kid) {
773
+ const res = await fetch(`${origin}/.well-known/jwks.json`, { redirect: "error", headers: { Accept: "application/json" } });
774
+ if (!res.ok) throw new Error(`jwks ${res.status}`);
775
+ const doc = await res.json();
776
+ const key = (doc.keys ?? []).find((k) => k && k.kid === kid);
777
+ if (!key) return null;
778
+ const slot = Object.entries(key).find(([name]) => SLOT_FIELD.test(name))?.[1];
779
+ if (slot !== SESSION_SLOT) return null;
780
+ const jwk = {};
781
+ for (const m of JWK_MEMBERS) if (key[m] !== void 0) jwk[m] = key[m];
782
+ return importJWK(jwk, "ES256");
783
+ }
781
784
  async function verifyDevelopersAccessToken(accessToken, apiOrigin) {
782
785
  const origin = new URL(apiOrigin).origin;
783
- const jwks = createRemoteJWKSet(new URL(`${origin}/.well-known/jwks.json`));
784
- const { payload, protectedHeader } = await jwtVerify(accessToken, jwks, {
785
- algorithms: ["ES256"],
786
- issuer: origin,
787
- audience: origin,
788
- requiredClaims: ["exp", "iss", "aud", "sub"]
786
+ let refused = false;
787
+ const { payload } = await jwtVerify(
788
+ accessToken,
789
+ async (header) => {
790
+ const key = typeof header.kid === "string" ? await sessionKey(origin, header.kid) : null;
791
+ if (!key) {
792
+ refused = true;
793
+ throw new Error("invalid_access_token_kid");
794
+ }
795
+ return key;
796
+ },
797
+ { algorithms: ["ES256"], issuer: origin, audience: origin, requiredClaims: ["exp", "iss", "aud", "sub"] }
798
+ ).catch((e) => {
799
+ if (refused) throw new Error("invalid_access_token_kid");
800
+ throw e;
789
801
  });
790
- const kid = typeof protectedHeader.kid === "string" ? protectedHeader.kid : "";
791
- if (!SESSION_SLOT_RE.test(kid)) throw new Error("invalid_access_token_kid");
792
802
  if (payload.tk !== "access") throw new Error("invalid_access_token_kind");
793
803
  if (typeof payload.sub !== "string" || payload.sub.length === 0) throw new Error("invalid_access_token_sub");
794
804
  if (typeof payload.exp !== "number" || !Number.isFinite(payload.exp)) throw new Error("invalid_access_token_exp");
@@ -1479,7 +1489,7 @@ function createSetupClient(options = {}) {
1479
1489
  operate_public_jwk: keys.publicJwk,
1480
1490
  redirect_paths: paths
1481
1491
  };
1482
- const payload_hash = await computePayloadCommitmentHex(manifest);
1492
+ const payload_hash = await orgPayloadDigest(manifest);
1483
1493
  const cer = await runPasskeyCeremony({
1484
1494
  origins: auth.data.origins,
1485
1495
  bearer: auth.data.session.accessToken,
@@ -1887,7 +1897,7 @@ var TOOLS = [
1887
1897
  },
1888
1898
  {
1889
1899
  name: "clavis_emit_app_env",
1890
- description: "Emit environment variables for @clavis.nexus/app after setup.",
1900
+ description: "Emit environment variables for @clavis.nexus/app after setup. Needs CLAVIS_TRUST_ROOT (the Clavis Master public JWK) in the environment.",
1891
1901
  inputSchema: {
1892
1902
  type: "object",
1893
1903
  properties: {