@orangecheck/me-client 0.62.0 → 0.64.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) {
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 > (options.maxAgeSeconds ?? DEFAULT_MAX_AGE_SECONDS) * 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,7 @@ 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();
1263
1312
  if (!sigHex) return { ok: false, reason: "missing signature" };
1264
1313
  if (!kid) return { ok: false, reason: "missing kid" };
1265
1314
  const sig2 = hexToBytes(sigHex);
@@ -1276,7 +1325,8 @@ async function verify2(rawBody, sigHex, kid, options = {}) {
1276
1325
  return { ok: false, reason: "could not import provided jwk" };
1277
1326
  }
1278
1327
  } else {
1279
- const issuer = options.issuer ?? DEFAULT_ISSUER;
1328
+ const issuer = parseIssuerOrigin(options.issuer ?? DEFAULT_ISSUER);
1329
+ if (issuer === null) return { ok: false, reason: `issuer must be ${ISSUER_ORIGIN_RULE}` };
1280
1330
  const ttl = options.jwksCacheTtlMs ?? DEFAULT_TTL_MS;
1281
1331
  const maxStale = options.jwksMaxStaleMs ?? DEFAULT_MAX_STALE_MS;
1282
1332
  try {
@@ -1292,12 +1342,13 @@ async function verify2(rawBody, sigHex, kid, options = {}) {
1292
1342
  }
1293
1343
  if (!key) return { ok: false, reason: `kid ${kid} not in JWKS` };
1294
1344
  }
1295
- const ok = await verifyEd25519(key, sig2, msg);
1296
- return ok ? { ok: true } : { ok: false, reason: "signature does not match" };
1345
+ if (!await verifyEd25519(key, sig2, msg)) return { ok: false, reason: "signature does not match" };
1346
+ return signedFields(msg, options);
1297
1347
  }
1298
1348
  async function fetchJwksForce(issuer = DEFAULT_ISSUER) {
1299
- jwksCache.delete(issuer);
1300
- await fetchJwks(issuer, DEFAULT_TTL_MS);
1349
+ const origin2 = issuerOrigin(issuer, "webhook.fetchJwks: issuer");
1350
+ jwksCache.delete(origin2);
1351
+ await fetchJwks(origin2, DEFAULT_TTL_MS);
1301
1352
  }
1302
1353
  var webhook = { verify: verify2, fetchJwks: fetchJwksForce };
1303
1354
 
@@ -1472,8 +1523,6 @@ function safeReturnPath(raw, fallback = "/") {
1472
1523
  return fallback;
1473
1524
  }
1474
1525
  }
1475
-
1476
- // src/identity.ts
1477
1526
  var DEFAULT_ISSUER2 = "https://me.ochk.io";
1478
1527
  var DEFAULT_TTL_MS2 = 60 * 60 * 1e3;
1479
1528
  var DEFAULT_MAX_STALE_MS2 = 6 * 60 * 60 * 1e3;
@@ -1673,7 +1722,12 @@ async function verifyActivityAttestation(bundle, options = {}) {
1673
1722
  return { ok: false, reason: "provided jwk failed import", checks };
1674
1723
  }
1675
1724
  } else {
1676
- const issuer = options.issuer ?? DEFAULT_ISSUER2;
1725
+ const issuer = parseIssuerOrigin(options.issuer ?? DEFAULT_ISSUER2);
1726
+ if (issuer === null) {
1727
+ const reason = `issuer must be ${ISSUER_ORIGIN_RULE}`;
1728
+ checks.push({ ok: false, label: "JWK with matching kid present", detail: reason });
1729
+ return { ok: false, reason, checks };
1730
+ }
1677
1731
  const ttl = options.jwksCacheTtlMs ?? DEFAULT_TTL_MS2;
1678
1732
  const maxStale = options.jwksMaxStaleMs ?? DEFAULT_MAX_STALE_MS2;
1679
1733
  try {
@@ -1733,8 +1787,9 @@ async function verifyActivityAttestation(bundle, options = {}) {
1733
1787
  return { ok: true, attestation: signed, checks };
1734
1788
  }
1735
1789
  async function refreshEnvelopeJwks(issuer = DEFAULT_ISSUER2) {
1736
- jwksCache2.delete(issuer);
1737
- await fetchEnvelopeJwks(issuer, DEFAULT_TTL_MS2);
1790
+ const origin2 = issuerOrigin(issuer, "refreshEnvelopeJwks: issuer");
1791
+ jwksCache2.delete(origin2);
1792
+ await fetchEnvelopeJwks(origin2, DEFAULT_TTL_MS2);
1738
1793
  }
1739
1794
  var identity = {
1740
1795
  verifyActivityAttestation,