unjwt 0.5.40 → 0.6.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.
Files changed (40) hide show
  1. package/README.md +147 -6
  2. package/dist/_chunks/_crypto.mjs +1014 -0
  3. package/dist/_chunks/error.d.mts +67 -0
  4. package/dist/_chunks/index.d.mts +149 -45
  5. package/dist/_chunks/jwk.d.mts +154 -57
  6. package/dist/_chunks/jws.mjs +729 -683
  7. package/dist/_chunks/utils.mjs +74 -70
  8. package/dist/{_chunks/jws.d.mts → adapters/h3v1/index.d.mts} +98 -58
  9. package/dist/{_chunks/jws2.mjs → adapters/h3v1/index.mjs} +198 -85
  10. package/dist/adapters/h3v2/index.d.mts +213 -0
  11. package/dist/adapters/{h3v2.mjs → h3v2/index.mjs} +195 -89
  12. package/dist/core/_crypto/index.d.mts +71 -0
  13. package/dist/core/_crypto/index.mjs +2 -0
  14. package/dist/core/jwe.d.mts +40 -0
  15. package/dist/{_chunks → core}/jwe.mjs +61 -32
  16. package/dist/core/jwk.d.mts +231 -0
  17. package/dist/core/jwk.mjs +686 -0
  18. package/dist/core/jws.d.mts +31 -0
  19. package/dist/core/jws.mjs +104 -0
  20. package/dist/{_chunks/index2.d.mts → core/utils/index.d.mts} +13 -12
  21. package/dist/core/utils/index.mjs +2 -0
  22. package/dist/index.d.mts +7 -6
  23. package/dist/index.mjs +5 -5
  24. package/package.json +49 -40
  25. package/dist/_chunks/chunk.mjs +0 -13
  26. package/dist/_chunks/jwe.d.mts +0 -43
  27. package/dist/_chunks/jwk.mjs +0 -1592
  28. package/dist/adapters/h3.d.mts +0 -3
  29. package/dist/adapters/h3.mjs +0 -3
  30. package/dist/adapters/h3v1.d.mts +0 -3
  31. package/dist/adapters/h3v1.mjs +0 -3
  32. package/dist/adapters/h3v2.d.mts +0 -183
  33. package/dist/jwe.d.mts +0 -3
  34. package/dist/jwe.mjs +0 -2
  35. package/dist/jwk.d.mts +0 -3
  36. package/dist/jwk.mjs +0 -2
  37. package/dist/jws.d.mts +0 -3
  38. package/dist/jws.mjs +0 -2
  39. package/dist/utils.d.mts +0 -3
  40. package/dist/utils.mjs +0 -2
@@ -0,0 +1,67 @@
1
+ //#region src/core/error.d.ts
2
+ /**
3
+ * All known error codes thrown by unjwt.
4
+ *
5
+ * Code naming follows the JOSE spec hierarchy:
6
+ * ERR_JWT_* — RFC 7519 JSON Web Token claim validation
7
+ * ERR_JWS_* — RFC 7515 JSON Web Signature
8
+ * ERR_JWE_* — RFC 7516 JSON Web Encryption
9
+ * ERR_JWK_* — RFC 7517 JSON Web Key
10
+ */
11
+ type JWTErrorCode = "ERR_JWT_EXPIRED" | "ERR_JWT_NBF" | "ERR_JWT_CLAIM_INVALID" | "ERR_JWT_CLAIM_MISSING" | "ERR_JWS_INVALID" | "ERR_JWS_SIGNATURE_INVALID" | "ERR_JWS_ALG_NOT_ALLOWED" | "ERR_JWE_INVALID" | "ERR_JWE_DECRYPTION_FAILED" | "ERR_JWE_ALG_NOT_ALLOWED" | "ERR_JWK_INVALID" | "ERR_JWK_KEY_NOT_FOUND" | (string & {});
12
+ /**
13
+ * Maps known error codes to their structured `cause` shapes.
14
+ * Only codes that carry decoded context beyond the message appear here.
15
+ * Extend this interface when new structured causes are introduced.
16
+ */
17
+ interface JWTErrorCauseMap {
18
+ /** Decoded claims available at the point the lifetime check failed. */
19
+ ERR_JWT_EXPIRED: {
20
+ jti?: string;
21
+ iat?: number;
22
+ exp?: number;
23
+ };
24
+ }
25
+ /**
26
+ * Base error class for all unjwt errors. Every error thrown by the library is
27
+ * an instance of `JWTError`, so a single `instanceof` check is sufficient to
28
+ * distinguish library errors from other thrown values. Use the `code` property
29
+ * (or the `isJWTError` type guard) to narrow to a specific error condition.
30
+ *
31
+ * @example
32
+ * import { JWTError, isJWTError } from "unjwt/jws";
33
+ *
34
+ * try {
35
+ * await verify(token, key);
36
+ * } catch (err) {
37
+ * if (isJWTError(err, "ERR_JWT_EXPIRED")) {
38
+ * console.log(err.cause.jti); // string | undefined
39
+ * }
40
+ * }
41
+ */
42
+ declare class JWTError<TCode extends JWTErrorCode = JWTErrorCode> extends Error {
43
+ readonly code: TCode;
44
+ override readonly cause?: unknown;
45
+ constructor(message: string, code: TCode, cause?: unknown);
46
+ }
47
+ /**
48
+ * Type guard that narrows `error` to `JWTError`.
49
+ *
50
+ * When called with a code that is a key of `JWTErrorCauseMap`, the return
51
+ * type is additionally narrowed to include the structured `cause` shape for
52
+ * that code. When called with any other `JWTErrorCode`, `code` is narrowed
53
+ * but `cause` remains `unknown`. When called without a code, narrows only to
54
+ * `JWTError`.
55
+ *
56
+ * @example
57
+ * if (isJWTError(err, "ERR_JWT_EXPIRED")) {
58
+ * err.cause.jti // string | undefined ✓
59
+ * }
60
+ */
61
+ declare function isJWTError<T extends keyof JWTErrorCauseMap>(error: unknown, code: T): error is JWTError<T> & {
62
+ cause: JWTErrorCauseMap[T];
63
+ };
64
+ declare function isJWTError<T extends JWTErrorCode>(error: unknown, code: T): error is JWTError<T>;
65
+ declare function isJWTError(error: unknown): error is JWTError;
66
+ //#endregion
67
+ export { isJWTError as i, JWTErrorCauseMap as n, JWTErrorCode as r, JWTError as t };
@@ -1,4 +1,58 @@
1
1
  //#region src/core/types/jwk.d.ts
2
+ /**
3
+ * The decoded JOSE protected header received by a {@link JWKLookupFunction}.
4
+ *
5
+ * The most commonly inspected fields are explicitly typed; additional
6
+ * algorithm-specific parameters (`epk`, `p2s`, `p2c`, `apu`, `apv`, …) are
7
+ * accessible through the index signature.
8
+ *
9
+ * `alg` is always present on both JWS and JWE tokens. `enc` is only present
10
+ * on JWE tokens.
11
+ */
12
+ type JWKLookupFunctionHeader = {
13
+ kid?: string;
14
+ alg?: string;
15
+ enc?: string;
16
+ typ?: string;
17
+ crit?: string[];
18
+ [propName: string]: unknown;
19
+ };
20
+ /**
21
+ * A callback that resolves the decryption or verification key from a token's
22
+ * decoded protected header and the raw compact token string.
23
+ *
24
+ * Accepted by both {@link verify} (JWS) and {@link decrypt} (JWE). When a
25
+ * {@link JWKSet} is returned, the library tries each key whose `kid` and `alg`
26
+ * are compatible with the header in order, stopping at the first that succeeds
27
+ * — see {@link getJWKsFromSet} for the selection logic.
28
+ *
29
+ * Returning a `string` is meaningful for JWE only (PBES2 password); for JWS
30
+ * a string return is encoded to bytes and used as a raw symmetric key.
31
+ */
32
+ type JWKLookupFunction = (header: JWKLookupFunctionHeader, token: string) => CryptoKey | JWK | JWKSet | string | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | JWKSet | string | Uint8Array<ArrayBuffer>>;
33
+ /**
34
+ * Interface for custom JWK import cache implementations.
35
+ *
36
+ * The adapter receives the full JWK object so each implementation can choose
37
+ * its own keying strategy:
38
+ * - {@link WeakMapJWKCache} uses the object reference (GC-friendly, no string hashing needed)
39
+ * - A `kid`-keyed cache extracts `jwk.kid` and uses it as a string key
40
+ * - A content-hash cache hashes `jwk.k` or `jwk.n`+`jwk.e`
41
+ *
42
+ * @example Use a custom kid-keyed cache:
43
+ * ```ts
44
+ * import { configureJWKCache } from 'unjwt/jwk';
45
+ * const map = new Map<string, CryptoKey>();
46
+ * configureJWKCache({
47
+ * get: (jwk, alg) => map.get(`${jwk.kid}:${alg}`),
48
+ * set: (jwk, alg, key) => map.set(`${jwk.kid}:${alg}`, key),
49
+ * });
50
+ * ```
51
+ */
52
+ interface JWKCacheAdapter {
53
+ get(jwk: JWK, alg: string): CryptoKey | undefined;
54
+ set(jwk: JWK, alg: string, key: CryptoKey): void;
55
+ }
2
56
  /** Structure returned for composite AES-CBC + HMAC keys. */
3
57
  type CompositeKey = {
4
58
  /** The AES-CBC encryption/decryption key. */encryptionKey: CryptoKey; /** The HMAC key for integrity/authentication. */
@@ -17,8 +71,12 @@ interface GenerateKeyOptions {
17
71
  publicExponent?: Uint8Array<ArrayBuffer>;
18
72
  /** Named curve for EC or OKP keys. Defaults to "P-256" for EC and "Ed25519" for OKP. */
19
73
  namedCurve?: "P-256" | "P-384" | "P-521" | "X25519" | "Ed25519" | "Ed448";
20
- /** Export the generated key(s) as JWK. If true, the key(s) will be returned in JWK format. */
21
- toJWK?: undefined | boolean | Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
74
+ /**
75
+ * Export the generated key(s) as JWK. When `true`, keys are returned in JWK
76
+ * format with a generated `kid`. To include additional JWK parameters
77
+ * (e.g. a custom `kid`), use {@link generateJWK} instead.
78
+ */
79
+ toJWK?: boolean;
22
80
  }
23
81
  type GenerateKeyReturnJWK<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_Asymmetric_Algorithm ? TAlg extends JWK_RSA_SIGN | JWK_RSA_PSS | JWK_RSA_ENC ? {
24
82
  privateKey: JWK_RSA_Private;
@@ -31,7 +89,7 @@ type GenerateKeyReturnJWK<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_
31
89
  publicKey: JWK_OKP_Public;
32
90
  } : never : TAlg extends JWK_AES_CBC_HMAC | JWK_Symmetric_Algorithm ? JWK_oct : never;
33
91
  type GenerateKeyReturnCrypto<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_AES_CBC_HMAC ? Uint8Array<ArrayBuffer> : TAlg extends JWK_Asymmetric_Algorithm ? CryptoKeyPair : CryptoKey;
34
- type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : TOptions["toJWK"] extends object ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
92
+ type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
35
93
  type GenerateJWKOptions = Omit<GenerateKeyOptions, "toJWK">;
36
94
  type GenerateJWKReturn<TAlg extends GenerateKeyAlgorithm> = GenerateKeyReturnJWK<TAlg>;
37
95
  /** Options for the deriveKeyFromPassword function. */
@@ -44,11 +102,15 @@ interface DeriveKeyOptions {
44
102
  keyUsage?: KeyUsage[];
45
103
  /** Mark the derived key as extractable. Defaults to false. */
46
104
  extractable?: boolean;
47
- /** Export the derived key as JWK. If true, the key will be returned in JWK_oct format. */
48
- toJWK?: undefined | boolean | Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
105
+ /**
106
+ * Export the derived key as JWK_oct. When `true`, returns the derived key in
107
+ * JWK format. To include additional JWK parameters (e.g. a custom `kid`),
108
+ * use {@link deriveJWKFromPassword} instead.
109
+ */
110
+ toJWK?: boolean;
49
111
  }
50
112
  type DeriveKeyReturn<TOptions extends DeriveKeyOptions> = TOptions["toJWK"] extends true ? JWK_oct : TOptions["toJWK"] extends object ? JWK_oct : CryptoKey;
51
- type KeyManagementAlgorithm = JWK_RSA_ENC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW | JWK_PBES2 | JWK_ECDH_ES;
113
+ type KeyManagementAlgorithm = JWK_RSA_ENC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW | JWK_PBES2 | JWK_ECDH_ES | "dir";
52
114
  type ContentEncryptionAlgorithm = JWK_AES_GCM | JWK_AES_CBC_HMAC;
53
115
  /** Options for the wrapKey function. */
54
116
  interface WrapKeyOptions {
@@ -58,12 +120,25 @@ interface WrapKeyOptions {
58
120
  p2s?: Uint8Array<ArrayBuffer>;
59
121
  /** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
60
122
  p2c?: number;
61
- /** ECDH-ES Ephemeral Public Key. Generated if not provided for ECDH-ES. */
62
- epk?: JWK_EC_Public;
63
- /** ECDH-ES Agreement PartyUInfo. */
64
- apu?: Uint8Array<ArrayBuffer>;
65
- /** ECDH-ES Agreement PartyVInfo. */
66
- apv?: Uint8Array<ArrayBuffer>;
123
+ /** ECDH-ES specific options. */
124
+ ecdh?: {
125
+ /**
126
+ * ECDH-ES ephemeral key material. A fresh ephemeral key pair is generated
127
+ * automatically when this is not provided.
128
+ */
129
+ ephemeralKey?: CryptoKey | JWK_EC_Private | CryptoKeyPair | {
130
+ publicKey: CryptoKey | JWK_EC_Public;
131
+ privateKey: CryptoKey | JWK_EC_Private;
132
+ }; /** Agreement PartyUInfo (apu). */
133
+ partyUInfo?: Uint8Array<ArrayBuffer>; /** Agreement PartyVInfo (apv). */
134
+ partyVInfo?: Uint8Array<ArrayBuffer>;
135
+ /**
136
+ * Content encryption algorithm used for key-length derivation.
137
+ * Required only when `alg` is `"ECDH-ES"` (direct key agreement).
138
+ * Not needed for `ECDH-ES+A128KW`, `+A192KW`, or `+A256KW`.
139
+ */
140
+ enc?: ContentEncryptionAlgorithm;
141
+ };
67
142
  }
68
143
  /** Result of the wrapKey function. */
69
144
  interface WrapKeyResult {
@@ -86,6 +161,13 @@ interface WrapKeyResult {
86
161
  }
87
162
  /** Options for the unwrapKey function. */
88
163
  interface UnwrapKeyOptions {
164
+ /**
165
+ * Output format of the unwrapped key.
166
+ * - `"cryptokey"` (default) — returns a `CryptoKey` ready for use with WebCrypto.
167
+ * - `"raw"` — returns the raw key bytes as `Uint8Array` (useful for
168
+ * interoperability or when the algorithm is not directly importable).
169
+ */
170
+ format?: "cryptokey" | "raw";
89
171
  /** Initialization Vector (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
90
172
  iv?: Uint8Array<ArrayBuffer> | string;
91
173
  /** Authentication Tag (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
@@ -108,7 +190,6 @@ interface UnwrapKeyOptions {
108
190
  keyUsage?: KeyUsage[];
109
191
  /** Mark the unwrapped key as extractable. Defaults to true. */
110
192
  extractable?: boolean;
111
- returnAs?: boolean | undefined;
112
193
  }
113
194
  /**
114
195
  * Forked from https://github.com/panva/jose/tree/v6.0.10
@@ -141,6 +222,12 @@ interface JWKParameters {
141
222
  x5u?: string;
142
223
  /** JWK "kid" (Key ID) Parameter */
143
224
  kid?: string;
225
+ /**
226
+ * Non-standard JWK content encryption algorithm hint.
227
+ * When `alg` is `"dir"`, the library reads this field to infer the
228
+ * content encryption algorithm (`enc`) when it is not provided in options.
229
+ */
230
+ enc?: ContentEncryptionAlgorithm;
144
231
  }
145
232
  /** Public EC JSON Web Keys */
146
233
  interface JWK_EC_Public extends JWKParameters {
@@ -256,7 +343,7 @@ type JWK_ECDH_ES = "ECDH-ES" | "ECDH-ES+A128KW" | "ECDH-ES+A192KW" | "ECDH-ES+A2
256
343
  type JWK_OKP_SIGN = "Ed25519" | "EdDSA";
257
344
  type JWK_Symmetric_Algorithm = JWK_HMAC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW;
258
345
  type JWK_Asymmetric_Algorithm = JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_RSA_ENC | JWK_OKP_SIGN | JWK_ECDH_ES;
259
- type JWKAlgorithm = JWK_Symmetric_Algorithm | JWK_Asymmetric_Algorithm | JWK_AES_CBC_HMAC | JWK_PBES2 | JWK_ECDH_ES | ("none" | "dir");
346
+ type JWKAlgorithm = JWK_Symmetric_Algorithm | JWK_Asymmetric_Algorithm | JWK_AES_CBC_HMAC | JWK_PBES2 | JWK_ECDH_ES | "dir";
260
347
  type JWKPEMAlgorithm = JWK_RSA_PSS | JWK_RSA_SIGN | JWK_RSA_ENC | JWK_ECDSA | JWK_ECDH_ES | JWK_OKP_SIGN;
261
348
  //#endregion
262
349
  //#region src/core/types/jwt.d.ts
@@ -283,6 +370,15 @@ interface JoseHeaderParameters {
283
370
  /** Any other JWS/JWE Header member. */
284
371
  [propName: string]: unknown;
285
372
  }
373
+ /**
374
+ * Payload accepted by {@link sign} and {@link encrypt}.
375
+ *
376
+ * Covers raw strings and bytes for non-JWT use, plus any JSON-serializable
377
+ * object for JWT and generic structured payloads. JWT-specific claim names
378
+ * are not enforced at this level — use {@link JWTClaims} when you want typed
379
+ * claim names and spec-compliant field types.
380
+ */
381
+ type JOSEPayload = string | Uint8Array<ArrayBuffer> | Record<string, unknown>;
286
382
  interface JWTClaims {
287
383
  /** "iss" (Issuer) Claim */
288
384
  iss?: string;
@@ -324,20 +420,18 @@ interface JWTClaimValidationOptions {
324
420
  currentDate?: Date;
325
421
  /** Additional required claim names. */
326
422
  requiredClaims?: string[];
327
- /** List of critical header parameters that must be understood and processed. */
328
- requiredHeaders?: string[];
329
423
  /**
330
- * Critical Header Parameters to be understood and processed.
331
- * If the JWT contains critical headers not in this list (and not inherently understood by the library), decryption will fail.
332
- * @deprecated use {@link requiredHeaders `recognizedHeaders` option} instead.
424
+ * Critical header parameters that this caller understands and has processed.
425
+ * If the token's `crit` header lists a parameter not present in this list
426
+ * (and not natively understood by the library), verification will fail per
427
+ * RFC 7515 §4.1.11 / RFC 7516 §4.1.13.
333
428
  */
334
- critical?: string[];
429
+ recognizedHeaders?: string[];
335
430
  }
336
431
  //#endregion
337
432
  //#region src/core/types/jws.d.ts
338
- type JWS_SIGN_EXTRA = "Ed25519" | "EdDSA";
339
433
  /** JWS Signing Algorithm Identifier. */
340
- type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWS_SIGN_EXTRA;
434
+ type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_OKP_SIGN;
341
435
  /** Recognized JWS Header Parameters, any other Header Members may also be present. */
342
436
  interface JWSHeaderParameters extends JoseHeaderParameters {
343
437
  /** JWS "alg" (Algorithm) Header Parameter */
@@ -373,7 +467,7 @@ interface JWSSignOptions {
373
467
  expiresIn?: ExpiresIn;
374
468
  }
375
469
  /** Result of JWS verification */
376
- interface JWSVerifyResult<T extends JWTClaims | Uint8Array<ArrayBuffer> | string = JWTClaims | Uint8Array<ArrayBuffer> | string> {
470
+ interface JWSVerifyResult<T extends JOSEPayload = JOSEPayload> {
377
471
  /** The decoded and verified payload. */
378
472
  payload: T;
379
473
  /** The JWS Protected Header. */
@@ -385,16 +479,15 @@ interface JWSVerifyOptions extends JWTClaimValidationOptions {
385
479
  algorithms?: JWSAlgorithm[];
386
480
  /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
387
481
  forceUint8Array?: boolean;
388
- /** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
389
- validateJWT?: boolean;
482
+ /**
483
+ * Controls JWT claim validation after a successful verification.
484
+ *
485
+ * - `true` — always validate claims regardless of the `typ` header
486
+ * - `false` — skip claim validation entirely
487
+ * - `undefined` (default) — validate when `typ` is `"JWT"` or contains `"jwt"`
488
+ */
489
+ validateClaims?: boolean;
390
490
  }
391
- /**
392
- * Key lookup function for JWS verification.
393
- * @param header The JWS Protected Header.
394
- * @param token The JWS token.
395
- * @returns The key material (CryptoKey, JWK, or raw Uint8Array) or a Promise resolving to it.
396
- */
397
- type JWSKeyLookupFunction = (header: JWSProtectedHeader, token: string) => CryptoKey | JWK | JWKSet | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | JWKSet | Uint8Array<ArrayBuffer>>;
398
491
  //#endregion
399
492
  //#region src/core/utils/types.d.ts
400
493
  type StrictOmit<T, K extends keyof T | (string & {}) | (number & {}) | (symbol & {})> = { [P in keyof T as P extends K ? never : P]: T[P] };
@@ -405,11 +498,11 @@ interface JWEHeaderParameters extends JoseHeaderParameters {
405
498
  /**
406
499
  * `alg` (Algorithm): Header Parameter - Key Management Algorithm
407
500
  */
408
- alg?: string;
501
+ alg?: KeyManagementAlgorithm | (string & {});
409
502
  /**
410
503
  * `enc` (Encryption Algorithm): Header Parameter - Content Encryption Algorithm
411
504
  */
412
- enc?: string;
505
+ enc?: ContentEncryptionAlgorithm | (string & {});
413
506
  /**
414
507
  * `p2c` (PBES2 Count): Header Parameter
415
508
  */
@@ -511,12 +604,6 @@ interface JWEEncryptOptions {
511
604
  contentEncryptionIV?: Uint8Array<ArrayBuffer>;
512
605
  }
513
606
  /**
514
- * Key lookup function for JWE decryption.
515
- * @param header The JWE Protected Header.
516
- * @returns The key material (CryptoKey, JWK, password string, or raw Uint8Array) or a Promise resolving to it.
517
- */
518
- type JWEKeyLookupFunction = (header: JWEHeaderParameters, token: string) => CryptoKey | JWK | string | Uint8Array<ArrayBuffer> | Promise<CryptoKey | JWK | string | Uint8Array<ArrayBuffer>>;
519
- /**
520
607
  * JWE (JSON Web Encryption) decryption options
521
608
  */
522
609
  interface JWEDecryptOptions extends JWTClaimValidationOptions {
@@ -532,19 +619,36 @@ interface JWEDecryptOptions extends JWTClaimValidationOptions {
532
619
  extractable?: boolean;
533
620
  /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
534
621
  forceUint8Array?: boolean;
535
- /** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
536
- validateJWT?: boolean;
622
+ /**
623
+ * Controls JWT claim validation after a successful decryption.
624
+ *
625
+ * - `true` — always validate claims regardless of the `typ` header
626
+ * - `false` — skip claim validation entirely
627
+ * - `undefined` (default) — validate when `typ` is `"JWT"` or contains `"jwt"`
628
+ */
629
+ validateClaims?: boolean;
537
630
  /** If true, include the Content Encryption Key (CEK) and Additional Authenticated Data (AAD) in the result. */
538
631
  returnCek?: boolean;
539
632
  }
540
633
  /**
634
+ * JWE Protected Header — the parsed and validated protected header returned
635
+ * after a successful {@link decrypt} call. Both `alg` and `enc` are present
636
+ * and strongly typed because decryption could not have succeeded without them.
637
+ */
638
+ interface JWEProtectedHeader extends JWEHeaderParameters {
639
+ /** Key management algorithm. */
640
+ alg: KeyManagementAlgorithm;
641
+ /** Content encryption algorithm. */
642
+ enc: ContentEncryptionAlgorithm;
643
+ }
644
+ /**
541
645
  * Result of a JWE decryption operation.
542
646
  */
543
- interface JWEDecryptResult<T extends JWTClaims | Uint8Array<ArrayBuffer> | string = JWTClaims | Uint8Array<ArrayBuffer> | string> {
647
+ interface JWEDecryptResult<T extends JOSEPayload = JOSEPayload> {
544
648
  /** The decrypted payload. */
545
649
  payload: T;
546
650
  /** The JWE Protected Header. */
547
- protectedHeader: JWEHeaderParameters;
651
+ protectedHeader: JWEProtectedHeader;
548
652
  /** The Content Encryption Key (CEK) used for decryption, as Uint8Array. Only present when `returnCek` is true. */
549
653
  cek?: Uint8Array<ArrayBuffer>;
550
654
  /** The Additional Authenticated Data (AAD) used, as Uint8Array. Only present when `returnCek` is true. */
@@ -555,4 +659,4 @@ interface JWEDecryptResult<T extends JWTClaims | Uint8Array<ArrayBuffer> | strin
555
659
  type ExpiresIn = number | `${number}` | `${number}${"s" | "second" | "seconds" | "m" | "minute" | "minutes" | "h" | "hour" | "hours" | "D" | "day" | "days" | "W" | "week" | "weeks" | "M" | "month" | "months" | "Y" | "year" | "years"}`;
556
660
  type MaxTokenAge = ExpiresIn;
557
661
  //#endregion
558
- export { JWK_Private as $, JWK as A, JWK_Asymmetric_Algorithm as B, DeriveKeyOptions as C, GenerateKeyAlgorithm as D, GenerateJWKReturn as E, JWK_AES_CBC_HMAC as F, JWK_EC_Public as G, JWK_ECDH_ES as H, JWK_AES_GCM as I, JWK_OKP_Private as J, JWK_HMAC as K, JWK_AES_GCM_KW as L, JWKPEMAlgorithm as M, JWKParameters as N, GenerateKeyOptions as O, JWKSet as P, JWK_Pair as Q, JWK_AES_KW as R, ContentEncryptionAlgorithm as S, GenerateJWKOptions as T, JWK_ECDSA as U, JWK_EC as V, JWK_EC_Private as W, JWK_OKP_SIGN as X, JWK_OKP_Public as Y, JWK_PBES2 as Z, JWT as _, JWEEncryptOptions as a, JWK_RSA_Public as at, JoseHeaderParameters as b, JWEKeyManagementHeaderParameters as c, JWK_Symmetric_Algorithm as ct, JWSHeaderParameters as d, UnwrapKeyOptions as dt, JWK_Public as et, JWSKeyLookupFunction as f, WrapKeyOptions as ft, JWSVerifyResult as g, JWSVerifyOptions as h, JWEDecryptResult as i, JWK_RSA_Private as it, JWKAlgorithm as j, GenerateKeyReturn as k, StrictOmit as l, JWK_oct as lt, JWSSignOptions as m, MaxTokenAge as n, JWK_RSA_ENC as nt, JWEHeaderParameters as o, JWK_RSA_SIGN as ot, JWSProtectedHeader as p, WrapKeyResult as pt, JWK_OKP as q, JWEDecryptOptions as r, JWK_RSA_PSS as rt, JWEKeyLookupFunction as s, JWK_Symmetric as st, ExpiresIn as t, JWK_RSA as tt, JWSAlgorithm as u, KeyManagementAlgorithm as ut, JWTClaimValidationOptions as v, DeriveKeyReturn as w, CompositeKey as x, JWTClaims as y, JWK_Asymmetric as z };
662
+ export { JWK_OKP_SIGN as $, JWK as A, JWK_AES_GCM_KW as B, DeriveKeyOptions as C, GenerateKeyAlgorithm as D, GenerateJWKReturn as E, JWKPEMAlgorithm as F, JWK_ECDH_ES as G, JWK_Asymmetric as H, JWKParameters as I, JWK_EC_Public as J, JWK_ECDSA as K, JWKSet as L, JWKCacheAdapter as M, JWKLookupFunction as N, GenerateKeyOptions as O, JWKLookupFunctionHeader as P, JWK_OKP_Public as Q, JWK_AES_CBC_HMAC as R, ContentEncryptionAlgorithm as S, GenerateJWKOptions as T, JWK_Asymmetric_Algorithm as U, JWK_AES_KW as V, JWK_EC as W, JWK_OKP as X, JWK_HMAC as Y, JWK_OKP_Private as Z, JWT as _, JWEEncryptOptions as a, JWK_RSA_ENC as at, JoseHeaderParameters as b, JWEProtectedHeader as c, JWK_RSA_Public as ct, JWSHeaderParameters as d, JWK_Symmetric_Algorithm as dt, JWK_PBES2 as et, JWSProtectedHeader as f, JWK_oct as ft, JOSEPayload as g, WrapKeyResult as gt, JWSVerifyResult as h, WrapKeyOptions as ht, JWEDecryptResult as i, JWK_RSA as it, JWKAlgorithm as j, GenerateKeyReturn as k, StrictOmit as l, JWK_RSA_SIGN as lt, JWSVerifyOptions as m, UnwrapKeyOptions as mt, MaxTokenAge as n, JWK_Private as nt, JWEHeaderParameters as o, JWK_RSA_PSS as ot, JWSSignOptions as p, KeyManagementAlgorithm as pt, JWK_EC_Private as q, JWEDecryptOptions as r, JWK_Public as rt, JWEKeyManagementHeaderParameters as s, JWK_RSA_Private as st, ExpiresIn as t, JWK_Pair as tt, JWSAlgorithm as u, JWK_Symmetric as ut, JWTClaimValidationOptions as v, DeriveKeyReturn as w, CompositeKey as x, JWTClaims as y, JWK_AES_GCM as z };