@orangecheck/me-client 0.63.0 → 0.65.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/index.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { parseIssuerOrigin, ISSUER_ORIGIN_RULE } from '@orangecheck/auth-core/issuer-origin';
1
2
  import { isDidOc } from '@orangecheck/auth-core/identifiers';
2
3
 
3
4
  var __defProp = Object.defineProperty;
@@ -1178,6 +1179,13 @@ async function importJWK(jwk, alg, options) {
1178
1179
  throw new JOSENotSupported('Unsupported "kty" (Key Type) Parameter value');
1179
1180
  }
1180
1181
  }
1182
+ function issuerOrigin(value, what) {
1183
+ const origin2 = parseIssuerOrigin(value);
1184
+ if (origin2 === null) {
1185
+ throw new TypeError(`@orangecheck/me-client: ${what} must be ${ISSUER_ORIGIN_RULE}; got ${JSON.stringify(value)}`);
1186
+ }
1187
+ return origin2;
1188
+ }
1181
1189
 
1182
1190
  // src/webhook.ts
1183
1191
  var DEFAULT_ISSUER = "https://me.ochk.io";
@@ -1185,6 +1193,46 @@ var JWKS_PATH = "/api/dev-jwks";
1185
1193
  var DEFAULT_TTL_MS = 60 * 60 * 1e3;
1186
1194
  var DEFAULT_MAX_STALE_MS = 6 * 60 * 60 * 1e3;
1187
1195
  var FORCED_REFRESH_MIN_INTERVAL_MS = 6e4;
1196
+ var DEFAULT_MAX_AGE_SECONDS = 24 * 60 * 60;
1197
+ var FUTURE_SKEW_MS = 5 * 60 * 1e3;
1198
+ var ISSUED_AT = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,9})?Z$/;
1199
+ var warnedNoProject = false;
1200
+ function warnNoProjectOnce() {
1201
+ if (warnedNoProject) return;
1202
+ warnedNoProject = true;
1203
+ console.warn(
1204
+ "@orangecheck/me-client: webhook.verify was called without `project`, so it accepts a delivery signed for any project. Pass your project key; it is required from the next major version."
1205
+ );
1206
+ }
1207
+ function isIsoTime(v) {
1208
+ if (!ISSUED_AT.test(v)) return false;
1209
+ const t = Date.parse(v);
1210
+ return Number.isFinite(t) && new Date(t).toISOString().slice(0, 19) === v.slice(0, 19);
1211
+ }
1212
+ function signedFields(msg, options, maxAgeSeconds) {
1213
+ let parsed;
1214
+ try {
1215
+ parsed = JSON.parse(new TextDecoder().decode(msg));
1216
+ } catch {
1217
+ return { ok: false, reason: "body is not JSON" };
1218
+ }
1219
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
1220
+ return { ok: false, reason: "body is not a JSON object" };
1221
+ }
1222
+ const { id, project_key, issued_at } = parsed;
1223
+ if (typeof id !== "string" || id === "" || typeof project_key !== "string" || project_key === "" || typeof issued_at !== "string" || !isIsoTime(issued_at)) {
1224
+ return { ok: false, reason: "missing a signed id, project_key or issued_at" };
1225
+ }
1226
+ if (options.project !== void 0 && project_key !== options.project) {
1227
+ return { ok: false, reason: `signed for another project (${project_key})` };
1228
+ }
1229
+ const age = Date.now() - Date.parse(issued_at);
1230
+ if (age > maxAgeSeconds * 1e3) {
1231
+ return { ok: false, reason: "older than the accepted age (issued_at)" };
1232
+ }
1233
+ if (-age > FUTURE_SKEW_MS) return { ok: false, reason: "issued_at is in the future" };
1234
+ return { ok: true, id, project_key, issued_at };
1235
+ }
1188
1236
  var jwksCache = /* @__PURE__ */ new Map();
1189
1237
  var inflight = /* @__PURE__ */ new Map();
1190
1238
  var lastForcedRefresh = /* @__PURE__ */ new Map();
@@ -1260,6 +1308,11 @@ async function verifyEd25519(key, sig2, msg) {
1260
1308
  }
1261
1309
  }
1262
1310
  async function verify2(rawBody, sigHex, kid, options = {}) {
1311
+ if (options.project === void 0) warnNoProjectOnce();
1312
+ const maxAgeSeconds = options.maxAgeSeconds === void 0 ? DEFAULT_MAX_AGE_SECONDS : options.maxAgeSeconds;
1313
+ if (!Number.isFinite(maxAgeSeconds) || maxAgeSeconds <= 0) {
1314
+ return { ok: false, reason: "maxAgeSeconds must be a finite number above 0" };
1315
+ }
1263
1316
  if (!sigHex) return { ok: false, reason: "missing signature" };
1264
1317
  if (!kid) return { ok: false, reason: "missing kid" };
1265
1318
  const sig2 = hexToBytes(sigHex);
@@ -1276,7 +1329,8 @@ async function verify2(rawBody, sigHex, kid, options = {}) {
1276
1329
  return { ok: false, reason: "could not import provided jwk" };
1277
1330
  }
1278
1331
  } else {
1279
- const issuer = options.issuer ?? DEFAULT_ISSUER;
1332
+ const issuer = parseIssuerOrigin(options.issuer ?? DEFAULT_ISSUER);
1333
+ if (issuer === null) return { ok: false, reason: `issuer must be ${ISSUER_ORIGIN_RULE}` };
1280
1334
  const ttl = options.jwksCacheTtlMs ?? DEFAULT_TTL_MS;
1281
1335
  const maxStale = options.jwksMaxStaleMs ?? DEFAULT_MAX_STALE_MS;
1282
1336
  try {
@@ -1292,12 +1346,13 @@ async function verify2(rawBody, sigHex, kid, options = {}) {
1292
1346
  }
1293
1347
  if (!key) return { ok: false, reason: `kid ${kid} not in JWKS` };
1294
1348
  }
1295
- const ok = await verifyEd25519(key, sig2, msg);
1296
- return ok ? { ok: true } : { ok: false, reason: "signature does not match" };
1349
+ if (!await verifyEd25519(key, sig2, msg)) return { ok: false, reason: "signature does not match" };
1350
+ return signedFields(msg, options, maxAgeSeconds);
1297
1351
  }
1298
1352
  async function fetchJwksForce(issuer = DEFAULT_ISSUER) {
1299
- jwksCache.delete(issuer);
1300
- await fetchJwks(issuer, DEFAULT_TTL_MS);
1353
+ const origin2 = issuerOrigin(issuer, "webhook.fetchJwks: issuer");
1354
+ jwksCache.delete(origin2);
1355
+ await fetchJwks(origin2, DEFAULT_TTL_MS);
1301
1356
  }
1302
1357
  var webhook = { verify: verify2, fetchJwks: fetchJwksForce };
1303
1358
 
@@ -1472,8 +1527,6 @@ function safeReturnPath(raw, fallback = "/") {
1472
1527
  return fallback;
1473
1528
  }
1474
1529
  }
1475
-
1476
- // src/identity.ts
1477
1530
  var DEFAULT_ISSUER2 = "https://me.ochk.io";
1478
1531
  var DEFAULT_TTL_MS2 = 60 * 60 * 1e3;
1479
1532
  var DEFAULT_MAX_STALE_MS2 = 6 * 60 * 60 * 1e3;
@@ -1673,7 +1726,12 @@ async function verifyActivityAttestation(bundle, options = {}) {
1673
1726
  return { ok: false, reason: "provided jwk failed import", checks };
1674
1727
  }
1675
1728
  } else {
1676
- const issuer = options.issuer ?? DEFAULT_ISSUER2;
1729
+ const issuer = parseIssuerOrigin(options.issuer ?? DEFAULT_ISSUER2);
1730
+ if (issuer === null) {
1731
+ const reason = `issuer must be ${ISSUER_ORIGIN_RULE}`;
1732
+ checks.push({ ok: false, label: "JWK with matching kid present", detail: reason });
1733
+ return { ok: false, reason, checks };
1734
+ }
1677
1735
  const ttl = options.jwksCacheTtlMs ?? DEFAULT_TTL_MS2;
1678
1736
  const maxStale = options.jwksMaxStaleMs ?? DEFAULT_MAX_STALE_MS2;
1679
1737
  try {
@@ -1733,8 +1791,9 @@ async function verifyActivityAttestation(bundle, options = {}) {
1733
1791
  return { ok: true, attestation: signed, checks };
1734
1792
  }
1735
1793
  async function refreshEnvelopeJwks(issuer = DEFAULT_ISSUER2) {
1736
- jwksCache2.delete(issuer);
1737
- await fetchEnvelopeJwks(issuer, DEFAULT_TTL_MS2);
1794
+ const origin2 = issuerOrigin(issuer, "refreshEnvelopeJwks: issuer");
1795
+ jwksCache2.delete(origin2);
1796
+ await fetchEnvelopeJwks(origin2, DEFAULT_TTL_MS2);
1738
1797
  }
1739
1798
  var identity = {
1740
1799
  verifyActivityAttestation,