@agent-score/commerce 2.4.0 → 2.5.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/dist/challenge/index.d.mts +5 -1
- package/dist/challenge/index.d.ts +5 -1
- package/dist/challenge/index.js +11 -4
- package/dist/challenge/index.js.map +1 -1
- package/dist/challenge/index.mjs +11 -4
- package/dist/challenge/index.mjs.map +1 -1
- package/dist/{checkout-B-MIzYzW.d.ts → checkout-McfNpZJf.d.ts} +72 -1
- package/dist/{checkout-Bn7ZKIBD.d.mts → checkout-o17dIxHi.d.mts} +72 -1
- package/dist/core.d.mts +23 -1
- package/dist/core.d.ts +23 -1
- package/dist/core.js +25 -11
- package/dist/core.js.map +1 -1
- package/dist/core.mjs +25 -11
- package/dist/core.mjs.map +1 -1
- package/dist/discovery/index.d.mts +17 -2
- package/dist/discovery/index.d.ts +17 -2
- package/dist/discovery/index.js +14 -4
- package/dist/discovery/index.js.map +1 -1
- package/dist/discovery/index.mjs +14 -4
- package/dist/discovery/index.mjs.map +1 -1
- package/dist/gate-CWP10xPQ.d.mts +339 -0
- package/dist/gate-CWP10xPQ.d.ts +339 -0
- package/dist/identity/express.d.mts +26 -1
- package/dist/identity/express.d.ts +26 -1
- package/dist/identity/express.js +587 -13
- package/dist/identity/express.js.map +1 -1
- package/dist/identity/express.mjs +583 -12
- package/dist/identity/express.mjs.map +1 -1
- package/dist/identity/fastify.d.mts +12 -2
- package/dist/identity/fastify.d.ts +12 -2
- package/dist/identity/fastify.js +595 -13
- package/dist/identity/fastify.js.map +1 -1
- package/dist/identity/fastify.mjs +591 -12
- package/dist/identity/fastify.mjs.map +1 -1
- package/dist/identity/hono.d.mts +26 -1
- package/dist/identity/hono.d.ts +26 -1
- package/dist/identity/hono.js +576 -13
- package/dist/identity/hono.js.map +1 -1
- package/dist/identity/hono.mjs +572 -12
- package/dist/identity/hono.mjs.map +1 -1
- package/dist/identity/nextjs.d.mts +3 -0
- package/dist/identity/nextjs.d.ts +3 -0
- package/dist/identity/nextjs.js +581 -13
- package/dist/identity/nextjs.js.map +1 -1
- package/dist/identity/nextjs.mjs +577 -12
- package/dist/identity/nextjs.mjs.map +1 -1
- package/dist/identity/web.d.mts +24 -1
- package/dist/identity/web.d.ts +24 -1
- package/dist/identity/web.js +581 -13
- package/dist/identity/web.js.map +1 -1
- package/dist/identity/web.mjs +577 -12
- package/dist/identity/web.mjs.map +1 -1
- package/dist/index.d.mts +119 -3
- package/dist/index.d.ts +119 -3
- package/dist/index.js +893 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +873 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
package/dist/identity/web.js
CHANGED
|
@@ -21,9 +21,12 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var web_exports = {};
|
|
22
22
|
__export(web_exports, {
|
|
23
23
|
createAgentScoreGate: () => createAgentScoreGate,
|
|
24
|
+
createAipGate: () => createAipGate,
|
|
24
25
|
createConditionalAgentScoreGate: () => createConditionalAgentScoreGate,
|
|
25
26
|
withAgentScoreGate: () => withAgentScoreGate,
|
|
26
|
-
|
|
27
|
+
withAipGate: () => withAipGate,
|
|
28
|
+
withConditionalAgentScoreGate: () => withConditionalAgentScoreGate,
|
|
29
|
+
withConditionalAipGate: () => withConditionalAipGate
|
|
27
30
|
});
|
|
28
31
|
module.exports = __toCommonJS(web_exports);
|
|
29
32
|
|
|
@@ -160,7 +163,520 @@ function denialReasonToBody(reason) {
|
|
|
160
163
|
return body;
|
|
161
164
|
}
|
|
162
165
|
|
|
166
|
+
// src/aip/verify.ts
|
|
167
|
+
var import_jose2 = require("jose");
|
|
168
|
+
|
|
169
|
+
// src/aip/http-signature.ts
|
|
170
|
+
var import_jose = require("jose");
|
|
171
|
+
var { subtle } = globalThis.crypto;
|
|
172
|
+
var b64ToBytes = (b64) => {
|
|
173
|
+
const bin = atob(b64);
|
|
174
|
+
const out = new Uint8Array(bin.length);
|
|
175
|
+
for (let i = 0; i < bin.length; i++) {
|
|
176
|
+
out[i] = bin.charCodeAt(i);
|
|
177
|
+
}
|
|
178
|
+
return out;
|
|
179
|
+
};
|
|
180
|
+
var AIP_COVERED_COMPONENTS = ["@method", "@authority", "@path", "agent-identity"];
|
|
181
|
+
var AIP_SIGNATURE_TAG = "agent-identity";
|
|
182
|
+
var DEFAULT_MAX_SKEW_SECONDS = 60;
|
|
183
|
+
var normalizeAuthority = (authority) => {
|
|
184
|
+
const lower = authority.trim().toLowerCase();
|
|
185
|
+
const colon = lower.lastIndexOf(":");
|
|
186
|
+
if (colon === -1) {
|
|
187
|
+
return lower;
|
|
188
|
+
}
|
|
189
|
+
if (lower.includes("]") && colon < lower.indexOf("]")) {
|
|
190
|
+
return lower;
|
|
191
|
+
}
|
|
192
|
+
const host = lower.slice(0, colon);
|
|
193
|
+
const port = lower.slice(colon + 1);
|
|
194
|
+
if (port === "80" || port === "443") {
|
|
195
|
+
return host;
|
|
196
|
+
}
|
|
197
|
+
return lower;
|
|
198
|
+
};
|
|
199
|
+
var componentValue = (name, input) => {
|
|
200
|
+
switch (name) {
|
|
201
|
+
case "@method":
|
|
202
|
+
return input.method.toUpperCase();
|
|
203
|
+
case "@authority":
|
|
204
|
+
return normalizeAuthority(input.authority);
|
|
205
|
+
case "@path":
|
|
206
|
+
return input.path;
|
|
207
|
+
case "agent-identity":
|
|
208
|
+
return input.agentIdentity.trim();
|
|
209
|
+
default:
|
|
210
|
+
return input.extra?.[name] ?? null;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var serializeComponentList = (components) => `(${components.map((c) => `"${c}"`).join(" ")})`;
|
|
214
|
+
var serializeParams = (p) => {
|
|
215
|
+
const parts = [];
|
|
216
|
+
if (p.created !== void 0) {
|
|
217
|
+
parts.push(`created=${p.created}`);
|
|
218
|
+
}
|
|
219
|
+
if (p.expires !== void 0) {
|
|
220
|
+
parts.push(`expires=${p.expires}`);
|
|
221
|
+
}
|
|
222
|
+
if (p.keyid !== void 0) {
|
|
223
|
+
parts.push(`keyid="${p.keyid}"`);
|
|
224
|
+
}
|
|
225
|
+
if (p.alg !== void 0) {
|
|
226
|
+
parts.push(`alg="${p.alg}"`);
|
|
227
|
+
}
|
|
228
|
+
if (p.tag !== void 0) {
|
|
229
|
+
parts.push(`tag="${p.tag}"`);
|
|
230
|
+
}
|
|
231
|
+
return parts.map((s) => `;${s}`).join("");
|
|
232
|
+
};
|
|
233
|
+
var buildSignatureBase = (params, input) => {
|
|
234
|
+
const lines = [];
|
|
235
|
+
for (const name of params.components) {
|
|
236
|
+
const value = componentValue(name, input);
|
|
237
|
+
if (value === null) {
|
|
238
|
+
throw new MissingComponentError(name);
|
|
239
|
+
}
|
|
240
|
+
lines.push(`"${name}": ${value}`);
|
|
241
|
+
}
|
|
242
|
+
const paramsValue = serializeComponentList(params.components) + serializeParams(params);
|
|
243
|
+
lines.push(`"@signature-params": ${paramsValue}`);
|
|
244
|
+
return lines.join("\n");
|
|
245
|
+
};
|
|
246
|
+
var MissingComponentError = class extends Error {
|
|
247
|
+
constructor(component) {
|
|
248
|
+
super(`signature base missing covered component: ${component}`);
|
|
249
|
+
this.component = component;
|
|
250
|
+
this.name = "MissingComponentError";
|
|
251
|
+
}
|
|
252
|
+
component;
|
|
253
|
+
};
|
|
254
|
+
var parseSignatureInput = (header, tag = AIP_SIGNATURE_TAG) => {
|
|
255
|
+
const members = splitDictionary(header);
|
|
256
|
+
if (members.length === 0) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
const parsed = members.map((m) => {
|
|
260
|
+
const params = parseInnerListMember(m.value);
|
|
261
|
+
return params ? { label: m.label, params } : null;
|
|
262
|
+
}).filter((x) => x !== null);
|
|
263
|
+
if (parsed.length === 0) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
const tagged = parsed.find((p) => p.params.tag === tag);
|
|
267
|
+
if (tagged) {
|
|
268
|
+
return tagged;
|
|
269
|
+
}
|
|
270
|
+
if (parsed.length === 1 && parsed[0].params.tag === void 0) {
|
|
271
|
+
return parsed[0];
|
|
272
|
+
}
|
|
273
|
+
return null;
|
|
274
|
+
};
|
|
275
|
+
var parseSignatureValue = (header, label) => {
|
|
276
|
+
const members = splitDictionary(header);
|
|
277
|
+
const member = members.find((m) => m.label === label);
|
|
278
|
+
if (!member) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
const v = member.value.trim();
|
|
282
|
+
if (!v.startsWith(":") || !v.endsWith(":") || v.length < 2) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
const b64 = v.slice(1, -1);
|
|
286
|
+
try {
|
|
287
|
+
return b64ToBytes(b64);
|
|
288
|
+
} catch {
|
|
289
|
+
return null;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
var splitDictionary = (header) => {
|
|
293
|
+
const out = [];
|
|
294
|
+
let depth = 0;
|
|
295
|
+
let inBytes = false;
|
|
296
|
+
let inString = false;
|
|
297
|
+
let current = "";
|
|
298
|
+
for (let i = 0; i < header.length; i++) {
|
|
299
|
+
const ch = header[i];
|
|
300
|
+
if (inString) {
|
|
301
|
+
current += ch;
|
|
302
|
+
if (ch === '"' && header[i - 1] !== "\\") {
|
|
303
|
+
inString = false;
|
|
304
|
+
}
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (ch === '"') {
|
|
308
|
+
inString = true;
|
|
309
|
+
current += ch;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (ch === ":") {
|
|
313
|
+
inBytes = !inBytes;
|
|
314
|
+
current += ch;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (!inBytes && ch === "(") {
|
|
318
|
+
depth++;
|
|
319
|
+
current += ch;
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
if (!inBytes && ch === ")") {
|
|
323
|
+
depth = Math.max(0, depth - 1);
|
|
324
|
+
current += ch;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
if (!inBytes && depth === 0 && ch === ",") {
|
|
328
|
+
pushMember(out, current);
|
|
329
|
+
current = "";
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
current += ch;
|
|
333
|
+
}
|
|
334
|
+
pushMember(out, current);
|
|
335
|
+
return out;
|
|
336
|
+
};
|
|
337
|
+
var pushMember = (out, raw) => {
|
|
338
|
+
const trimmed = raw.trim();
|
|
339
|
+
if (!trimmed) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const eq = trimmed.indexOf("=");
|
|
343
|
+
if (eq === -1) {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const label = trimmed.slice(0, eq).trim();
|
|
347
|
+
const value = trimmed.slice(eq + 1).trim();
|
|
348
|
+
if (label) {
|
|
349
|
+
out.push({ label, value });
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
var parseInnerListMember = (value) => {
|
|
353
|
+
const open = value.indexOf("(");
|
|
354
|
+
const close = value.indexOf(")", open + 1);
|
|
355
|
+
if (open === -1 || close === -1) {
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
const listBody = value.slice(open + 1, close).trim();
|
|
359
|
+
const components = listBody.length === 0 ? [] : (listBody.match(/"[^"]*"/g) ?? []).map((s) => s.slice(1, -1));
|
|
360
|
+
const params = { components };
|
|
361
|
+
const paramStr = value.slice(close + 1);
|
|
362
|
+
const re = /;\s*([a-zA-Z][a-zA-Z0-9_-]*)\s*=\s*("(?:[^"\\]|\\.)*"|-?\d+)/g;
|
|
363
|
+
let match;
|
|
364
|
+
while ((match = re.exec(paramStr)) !== null) {
|
|
365
|
+
const key = match[1];
|
|
366
|
+
const raw = match[2];
|
|
367
|
+
const val = raw.startsWith('"') ? raw.slice(1, -1) : Number(raw);
|
|
368
|
+
if (key === "created") {
|
|
369
|
+
params.created = val;
|
|
370
|
+
} else if (key === "expires") {
|
|
371
|
+
params.expires = val;
|
|
372
|
+
} else if (key === "keyid") {
|
|
373
|
+
params.keyid = val;
|
|
374
|
+
} else if (key === "tag") {
|
|
375
|
+
params.tag = val;
|
|
376
|
+
} else if (key === "alg") {
|
|
377
|
+
params.alg = val;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return params;
|
|
381
|
+
};
|
|
382
|
+
var verifyMessageSignature = async (input) => {
|
|
383
|
+
const selected = parseSignatureInput(input.signatureInput);
|
|
384
|
+
if (!selected) {
|
|
385
|
+
return { ok: false, reason: "no_aip_signature" };
|
|
386
|
+
}
|
|
387
|
+
const { label, params } = selected;
|
|
388
|
+
if (params.alg !== void 0 && !["ed25519", "eddsa"].includes(params.alg.toLowerCase())) {
|
|
389
|
+
return { ok: false, reason: "unsupported_alg" };
|
|
390
|
+
}
|
|
391
|
+
for (const required of AIP_COVERED_COMPONENTS) {
|
|
392
|
+
if (!params.components.includes(required)) {
|
|
393
|
+
return { ok: false, reason: "missing_covered_component" };
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const now = input.now ?? Math.floor(Date.now() / 1e3);
|
|
397
|
+
const skew = input.maxSkewSeconds ?? DEFAULT_MAX_SKEW_SECONDS;
|
|
398
|
+
if (params.created !== void 0 && params.created > now + skew) {
|
|
399
|
+
return { ok: false, reason: "created_in_future" };
|
|
400
|
+
}
|
|
401
|
+
if (params.expires !== void 0 && params.expires < now - skew) {
|
|
402
|
+
return { ok: false, reason: "expired" };
|
|
403
|
+
}
|
|
404
|
+
if (!params.keyid) {
|
|
405
|
+
return { ok: false, reason: "missing_keyid" };
|
|
406
|
+
}
|
|
407
|
+
const cnf = input.cnfJwk;
|
|
408
|
+
if (cnf.kty !== "OKP" || cnf.crv !== "Ed25519" || typeof cnf.x !== "string" || cnf.x.length === 0) {
|
|
409
|
+
return { ok: false, reason: "unsupported_cnf_key" };
|
|
410
|
+
}
|
|
411
|
+
let thumbprint;
|
|
412
|
+
try {
|
|
413
|
+
thumbprint = await (0, import_jose.calculateJwkThumbprint)(input.cnfJwk, "sha256");
|
|
414
|
+
} catch {
|
|
415
|
+
return { ok: false, reason: "unsupported_cnf_key" };
|
|
416
|
+
}
|
|
417
|
+
if (params.keyid !== thumbprint) {
|
|
418
|
+
return { ok: false, reason: "keyid_mismatch" };
|
|
419
|
+
}
|
|
420
|
+
const sig = parseSignatureValue(input.signature, label);
|
|
421
|
+
if (!sig) {
|
|
422
|
+
return { ok: false, reason: "malformed_signature" };
|
|
423
|
+
}
|
|
424
|
+
let base;
|
|
425
|
+
try {
|
|
426
|
+
base = buildSignatureBase(params, {
|
|
427
|
+
method: input.method,
|
|
428
|
+
authority: input.authority,
|
|
429
|
+
path: input.path,
|
|
430
|
+
agentIdentity: input.agentIdentity,
|
|
431
|
+
extra: input.extraComponents
|
|
432
|
+
});
|
|
433
|
+
} catch (err) {
|
|
434
|
+
if (err instanceof MissingComponentError) {
|
|
435
|
+
return { ok: false, reason: "missing_covered_component" };
|
|
436
|
+
}
|
|
437
|
+
throw err;
|
|
438
|
+
}
|
|
439
|
+
let valid;
|
|
440
|
+
try {
|
|
441
|
+
const key = await (0, import_jose.importJWK)(input.cnfJwk, "EdDSA");
|
|
442
|
+
if (!(key instanceof CryptoKey)) {
|
|
443
|
+
return { ok: false, reason: "signature_invalid" };
|
|
444
|
+
}
|
|
445
|
+
valid = await subtle.verify(
|
|
446
|
+
{ name: "Ed25519" },
|
|
447
|
+
key,
|
|
448
|
+
sig,
|
|
449
|
+
new TextEncoder().encode(base)
|
|
450
|
+
);
|
|
451
|
+
} catch {
|
|
452
|
+
return { ok: false, reason: "signature_invalid" };
|
|
453
|
+
}
|
|
454
|
+
return valid ? { ok: true, params } : { ok: false, reason: "signature_invalid" };
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// src/aip/types.ts
|
|
458
|
+
var isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
459
|
+
var isNonEmptyString = (v) => typeof v === "string" && v.length > 0;
|
|
460
|
+
var isAitShape = (payload) => isObject(payload) && isObject(payload.cnf) && isObject(payload.agent);
|
|
461
|
+
var validateAitPayload = (payload) => {
|
|
462
|
+
if (!isObject(payload)) {
|
|
463
|
+
return { ok: false, reason: "not_an_object" };
|
|
464
|
+
}
|
|
465
|
+
if (!isNonEmptyString(payload.aip_version)) {
|
|
466
|
+
return { ok: false, reason: "missing_aip_version" };
|
|
467
|
+
}
|
|
468
|
+
if (!isNonEmptyString(payload.iss)) {
|
|
469
|
+
return { ok: false, reason: "missing_iss" };
|
|
470
|
+
}
|
|
471
|
+
if (!isNonEmptyString(payload.sub)) {
|
|
472
|
+
return { ok: false, reason: "missing_sub" };
|
|
473
|
+
}
|
|
474
|
+
if (typeof payload.iat !== "number") {
|
|
475
|
+
return { ok: false, reason: "missing_iat" };
|
|
476
|
+
}
|
|
477
|
+
if (typeof payload.exp !== "number") {
|
|
478
|
+
return { ok: false, reason: "missing_exp" };
|
|
479
|
+
}
|
|
480
|
+
if (!isObject(payload.cnf) || !isObject(payload.cnf.jwk)) {
|
|
481
|
+
return { ok: false, reason: "missing_cnf" };
|
|
482
|
+
}
|
|
483
|
+
if (!isObject(payload.agent) || !isNonEmptyString(payload.agent.provider)) {
|
|
484
|
+
return { ok: false, reason: "missing_agent_provider" };
|
|
485
|
+
}
|
|
486
|
+
if (payload.trust_level === "human_confirmed") {
|
|
487
|
+
const auth = payload.auth;
|
|
488
|
+
const amr = isObject(auth) ? auth.amr : void 0;
|
|
489
|
+
if (!Array.isArray(amr) || amr.length === 0) {
|
|
490
|
+
return { ok: false, reason: "human_confirmed_without_amr" };
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return { ok: true, payload };
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// src/aip/verify.ts
|
|
497
|
+
var AGENT_IDENTITY_HEADER = "agent-identity";
|
|
498
|
+
var verifyAit = async (ctx, opts) => {
|
|
499
|
+
if (ctx.agentIdentityHeaders.length === 0) {
|
|
500
|
+
return { ok: false, reason: "no_token" };
|
|
501
|
+
}
|
|
502
|
+
if (!ctx.signatureInput || !ctx.signature) {
|
|
503
|
+
return { ok: false, reason: "pop_signature_missing" };
|
|
504
|
+
}
|
|
505
|
+
let lastFailure = "malformed_token";
|
|
506
|
+
for (const raw of ctx.agentIdentityHeaders) {
|
|
507
|
+
const token = stripBearer(raw);
|
|
508
|
+
let header;
|
|
509
|
+
let payload;
|
|
510
|
+
try {
|
|
511
|
+
header = (0, import_jose2.decodeProtectedHeader)(token);
|
|
512
|
+
payload = (0, import_jose2.decodeJwt)(token);
|
|
513
|
+
} catch {
|
|
514
|
+
lastFailure = "malformed_token";
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
if (header.alg === void 0 || header.alg.toLowerCase() === "none") {
|
|
518
|
+
lastFailure = "malformed_token";
|
|
519
|
+
continue;
|
|
520
|
+
}
|
|
521
|
+
if (!isAitShape(payload)) {
|
|
522
|
+
lastFailure = "malformed_token";
|
|
523
|
+
continue;
|
|
524
|
+
}
|
|
525
|
+
const validated = validateAitPayload(payload);
|
|
526
|
+
if (!validated.ok) {
|
|
527
|
+
lastFailure = "invalid_claims";
|
|
528
|
+
continue;
|
|
529
|
+
}
|
|
530
|
+
const claims = validated.payload;
|
|
531
|
+
const keyLookup = await opts.jwks.getKey(claims.iss, header.kid);
|
|
532
|
+
if (!keyLookup.ok) {
|
|
533
|
+
lastFailure = keyLookup.reason === "untrusted_issuer" || keyLookup.reason === "insecure_issuer" ? "untrusted_issuer" : "key_unavailable";
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const jwtClockTolerance = opts.maxSkewSeconds ?? 60;
|
|
537
|
+
try {
|
|
538
|
+
const idpKey = await (0, import_jose2.importJWK)(keyLookup.key, normalizeAlg(header.alg));
|
|
539
|
+
await (0, import_jose2.jwtVerify)(token, idpKey, {
|
|
540
|
+
// Pin the signature algorithm allowlist (RFC 8725 §3.1) — also rejects `alg:none`. Without
|
|
541
|
+
// this, jose accepts whatever alg the resolved JWK supports, so a trusted IdP publishing a
|
|
542
|
+
// non-Ed25519 (e.g. RSA/EC) `use:sig` key would let an attacker present an RS256/ES256
|
|
543
|
+
// token that verifies. Matches the server-side allowlist in core/api aip-verify.
|
|
544
|
+
algorithms: AIT_SIGNING_ALGS,
|
|
545
|
+
clockTolerance: jwtClockTolerance,
|
|
546
|
+
currentDate: opts.now !== void 0 ? new Date(opts.now * 1e3) : void 0
|
|
547
|
+
});
|
|
548
|
+
} catch (err) {
|
|
549
|
+
lastFailure = isExpiry(err) ? "expired_token" : "idp_signature_invalid";
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
const nowSec = opts.now ?? Math.floor(Date.now() / 1e3);
|
|
553
|
+
if (claims.iat > nowSec + jwtClockTolerance) {
|
|
554
|
+
lastFailure = "expired_token";
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
const popResult = await verifyMessageSignature({
|
|
558
|
+
method: ctx.method,
|
|
559
|
+
authority: ctx.authority,
|
|
560
|
+
path: ctx.path,
|
|
561
|
+
agentIdentity: raw,
|
|
562
|
+
signatureInput: ctx.signatureInput,
|
|
563
|
+
signature: ctx.signature,
|
|
564
|
+
cnfJwk: claims.cnf.jwk,
|
|
565
|
+
now: opts.now,
|
|
566
|
+
maxSkewSeconds: opts.maxSkewSeconds
|
|
567
|
+
});
|
|
568
|
+
if (!popResult.ok) {
|
|
569
|
+
lastFailure = "pop_signature_invalid";
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
return { ok: true, ait: { payload: claims, iss: claims.iss, cnfJwk: claims.cnf.jwk, token } };
|
|
573
|
+
}
|
|
574
|
+
return { ok: false, reason: lastFailure };
|
|
575
|
+
};
|
|
576
|
+
var stripBearer = (value) => {
|
|
577
|
+
const trimmed = value.trim();
|
|
578
|
+
return /^bearer\s+/i.test(trimmed) ? trimmed.replace(/^bearer\s+/i, "") : trimmed;
|
|
579
|
+
};
|
|
580
|
+
var AIT_SIGNING_ALGS = ["EdDSA", "ES256"];
|
|
581
|
+
var normalizeAlg = (alg) => alg.toLowerCase() === "eddsa" ? "EdDSA" : alg;
|
|
582
|
+
var isExpiry = (err) => typeof err === "object" && err !== null && err.code === "ERR_JWT_EXPIRED";
|
|
583
|
+
|
|
584
|
+
// src/aip/request.ts
|
|
585
|
+
var readAgentIdentityHeaders = (headers) => {
|
|
586
|
+
const raw = headers.get(AGENT_IDENTITY_HEADER);
|
|
587
|
+
if (!raw) {
|
|
588
|
+
return [];
|
|
589
|
+
}
|
|
590
|
+
return raw.split(",").map((s) => s.trim()).filter((s) => s.length > 0);
|
|
591
|
+
};
|
|
592
|
+
var deriveAuthority = (req, url) => req.headers.get("host") ?? url.host;
|
|
593
|
+
var buildVerifyContextFromRequest = (req) => {
|
|
594
|
+
const url = new URL(req.url);
|
|
595
|
+
return {
|
|
596
|
+
method: req.method,
|
|
597
|
+
authority: deriveAuthority(req, url),
|
|
598
|
+
path: url.pathname,
|
|
599
|
+
agentIdentityHeaders: readAgentIdentityHeaders(req.headers),
|
|
600
|
+
signatureInput: req.headers.get("signature-input"),
|
|
601
|
+
signature: req.headers.get("signature")
|
|
602
|
+
};
|
|
603
|
+
};
|
|
604
|
+
var hasAgentIdentityHeader = (req) => readAgentIdentityHeaders(req.headers).length > 0;
|
|
605
|
+
|
|
606
|
+
// src/aip/gate.ts
|
|
607
|
+
var verifyFromContext = async (ctx, opts) => {
|
|
608
|
+
const result = await verifyAit(ctx, { jwks: opts.jwks, now: opts.now, maxSkewSeconds: opts.maxSkewSeconds });
|
|
609
|
+
return result.ok ? { ok: true, ait: result.ait } : { ok: false, failure: result.reason };
|
|
610
|
+
};
|
|
611
|
+
var verifyAitRequest = (req, opts) => verifyFromContext(buildVerifyContextFromRequest(req), opts);
|
|
612
|
+
var aipErrorCode = (failure) => {
|
|
613
|
+
switch (failure) {
|
|
614
|
+
case "no_token":
|
|
615
|
+
case "pop_signature_missing":
|
|
616
|
+
return "agent_identity_required";
|
|
617
|
+
case "untrusted_issuer":
|
|
618
|
+
return "untrusted_issuer";
|
|
619
|
+
case "expired_token":
|
|
620
|
+
return "expired_token";
|
|
621
|
+
case "invalid_claims":
|
|
622
|
+
return "insufficient_claims";
|
|
623
|
+
case "key_unavailable":
|
|
624
|
+
return "idp_unavailable";
|
|
625
|
+
case "malformed_token":
|
|
626
|
+
case "idp_signature_invalid":
|
|
627
|
+
case "pop_signature_invalid":
|
|
628
|
+
return "invalid_signature";
|
|
629
|
+
default:
|
|
630
|
+
return "invalid_signature";
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
var aipErrorStatus = (failure) => {
|
|
634
|
+
switch (failure) {
|
|
635
|
+
case "key_unavailable":
|
|
636
|
+
return 503;
|
|
637
|
+
case "untrusted_issuer":
|
|
638
|
+
case "invalid_claims":
|
|
639
|
+
return 403;
|
|
640
|
+
default:
|
|
641
|
+
return 401;
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
var aipErrorDetail = (failure) => {
|
|
645
|
+
switch (failure) {
|
|
646
|
+
case "no_token":
|
|
647
|
+
return "No Agent-Identity token was presented.";
|
|
648
|
+
case "pop_signature_missing":
|
|
649
|
+
return "The request is missing the RFC 9421 HTTP Message Signature that proves possession of the token-bound key.";
|
|
650
|
+
case "untrusted_issuer":
|
|
651
|
+
return "The token's issuer is not in this service's trusted-issuer list.";
|
|
652
|
+
case "expired_token":
|
|
653
|
+
return "The Agent Identity Token has expired.";
|
|
654
|
+
case "invalid_claims":
|
|
655
|
+
return "The token is missing required claims for this endpoint.";
|
|
656
|
+
case "malformed_token":
|
|
657
|
+
return "The Agent-Identity header could not be parsed as an Agent Identity Token.";
|
|
658
|
+
case "idp_signature_invalid":
|
|
659
|
+
return "The identity provider's signature on the token failed verification.";
|
|
660
|
+
case "pop_signature_invalid":
|
|
661
|
+
return "The request signature did not match the key bound to the token.";
|
|
662
|
+
case "key_unavailable":
|
|
663
|
+
return "The identity provider's signing key could not be resolved.";
|
|
664
|
+
default:
|
|
665
|
+
return "Token verification failed.";
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
var buildAipErrorBody = (failure) => {
|
|
669
|
+
const code = aipErrorCode(failure);
|
|
670
|
+
return {
|
|
671
|
+
type: `urn:aip:error:${code}`,
|
|
672
|
+
title: code.replace(/_/g, " "),
|
|
673
|
+
status: aipErrorStatus(failure),
|
|
674
|
+
detail: aipErrorDetail(failure)
|
|
675
|
+
};
|
|
676
|
+
};
|
|
677
|
+
|
|
163
678
|
// src/core.ts
|
|
679
|
+
var import_node_crypto = require("crypto");
|
|
164
680
|
var import_sdk = require("@agent-score/sdk");
|
|
165
681
|
|
|
166
682
|
// src/address.ts
|
|
@@ -255,8 +771,9 @@ var INVALID_CREDENTIAL_INSTRUCTIONS = JSON.stringify({
|
|
|
255
771
|
],
|
|
256
772
|
user_message: "The operator_token is not recognized. Use a different stored token, or restart the verification session flow to mint a new one."
|
|
257
773
|
});
|
|
258
|
-
function buildAgentMemoryHint() {
|
|
774
|
+
function buildAgentMemoryHint(aipTrustedIssuers) {
|
|
259
775
|
const api = CANONICAL_AGENTSCORE_API;
|
|
776
|
+
const acceptsAip = aipTrustedIssuers !== void 0 && aipTrustedIssuers.length > 0;
|
|
260
777
|
return {
|
|
261
778
|
save_for_future_agentscore_gates: true,
|
|
262
779
|
pattern_summary: "AgentScore is a cross-merchant identity substrate for agent commerce. Any endpoint that returns 403 missing_identity / identity_verification_required or 402 challenges referencing AgentScore uses the same identity system. One KYC works across all AgentScore merchants \u2014 no re-verification per site. Save this pattern (and the endpoints below) to persistent memory so future AgentScore encounters skip the cold-start session flow.",
|
|
@@ -264,8 +781,12 @@ function buildAgentMemoryHint() {
|
|
|
264
781
|
identity_check_endpoint: `${api}/v1/credentials`,
|
|
265
782
|
identity_paths: {
|
|
266
783
|
wallet: "If paying from a crypto wallet you have used before on any AgentScore merchant, send `X-Wallet-Address: 0x...`. Shortest path; no token lifecycle to manage.",
|
|
267
|
-
operator_token: "For any rail (Stripe SPT, card, or a wallet you have not linked yet), send `X-Operator-Token: opc_...`. Reusable across merchants until the token expires."
|
|
784
|
+
operator_token: "For any rail (Stripe SPT, card, or a wallet you have not linked yet), send `X-Operator-Token: opc_...`. Reusable across merchants until the token expires.",
|
|
785
|
+
...acceptsAip && {
|
|
786
|
+
agent_identity: 'If you hold an AIP Agent Identity Token from a trusted issuer (see aip_trusted_issuers), present it: send the JWT in an `Agent-Identity` header plus an RFC 9421 HTTP Message Signature (Signature-Input + Signature over @method @authority @path agent-identity, tag="agent-identity") signed with the token-bound cnf key. The merchant verifies it and applies its policy \u2014 no separate AgentScore credential needed.'
|
|
787
|
+
}
|
|
268
788
|
},
|
|
789
|
+
...acceptsAip && { aip_trusted_issuers: aipTrustedIssuers },
|
|
269
790
|
bootstrap: "If you have neither a linked wallet nor a valid operator_token, follow the session/verify flow in the per-request `agent_instructions` block. This happens at most once per agent identity \u2014 after first verification, the operator_token and any subsequently used wallet are reusable everywhere.",
|
|
270
791
|
do_not_persist_in_memory: ["operator_token", "poll_secret"],
|
|
271
792
|
persist_in_credential_store: ["operator_token"]
|
|
@@ -287,11 +808,12 @@ function createAgentScoreCore(options) {
|
|
|
287
808
|
baseUrl: rawBaseUrl = "https://api.agentscore.sh",
|
|
288
809
|
chain: gateChain,
|
|
289
810
|
userAgent,
|
|
290
|
-
createSessionOnMissing
|
|
811
|
+
createSessionOnMissing,
|
|
812
|
+
aipTrustedIssuers
|
|
291
813
|
} = options;
|
|
292
814
|
const baseUrl = stripTrailingSlashes(rawBaseUrl);
|
|
293
|
-
const agentMemoryHint = buildAgentMemoryHint();
|
|
294
|
-
const defaultUa = `@agent-score/commerce@${"2.
|
|
815
|
+
const agentMemoryHint = buildAgentMemoryHint(aipTrustedIssuers);
|
|
816
|
+
const defaultUa = `@agent-score/commerce@${"2.5.0"}`;
|
|
295
817
|
const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;
|
|
296
818
|
const sdk = new import_sdk.AgentScore({ apiKey, baseUrl, userAgent: userAgentHeader });
|
|
297
819
|
const sessionSdkCache = /* @__PURE__ */ new Map();
|
|
@@ -309,6 +831,7 @@ function createAgentScoreCore(options) {
|
|
|
309
831
|
return s;
|
|
310
832
|
}
|
|
311
833
|
const cache = new TTLCache(cacheSeconds * 1e3);
|
|
834
|
+
const lastSignerRaw = /* @__PURE__ */ new Map();
|
|
312
835
|
async function tryMintSessionDenial(ctx) {
|
|
313
836
|
if (!createSessionOnMissing) return void 0;
|
|
314
837
|
try {
|
|
@@ -366,13 +889,18 @@ function createAgentScoreCore(options) {
|
|
|
366
889
|
}
|
|
367
890
|
}
|
|
368
891
|
async function evaluate(identity, ctx, signer) {
|
|
369
|
-
if (!identity || !identity.address && !identity.operatorToken) {
|
|
892
|
+
if (!identity || !identity.address?.trim() && !identity.operatorToken?.trim() && !identity.aipToken?.trim()) {
|
|
370
893
|
if (failOpen) return { kind: "allow" };
|
|
371
894
|
const sessionReason = await tryMintSessionDenial(ctx);
|
|
372
895
|
if (sessionReason) return { kind: "deny", reason: sessionReason };
|
|
896
|
+
const acceptsAip = aipTrustedIssuers !== void 0 && aipTrustedIssuers.length > 0;
|
|
897
|
+
const aipStep = acceptsAip ? [
|
|
898
|
+
`If you hold an AIP Agent Identity Token from a trusted issuer (${aipTrustedIssuers.join(", ")}): present it \u2014 send the JWT in an Agent-Identity header plus an RFC 9421 HTTP Message Signature (Signature-Input + Signature over @method @authority @path agent-identity, tag="agent-identity") signed with the token-bound cnf key. This satisfies identity in one round trip without an AgentScore credential.`
|
|
899
|
+
] : [];
|
|
373
900
|
const missingIdentityInstructions = JSON.stringify({
|
|
374
901
|
action: "probe_identity_then_session",
|
|
375
902
|
steps: [
|
|
903
|
+
...aipStep,
|
|
376
904
|
"If you have a wallet AND this merchant accepts a wallet-signing rail (Tempo MPP or x402 \u2014 see openapi.json x-payment-info): send X-Wallet-Address and retry. If the wallet is already linked to an AgentScore operator (via prior SIWE claim or prior captureWallet), this passes in one round trip. If the wallet is unlinked or the account has no KYC, the 403 will include a verify_url \u2014 share it with the user to claim the wallet + complete KYC, then retry.",
|
|
377
905
|
"If step 1 is denied or you already have a stored operator_token (valid + not expired): send X-Operator-Token: opc_... and retry.",
|
|
378
906
|
"If neither applies: retry with NO identity header. Merchants that auto-create verification sessions (most AgentScore merchants do) return verify_url + session_id + poll_secret in the 403 body \u2014 share verify_url with the user, then poll poll_url every 5s with the X-Poll-Secret header until status=verified (the poll returns a one-time operator_token). If the retry returns the same bare 403, this merchant does not support self-service session bootstrapping \u2014 direct the user to https://agentscore.sh/sign-up to create an AgentScore identity and mint an operator_token from their dashboard (https://agentscore.sh/dashboard/verify). The user hands the opc_... to you, and you retry with X-Operator-Token."
|
|
@@ -388,7 +916,7 @@ function createAgentScoreCore(options) {
|
|
|
388
916
|
}
|
|
389
917
|
};
|
|
390
918
|
}
|
|
391
|
-
const cacheKey = identity.operatorToken?.toLowerCase() ?? (identity.address ? normalizeAddress(identity.address) : "");
|
|
919
|
+
const cacheKey = identity.aipToken ? `aip:${(0, import_node_crypto.createHash)("sha256").update(identity.aipToken).digest("hex")}` : identity.operatorToken?.toLowerCase() ?? (identity.address ? normalizeAddress(identity.address) : "");
|
|
392
920
|
const cached = cache.get(cacheKey);
|
|
393
921
|
if (cached) {
|
|
394
922
|
if (cached.allow) {
|
|
@@ -433,7 +961,7 @@ function createAgentScoreCore(options) {
|
|
|
433
961
|
// regardless of policy.require_sanctions_clear (which gates the separate NAME screen).
|
|
434
962
|
...signer && { signer: { address: signer.address, network: signer.network } }
|
|
435
963
|
};
|
|
436
|
-
const result = identity.address ? await sdk.assess(identity.address, { ...opts, operatorToken: identity.operatorToken }) : await sdk.assess(null, { ...opts, operatorToken: identity.operatorToken });
|
|
964
|
+
const result = identity.aipToken ? await sdk.assess(null, { ...opts, aipToken: identity.aipToken }) : identity.address ? await sdk.assess(identity.address, { ...opts, operatorToken: identity.operatorToken }) : await sdk.assess(null, { ...opts, operatorToken: identity.operatorToken });
|
|
437
965
|
data = result;
|
|
438
966
|
} catch (err) {
|
|
439
967
|
if (err instanceof import_sdk.PaymentRequiredError) {
|
|
@@ -504,6 +1032,9 @@ function createAgentScoreCore(options) {
|
|
|
504
1032
|
const decisionReasons = data.decision_reasons ?? [];
|
|
505
1033
|
const allow = decision === "allow" || decision == null;
|
|
506
1034
|
cache.set(cacheKey, { allow, decision: decision ?? void 0, reasons: decisionReasons, raw: data });
|
|
1035
|
+
if (identity.address !== void 0 && identity.operatorToken === void 0 && identity.aipToken === void 0 && (data.signer_match !== void 0 || data.signer_sanctions !== void 0)) {
|
|
1036
|
+
lastSignerRaw.set(normalizeAddress(identity.address), data);
|
|
1037
|
+
}
|
|
507
1038
|
if (allow) {
|
|
508
1039
|
const quota = data.quota;
|
|
509
1040
|
return {
|
|
@@ -568,9 +1099,7 @@ function createAgentScoreCore(options) {
|
|
|
568
1099
|
}
|
|
569
1100
|
function getSignerVerdict(claimedAddress) {
|
|
570
1101
|
const claimedNorm = normalizeAddress(claimedAddress);
|
|
571
|
-
const
|
|
572
|
-
if (!cached) return void 0;
|
|
573
|
-
const raw = cached.raw;
|
|
1102
|
+
const raw = lastSignerRaw.get(claimedNorm) ?? cache.get(claimedNorm)?.raw;
|
|
574
1103
|
if (!raw) return void 0;
|
|
575
1104
|
const rawMatch = raw.signer_match;
|
|
576
1105
|
const rawSanctions = raw.signer_sanctions;
|
|
@@ -756,11 +1285,50 @@ function withConditionalAgentScoreGate(options, handler) {
|
|
|
756
1285
|
return wrapped(req, ctx);
|
|
757
1286
|
};
|
|
758
1287
|
}
|
|
1288
|
+
var defaultAipResponse = (body) => new Response(JSON.stringify(body), { status: body.status, headers: { "content-type": "application/problem+json" } });
|
|
1289
|
+
function createAipGate(options) {
|
|
1290
|
+
const { onDenied, ...gateOpts } = options;
|
|
1291
|
+
return async (req) => {
|
|
1292
|
+
const result = await verifyAitRequest(req, gateOpts);
|
|
1293
|
+
if (result.ok) {
|
|
1294
|
+
return { allowed: true, ait: result.ait };
|
|
1295
|
+
}
|
|
1296
|
+
const body = buildAipErrorBody(result.failure);
|
|
1297
|
+
const response = onDenied ? await onDenied(req, body) : defaultAipResponse(body);
|
|
1298
|
+
return { allowed: false, response };
|
|
1299
|
+
};
|
|
1300
|
+
}
|
|
1301
|
+
function withAipGate(options, handler) {
|
|
1302
|
+
const guard = createAipGate(options);
|
|
1303
|
+
return async (req, ctx) => {
|
|
1304
|
+
const result = await guard(req);
|
|
1305
|
+
if (!result.allowed) {
|
|
1306
|
+
return result.response;
|
|
1307
|
+
}
|
|
1308
|
+
return handler(req, { ait: result.ait }, ctx);
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
function withConditionalAipGate(options, handler) {
|
|
1312
|
+
const guard = createAipGate(options);
|
|
1313
|
+
return async (req, ctx) => {
|
|
1314
|
+
if (!hasAgentIdentityHeader(req)) {
|
|
1315
|
+
return handler(req, {}, ctx);
|
|
1316
|
+
}
|
|
1317
|
+
const result = await guard(req);
|
|
1318
|
+
if (!result.allowed) {
|
|
1319
|
+
return result.response;
|
|
1320
|
+
}
|
|
1321
|
+
return handler(req, { ait: result.ait }, ctx);
|
|
1322
|
+
};
|
|
1323
|
+
}
|
|
759
1324
|
// Annotate the CommonJS export names for ESM import in node:
|
|
760
1325
|
0 && (module.exports = {
|
|
761
1326
|
createAgentScoreGate,
|
|
1327
|
+
createAipGate,
|
|
762
1328
|
createConditionalAgentScoreGate,
|
|
763
1329
|
withAgentScoreGate,
|
|
764
|
-
|
|
1330
|
+
withAipGate,
|
|
1331
|
+
withConditionalAgentScoreGate,
|
|
1332
|
+
withConditionalAipGate
|
|
765
1333
|
});
|
|
766
1334
|
//# sourceMappingURL=web.js.map
|