unjwt 0.6.1 → 0.7.1

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.
@@ -1,708 +0,0 @@
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
- }
56
- /** Structure returned for composite AES-CBC + HMAC keys. */
57
- type CompositeKey = {
58
- /** The AES-CBC encryption/decryption key. */encryptionKey: CryptoKey; /** The HMAC key for integrity/authentication. */
59
- macKey: CryptoKey;
60
- };
61
- type GenerateKeyAlgorithm = Exclude<JWKAlgorithm, "none" | "dir" | JWK_PBES2>;
62
- /** Options for the generateKey function. */
63
- interface GenerateKeyOptions {
64
- /** Key usages for the generated key(s). Note: For composite keys (CBC), default usages are applied separately. */
65
- keyUsage?: KeyUsage[];
66
- /** Mark the key(s) as extractable. Defaults to true. */
67
- extractable?: boolean;
68
- /** RSA modulus length. Defaults to 2048. */
69
- modulusLength?: number;
70
- /** RSA public exponent. Defaults to 65537 (0x010001). */
71
- publicExponent?: Uint8Array<ArrayBuffer>;
72
- /** Named curve for EC or OKP keys. Defaults to "P-256" for EC and "Ed25519" for OKP. */
73
- namedCurve?: "P-256" | "P-384" | "P-521" | "X25519" | "Ed25519" | "Ed448";
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;
80
- }
81
- type GenerateKeyReturnJWK<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_Asymmetric_Algorithm ? TAlg extends JWK_RSA_SIGN | JWK_RSA_PSS | JWK_RSA_ENC ? {
82
- privateKey: JWK_RSA_Private;
83
- publicKey: JWK_RSA_Public;
84
- } : TAlg extends JWK_ECDSA | JWK_ECDH_ES ? {
85
- privateKey: JWK_EC_Private;
86
- publicKey: JWK_EC_Public;
87
- } : TAlg extends JWK_OKP_SIGN ? {
88
- privateKey: JWK_OKP_Private;
89
- publicKey: JWK_OKP_Public;
90
- } : never : TAlg extends JWK_AES_CBC_HMAC | JWK_Symmetric_Algorithm ? JWK_oct : never;
91
- type GenerateKeyReturnCrypto<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_AES_CBC_HMAC ? Uint8Array<ArrayBuffer> : TAlg extends JWK_Asymmetric_Algorithm ? CryptoKeyPair : CryptoKey;
92
- type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
93
- type GenerateJWKOptions = Omit<GenerateKeyOptions, "toJWK">;
94
- type GenerateJWKReturn<TAlg extends GenerateKeyAlgorithm> = GenerateKeyReturnJWK<TAlg>;
95
- /** Options for the deriveKeyFromPassword function. */
96
- interface DeriveKeyOptions {
97
- /** Salt value (p2s). Must be at least 8 bytes. */
98
- salt: Uint8Array<ArrayBuffer>;
99
- /** Iteration count (p2c). Must be a positive integer. */
100
- iterations: number;
101
- /** Key usages for the derived key. Defaults to ["wrapKey", "unwrapKey"]. */
102
- keyUsage?: KeyUsage[];
103
- /** Mark the derived key as extractable. Defaults to false. */
104
- extractable?: boolean;
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;
111
- }
112
- type DeriveKeyReturn<TOptions extends DeriveKeyOptions> = TOptions["toJWK"] extends true ? JWK_oct : TOptions["toJWK"] extends object ? JWK_oct : CryptoKey;
113
- type KeyManagementAlgorithm = JWK_RSA_ENC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW | JWK_PBES2 | JWK_ECDH_ES | "dir";
114
- type ContentEncryptionAlgorithm = JWK_AES_GCM | JWK_AES_CBC_HMAC;
115
- /** Options for the wrapKey function. */
116
- interface WrapKeyOptions {
117
- /** Initialization Vector for AES-GCMKW. Generated if not provided. */
118
- iv?: Uint8Array<ArrayBuffer>;
119
- /** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
120
- p2s?: Uint8Array<ArrayBuffer>;
121
- /** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
122
- p2c?: number;
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
- };
142
- }
143
- /** Result of the wrapKey function. */
144
- interface WrapKeyResult {
145
- /** The wrapped key (Ciphertext). */
146
- encryptedKey: Uint8Array<ArrayBuffer>;
147
- /** Initialization Vector used (only for AES-GCMKW). Base64URL encoded. */
148
- iv?: string;
149
- /** Authentication Tag generated (only for AES-GCMKW). Base64URL encoded. */
150
- tag?: string;
151
- /** PBES2 Salt value used (only for PBES2). Base64URL encoded. */
152
- p2s?: string;
153
- /** PBES2 Iteration count used (only for PBES2). */
154
- p2c?: number;
155
- /** ECDH-ES Ephemeral Public Key used (only for ECDH-ES). */
156
- epk?: JWK_EC_Public;
157
- /** ECDH-ES Agreement PartyUInfo used (only for ECDH-ES). Base64URL encoded. */
158
- apu?: string;
159
- /** ECDH-ES Agreement PartyVInfo used (only for ECDH-ES). Base64URL encoded. */
160
- apv?: string;
161
- }
162
- /** Options for the unwrapKey function. */
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";
171
- /** Initialization Vector (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
172
- iv?: Uint8Array<ArrayBuffer> | string;
173
- /** Authentication Tag (required for AES-GCMKW). Base64URL encoded or Uint8Array. */
174
- tag?: Uint8Array<ArrayBuffer> | string;
175
- /** PBES2 Salt value (required for PBES2). Base64URL encoded or Uint8Array. */
176
- p2s?: Uint8Array<ArrayBuffer> | string;
177
- /** PBES2 Iteration count (required for PBES2). */
178
- p2c?: number;
179
- /** ECDH-ES Ephemeral Public Key (required for ECDH-ES). */
180
- epk?: JWK_EC_Public | CryptoKey;
181
- /** ECDH-ES Agreement PartyUInfo. Base64URL encoded or Uint8Array. */
182
- apu?: Uint8Array<ArrayBuffer> | string;
183
- /** ECDH-ES Agreement PartyVInfo. Base64URL encoded or Uint8Array. */
184
- apv?: Uint8Array<ArrayBuffer> | string;
185
- /** Content Encryption Algorithm used with ECDH-ES (required for direct key agreement). */
186
- enc?: ContentEncryptionAlgorithm;
187
- /** Expected unwrapped key algorithm (e.g., 'AES-GCM', 'AES-CBC'). Used by crypto.subtle.unwrapKey. */
188
- unwrappedKeyAlgorithm?: string | Algorithm | RsaOaepParams | AesCtrParams | AesCbcParams | AesGcmParams;
189
- /** Expected key usages for the unwrapped key. */
190
- keyUsage?: KeyUsage[];
191
- /** Mark the unwrapped key as extractable. Defaults to true. */
192
- extractable?: boolean;
193
- }
194
- /**
195
- * Forked from https://github.com/panva/jose/tree/v6.0.10
196
- * Copyright (c) 2018 Filip Skokan.
197
- * LICENSE: https://github.com/panva/jose/blob/v6.0.10/LICENSE.md
198
- */
199
- /** Generic JSON Web Key Parameters. */
200
- interface JWKParameters {
201
- /** JWK "kty" (Key Type) Parameter */
202
- kty: string;
203
- /**
204
- * JWK "alg" (Algorithm) Parameter
205
- *
206
- * @see {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements}
207
- */
208
- alg?: string;
209
- /** JWK "key_ops" (Key Operations) Parameter */
210
- key_ops?: KeyUsage[];
211
- /** JWK "ext" (Extractable) Parameter */
212
- ext?: boolean;
213
- /** JWK "use" (Public Key Use) Parameter */
214
- use?: string;
215
- /** JWK "x5c" (X.509 Certificate Chain) Parameter */
216
- x5c?: string[];
217
- /** JWK "x5t" (X.509 Certificate SHA-1 Thumbprint) Parameter */
218
- x5t?: string;
219
- /** JWK "x5t#S256" (X.509 Certificate SHA-256 Thumbprint) Parameter */
220
- "x5t#S256"?: string;
221
- /** JWK "x5u" (X.509 URL) Parameter */
222
- x5u?: string;
223
- /** JWK "kid" (Key ID) Parameter */
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;
231
- }
232
- /** Public EC JSON Web Keys */
233
- interface JWK_EC_Public extends JWKParameters {
234
- /** EC JWK "kty" (Key Type) Parameter */
235
- kty: "EC";
236
- /** EC JWK "crv" (Curve) Parameter */
237
- crv: string;
238
- /** EC JWK "x" (X Coordinate) Parameter */
239
- x: string;
240
- /** EC JWK "y" (Y Coordinate) Parameter */
241
- y: string;
242
- }
243
- /** Private EC JSON Web Keys */
244
- interface JWK_EC_Private extends JWK_EC_Public {
245
- /** EC JWK "d" (ECC Private Key) Parameter */
246
- d: string;
247
- }
248
- /** EC JSON Web Keys */
249
- type JWK_EC = JWK_EC_Public | JWK_EC_Private;
250
- /** Public RSA JSON Web Keys */
251
- interface JWK_RSA_Public extends JWKParameters {
252
- /** RSA JWK "kty" (Key Type) Parameter */
253
- kty: "RSA";
254
- /** RSA JWK "e" (Exponent) Parameter */
255
- e: string;
256
- /** RSA JWK "n" (Modulus) Parameter */
257
- n: string;
258
- oth?: RsaOtherPrimesInfo[];
259
- }
260
- /** Private RSA JSON Web Keys */
261
- interface JWK_RSA_Private extends JWK_RSA_Public {
262
- /** RSA JWK "d" (Private Exponent) Parameter */
263
- d: string;
264
- /** RSA JWK "dp" (First Factor CRT Exponent) Parameter */
265
- dp: string;
266
- /** RSA JWK "dq" (Second Factor CRT Exponent) Parameter */
267
- dq: string;
268
- /** RSA JWK "p" (First Prime Factor) Parameter */
269
- p: string;
270
- /** RSA JWK "q" (Second Prime Factor) Parameter */
271
- q: string;
272
- /** RSA JWK "qi" (First CRT Coefficient) Parameter */
273
- qi: string;
274
- }
275
- /** RSA JSON Web Keys */
276
- type JWK_RSA = JWK_RSA_Public | JWK_RSA_Private;
277
- /** Public ED JSON Web Keys */
278
- interface JWK_OKP_Public extends JWKParameters {
279
- /** ED JWK "kty" (Key Type) Parameter */
280
- kty: "OKP";
281
- /** ED JWK "crv" (Curve) Parameter */
282
- crv: string;
283
- /** ED JWK "x" (X Coordinate) Parameter */
284
- x: string;
285
- }
286
- /** Private ED JSON Web Keys */
287
- interface JWK_OKP_Private extends JWK_OKP_Public {
288
- /** ED JWK "d" (Private Key) Parameter */
289
- d: string;
290
- }
291
- /** OKP JSON Web Keys */
292
- type JWK_OKP = JWK_OKP_Public | JWK_OKP_Private;
293
- /** oct JSON Web Keys */
294
- interface JWK_oct extends JWKParameters {
295
- /** Oct JWK "k" (Key Value) Parameter */
296
- k: string;
297
- }
298
- /** Symmetric JSON Web Keys */
299
- type JWK_Symmetric = JWK_oct;
300
- /** Asymmetric JSON Web Keys */
301
- type JWK_Asymmetric = JWK_RSA | JWK_EC | JWK_OKP;
302
- /** Public JSON Web Keys */
303
- type JWK_Public = JWK_RSA_Public | JWK_EC_Public | JWK_OKP_Public;
304
- /** Private JSON Web Keys */
305
- type JWK_Private = JWK_RSA_Private | JWK_EC_Private | JWK_OKP_Private;
306
- /**
307
- * JSON Web Key ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK}). "RSA", "EC", "OKP" and "oct" key types are supported.
308
- */
309
- type JWK = JWK_oct | JWK_RSA | JWK_EC | JWK_OKP;
310
- /**
311
- * A pair of public and private JSON Web Keys.
312
- */
313
- type JWK_Pair = {
314
- publicKey: JWK_RSA_Public;
315
- privateKey: JWK_RSA_Private;
316
- } | {
317
- publicKey: JWK_EC_Public;
318
- privateKey: JWK_EC_Private;
319
- } | {
320
- publicKey: JWK_OKP_Public;
321
- privateKey: JWK_OKP_Private;
322
- };
323
- /**
324
- * JSON Web Key Set ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK Set}). "RSA", "EC" and "oct" key types are supported.
325
- */
326
- interface JWKSet {
327
- /** JWK Set "keys" Parameter */
328
- keys: JWK[];
329
- [parameter: string]: unknown;
330
- }
331
- /** JWK Key Algorithms */
332
- type JWK_HMAC = "HS256" | "HS384" | "HS512";
333
- type JWK_RSA_SIGN = "RS256" | "RS384" | "RS512";
334
- type JWK_RSA_PSS = "PS256" | "PS384" | "PS512";
335
- type JWK_ECDSA = "ES256" | "ES384" | "ES512";
336
- type JWK_RSA_ENC = "RSA-OAEP" | "RSA-OAEP-256" | "RSA-OAEP-384" | "RSA-OAEP-512";
337
- type JWK_AES_KW = "A128KW" | "A192KW" | "A256KW";
338
- type JWK_AES_CBC_HMAC = "A128CBC-HS256" | "A192CBC-HS384" | "A256CBC-HS512";
339
- type JWK_AES_GCM = "A128GCM" | "A192GCM" | "A256GCM";
340
- type JWK_AES_GCM_KW = "A128GCMKW" | "A192GCMKW" | "A256GCMKW";
341
- type JWK_PBES2 = "PBES2-HS256+A128KW" | "PBES2-HS384+A192KW" | "PBES2-HS512+A256KW";
342
- type JWK_ECDH_ES = "ECDH-ES" | "ECDH-ES+A128KW" | "ECDH-ES+A192KW" | "ECDH-ES+A256KW";
343
- type JWK_OKP_SIGN = "Ed25519" | "EdDSA";
344
- type JWK_Symmetric_Algorithm = JWK_HMAC | JWK_AES_KW | JWK_AES_GCM | JWK_AES_GCM_KW;
345
- type JWK_Asymmetric_Algorithm = JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_RSA_ENC | JWK_OKP_SIGN | JWK_ECDH_ES;
346
- type JWKAlgorithm = JWK_Symmetric_Algorithm | JWK_Asymmetric_Algorithm | JWK_AES_CBC_HMAC | JWK_PBES2 | JWK_ECDH_ES | "dir";
347
- type JWKPEMAlgorithm = JWK_RSA_PSS | JWK_RSA_SIGN | JWK_RSA_ENC | JWK_ECDSA | JWK_ECDH_ES | JWK_OKP_SIGN;
348
- //#endregion
349
- //#region src/core/types/jwt.d.ts
350
- /** Header Parameters common to JWE and JWS */
351
- interface JoseHeaderParameters {
352
- /** "kid" (Key ID) Header Parameter */
353
- kid?: string;
354
- /** "x5t" (X.509 Certificate SHA-1 Thumbprint) Header Parameter */
355
- x5t?: string;
356
- /** "x5c" (X.509 Certificate Chain) Header Parameter */
357
- x5c?: string[];
358
- /** "x5u" (X.509 URL) Header Parameter */
359
- x5u?: string;
360
- /** "jku" (JWK Set URL) Header Parameter */
361
- jku?: string;
362
- /** "jwk" (JSON Web Key) Header Parameter */
363
- jwk?: JWK_Public;
364
- /** "typ" (Type) Header Parameter */
365
- typ?: string;
366
- /** "cty" (Content Type) Header Parameter */
367
- cty?: string;
368
- /** "crit" (Critical) Header Parameter */
369
- crit?: string[];
370
- /** Any other JWS/JWE Header member. */
371
- [propName: string]: unknown;
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>;
382
- interface JWTClaims {
383
- /** "iss" (Issuer) Claim */
384
- iss?: string;
385
- /** "sub" (Subject) Claim */
386
- sub?: string;
387
- /** "aud" (Audience) Claim */
388
- aud?: string | string[];
389
- /** "exp" (Expiration Time) Claim */
390
- exp?: number;
391
- /** "nbf" (Not Before) Claim */
392
- nbf?: number;
393
- /** "iat" (Issued At) Claim */
394
- iat?: number;
395
- /** "jti" (JWT ID) Claim */
396
- jti?: string;
397
- /** Any other JWT member. */
398
- [propName: string]: unknown;
399
- }
400
- interface JWT<T = JWTClaims> {
401
- header: JoseHeaderParameters & {
402
- /** "alg" (Algorithm) Header Parameter */alg: string;
403
- };
404
- claims: T;
405
- }
406
- interface JWTClaimValidationOptions {
407
- /** Expected JWT "aud" (Audience) Claim value(s). Implies presence requirement. */
408
- audience?: string | string[];
409
- /** Expected JWT "iss" (Issuer) Claim value(s). Implies presence requirement. */
410
- issuer?: string | string[];
411
- /** Expected JWT "sub" (Subject) Claim value. Implies presence requirement. */
412
- subject?: string;
413
- /** Maximum token age, from the JWT "iat" (Issued At) Claim value. Implies presence requirement. */
414
- maxTokenAge?: MaxTokenAge;
415
- /** Clock skew tolerance (in seconds) for validating time-based claims (nbf/exp/iat). */
416
- clockTolerance?: number;
417
- /** Expected JWT "typ" (Type) Header Parameter value. Implies presence requirement. */
418
- typ?: string;
419
- /** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
420
- currentDate?: Date;
421
- /** Additional required claim names. */
422
- requiredClaims?: string[];
423
- /**
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.
428
- */
429
- recognizedHeaders?: string[];
430
- }
431
- //#endregion
432
- //#region src/core/types/jws.d.ts
433
- /** JWS Signing Algorithm Identifier. */
434
- type JWSAlgorithm = JWK_HMAC | JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_OKP_SIGN;
435
- /** Recognized JWS Header Parameters, any other Header Members may also be present. */
436
- interface JWSHeaderParameters extends JoseHeaderParameters {
437
- /** JWS "alg" (Algorithm) Header Parameter */
438
- alg?: JWSAlgorithm;
439
- /**
440
- * This JWS Extension Header Parameter modifies the JWS Payload representation and the JWS Signing
441
- * Input computation as per {@link https://www.rfc-editor.org/rfc/rfc7797 RFC7797}.
442
- * If `false`, the payload is used directly without Base64URL encoding.
443
- * Defaults to `true`.
444
- */
445
- b64?: boolean;
446
- }
447
- /** JWS Protected Header */
448
- interface JWSProtectedHeader extends JWSHeaderParameters {
449
- alg: JWSAlgorithm;
450
- }
451
- /**
452
- * JWS (JSON Web Signature) options for signing
453
- */
454
- interface JWSSignOptions {
455
- /** The JWS Algorithm to use. Must be provided. */
456
- alg?: JWSAlgorithm;
457
- /**
458
- * Additional protected header parameters. `alg` is automatically included.
459
- * `typ` defaults to "JWT" if not provided and payload is an object.
460
- */
461
- protectedHeader?: JWSHeaderParameters;
462
- /** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
463
- currentDate?: Date;
464
- /**
465
- * Time at which the JWS should expire, if no `exp` was already provided.
466
- */
467
- expiresIn?: ExpiresIn;
468
- }
469
- /** Result of JWS verification */
470
- interface JWSVerifyResult<T extends JOSEPayload = JOSEPayload> {
471
- /** The decoded and verified payload. */
472
- payload: T;
473
- /** The JWS Protected Header. */
474
- protectedHeader: JWSProtectedHeader;
475
- }
476
- /** Options for JWS verification */
477
- interface JWSVerifyOptions extends JWTClaimValidationOptions {
478
- /** List of allowed algorithms. If provided, the JWS `alg` must be in this list. */
479
- algorithms?: JWSAlgorithm[];
480
- /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
481
- forceUint8Array?: 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;
490
- }
491
- //#endregion
492
- //#region src/core/utils/types.d.ts
493
- type StrictOmit<T, K extends keyof T | (string & {}) | (number & {}) | (symbol & {})> = { [P in keyof T as P extends K ? never : P]: T[P] };
494
- //#endregion
495
- //#region src/core/types/jwe.d.ts
496
- /** Recognized JWE Header Parameters, any other Header Members may also be present. */
497
- interface JWEHeaderParameters extends JoseHeaderParameters {
498
- /**
499
- * `alg` (Algorithm): Header Parameter - Key Management Algorithm
500
- */
501
- alg?: KeyManagementAlgorithm | (string & {});
502
- /**
503
- * `enc` (Encryption Algorithm): Header Parameter - Content Encryption Algorithm
504
- */
505
- enc?: ContentEncryptionAlgorithm | (string & {});
506
- /**
507
- * `p2c` (PBES2 Count): Header Parameter
508
- */
509
- p2c?: number;
510
- /**
511
- * `p2s` (PBES2 Salt): Header Parameter. Base64URL encoded.
512
- */
513
- p2s?: string;
514
- /**
515
- * "iv" (Initialization Vector) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
516
- */
517
- iv?: string;
518
- /**
519
- * "tag" (Authentication Tag) Header Parameter for AES GCM Key Wrapping. Base64URL encoded.
520
- */
521
- tag?: string;
522
- /**
523
- * "epk" (Ephemeral Public Key) Header Parameter for ECDH-ES.
524
- */
525
- epk?: JWK_EC_Public;
526
- /**
527
- * "apu" (Agreement PartyUInfo) Header Parameter for ECDH-ES. Base64URL encoded.
528
- */
529
- apu?: string;
530
- /**
531
- * "apv" (Agreement PartyVInfo) Header Parameter for ECDH-ES. Base64URL encoded.
532
- */
533
- apv?: string;
534
- /** Any other JWE Header member. */
535
- [propName: string]: unknown;
536
- }
537
- /** Recognized JWE Key Management-related Header Parameters. */
538
- interface JWEKeyManagementHeaderParameters {
539
- apu?: Uint8Array<ArrayBuffer>;
540
- apv?: Uint8Array<ArrayBuffer>;
541
- /**
542
- * For Internal use only
543
- */
544
- p2c?: number;
545
- /**
546
- * For Internal use only
547
- */
548
- p2s?: Uint8Array<ArrayBuffer>;
549
- /**
550
- * For Internal use only
551
- */
552
- iv?: Uint8Array<ArrayBuffer>;
553
- /**
554
- * For Internal use only
555
- */
556
- epk?: CryptoKey | JWK_EC_Public;
557
- /**
558
- * For Internal use only
559
- */
560
- epkPrivateKey?: CryptoKey | JWK_EC_Private;
561
- }
562
- /**
563
- * JWE (JSON Web Encryption) encryption options
564
- */
565
- interface JWEEncryptOptions {
566
- /** JWE "alg" (Algorithm) Header Parameter. Key Management Algorithm. */
567
- alg?: KeyManagementAlgorithm;
568
- /** JWE "enc" (Encryption Algorithm) Header Parameter. Content Encryption Algorithm. */
569
- enc?: ContentEncryptionAlgorithm;
570
- /** Date to use when computing NumericDate claims, defaults to `new Date()`. */
571
- currentDate?: Date;
572
- /** Time at which the JWT should expire, if no `exp` was already provided (only when typ is "JWT" or explicitly parsing JWT). */
573
- expiresIn?: ExpiresIn;
574
- /** Additional JWE Protected Header parameters. */
575
- protectedHeader?: StrictOmit<JWEHeaderParameters, "alg" | "enc" | "iv" | "tag" | "p2s" | "p2c" | "epk" | "apu" | "apv">;
576
- /** Initialization Vector for AES-GCMKW key wrapping. Generated if not provided. */
577
- keyManagementIV?: Uint8Array<ArrayBuffer>;
578
- /** PBES2 Salt value (p2s). Required for PBES2 algorithms. */
579
- p2s?: Uint8Array<ArrayBuffer>;
580
- /** PBES2 Iteration count (p2c). Required for PBES2 algorithms. */
581
- p2c?: number;
582
- /** ECDH-ES specific options. */
583
- ecdh?: {
584
- /**
585
- * ECDH-ES Ephemeral key material. Provide a private key (CryptoKey or JWK),
586
- * a CryptoKeyPair, or an object exposing both public and private parts.
587
- */
588
- ephemeralKey?: CryptoKey | JWK_EC_Private | CryptoKeyPair | {
589
- publicKey: CryptoKey | JWK_EC_Public;
590
- privateKey: CryptoKey | JWK_EC_Private;
591
- }; /** ECDH-ES Agreement PartyUInfo. */
592
- partyUInfo?: Uint8Array<ArrayBuffer>; /** ECDH-ES Agreement PartyVInfo. */
593
- partyVInfo?: Uint8Array<ArrayBuffer>;
594
- };
595
- /**
596
- * Content Encryption Key (CEK) to use.
597
- * If provided, it will be used directly. Otherwise, a CEK will be generated.
598
- */
599
- cek?: Uint8Array<ArrayBuffer>;
600
- /**
601
- * Initialization Vector for content encryption.
602
- * If provided, it will be used. Otherwise, one will be generated.
603
- */
604
- contentEncryptionIV?: Uint8Array<ArrayBuffer>;
605
- }
606
- /**
607
- * JWE (JSON Web Encryption) decryption options
608
- */
609
- interface JWEDecryptOptions extends JWTClaimValidationOptions {
610
- /** A list of allowed JWE "alg" (Algorithm) Header Parameter values for key management. */
611
- algorithms?: KeyManagementAlgorithm[];
612
- /** A list of allowed JWE "enc" (Encryption Algorithm) Header Parameter values for content encryption. */
613
- encryptionAlgorithms?: ContentEncryptionAlgorithm[];
614
- /** Algorithm to import the unwrapped CEK as (e.g., { name: 'AES-GCM' }). Defaults based on 'enc'. */
615
- unwrappedKeyAlgorithm?: Parameters<typeof crypto.subtle.importKey>[2];
616
- /** Key usages for the unwrapped CEK. Defaults based on 'enc' (typically ['encrypt', 'decrypt']). */
617
- keyUsage?: KeyUsage[];
618
- /** Mark the unwrapped CEK as extractable. Defaults to true. */
619
- extractable?: boolean;
620
- /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
621
- forceUint8Array?: 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;
630
- /** If true, include the Content Encryption Key (CEK) and Additional Authenticated Data (AAD) in the result. */
631
- returnCek?: boolean;
632
- }
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
- /**
645
- * Result of a JWE decryption operation.
646
- */
647
- interface JWEDecryptResult<T extends JOSEPayload = JOSEPayload> {
648
- /** The decrypted payload. */
649
- payload: T;
650
- /** The JWE Protected Header. */
651
- protectedHeader: JWEProtectedHeader;
652
- /** The Content Encryption Key (CEK) used for decryption, as Uint8Array. Only present when `returnCek` is true. */
653
- cek?: Uint8Array<ArrayBuffer>;
654
- /** The Additional Authenticated Data (AAD) used, as Uint8Array. Only present when `returnCek` is true. */
655
- aad?: Uint8Array<ArrayBuffer>;
656
- }
657
- //#endregion
658
- //#region src/core/types/index.d.ts
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"}`;
660
- type MaxTokenAge = ExpiresIn;
661
- //#endregion
662
- //#region src/core/jwk.d.ts
663
- /**
664
- * Generates a Json Web Key (JWK) for the specified algorithm.
665
- *
666
- * @param alg The JWA algorithm identifier (e.g., "HS256", "RS256", "A128GCM").
667
- * @param jwkParams Optional partial JWK to merge with the generated key, allowing overrides.
668
- * @param options Configuration options for key generation.
669
- * @returns A Promise resolving to the generated JWK or JWK pair representation.
670
- */
671
- declare function generateJWK<TAlg extends GenerateKeyAlgorithm>(alg: TAlg, jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">, options?: GenerateJWKOptions): Promise<GenerateJWKReturn<TAlg>>;
672
- /**
673
- * Derives a Json Web Key (JWK) from a password using PBKDF2 as specified by PBES2 algorithms.
674
- *
675
- * @param password The password to derive the key from (string or Uint8Array).
676
- * @param alg The PBES2 algorithm identifier (e.g., "PBES2-HS256+A128KW").
677
- * @param jwkParams Optional partial JWK to merge with the generated key, allowing overrides.
678
- * @param options Configuration options including salt and iterations.
679
- * @returns A Promise resolving to the derived JWK (JWK_oct).
680
- */
681
- declare function deriveJWKFromPassword(password: string | Uint8Array<ArrayBuffer>, alg: JWK_PBES2, options: Omit<DeriveKeyOptions, "toJWK">, jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">): Promise<JWK_oct>;
682
- /**
683
- * Imports a key from a PEM-encoded string and converts it to a JWK.
684
- *
685
- * @param pem The PEM-encoded string.
686
- * @param pemType The type of PEM encoding ('pkcs8' for private keys, 'spki' for public keys, 'x509' for certificates).
687
- * @param alg The JWA algorithm identifier. This is crucial for `crypto.subtle.importKey`
688
- * to understand the key's intended algorithm and for setting the 'alg' field in the resulting JWK.
689
- * @param importOptions Options for the PEM import process (e.g., extractable for the CryptoKey).
690
- * @param jwkExtras Additional properties to merge into the resulting JWK.
691
- * @returns A Promise resolving to the imported key as a JWK.
692
- */
693
- declare function importFromPEM<T extends JWK>(pem: string, pemType: "pkcs8" | "spki" | "x509", alg: JWKPEMAlgorithm, options?: {
694
- /** Passed to `crypto.subtle.importKey`. Defaults to `false` for private keys, `true` otherwise. */extractable?: boolean; /** Additional JWK properties merged into the exported key (e.g. `kid`). */
695
- jwkParams?: Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
696
- }): Promise<T>;
697
- /**
698
- * Exports a JWK to a PEM-encoded string.
699
- *
700
- * @param jwk The JWK to export.
701
- * @param pemFormat The desired PEM format ('pkcs8' for private keys, 'spki' for public keys).
702
- * @param algForCryptoKeyImport If the JWK does not have an 'alg' property, this algorithm hint is
703
- * required to correctly convert it to a CryptoKey first.
704
- * @returns A Promise resolving to the PEM-encoded key string.
705
- */
706
- declare function exportToPEM(jwk: JWK, pemFormat: "pkcs8" | "spki", algForCryptoKeyImport?: JWKPEMAlgorithm): Promise<string>;
707
- //#endregion
708
- export { JWK_HMAC as $, GenerateJWKReturn as A, JWKParameters as B, JWTClaims as C, DeriveKeyOptions as D, ContentEncryptionAlgorithm as E, JWKAlgorithm as F, JWK_AES_KW as G, JWK_AES_CBC_HMAC as H, JWKCacheAdapter as I, JWK_EC as J, JWK_Asymmetric as K, JWKLookupFunction as L, GenerateKeyOptions as M, GenerateKeyReturn as N, DeriveKeyReturn as O, JWK as P, JWK_EC_Public as Q, JWKLookupFunctionHeader as R, JWTClaimValidationOptions as S, CompositeKey as T, JWK_AES_GCM as U, JWKSet as V, JWK_AES_GCM_KW as W, JWK_ECDSA as X, JWK_ECDH_ES as Y, JWK_EC_Private as Z, JWSSignOptions as _, KeyManagementAlgorithm as _t, ExpiresIn as a, JWK_Pair as at, JOSEPayload as b, WrapKeyResult as bt, JWEDecryptResult as c, JWK_RSA as ct, JWEKeyManagementHeaderParameters as d, JWK_RSA_Private as dt, JWK_OKP as et, JWEProtectedHeader as f, JWK_RSA_Public as ft, JWSProtectedHeader as g, JWK_oct as gt, JWSHeaderParameters as h, JWK_Symmetric_Algorithm as ht, importFromPEM as i, JWK_PBES2 as it, GenerateKeyAlgorithm as j, GenerateJWKOptions as k, JWEEncryptOptions as l, JWK_RSA_ENC as lt, JWSAlgorithm as m, JWK_Symmetric as mt, exportToPEM as n, JWK_OKP_Public as nt, MaxTokenAge as o, JWK_Private as ot, StrictOmit as p, JWK_RSA_SIGN as pt, JWK_Asymmetric_Algorithm as q, generateJWK as r, JWK_OKP_SIGN as rt, JWEDecryptOptions as s, JWK_Public as st, deriveJWKFromPassword as t, JWK_OKP_Private as tt, JWEHeaderParameters as u, JWK_RSA_PSS as ut, JWSVerifyOptions as v, UnwrapKeyOptions as vt, JoseHeaderParameters as w, JWT as x, JWSVerifyResult as y, WrapKeyOptions as yt, JWKPEMAlgorithm as z };