@arcblock/jwt 1.30.23 → 1.30.25

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/esm/index.d.mts CHANGED
@@ -21,7 +21,13 @@ type JwtToken = {
21
21
  type JwtVerifyOptions = Partial<{
22
22
  tolerance: number;
23
23
  enforceTimestamp: boolean;
24
+ /** Accept `exp: 0` as "never expires" when enforceTimestamp is on (default false, #965) */
25
+ allowNoExpiry: boolean;
24
26
  signerKey: string;
27
+ audience: string | string[];
28
+ issuer: string | string[];
29
+ subject: string;
30
+ nonce: string;
25
31
  }>;
26
32
  /**
27
33
  *
@@ -67,6 +73,7 @@ declare function decode(token: string, bodyOnly?: false): JwtToken;
67
73
  * @param {{
68
74
  * tolerance: number; - number of seconds to tolerant expire
69
75
  * enforceTimestamp: boolean; - whether should be verify timestamps?
76
+ * allowNoExpiry: boolean; - accept exp=0 as "never expires" (default false)
70
77
  * signerKey: string; - which field should be used to pick the signer
71
78
  * }} [{
72
79
  * tolerance,
@@ -128,7 +135,11 @@ declare function signDelegationTokenUnsigned(signer: string, pk: BytesType, payl
128
135
  */
129
136
  declare function assembleDelegationToken(unsignedToken: string, assertion: PasskeyAssertion): string;
130
137
  declare function decodeDelegationToken(token: string): DelegationToken;
131
- declare function verifyDelegationToken(token: string): Promise<boolean>;
138
+ type DelegationVerifyOptions = Partial<{
139
+ subject: string;
140
+ maxDepth: number;
141
+ }>;
142
+ declare function verifyDelegationToken(token: string, options?: DelegationVerifyOptions): Promise<boolean>;
132
143
  declare function checkDelegationTokenScope(scope: string, {
133
144
  ops,
134
145
  deny
@@ -137,4 +148,4 @@ declare function checkDelegationTokenScope(scope: string, {
137
148
  deny?: string[];
138
149
  }): boolean;
139
150
  //#endregion
140
- export { DelegationToken, DelegationTokenBody, DelegationTokenHeader, JwtBody, JwtHeader, JwtToken, JwtVerifyOptions, PasskeyAssertion, assemble, assembleDelegationToken, checkDelegationTokenScope, decode, decodeDelegationToken, getChallenge, sign, signDelegationToken, signDelegationTokenUnsigned, signV2, verify, verifyDelegationToken };
151
+ export { DelegationToken, DelegationTokenBody, DelegationTokenHeader, DelegationVerifyOptions, JwtBody, JwtHeader, JwtToken, JwtVerifyOptions, PasskeyAssertion, assemble, assembleDelegationToken, checkDelegationTokenScope, decode, decodeDelegationToken, getChallenge, sign, signDelegationToken, signDelegationTokenUnsigned, signV2, verify, verifyDelegationToken };
package/esm/index.mjs CHANGED
@@ -111,7 +111,9 @@ function assemble(unsignedToken, assertion) {
111
111
  return `${unsignedToken}.${toBase64(JSON.stringify(assertion))}`;
112
112
  }
113
113
  function decode(token, bodyOnly = true) {
114
- const [headerB64, bodyB64, sigB64] = token.split(".");
114
+ const parts = token.split(".");
115
+ if (parts.length !== 3) throw new Error(`Cannot decode JWT: expected token with exactly 3 parts (header.body.signature), got ${parts.length}`);
116
+ const [headerB64, bodyB64, sigB64] = parts;
115
117
  const header = JSON.parse(fromBase64(headerB64).toString());
116
118
  const body = JSON.parse(fromBase64(bodyB64).toString());
117
119
  const sig = Buffer.from(Uint8Array.from(fromBase64(sigB64))).toString("hex");
@@ -130,6 +132,7 @@ function decode(token, bodyOnly = true) {
130
132
  * @param {{
131
133
  * tolerance: number; - number of seconds to tolerant expire
132
134
  * enforceTimestamp: boolean; - whether should be verify timestamps?
135
+ * allowNoExpiry: boolean; - accept exp=0 as "never expires" (default false)
133
136
  * signerKey: string; - which field should be used to pick the signer
134
137
  * }} [{
135
138
  * tolerance,
@@ -143,13 +146,19 @@ function decode(token, bodyOnly = true) {
143
146
  * @return {*} {boolean}
144
147
  */
145
148
  async function verify(token, signerPk, options) {
146
- const { tolerance, enforceTimestamp, signerKey } = Object.assign({
149
+ const { tolerance, enforceTimestamp, allowNoExpiry, signerKey } = Object.assign({
147
150
  tolerance: 5,
148
151
  enforceTimestamp: true,
152
+ allowNoExpiry: false,
149
153
  signerKey: "iss"
150
154
  }, options);
151
155
  try {
152
- const [headerB64, bodyB64] = token.split(".");
156
+ const parts = token.split(".");
157
+ if (parts.length !== 3) {
158
+ debug("verify.error.malformedToken", { segments: parts.length });
159
+ return false;
160
+ }
161
+ const [headerB64, bodyB64] = parts;
153
162
  const { header, body, signature } = decode(token, false);
154
163
  if (!signature) {
155
164
  debug("verify.error.emptySig");
@@ -168,9 +177,47 @@ async function verify(token, signerPk, options) {
168
177
  debug("verify.error.signerDidAndPkNotMatch");
169
178
  return false;
170
179
  }
180
+ if (options?.issuer !== void 0) {
181
+ const allowedIssuers = Array.isArray(options.issuer) ? options.issuer : [options.issuer];
182
+ if (!body.iss || !allowedIssuers.includes(String(body.iss))) {
183
+ debug("verify.error.issuerMismatch");
184
+ return false;
185
+ }
186
+ }
187
+ if (options?.subject !== void 0) {
188
+ if (body.sub !== options.subject) {
189
+ debug("verify.error.subjectMismatch");
190
+ return false;
191
+ }
192
+ }
193
+ if (options?.audience !== void 0) {
194
+ const expectedAud = Array.isArray(options.audience) ? options.audience : [options.audience];
195
+ if (!(Array.isArray(body.aud) ? body.aud.map(String) : body.aud ? [String(body.aud)] : []).some((a) => expectedAud.includes(a))) {
196
+ debug("verify.error.audienceMismatch");
197
+ return false;
198
+ }
199
+ }
200
+ if (options?.nonce !== void 0) {
201
+ if (body.nonce !== options.nonce) {
202
+ debug("verify.error.nonceMismatch");
203
+ return false;
204
+ }
205
+ }
171
206
  if (enforceTimestamp) {
172
207
  const now = Math.ceil(Date.now() / 1e3);
173
- const exp = Number(body.exp) || 0;
208
+ if (body.exp === void 0 || body.exp === null || body.exp === "" || Number.isNaN(Number(body.exp))) {
209
+ debug("verify.error.invalidExp");
210
+ return false;
211
+ }
212
+ if (body.iat !== void 0 && body.iat !== null && Number.isNaN(Number(body.iat))) {
213
+ debug("verify.error.invalidIat");
214
+ return false;
215
+ }
216
+ if (body.nbf !== void 0 && body.nbf !== null && Number.isNaN(Number(body.nbf))) {
217
+ debug("verify.error.invalidNbf");
218
+ return false;
219
+ }
220
+ const exp = Number(body.exp);
174
221
  const iat = Number(body.iat) || 0;
175
222
  const nbf = Number(body.nbf) || 0;
176
223
  debug("verify.enforceTimestamp", {
@@ -179,7 +226,15 @@ async function verify(token, signerPk, options) {
179
226
  iat,
180
227
  nbf
181
228
  });
182
- if (exp && exp + tolerance < now) {
229
+ if (!Number.isFinite(exp)) {
230
+ debug("verify.error.nonFiniteExp");
231
+ return false;
232
+ }
233
+ if (exp === 0 && !allowNoExpiry) {
234
+ debug("verify.error.noExpiry");
235
+ return false;
236
+ }
237
+ if (exp < 0 || exp && exp + tolerance < now) {
183
238
  debug("verify.error.expired");
184
239
  return false;
185
240
  }
@@ -216,7 +271,7 @@ async function verify(token, signerPk, options) {
216
271
  if (signers[alg]) {
217
272
  const msgHex = toHex(`${headerB64}.${bodyB64}`);
218
273
  const version = (body.version ? coerceVersion(body.version) : null) || "";
219
- if (version && version === JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) return signers[alg].verify(hasher(msgHex), signature, signerPk);
274
+ if (version && semverGte(version, JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN)) return signers[alg].verify(hasher(msgHex), signature, signerPk);
220
275
  return signers[alg].verify(msgHex, signature, signerPk);
221
276
  }
222
277
  debug("verify.error.crypto");
@@ -326,10 +381,19 @@ function decodeDelegationToken(token) {
326
381
  signature: `0x${toStrictHex(Buffer.from(Uint8Array.from(fromBase64(sigB64))).toString("hex"))}`
327
382
  };
328
383
  }
329
- async function verifyDelegationToken(token) {
384
+ async function verifyDelegationToken(token, options = {}) {
330
385
  try {
386
+ const maxDepth = options.maxDepth ?? 5;
387
+ if (maxDepth < 0) {
388
+ debug("delegationToken.verify.error.maxDepthExceeded");
389
+ return false;
390
+ }
331
391
  const [headerB64, bodyB64] = token.split(".");
332
392
  const { header, body, signature } = decodeDelegationToken(token);
393
+ if (options.subject !== void 0 && toDid(body.sub) !== toDid(options.subject)) {
394
+ debug("delegationToken.verify.error.subjectMismatch");
395
+ return false;
396
+ }
333
397
  if (header.typ !== "DelegationToken") {
334
398
  debug("delegationToken.verify.error.wrongTyp");
335
399
  return false;
@@ -343,10 +407,22 @@ async function verifyDelegationToken(token) {
343
407
  return false;
344
408
  }
345
409
  const now = Math.floor(Date.now() / 1e3);
346
- const exp = Number(body.exp) || 0;
410
+ if (body.exp === void 0 || body.exp === null || body.exp === "" || Number.isNaN(Number(body.exp))) {
411
+ debug("delegationToken.verify.error.invalidExp");
412
+ return false;
413
+ }
414
+ if (body.nbf !== void 0 && body.nbf !== null && Number.isNaN(Number(body.nbf))) {
415
+ debug("delegationToken.verify.error.invalidNbf");
416
+ return false;
417
+ }
418
+ if (body.iat !== void 0 && body.iat !== null && Number.isNaN(Number(body.iat))) {
419
+ debug("delegationToken.verify.error.invalidIat");
420
+ return false;
421
+ }
422
+ const exp = Number(body.exp);
347
423
  const nbf = Number(body.nbf) || 0;
348
424
  const iat = Number(body.iat) || 0;
349
- if (exp && exp < now) {
425
+ if (exp < 0 || exp && exp < now) {
350
426
  debug("delegationToken.verify.error.expired");
351
427
  return false;
352
428
  }
@@ -359,6 +435,7 @@ async function verifyDelegationToken(token) {
359
435
  return false;
360
436
  }
361
437
  const alg = header.alg.toLowerCase();
438
+ let sigValid = false;
362
439
  if (alg === "passkey") {
363
440
  const [, , sigB64] = token.split(".");
364
441
  const assertionRaw = JSON.parse(fromBase64(sigB64).toString());
@@ -371,20 +448,56 @@ async function verifyDelegationToken(token) {
371
448
  authenticatorData: assertionRaw.authenticatorData,
372
449
  clientDataJSON: assertionRaw.clientDataJSON
373
450
  });
374
- return await getSigner(types.KeyType.PASSKEY).verify(challenge, assertionRaw.signature, body.pk, extra);
451
+ sigValid = await getSigner(types.KeyType.PASSKEY).verify(challenge, assertionRaw.signature, body.pk, extra);
452
+ } else {
453
+ const signers = {
454
+ ed25519: getSigner(types.KeyType.ED25519),
455
+ es256k: getSigner(types.KeyType.SECP256K1),
456
+ secp256k1: getSigner(types.KeyType.SECP256K1),
457
+ ethereum: getSigner(types.KeyType.ETHEREUM)
458
+ };
459
+ if (!signers[alg]) {
460
+ debug("delegationToken.verify.error.unknownAlg");
461
+ return false;
462
+ }
463
+ const msgHash = hasher(toHex(`${headerB64}.${bodyB64}`));
464
+ sigValid = signers[alg].verify(msgHash, signature, body.pk);
375
465
  }
376
- const signers = {
377
- ed25519: getSigner(types.KeyType.ED25519),
378
- es256k: getSigner(types.KeyType.SECP256K1),
379
- secp256k1: getSigner(types.KeyType.SECP256K1),
380
- ethereum: getSigner(types.KeyType.ETHEREUM)
381
- };
382
- if (!signers[alg]) {
383
- debug("delegationToken.verify.error.unknownAlg");
384
- return false;
466
+ if (!sigValid) return false;
467
+ if (body.delegation !== void 0) {
468
+ if (typeof body.delegation !== "string" || !body.delegation || maxDepth <= 0) {
469
+ debug("delegationToken.verify.error.invalidParentOrDepth");
470
+ return false;
471
+ }
472
+ const parent = decodeDelegationToken(body.delegation);
473
+ if (toDid(parent.body.sub) !== toDid(body.iss)) {
474
+ debug("delegationToken.verify.error.chainSubIssMismatch");
475
+ return false;
476
+ }
477
+ if (toDid(parent.body.iss) === toDid(body.sub)) {
478
+ debug("delegationToken.verify.error.chainCycle");
479
+ return false;
480
+ }
481
+ const parentExp = Number(parent.body.exp);
482
+ const childExp = Number(body.exp);
483
+ if (parentExp > 0 && (childExp === 0 || childExp > parentExp)) {
484
+ debug("delegationToken.verify.error.childOutlivesParent");
485
+ return false;
486
+ }
487
+ if (!Array.isArray(body.ops) || body.ops.length === 0 || !body.ops.every((op) => checkDelegationTokenScope(op, parent.body))) {
488
+ debug("delegationToken.verify.error.scopeEscalation");
489
+ return false;
490
+ }
491
+ for (const parentDeny of parent.body.deny ?? []) if (scopeMatchAny(parentDeny, body.ops) && !(body.deny ?? []).includes(parentDeny)) {
492
+ debug("delegationToken.verify.error.denyDropped");
493
+ return false;
494
+ }
495
+ if (!await verifyDelegationToken(body.delegation, { maxDepth: maxDepth - 1 })) {
496
+ debug("delegationToken.verify.error.parentVerifyFailed");
497
+ return false;
498
+ }
385
499
  }
386
- const msgHash = hasher(toHex(`${headerB64}.${bodyB64}`));
387
- return signers[alg].verify(msgHash, signature, body.pk);
500
+ return true;
388
501
  } catch (err) {
389
502
  debug("delegationToken.verify.error.exception", err);
390
503
  return false;
package/lib/index.cjs CHANGED
@@ -114,7 +114,9 @@ function assemble(unsignedToken, assertion) {
114
114
  return `${unsignedToken}.${(0, _ocap_util.toBase64)(JSON.stringify(assertion))}`;
115
115
  }
116
116
  function decode(token, bodyOnly = true) {
117
- const [headerB64, bodyB64, sigB64] = token.split(".");
117
+ const parts = token.split(".");
118
+ if (parts.length !== 3) throw new Error(`Cannot decode JWT: expected token with exactly 3 parts (header.body.signature), got ${parts.length}`);
119
+ const [headerB64, bodyB64, sigB64] = parts;
118
120
  const header = JSON.parse((0, _ocap_util.fromBase64)(headerB64).toString());
119
121
  const body = JSON.parse((0, _ocap_util.fromBase64)(bodyB64).toString());
120
122
  const sig = Buffer.from(Uint8Array.from((0, _ocap_util.fromBase64)(sigB64))).toString("hex");
@@ -133,6 +135,7 @@ function decode(token, bodyOnly = true) {
133
135
  * @param {{
134
136
  * tolerance: number; - number of seconds to tolerant expire
135
137
  * enforceTimestamp: boolean; - whether should be verify timestamps?
138
+ * allowNoExpiry: boolean; - accept exp=0 as "never expires" (default false)
136
139
  * signerKey: string; - which field should be used to pick the signer
137
140
  * }} [{
138
141
  * tolerance,
@@ -146,13 +149,19 @@ function decode(token, bodyOnly = true) {
146
149
  * @return {*} {boolean}
147
150
  */
148
151
  async function verify(token, signerPk, options) {
149
- const { tolerance, enforceTimestamp, signerKey } = Object.assign({
152
+ const { tolerance, enforceTimestamp, allowNoExpiry, signerKey } = Object.assign({
150
153
  tolerance: 5,
151
154
  enforceTimestamp: true,
155
+ allowNoExpiry: false,
152
156
  signerKey: "iss"
153
157
  }, options);
154
158
  try {
155
- const [headerB64, bodyB64] = token.split(".");
159
+ const parts = token.split(".");
160
+ if (parts.length !== 3) {
161
+ debug$1("verify.error.malformedToken", { segments: parts.length });
162
+ return false;
163
+ }
164
+ const [headerB64, bodyB64] = parts;
156
165
  const { header, body, signature } = decode(token, false);
157
166
  if (!signature) {
158
167
  debug$1("verify.error.emptySig");
@@ -171,9 +180,47 @@ async function verify(token, signerPk, options) {
171
180
  debug$1("verify.error.signerDidAndPkNotMatch");
172
181
  return false;
173
182
  }
183
+ if (options?.issuer !== void 0) {
184
+ const allowedIssuers = Array.isArray(options.issuer) ? options.issuer : [options.issuer];
185
+ if (!body.iss || !allowedIssuers.includes(String(body.iss))) {
186
+ debug$1("verify.error.issuerMismatch");
187
+ return false;
188
+ }
189
+ }
190
+ if (options?.subject !== void 0) {
191
+ if (body.sub !== options.subject) {
192
+ debug$1("verify.error.subjectMismatch");
193
+ return false;
194
+ }
195
+ }
196
+ if (options?.audience !== void 0) {
197
+ const expectedAud = Array.isArray(options.audience) ? options.audience : [options.audience];
198
+ if (!(Array.isArray(body.aud) ? body.aud.map(String) : body.aud ? [String(body.aud)] : []).some((a) => expectedAud.includes(a))) {
199
+ debug$1("verify.error.audienceMismatch");
200
+ return false;
201
+ }
202
+ }
203
+ if (options?.nonce !== void 0) {
204
+ if (body.nonce !== options.nonce) {
205
+ debug$1("verify.error.nonceMismatch");
206
+ return false;
207
+ }
208
+ }
174
209
  if (enforceTimestamp) {
175
210
  const now = Math.ceil(Date.now() / 1e3);
176
- const exp = Number(body.exp) || 0;
211
+ if (body.exp === void 0 || body.exp === null || body.exp === "" || Number.isNaN(Number(body.exp))) {
212
+ debug$1("verify.error.invalidExp");
213
+ return false;
214
+ }
215
+ if (body.iat !== void 0 && body.iat !== null && Number.isNaN(Number(body.iat))) {
216
+ debug$1("verify.error.invalidIat");
217
+ return false;
218
+ }
219
+ if (body.nbf !== void 0 && body.nbf !== null && Number.isNaN(Number(body.nbf))) {
220
+ debug$1("verify.error.invalidNbf");
221
+ return false;
222
+ }
223
+ const exp = Number(body.exp);
177
224
  const iat = Number(body.iat) || 0;
178
225
  const nbf = Number(body.nbf) || 0;
179
226
  debug$1("verify.enforceTimestamp", {
@@ -182,7 +229,15 @@ async function verify(token, signerPk, options) {
182
229
  iat,
183
230
  nbf
184
231
  });
185
- if (exp && exp + tolerance < now) {
232
+ if (!Number.isFinite(exp)) {
233
+ debug$1("verify.error.nonFiniteExp");
234
+ return false;
235
+ }
236
+ if (exp === 0 && !allowNoExpiry) {
237
+ debug$1("verify.error.noExpiry");
238
+ return false;
239
+ }
240
+ if (exp < 0 || exp && exp + tolerance < now) {
186
241
  debug$1("verify.error.expired");
187
242
  return false;
188
243
  }
@@ -219,7 +274,7 @@ async function verify(token, signerPk, options) {
219
274
  if (signers[alg]) {
220
275
  const msgHex = (0, _ocap_util.toHex)(`${headerB64}.${bodyB64}`);
221
276
  const version = (body.version ? coerceVersion(body.version) : null) || "";
222
- if (version && version === JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN) return signers[alg].verify(hasher(msgHex), signature, signerPk);
277
+ if (version && semverGte(version, JWT_VERSION_REQUIRE_HASH_BEFORE_SIGN)) return signers[alg].verify(hasher(msgHex), signature, signerPk);
223
278
  return signers[alg].verify(msgHex, signature, signerPk);
224
279
  }
225
280
  debug$1("verify.error.crypto");
@@ -329,10 +384,19 @@ function decodeDelegationToken(token) {
329
384
  signature: `0x${(0, _arcblock_did.toStrictHex)(Buffer.from(Uint8Array.from((0, _ocap_util.fromBase64)(sigB64))).toString("hex"))}`
330
385
  };
331
386
  }
332
- async function verifyDelegationToken(token) {
387
+ async function verifyDelegationToken(token, options = {}) {
333
388
  try {
389
+ const maxDepth = options.maxDepth ?? 5;
390
+ if (maxDepth < 0) {
391
+ debug$1("delegationToken.verify.error.maxDepthExceeded");
392
+ return false;
393
+ }
334
394
  const [headerB64, bodyB64] = token.split(".");
335
395
  const { header, body, signature } = decodeDelegationToken(token);
396
+ if (options.subject !== void 0 && (0, _arcblock_did.toDid)(body.sub) !== (0, _arcblock_did.toDid)(options.subject)) {
397
+ debug$1("delegationToken.verify.error.subjectMismatch");
398
+ return false;
399
+ }
336
400
  if (header.typ !== "DelegationToken") {
337
401
  debug$1("delegationToken.verify.error.wrongTyp");
338
402
  return false;
@@ -346,10 +410,22 @@ async function verifyDelegationToken(token) {
346
410
  return false;
347
411
  }
348
412
  const now = Math.floor(Date.now() / 1e3);
349
- const exp = Number(body.exp) || 0;
413
+ if (body.exp === void 0 || body.exp === null || body.exp === "" || Number.isNaN(Number(body.exp))) {
414
+ debug$1("delegationToken.verify.error.invalidExp");
415
+ return false;
416
+ }
417
+ if (body.nbf !== void 0 && body.nbf !== null && Number.isNaN(Number(body.nbf))) {
418
+ debug$1("delegationToken.verify.error.invalidNbf");
419
+ return false;
420
+ }
421
+ if (body.iat !== void 0 && body.iat !== null && Number.isNaN(Number(body.iat))) {
422
+ debug$1("delegationToken.verify.error.invalidIat");
423
+ return false;
424
+ }
425
+ const exp = Number(body.exp);
350
426
  const nbf = Number(body.nbf) || 0;
351
427
  const iat = Number(body.iat) || 0;
352
- if (exp && exp < now) {
428
+ if (exp < 0 || exp && exp < now) {
353
429
  debug$1("delegationToken.verify.error.expired");
354
430
  return false;
355
431
  }
@@ -362,6 +438,7 @@ async function verifyDelegationToken(token) {
362
438
  return false;
363
439
  }
364
440
  const alg = header.alg.toLowerCase();
441
+ let sigValid = false;
365
442
  if (alg === "passkey") {
366
443
  const [, , sigB64] = token.split(".");
367
444
  const assertionRaw = JSON.parse((0, _ocap_util.fromBase64)(sigB64).toString());
@@ -374,20 +451,56 @@ async function verifyDelegationToken(token) {
374
451
  authenticatorData: assertionRaw.authenticatorData,
375
452
  clientDataJSON: assertionRaw.clientDataJSON
376
453
  });
377
- return await (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.PASSKEY).verify(challenge, assertionRaw.signature, body.pk, extra);
454
+ sigValid = await (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.PASSKEY).verify(challenge, assertionRaw.signature, body.pk, extra);
455
+ } else {
456
+ const signers = {
457
+ ed25519: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.ED25519),
458
+ es256k: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.SECP256K1),
459
+ secp256k1: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.SECP256K1),
460
+ ethereum: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.ETHEREUM)
461
+ };
462
+ if (!signers[alg]) {
463
+ debug$1("delegationToken.verify.error.unknownAlg");
464
+ return false;
465
+ }
466
+ const msgHash = hasher((0, _ocap_util.toHex)(`${headerB64}.${bodyB64}`));
467
+ sigValid = signers[alg].verify(msgHash, signature, body.pk);
378
468
  }
379
- const signers = {
380
- ed25519: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.ED25519),
381
- es256k: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.SECP256K1),
382
- secp256k1: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.SECP256K1),
383
- ethereum: (0, _ocap_mcrypto.getSigner)(_ocap_mcrypto.types.KeyType.ETHEREUM)
384
- };
385
- if (!signers[alg]) {
386
- debug$1("delegationToken.verify.error.unknownAlg");
387
- return false;
469
+ if (!sigValid) return false;
470
+ if (body.delegation !== void 0) {
471
+ if (typeof body.delegation !== "string" || !body.delegation || maxDepth <= 0) {
472
+ debug$1("delegationToken.verify.error.invalidParentOrDepth");
473
+ return false;
474
+ }
475
+ const parent = decodeDelegationToken(body.delegation);
476
+ if ((0, _arcblock_did.toDid)(parent.body.sub) !== (0, _arcblock_did.toDid)(body.iss)) {
477
+ debug$1("delegationToken.verify.error.chainSubIssMismatch");
478
+ return false;
479
+ }
480
+ if ((0, _arcblock_did.toDid)(parent.body.iss) === (0, _arcblock_did.toDid)(body.sub)) {
481
+ debug$1("delegationToken.verify.error.chainCycle");
482
+ return false;
483
+ }
484
+ const parentExp = Number(parent.body.exp);
485
+ const childExp = Number(body.exp);
486
+ if (parentExp > 0 && (childExp === 0 || childExp > parentExp)) {
487
+ debug$1("delegationToken.verify.error.childOutlivesParent");
488
+ return false;
489
+ }
490
+ if (!Array.isArray(body.ops) || body.ops.length === 0 || !body.ops.every((op) => checkDelegationTokenScope(op, parent.body))) {
491
+ debug$1("delegationToken.verify.error.scopeEscalation");
492
+ return false;
493
+ }
494
+ for (const parentDeny of parent.body.deny ?? []) if ((0, _ocap_util.scopeMatchAny)(parentDeny, body.ops) && !(body.deny ?? []).includes(parentDeny)) {
495
+ debug$1("delegationToken.verify.error.denyDropped");
496
+ return false;
497
+ }
498
+ if (!await verifyDelegationToken(body.delegation, { maxDepth: maxDepth - 1 })) {
499
+ debug$1("delegationToken.verify.error.parentVerifyFailed");
500
+ return false;
501
+ }
388
502
  }
389
- const msgHash = hasher((0, _ocap_util.toHex)(`${headerB64}.${bodyB64}`));
390
- return signers[alg].verify(msgHash, signature, body.pk);
503
+ return true;
391
504
  } catch (err) {
392
505
  debug$1("delegationToken.verify.error.exception", err);
393
506
  return false;
package/lib/index.d.cts CHANGED
@@ -21,7 +21,13 @@ type JwtToken = {
21
21
  type JwtVerifyOptions = Partial<{
22
22
  tolerance: number;
23
23
  enforceTimestamp: boolean;
24
+ /** Accept `exp: 0` as "never expires" when enforceTimestamp is on (default false, #965) */
25
+ allowNoExpiry: boolean;
24
26
  signerKey: string;
27
+ audience: string | string[];
28
+ issuer: string | string[];
29
+ subject: string;
30
+ nonce: string;
25
31
  }>;
26
32
  /**
27
33
  *
@@ -67,6 +73,7 @@ declare function decode(token: string, bodyOnly?: false): JwtToken;
67
73
  * @param {{
68
74
  * tolerance: number; - number of seconds to tolerant expire
69
75
  * enforceTimestamp: boolean; - whether should be verify timestamps?
76
+ * allowNoExpiry: boolean; - accept exp=0 as "never expires" (default false)
70
77
  * signerKey: string; - which field should be used to pick the signer
71
78
  * }} [{
72
79
  * tolerance,
@@ -128,7 +135,11 @@ declare function signDelegationTokenUnsigned(signer: string, pk: BytesType, payl
128
135
  */
129
136
  declare function assembleDelegationToken(unsignedToken: string, assertion: PasskeyAssertion): string;
130
137
  declare function decodeDelegationToken(token: string): DelegationToken;
131
- declare function verifyDelegationToken(token: string): Promise<boolean>;
138
+ type DelegationVerifyOptions = Partial<{
139
+ subject: string;
140
+ maxDepth: number;
141
+ }>;
142
+ declare function verifyDelegationToken(token: string, options?: DelegationVerifyOptions): Promise<boolean>;
132
143
  declare function checkDelegationTokenScope(scope: string, {
133
144
  ops,
134
145
  deny
@@ -137,4 +148,4 @@ declare function checkDelegationTokenScope(scope: string, {
137
148
  deny?: string[];
138
149
  }): boolean;
139
150
  //#endregion
140
- export { DelegationToken, DelegationTokenBody, DelegationTokenHeader, JwtBody, JwtHeader, JwtToken, JwtVerifyOptions, PasskeyAssertion, assemble, assembleDelegationToken, checkDelegationTokenScope, decode, decodeDelegationToken, getChallenge, sign, signDelegationToken, signDelegationTokenUnsigned, signV2, verify, verifyDelegationToken };
151
+ export { DelegationToken, DelegationTokenBody, DelegationTokenHeader, DelegationVerifyOptions, JwtBody, JwtHeader, JwtToken, JwtVerifyOptions, PasskeyAssertion, assemble, assembleDelegationToken, checkDelegationTokenScope, decode, decodeDelegationToken, getChallenge, sign, signDelegationToken, signDelegationTokenUnsigned, signV2, verify, verifyDelegationToken };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@arcblock/jwt",
3
3
  "description": "JSON Web Token variant for arcblock DID solutions",
4
4
  "type": "module",
5
- "version": "1.30.23",
5
+ "version": "1.30.25",
6
6
  "author": {
7
7
  "name": "wangshijun",
8
8
  "email": "shijun@arcblock.io",
@@ -19,9 +19,9 @@
19
19
  "access": "public"
20
20
  },
21
21
  "dependencies": {
22
- "@arcblock/did": "1.30.23",
23
- "@ocap/mcrypto": "1.30.23",
24
- "@ocap/util": "1.30.23",
22
+ "@arcblock/did": "1.30.25",
23
+ "@ocap/mcrypto": "1.30.25",
24
+ "@ocap/util": "1.30.25",
25
25
  "debug": "^4.4.3",
26
26
  "fast-json-stable-stringify": "^2.1.0"
27
27
  },