unjwt 0.5.13 → 0.5.15

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/README.md CHANGED
@@ -111,7 +111,7 @@ Creates a JWS token.
111
111
  - `key`: The signing key (`CryptoKey`, `JWK`, or `Uint8Array` for symmetric keys).
112
112
  - `options`:
113
113
  - `alg`: (Required) The JWS algorithm (e.g., `"HS256"`, `"RS256"`, `"ES256"`, `"PS256"`).
114
- - `protectedHeader`: An object for additional JWS Protected Header parameters (e.g., `kid`, `typ`, `cty`, `crit`, `b64`). If `payload` is an object and `typ` is not set, it defaults to `"at+jwt"`. The `b64` parameter ([RFC7797 section-3](https://datatracker.ietf.org/doc/html/rfc7797#section-3)) controls payload encoding (defaults to `true`, meaning Base64URL encoded).
114
+ - `protectedHeader`: An object for additional JWS Protected Header parameters (e.g., `kid`, `typ`, `cty`, `crit`, `b64`). If `payload` is an object and `typ` is not set, it defaults to `"JWT"`. The `b64` parameter ([RFC7797 section-3](https://datatracker.ietf.org/doc/html/rfc7797#section-3)) controls payload encoding (defaults to `true`, meaning Base64URL encoded).
115
115
  - `expiresIn`: Sets an expiration time in seconds (e.g., `3600` for 1 hour). If `iat` is missing it will be set to the current time. If `exp` is missing it will be set to `iat + expiresIn`. This is only applied if `payload` is a JWT.
116
116
  - `currentDate`: The current date for computing `expiresIn` option. Defaults to `new Date()`.
117
117
 
@@ -155,6 +155,7 @@ Verifies a JWS token.
155
155
  A `KeyLookupFunction` has the signature `(header: JWSProtectedHeader, jws: string) => Promise<CryptoKey | JWK | JWKSet | Uint8Array> | CryptoKey | JWK | JWKSet | Uint8Array`.
156
156
  - `options` (optional):
157
157
  - `algorithms`: An array of allowed JWS `alg` values. If not provided, the `alg` from the JWS header is used.
158
+ - `validateJWT`: Unless false, will parse payload as JWT and validate claims if applicable (typ includes "jwt", case insensitive). Default `undefined`.
158
159
  - `critical`: An array of JWS header parameter names that the application understands and processes.
159
160
 
160
161
  Returns a `Promise<JWSVerifyResult<T>>` which is an object `{ payload: T, protectedHeader: JWSProtectedHeader }`.
@@ -170,7 +171,7 @@ const secret = "supersecretkey";
170
171
 
171
172
  const { payload, protectedHeader } = await verify(token, secret);
172
173
  console.log(payload); // { message: "My important data" }
173
- console.log(protectedHeader); // { alg: "HS256", typ: "at+jwt" }
174
+ console.log(protectedHeader); // { alg: "HS256", typ: "JWT" }
174
175
  ```
175
176
 
176
177
  **Example (RS256 with key lookup):**
@@ -210,7 +211,7 @@ Encrypts data to produce a JWE token.
210
211
  - `options`:
211
212
  - `alg`: (Required) The JWE Key Management algorithm (e.g., `"A128KW"`, `"RSA-OAEP-256"`, `"PBES2-HS256+A128KW"`, `"ECDH-ES+A128KW"`), defaults depends on the key provided.
212
213
  - `enc`: (Required) The JWE Content Encryption algorithm (e.g., `"A128GCM"`, `"A256CBC-HS512"`), defaults depends on the key provided.
213
- - `protectedHeader`: An object for JWE Protected Header parameters (e.g., `kid`, `typ`, `cty`, `crit`, `apu`, `apv`, `p2s`, `p2c`). If `payload` is an object and `typ` is not set, it defaults to `"at+jwt"`.
214
+ - `protectedHeader`: An object for JWE Protected Header parameters (e.g., `kid`, `typ`, `cty`, `crit`, `apu`, `apv`, `p2s`, `p2c`). If `payload` is an object and `typ` is not set, it defaults to `"JWT"`.
214
215
  - `cek`: (Optional) Provide your own Content Encryption Key (`CryptoKey` or `Uint8Array`).
215
216
  - `contentEncryptionIV`: (Optional) Provide your own Initialization Vector for content encryption (`Uint8Array`).
216
217
  - Other algorithm-specific options like `p2s`, `p2c` (for PBES2), `keyManagementIV`, `ecdhPartyUInfo`, `ecdhPartyVInfo`.
@@ -259,6 +260,7 @@ Decrypts a JWE token.
259
260
  A `JWEKeyLookupFunction` has the signature `(header: JWEHeaderParameters) => Promise<CryptoKey | JWK | string | Uint8Array> | CryptoKey | JWK | string | Uint8Array`.
260
261
  - `options` (optional):
261
262
  - `algorithms`: Array of allowed JWE Key Management `alg` values.
263
+ - `validateJWT`: Unless false, will parse payload as JWT and validate claims if applicable (typ includes "jwt", case insensitive). Default `undefined`.
262
264
  - `encryptionAlgorithms`: Array of allowed JWE Content Encryption `enc` values.
263
265
  - `critical`: Array of JWE header parameter names that the application understands.
264
266
  - `unwrappedKeyAlgorithm`: (For `unwrapKey` internally) Algorithm details for the CEK after unwrapping.
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, E as JWKAlgorithm, F as JWKPEMAlgorithm, J as JWKParameters, n as JWKSet, v as JWK_AES_CBC_HMAC, w as JWK_AES_GCM, u as JWK_AES_GCM_KW, t as JWK_AES_KW, B as JWK_Asymmetric_Algorithm, h as JWK_EC, y as JWK_ECDH_ES, r as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, z as JWK_ED, o as JWK_HMAC, x as JWK_PBES2, k as JWK_RSA, s as JWK_RSA_ENC, q as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, p as JWK_RSA_SIGN, A as JWK_Symmetric_Algorithm, l as JWK_oct, L as JWT, M as JWTClaimValidationOptions, I as JWTClaims, H as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.CN1C8TlR.mjs';
2
- export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, j as jws } from './shared/unjwt.BRtnm5sB.mjs';
3
- export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, j as jwe } from './shared/unjwt.B9sSOKE3.mjs';
4
- export { i as utils } from './shared/unjwt.Cay_cOOI.mjs';
5
- export { j as jwk } from './shared/unjwt.DtVML6Qp.mjs';
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, r as JWK, M as JWKAlgorithm, N as JWKPEMAlgorithm, J as JWKParameters, s as JWKSet, A as JWK_AES_CBC_HMAC, B as JWK_AES_GCM, z as JWK_AES_GCM_KW, y as JWK_AES_KW, L as JWK_Asymmetric_Algorithm, h as JWK_EC, F as JWK_ECDH_ES, w as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, t as JWK_HMAC, n as JWK_OKP, m as JWK_OKP_Private, l as JWK_OKP_Public, H as JWK_OKP_SIGN, E as JWK_PBES2, q as JWK_Private, p as JWK_Public, k as JWK_RSA, x as JWK_RSA_ENC, v as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, u as JWK_RSA_SIGN, I as JWK_Symmetric_Algorithm, o as JWK_oct, Q as JWT, R as JWTClaimValidationOptions, P as JWTClaims, O as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.BsgIQHVD.mjs';
2
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, j as jws } from './shared/unjwt.Bw4pzdE0.mjs';
3
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, j as jwe } from './shared/unjwt.BeWVYyaf.mjs';
4
+ export { i as utils } from './shared/unjwt.CfL4cArP.mjs';
5
+ export { j as jwk } from './shared/unjwt.C6b_GyXm.mjs';
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- export { i as utils } from './shared/unjwt.mBSKL6g4.mjs';
2
- export { j as jwe } from './shared/unjwt.UfthlTWt.mjs';
3
- export { j as jwk } from './shared/unjwt.CX8WRx20.mjs';
4
- export { j as jws } from './shared/unjwt.C6flzAeB.mjs';
5
- import './shared/unjwt.DFAKwqBz.mjs';
1
+ export { i as utils } from './shared/unjwt.CRHfsi7v.mjs';
2
+ export { j as jwe } from './shared/unjwt.BZ3B0Ak8.mjs';
3
+ export { j as jwk } from './shared/unjwt.D1MC63Yb.mjs';
4
+ export { j as jws } from './shared/unjwt.u3h40Nr-.mjs';
5
+ import './shared/unjwt.BpusjhPR.mjs';
package/dist/jwe.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { L as JWT, M as JWTClaimValidationOptions, I as JWTClaims, H as JoseHeaderParameters } from './shared/unjwt.CN1C8TlR.mjs';
2
- export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, g as decrypt, f as encrypt } from './shared/unjwt.B9sSOKE3.mjs';
1
+ export { Q as JWT, R as JWTClaimValidationOptions, P as JWTClaims, O as JoseHeaderParameters } from './shared/unjwt.BsgIQHVD.mjs';
2
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters, g as decrypt, f as encrypt } from './shared/unjwt.BeWVYyaf.mjs';
package/dist/jwe.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import './shared/unjwt.CX8WRx20.mjs';
2
- import './shared/unjwt.mBSKL6g4.mjs';
3
- export { d as decrypt, e as encrypt } from './shared/unjwt.UfthlTWt.mjs';
4
- import './shared/unjwt.DFAKwqBz.mjs';
1
+ import './shared/unjwt.D1MC63Yb.mjs';
2
+ import './shared/unjwt.CRHfsi7v.mjs';
3
+ export { d as decrypt, e as encrypt } from './shared/unjwt.BZ3B0Ak8.mjs';
4
+ import './shared/unjwt.BpusjhPR.mjs';
package/dist/jwk.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, m as JWK, E as JWKAlgorithm, F as JWKPEMAlgorithm, J as JWKParameters, n as JWKSet, v as JWK_AES_CBC_HMAC, w as JWK_AES_GCM, u as JWK_AES_GCM_KW, t as JWK_AES_KW, B as JWK_Asymmetric_Algorithm, h as JWK_EC, y as JWK_ECDH_ES, r as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, z as JWK_ED, o as JWK_HMAC, x as JWK_PBES2, k as JWK_RSA, s as JWK_RSA_ENC, q as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, p as JWK_RSA_SIGN, A as JWK_Symmetric_Algorithm, l as JWK_oct, L as JWT, M as JWTClaimValidationOptions, I as JWTClaims, H as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.CN1C8TlR.mjs';
2
- export { d as deriveKeyFromPassword, b as exportJWKToPEM, e as exportKey, g as generateKey, c as getJWKFromSet, a as importJWKFromPEM, i as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.DtVML6Qp.mjs';
1
+ export { C as CompositeKey, d as ContentEncryptionAlgorithm, D as DeriveKeyOptions, c as DeriveKeyReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, r as JWK, M as JWKAlgorithm, N as JWKPEMAlgorithm, J as JWKParameters, s as JWKSet, A as JWK_AES_CBC_HMAC, B as JWK_AES_GCM, z as JWK_AES_GCM_KW, y as JWK_AES_KW, L as JWK_Asymmetric_Algorithm, h as JWK_EC, F as JWK_ECDH_ES, w as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, t as JWK_HMAC, n as JWK_OKP, m as JWK_OKP_Private, l as JWK_OKP_Public, H as JWK_OKP_SIGN, E as JWK_PBES2, q as JWK_Private, p as JWK_Public, k as JWK_RSA, x as JWK_RSA_ENC, v as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, u as JWK_RSA_SIGN, I as JWK_Symmetric_Algorithm, o as JWK_oct, Q as JWT, R as JWTClaimValidationOptions, P as JWTClaims, O as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, e as WrapKeyResult } from './shared/unjwt.BsgIQHVD.mjs';
2
+ export { d as deriveKeyFromPassword, b as exportJWKToPEM, e as exportKey, g as generateKey, c as getJWKFromSet, a as importJWKFromPEM, i as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.C6b_GyXm.mjs';
package/dist/jwk.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import './shared/unjwt.mBSKL6g4.mjs';
2
- export { p as deriveKeyFromPassword, t as exportJWKToPEM, q as exportKey, o as generateKey, g as getJWKFromSet, s as importJWKFromPEM, i as importKey, m as unwrapKey, r as wrapKey } from './shared/unjwt.CX8WRx20.mjs';
1
+ import './shared/unjwt.CRHfsi7v.mjs';
2
+ export { p as deriveKeyFromPassword, t as exportJWKToPEM, q as exportKey, o as generateKey, g as getJWKFromSet, s as importJWKFromPEM, i as importKey, m as unwrapKey, r as wrapKey } from './shared/unjwt.D1MC63Yb.mjs';
package/dist/jws.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- export { L as JWT, M as JWTClaimValidationOptions, I as JWTClaims, H as JoseHeaderParameters } from './shared/unjwt.CN1C8TlR.mjs';
2
- export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, s as sign, v as verify } from './shared/unjwt.BRtnm5sB.mjs';
1
+ export { Q as JWT, R as JWTClaimValidationOptions, P as JWTClaims, O as JoseHeaderParameters } from './shared/unjwt.BsgIQHVD.mjs';
2
+ export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult, s as sign, v as verify } from './shared/unjwt.Bw4pzdE0.mjs';
package/dist/jws.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import './shared/unjwt.CX8WRx20.mjs';
2
- import './shared/unjwt.mBSKL6g4.mjs';
3
- import './shared/unjwt.DFAKwqBz.mjs';
4
- export { s as sign, v as verify } from './shared/unjwt.C6flzAeB.mjs';
1
+ import './shared/unjwt.D1MC63Yb.mjs';
2
+ import './shared/unjwt.CRHfsi7v.mjs';
3
+ import './shared/unjwt.BpusjhPR.mjs';
4
+ export { s as sign, v as verify } from './shared/unjwt.u3h40Nr-.mjs';
@@ -1,6 +1,6 @@
1
- import { a as checkEncCryptoKey, u as uint32be, b as generateCEK, e as encryptIV, w as wrap, k as keyToJWK, d as bitLengthCEK, f as jwkTokey, i as importKey, h as generateIV, l as encrypt$1, m as unwrapKey, n as decrypt$1 } from './unjwt.CX8WRx20.mjs';
2
- import { k as concatUint8Arrays, t as textEncoder, l as assertCryptoKey, d as base64UrlEncode, m as isCryptoKey, a as isJWK, s as sanitizeObject, f as base64UrlDecode, r as randomBytes, b as applyTypCtyDefaults, c as computeJwtTimeClaims, n as getPlaintextBytes, o as decodePayloadFromBytes, p as validateCriticalHeadersJWE } from './unjwt.mBSKL6g4.mjs';
3
- import { c as checkKeyLength } from './unjwt.DFAKwqBz.mjs';
1
+ import { a as checkEncCryptoKey, u as uint32be, b as generateCEK, e as encryptIV, w as wrap, k as keyToJWK, d as bitLengthCEK, f as jwkTokey, i as importKey, h as generateIV, l as encrypt$1, m as unwrapKey, n as decrypt$1 } from './unjwt.D1MC63Yb.mjs';
2
+ import { k as concatUint8Arrays, t as textEncoder, l as assertCryptoKey, d as base64UrlEncode, m as isCryptoKey, a as isJWK, s as sanitizeObject, f as base64UrlDecode, r as randomBytes, b as applyTypCtyDefaults, c as computeJwtTimeClaims, n as getPlaintextBytes, o as decodePayloadFromBytes, p as validateCriticalHeadersJWE, j as validateJwtClaims } from './unjwt.CRHfsi7v.mjs';
3
+ import { c as checkKeyLength } from './unjwt.BpusjhPR.mjs';
4
4
 
5
5
  function lengthAndInput(input) {
6
6
  return concatUint8Arrays(uint32be(input.length), input);
@@ -293,7 +293,7 @@ async function encrypt(payload, key, options = {}) {
293
293
  ];
294
294
  return jweParts.join(".");
295
295
  }
296
- async function decrypt(jwe, key, options) {
296
+ async function decrypt(jwe, key, options = {}) {
297
297
  const parts = jwe.split(".");
298
298
  if (parts.length !== 5) {
299
299
  throw new Error(
@@ -375,6 +375,9 @@ async function decrypt(jwe, key, options) {
375
375
  ...options?.critical || [],
376
376
  ...options?.requiredHeaders || []
377
377
  ]);
378
+ if (payload && typeof payload === "object" && options.validateJWT !== false && (options.validateJWT === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) {
379
+ validateJwtClaims(payload, options);
380
+ }
378
381
  return {
379
382
  payload,
380
383
  protectedHeader,
@@ -1,4 +1,4 @@
1
- import { K as KeyManagementAlgorithm, d as ContentEncryptionAlgorithm, H as JoseHeaderParameters, f as JWK_EC_Public, m as JWK, M as JWTClaimValidationOptions, I as JWTClaims, L as JWT } from './unjwt.CN1C8TlR.mjs';
1
+ import { K as KeyManagementAlgorithm, d as ContentEncryptionAlgorithm, O as JoseHeaderParameters, f as JWK_EC_Public, r as JWK, R as JWTClaimValidationOptions, P as JWTClaims, Q as JWT } from './unjwt.BsgIQHVD.mjs';
2
2
 
3
3
  /** Recognized JWE Header Parameters, any other Header Members may also be present. */
4
4
  interface JWEHeaderParameters extends JoseHeaderParameters {
@@ -72,7 +72,7 @@ interface JWEEncryptOptions {
72
72
  enc?: ContentEncryptionAlgorithm;
73
73
  /** Date to use when computing NumericDate claims, defaults to `new Date()`. */
74
74
  currentDate?: Date;
75
- /** Time at which the JWT should expire, if no `exp` was already provided (only when typ is "at+jwt"). */
75
+ /** Time at which the JWT should expire, if no `exp` was already provided (only when typ is "JWT" or explicitly parsing JWT). */
76
76
  expiresIn?: number;
77
77
  /** Additional JWE Protected Header parameters. */
78
78
  protectedHeader?: Omit<JWEHeaderParameters, "alg" | "enc" | "iv" | "tag" | "p2s" | "p2c" | "apu" | "apv">;
@@ -119,6 +119,8 @@ interface JWEDecryptOptions extends JWTClaimValidationOptions {
119
119
  extractable?: boolean;
120
120
  /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred based on JWE headers. */
121
121
  forceUint8Array?: boolean;
122
+ /** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
123
+ validateJWT?: boolean;
122
124
  }
123
125
  /**
124
126
  * Result of a JWE decryption operation.
@@ -1,4 +1,4 @@
1
- import { c as checkSigCryptoKey } from './unjwt.CX8WRx20.mjs';
1
+ import { c as checkSigCryptoKey } from './unjwt.D1MC63Yb.mjs';
2
2
 
3
3
  /**
4
4
  * This is a fork of Jose library's utility functions.
@@ -17,14 +17,14 @@ interface GenerateKeyOptions {
17
17
  /** RSA public exponent. Defaults to 65537 (0x010001). */
18
18
  publicExponent?: Uint8Array<ArrayBuffer>;
19
19
  /** Export the generated key(s) as JWK. If true, the key(s) will be returned in JWK format. */
20
- toJWK?: boolean | undefined;
20
+ toJWK?: undefined | boolean | Omit<JWKParameters, "alg" | "kty" | "key_ops" | "ext">;
21
21
  }
22
22
  type GenerateKeyReturnJWK<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_Asymmetric_Algorithm ? {
23
23
  privateKey: JWK;
24
24
  publicKey: JWK;
25
25
  } : JWK;
26
26
  type GenerateKeyReturnCrypto<TAlg extends GenerateKeyAlgorithm> = TAlg extends JWK_AES_CBC_HMAC ? Uint8Array<ArrayBuffer> : TAlg extends JWK_Asymmetric_Algorithm ? CryptoKeyPair : CryptoKey;
27
- type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
27
+ type GenerateKeyReturn<TAlg extends GenerateKeyAlgorithm, TOptions extends GenerateKeyOptions> = TOptions["toJWK"] extends true ? GenerateKeyReturnJWK<TAlg> : TOptions["toJWK"] extends object ? GenerateKeyReturnJWK<TAlg> : GenerateKeyReturnCrypto<TAlg>;
28
28
  /** Options for the deriveKeyFromPassword function. */
29
29
  interface DeriveKeyOptions {
30
30
  /** Salt value (p2s). Must be at least 8 bytes. */
@@ -133,6 +133,8 @@ interface JWKParameters {
133
133
  }
134
134
  /** Public EC JSON Web Keys */
135
135
  interface JWK_EC_Public extends JWKParameters {
136
+ /** EC JWK "kty" (Key Type) Parameter */
137
+ kty: "EC";
136
138
  /** EC JWK "crv" (Curve) Parameter */
137
139
  crv: string;
138
140
  /** EC JWK "x" (X Coordinate) Parameter */
@@ -141,7 +143,7 @@ interface JWK_EC_Public extends JWKParameters {
141
143
  y: string;
142
144
  }
143
145
  /** Private EC JSON Web Keys */
144
- interface JWK_EC_Private extends JWK_EC_Public, JWKParameters {
146
+ interface JWK_EC_Private extends JWK_EC_Public {
145
147
  /** EC JWK "d" (ECC Private Key) Parameter */
146
148
  d: string;
147
149
  }
@@ -149,6 +151,8 @@ interface JWK_EC_Private extends JWK_EC_Public, JWKParameters {
149
151
  type JWK_EC = JWK_EC_Public | JWK_EC_Private;
150
152
  /** Public RSA JSON Web Keys */
151
153
  interface JWK_RSA_Public extends JWKParameters {
154
+ /** RSA JWK "kty" (Key Type) Parameter */
155
+ kty: "RSA";
152
156
  /** RSA JWK "e" (Exponent) Parameter */
153
157
  e: string;
154
158
  /** RSA JWK "n" (Modulus) Parameter */
@@ -156,7 +160,7 @@ interface JWK_RSA_Public extends JWKParameters {
156
160
  oth?: RsaOtherPrimesInfo[];
157
161
  }
158
162
  /** Private RSA JSON Web Keys */
159
- interface JWK_RSA_Private extends JWK_RSA_Public, JWKParameters {
163
+ interface JWK_RSA_Private extends JWK_RSA_Public {
160
164
  /** RSA JWK "d" (Private Exponent) Parameter */
161
165
  d: string;
162
166
  /** RSA JWK "dp" (First Factor CRT Exponent) Parameter */
@@ -172,15 +176,35 @@ interface JWK_RSA_Private extends JWK_RSA_Public, JWKParameters {
172
176
  }
173
177
  /** RSA JSON Web Keys */
174
178
  type JWK_RSA = JWK_RSA_Public | JWK_RSA_Private;
179
+ /** Public ED JSON Web Keys */
180
+ interface JWK_OKP_Public extends JWKParameters {
181
+ /** ED JWK "kty" (Key Type) Parameter */
182
+ kty: "OKP";
183
+ /** ED JWK "crv" (Curve) Parameter */
184
+ crv: string;
185
+ /** ED JWK "x" (X Coordinate) Parameter */
186
+ x: string;
187
+ }
188
+ /** Private ED JSON Web Keys */
189
+ interface JWK_OKP_Private extends JWK_OKP_Public {
190
+ /** ED JWK "d" (Private Key) Parameter */
191
+ d: string;
192
+ }
193
+ /** OKP JSON Web Keys */
194
+ type JWK_OKP = JWK_OKP_Public | JWK_OKP_Private;
175
195
  /** oct JSON Web Keys */
176
196
  interface JWK_oct extends JWKParameters {
177
197
  /** Oct JWK "k" (Key Value) Parameter */
178
198
  k: string;
179
199
  }
200
+ /** Public JSON Web Keys */
201
+ type JWK_Public = JWK_RSA_Public | JWK_EC_Public | JWK_OKP_Public | JWK_oct;
202
+ /** Private JSON Web Keys */
203
+ type JWK_Private = JWK_RSA_Private | JWK_EC_Private | JWK_OKP_Private;
180
204
  /**
181
- * JSON Web Key ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK}). "RSA", "EC" and "oct" key types are supported.
205
+ * JSON Web Key ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK}). "RSA", "EC", "OKP" and "oct" key types are supported.
182
206
  */
183
- type JWK = JWK_oct | JWK_RSA | JWK_EC;
207
+ type JWK = JWK_oct | JWK_RSA | JWK_EC | JWK_OKP;
184
208
  /**
185
209
  * JSON Web Key Set ({@link https://www.rfc-editor.org/rfc/rfc7517 JWK Set}). "RSA", "EC" and "oct" key types are supported.
186
210
  */
@@ -201,11 +225,11 @@ type JWK_AES_CBC_HMAC = "A128CBC-HS256" | "A192CBC-HS384" | "A256CBC-HS512";
201
225
  type JWK_AES_GCM = "A128GCM" | "A192GCM" | "A256GCM";
202
226
  type JWK_PBES2 = "PBES2-HS256+A128KW" | "PBES2-HS384+A192KW" | "PBES2-HS512+A256KW";
203
227
  type JWK_ECDH_ES = "ECDH-ES" | "ECDH-ES+A128KW" | "ECDH-ES+A192KW" | "ECDH-ES+A256KW";
204
- type JWK_ED = "Ed25519" | "EdDSA";
228
+ type JWK_OKP_SIGN = "Ed25519" | "EdDSA";
205
229
  type JWK_Symmetric_Algorithm = JWK_HMAC | JWK_AES_KW | JWK_AES_GCM;
206
- type JWK_Asymmetric_Algorithm = JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_RSA_ENC | JWK_ED;
230
+ type JWK_Asymmetric_Algorithm = JWK_RSA_SIGN | JWK_RSA_PSS | JWK_ECDSA | JWK_RSA_ENC | JWK_OKP_SIGN;
207
231
  type JWKAlgorithm = JWK_Symmetric_Algorithm | JWK_Asymmetric_Algorithm | JWK_AES_CBC_HMAC | JWK_PBES2 | JWK_ECDH_ES | ("none" | "dir");
208
- type JWKPEMAlgorithm = JWK_RSA_PSS | JWK_RSA_SIGN | JWK_RSA_ENC | JWK_ECDSA | JWK_ECDH_ES | JWK_ED;
232
+ type JWKPEMAlgorithm = JWK_RSA_PSS | JWK_RSA_SIGN | JWK_RSA_ENC | JWK_ECDSA | JWK_ECDH_ES | JWK_OKP_SIGN;
209
233
 
210
234
  /** Header Parameters common to JWE and JWS */
211
235
  interface JoseHeaderParameters {
@@ -220,7 +244,7 @@ interface JoseHeaderParameters {
220
244
  /** "jku" (JWK Set URL) Header Parameter */
221
245
  jku?: string;
222
246
  /** "jwk" (JSON Web Key) Header Parameter */
223
- jwk?: JWK_RSA_Public;
247
+ jwk?: JWK_Public;
224
248
  /** "typ" (Type) Header Parameter */
225
249
  typ?: string;
226
250
  /** "cty" (Content Type) Header Parameter */
@@ -282,4 +306,4 @@ interface JWTClaimValidationOptions {
282
306
  critical?: string[];
283
307
  }
284
308
 
285
- export type { JWK_Symmetric_Algorithm as A, JWK_Asymmetric_Algorithm as B, CompositeKey as C, DeriveKeyOptions as D, JWKAlgorithm as E, JWKPEMAlgorithm as F, GenerateKeyAlgorithm as G, JoseHeaderParameters as H, JWTClaims as I, JWKParameters as J, KeyManagementAlgorithm as K, JWT as L, JWTClaimValidationOptions as M, UnwrapKeyOptions as U, WrapKeyOptions as W, GenerateKeyOptions as a, GenerateKeyReturn as b, DeriveKeyReturn as c, ContentEncryptionAlgorithm as d, WrapKeyResult as e, JWK_EC_Public as f, JWK_EC_Private as g, JWK_EC as h, JWK_RSA_Public as i, JWK_RSA_Private as j, JWK_RSA as k, JWK_oct as l, JWK as m, JWKSet as n, JWK_HMAC as o, JWK_RSA_SIGN as p, JWK_RSA_PSS as q, JWK_ECDSA as r, JWK_RSA_ENC as s, JWK_AES_KW as t, JWK_AES_GCM_KW as u, JWK_AES_CBC_HMAC as v, JWK_AES_GCM as w, JWK_PBES2 as x, JWK_ECDH_ES as y, JWK_ED as z };
309
+ export type { JWK_AES_CBC_HMAC as A, JWK_AES_GCM as B, CompositeKey as C, DeriveKeyOptions as D, JWK_PBES2 as E, JWK_ECDH_ES as F, GenerateKeyAlgorithm as G, JWK_OKP_SIGN as H, JWK_Symmetric_Algorithm as I, JWKParameters as J, KeyManagementAlgorithm as K, JWK_Asymmetric_Algorithm as L, JWKAlgorithm as M, JWKPEMAlgorithm as N, JoseHeaderParameters as O, JWTClaims as P, JWT as Q, JWTClaimValidationOptions as R, UnwrapKeyOptions as U, WrapKeyOptions as W, GenerateKeyOptions as a, GenerateKeyReturn as b, DeriveKeyReturn as c, ContentEncryptionAlgorithm as d, WrapKeyResult as e, JWK_EC_Public as f, JWK_EC_Private as g, JWK_EC as h, JWK_RSA_Public as i, JWK_RSA_Private as j, JWK_RSA as k, JWK_OKP_Public as l, JWK_OKP_Private as m, JWK_OKP as n, JWK_oct as o, JWK_Public as p, JWK_Private as q, JWK as r, JWKSet as s, JWK_HMAC as t, JWK_RSA_SIGN as u, JWK_RSA_PSS as v, JWK_ECDSA as w, JWK_RSA_ENC as x, JWK_AES_KW as y, JWK_AES_GCM_KW as z };
@@ -1,4 +1,4 @@
1
- import { o as JWK_HMAC, p as JWK_RSA_SIGN, q as JWK_RSA_PSS, r as JWK_ECDSA, H as JoseHeaderParameters, m as JWK, n as JWKSet, M as JWTClaimValidationOptions, I as JWTClaims, L as JWT } from './unjwt.CN1C8TlR.mjs';
1
+ import { t as JWK_HMAC, u as JWK_RSA_SIGN, v as JWK_RSA_PSS, w as JWK_ECDSA, O as JoseHeaderParameters, r as JWK, s as JWKSet, R as JWTClaimValidationOptions, P as JWTClaims, Q as JWT } from './unjwt.BsgIQHVD.mjs';
2
2
 
3
3
  type JWS_SIGN_EXTRA = "Ed25519" | "EdDSA";
4
4
  /** JWS Signing Algorithm Identifier. */
@@ -27,7 +27,7 @@ interface JWSSignOptions {
27
27
  alg?: JWSAlgorithm;
28
28
  /**
29
29
  * Additional protected header parameters. `alg` is automatically included.
30
- * `typ` defaults to "at+jwt" if not provided.
30
+ * `typ` defaults to "JWT" if not provided and payload is an object.
31
31
  */
32
32
  protectedHeader?: JWSHeaderParameters;
33
33
  /** Date to use when comparing NumericDate claims, defaults to `new Date()`. */
@@ -50,6 +50,8 @@ interface JWSVerifyOptions extends JWTClaimValidationOptions {
50
50
  algorithms?: JWSAlgorithm[];
51
51
  /** If true, forces the payload to be returned as a Uint8Array, otherwise type is inferred. */
52
52
  forceUint8Array?: boolean;
53
+ /** Unless false, will parse payload as JWT and validate claims if applicable (typ "JWT"). */
54
+ validateJWT?: boolean;
53
55
  }
54
56
  /**
55
57
  * Key lookup function for JWS verification.
@@ -1,4 +1,4 @@
1
- import { G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, x as JWK_PBES2, D as DeriveKeyOptions, c as DeriveKeyReturn, l as JWK_oct, m as JWK, K as KeyManagementAlgorithm, W as WrapKeyOptions, e as WrapKeyResult, U as UnwrapKeyOptions, F as JWKPEMAlgorithm, n as JWKSet, H as JoseHeaderParameters, C as CompositeKey, d as ContentEncryptionAlgorithm, E as JWKAlgorithm, J as JWKParameters, v as JWK_AES_CBC_HMAC, w as JWK_AES_GCM, u as JWK_AES_GCM_KW, t as JWK_AES_KW, B as JWK_Asymmetric_Algorithm, h as JWK_EC, y as JWK_ECDH_ES, r as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, z as JWK_ED, o as JWK_HMAC, k as JWK_RSA, s as JWK_RSA_ENC, q as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, p as JWK_RSA_SIGN, A as JWK_Symmetric_Algorithm, L as JWT, M as JWTClaimValidationOptions, I as JWTClaims } from './unjwt.CN1C8TlR.mjs';
1
+ import { G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, E as JWK_PBES2, D as DeriveKeyOptions, c as DeriveKeyReturn, o as JWK_oct, r as JWK, K as KeyManagementAlgorithm, W as WrapKeyOptions, e as WrapKeyResult, U as UnwrapKeyOptions, N as JWKPEMAlgorithm, s as JWKSet, O as JoseHeaderParameters, C as CompositeKey, d as ContentEncryptionAlgorithm, M as JWKAlgorithm, J as JWKParameters, A as JWK_AES_CBC_HMAC, B as JWK_AES_GCM, z as JWK_AES_GCM_KW, y as JWK_AES_KW, L as JWK_Asymmetric_Algorithm, h as JWK_EC, F as JWK_ECDH_ES, w as JWK_ECDSA, g as JWK_EC_Private, f as JWK_EC_Public, t as JWK_HMAC, n as JWK_OKP, m as JWK_OKP_Private, l as JWK_OKP_Public, H as JWK_OKP_SIGN, q as JWK_Private, p as JWK_Public, k as JWK_RSA, x as JWK_RSA_ENC, v as JWK_RSA_PSS, j as JWK_RSA_Private, i as JWK_RSA_Public, u as JWK_RSA_SIGN, I as JWK_Symmetric_Algorithm, Q as JWT, R as JWTClaimValidationOptions, P as JWTClaims } from './unjwt.BsgIQHVD.mjs';
2
2
 
3
3
  /**
4
4
  * This is a fork of Jose library's utility functions.
@@ -142,9 +142,14 @@ declare const jwk_JWK_ECDH_ES: typeof JWK_ECDH_ES;
142
142
  declare const jwk_JWK_ECDSA: typeof JWK_ECDSA;
143
143
  declare const jwk_JWK_EC_Private: typeof JWK_EC_Private;
144
144
  declare const jwk_JWK_EC_Public: typeof JWK_EC_Public;
145
- declare const jwk_JWK_ED: typeof JWK_ED;
146
145
  declare const jwk_JWK_HMAC: typeof JWK_HMAC;
146
+ declare const jwk_JWK_OKP: typeof JWK_OKP;
147
+ declare const jwk_JWK_OKP_Private: typeof JWK_OKP_Private;
148
+ declare const jwk_JWK_OKP_Public: typeof JWK_OKP_Public;
149
+ declare const jwk_JWK_OKP_SIGN: typeof JWK_OKP_SIGN;
147
150
  declare const jwk_JWK_PBES2: typeof JWK_PBES2;
151
+ declare const jwk_JWK_Private: typeof JWK_Private;
152
+ declare const jwk_JWK_Public: typeof JWK_Public;
148
153
  declare const jwk_JWK_RSA: typeof JWK_RSA;
149
154
  declare const jwk_JWK_RSA_ENC: typeof JWK_RSA_ENC;
150
155
  declare const jwk_JWK_RSA_PSS: typeof JWK_RSA_PSS;
@@ -194,9 +199,14 @@ declare namespace jwk {
194
199
  jwk_JWK_ECDSA as JWK_ECDSA,
195
200
  jwk_JWK_EC_Private as JWK_EC_Private,
196
201
  jwk_JWK_EC_Public as JWK_EC_Public,
197
- jwk_JWK_ED as JWK_ED,
198
202
  jwk_JWK_HMAC as JWK_HMAC,
203
+ jwk_JWK_OKP as JWK_OKP,
204
+ jwk_JWK_OKP_Private as JWK_OKP_Private,
205
+ jwk_JWK_OKP_Public as JWK_OKP_Public,
206
+ jwk_JWK_OKP_SIGN as JWK_OKP_SIGN,
199
207
  jwk_JWK_PBES2 as JWK_PBES2,
208
+ jwk_JWK_Private as JWK_Private,
209
+ jwk_JWK_Public as JWK_Public,
200
210
  jwk_JWK_RSA as JWK_RSA,
201
211
  jwk_JWK_RSA_ENC as JWK_RSA_ENC,
202
212
  jwk_JWK_RSA_PSS as JWK_RSA_PSS,
@@ -1,22 +1,21 @@
1
1
  function applyTypCtyDefaults(header, payload) {
2
2
  const isObjectPayload = typeof payload === "object" && payload !== null && !(payload instanceof Uint8Array);
3
3
  if (header.typ === void 0 && isObjectPayload) {
4
- header.typ = "at+jwt";
4
+ header.typ = "JWT";
5
5
  }
6
- if ((header.typ === "at+jwt" || header.typ === "JWT") && isObjectPayload) {
6
+ if (header.typ?.toLowerCase().includes("jwt") && isObjectPayload) {
7
7
  header.cty ||= "application/json";
8
8
  }
9
9
  return header;
10
10
  }
11
- function isJsonContent(header) {
11
+ function isJWTContent(header) {
12
12
  if (!header) return false;
13
- if (header.typ === "at+jwt") return true;
14
- if (header.typ === "JWT") return true;
13
+ if (header.typ?.toLowerCase().includes("jwt")) return true;
15
14
  const cty = header.cty?.toLowerCase();
16
15
  return cty === "json" || cty === "application/json" || !!cty && cty.endsWith("+json");
17
16
  }
18
- function decodeMaybeJsonString(decodedString, header) {
19
- if (isJsonContent(header)) {
17
+ function decodeMaybeJWTString(decodedString, header) {
18
+ if (isJWTContent(header)) {
20
19
  const looksLikeJson = decodedString.startsWith("{") && decodedString.endsWith("}") || decodedString.startsWith("[") && decodedString.endsWith("]");
21
20
  if (looksLikeJson) {
22
21
  try {
@@ -31,12 +30,12 @@ function decodeMaybeJsonString(decodedString, header) {
31
30
  function decodePayloadFromBytes(bytes, header, forceUint8Array) {
32
31
  if (forceUint8Array) return bytes;
33
32
  const decodedString = textDecoder.decode(bytes);
34
- return decodeMaybeJsonString(decodedString, header);
33
+ return decodeMaybeJWTString(decodedString, header);
35
34
  }
36
35
  function decodePayloadFromB64UrlSegment(payloadEncoded, header, forceUint8Array) {
37
36
  if (forceUint8Array) return base64UrlDecode(payloadEncoded, false);
38
37
  const decodedString = base64UrlDecode(payloadEncoded);
39
- return decodeMaybeJsonString(decodedString, header);
38
+ return decodeMaybeJWTString(decodedString, header);
40
39
  }
41
40
  function getPlaintextBytes(payload) {
42
41
  if (payload instanceof Uint8Array) return payload;
@@ -48,13 +47,13 @@ function getPlaintextBytes(payload) {
48
47
  );
49
48
  }
50
49
  function computeJwtTimeClaims(payload, headerTyp, expiresIn, currentDate = /* @__PURE__ */ new Date()) {
51
- if (expiresIn === void 0 || headerTyp !== "at+jwt" && headerTyp !== "JWT" || !(payload && typeof payload === "object") || payload instanceof Uint8Array || payload.exp) {
50
+ if (expiresIn === void 0 || !headerTyp?.toLowerCase().includes("jwt") || !(payload && typeof payload === "object") || payload instanceof Uint8Array || payload.exp) {
52
51
  return void 0;
53
52
  }
54
53
  const now = Math.round(currentDate.getTime() / 1e3);
55
54
  const claims = { ...payload };
56
55
  claims.iat ||= now;
57
- claims.exp = now + expiresIn;
56
+ claims.exp = claims.iat + expiresIn;
58
57
  return claims;
59
58
  }
60
59
  function validateJwtClaims(jwtClaims, options = {}) {
@@ -316,6 +315,41 @@ function isCryptoKey(key) {
316
315
  return key?.[Symbol.toStringTag] === "CryptoKey";
317
316
  }
318
317
  const isCryptoKeyPair = (key) => key && typeof key === "object" && isCryptoKey(key.publicKey) && isCryptoKey(key.privateKey);
318
+ function isSymmetricJWK(key) {
319
+ return isJWK(key) && key.kty === "oct" && typeof key.k === "string";
320
+ }
321
+ function isAsymmetricJWK(key) {
322
+ return !isSymmetricJWK(key);
323
+ }
324
+ function isPrivateJWK(key) {
325
+ if (!isJWK(key)) return false;
326
+ if (key.kty === "EC") {
327
+ return typeof key.d === "string";
328
+ }
329
+ if (key.kty === "OKP") {
330
+ return typeof key.d === "string";
331
+ }
332
+ if (key.kty === "RSA") {
333
+ return typeof key.d === "string";
334
+ }
335
+ return false;
336
+ }
337
+ function isPublicJWK(key) {
338
+ if (!isJWK(key)) return false;
339
+ if (key.kty === "EC") {
340
+ const ec = key;
341
+ return typeof ec.x === "string" && typeof ec.y === "string" && typeof ec.d !== "string";
342
+ }
343
+ if (key.kty === "OKP") {
344
+ const okp = key;
345
+ return typeof okp.x === "string" && typeof okp.d !== "string";
346
+ }
347
+ if (key.kty === "RSA") {
348
+ const rsa = key;
349
+ return typeof rsa.n === "string" && typeof rsa.e === "string" && typeof rsa.d !== "string";
350
+ }
351
+ return false;
352
+ }
319
353
 
320
354
  const index = {
321
355
  __proto__: null,
@@ -327,15 +361,19 @@ const index = {
327
361
  base64UrlEncode: base64UrlEncode,
328
362
  computeJwtTimeClaims: computeJwtTimeClaims,
329
363
  concatUint8Arrays: concatUint8Arrays,
330
- decodeMaybeJsonString: decodeMaybeJsonString,
364
+ decodeMaybeJWTString: decodeMaybeJWTString,
331
365
  decodePayloadFromB64UrlSegment: decodePayloadFromB64UrlSegment,
332
366
  decodePayloadFromBytes: decodePayloadFromBytes,
333
367
  getPlaintextBytes: getPlaintextBytes,
368
+ isAsymmetricJWK: isAsymmetricJWK,
334
369
  isCryptoKey: isCryptoKey,
335
370
  isCryptoKeyPair: isCryptoKeyPair,
336
371
  isJWK: isJWK,
337
372
  isJWKSet: isJWKSet,
338
- isJsonContent: isJsonContent,
373
+ isJWTContent: isJWTContent,
374
+ isPrivateJWK: isPrivateJWK,
375
+ isPublicJWK: isPublicJWK,
376
+ isSymmetricJWK: isSymmetricJWK,
339
377
  maybeArray: maybeArray,
340
378
  randomBytes: randomBytes,
341
379
  sanitizeObject: sanitizeObject,
@@ -346,4 +384,4 @@ const index = {
346
384
  validateJwtClaims: validateJwtClaims
347
385
  };
348
386
 
349
- export { isJWK as a, applyTypCtyDefaults as b, computeJwtTimeClaims as c, base64UrlEncode as d, textDecoder as e, base64UrlDecode as f, isJWKSet as g, decodePayloadFromB64UrlSegment as h, index as i, validateJwtClaims as j, concatUint8Arrays as k, assertCryptoKey as l, isCryptoKey as m, getPlaintextBytes as n, decodePayloadFromBytes as o, validateCriticalHeadersJWE as p, base64Decode as q, randomBytes as r, sanitizeObject as s, textEncoder as t, base64Encode as u, validateCriticalHeadersJWS as v, maybeArray as w, isCryptoKeyPair as x, isJsonContent as y, decodeMaybeJsonString as z };
387
+ export { isPrivateJWK as A, isPublicJWK as B, isJWTContent as C, decodeMaybeJWTString as D, isJWK as a, applyTypCtyDefaults as b, computeJwtTimeClaims as c, base64UrlEncode as d, textDecoder as e, base64UrlDecode as f, isJWKSet as g, decodePayloadFromB64UrlSegment as h, index as i, validateJwtClaims as j, concatUint8Arrays as k, assertCryptoKey as l, isCryptoKey as m, getPlaintextBytes as n, decodePayloadFromBytes as o, validateCriticalHeadersJWE as p, base64Decode as q, randomBytes as r, sanitizeObject as s, textEncoder as t, base64Encode as u, validateCriticalHeadersJWS as v, maybeArray as w, isCryptoKeyPair as x, isSymmetricJWK as y, isAsymmetricJWK as z };
@@ -1,23 +1,23 @@
1
- import { I as JWTClaims, M as JWTClaimValidationOptions, m as JWK, n as JWKSet } from './unjwt.CN1C8TlR.mjs';
1
+ import { P as JWTClaims, R as JWTClaimValidationOptions, r as JWK, s as JWKSet, o as JWK_oct, q as JWK_Private, p as JWK_Public } from './unjwt.BsgIQHVD.mjs';
2
2
 
3
3
  /**
4
4
  * Apply default typ/cty semantics shared by JWS & JWE.
5
- * - If typ is undefined and payload is a non-Uint8Array object, set typ="at+jwt".
6
- * - If typ is "at+jwt" and payload is a non-Uint8Array object, ensure cty defaults to "application/json".
5
+ * - If typ is undefined and payload is a non-Uint8Array object, set typ="JWT".
6
+ * - If typ is "JWT" and payload is a non-Uint8Array object, ensure cty defaults to "application/json".
7
7
  */
8
8
  declare function applyTypCtyDefaults<T extends {
9
9
  typ?: string;
10
10
  cty?: string;
11
11
  }>(header: T, payload: unknown): T;
12
12
  /** Returns true when headers indicate JSON content. */
13
- declare function isJsonContent(header: {
13
+ declare function isJWTContent(header: {
14
14
  typ?: string;
15
15
  cty?: string;
16
16
  } | undefined): boolean;
17
17
  /**
18
18
  * Given a decoded string and JOSE headers, return an object if JSON, otherwise the string.
19
19
  */
20
- declare function decodeMaybeJsonString<T = unknown>(decodedString: string, header: {
20
+ declare function decodeMaybeJWTString<T = unknown>(decodedString: string, header: {
21
21
  typ?: string;
22
22
  cty?: string;
23
23
  } | undefined): T | string;
@@ -84,6 +84,20 @@ declare function isJWKSet(key: any): key is JWKSet;
84
84
  declare function assertCryptoKey(key: unknown): asserts key is CryptoKey;
85
85
  declare function isCryptoKey(key: unknown): key is CryptoKey;
86
86
  declare const isCryptoKeyPair: (key: any) => key is CryptoKeyPair;
87
+ /** Returns true if the JWK is a symmetric (oct) key. */
88
+ declare function isSymmetricJWK(key: unknown): key is JWK_oct;
89
+ /** Returns true if the JWK is an asymmetric (RSA, EC, OKP) key. */
90
+ declare function isAsymmetricJWK(key: unknown): key is JWK_oct;
91
+ /**
92
+ * Type guard that checks if the provided JWK contains private key material.
93
+ * It relies purely on the presence of private components (e.g. "d"), not on the alg value.
94
+ */
95
+ declare function isPrivateJWK(key: unknown): key is JWK_Private;
96
+ /**
97
+ * Type guard that checks if the provided JWK is a public (asymmetric) key.
98
+ * This checks for the presence of public components and absence of private material.
99
+ */
100
+ declare function isPublicJWK(key: unknown): key is JWK_Public;
87
101
 
88
102
  declare const index_applyTypCtyDefaults: typeof applyTypCtyDefaults;
89
103
  declare const index_assertCryptoKey: typeof assertCryptoKey;
@@ -93,15 +107,19 @@ declare const index_base64UrlDecode: typeof base64UrlDecode;
93
107
  declare const index_base64UrlEncode: typeof base64UrlEncode;
94
108
  declare const index_computeJwtTimeClaims: typeof computeJwtTimeClaims;
95
109
  declare const index_concatUint8Arrays: typeof concatUint8Arrays;
96
- declare const index_decodeMaybeJsonString: typeof decodeMaybeJsonString;
110
+ declare const index_decodeMaybeJWTString: typeof decodeMaybeJWTString;
97
111
  declare const index_decodePayloadFromB64UrlSegment: typeof decodePayloadFromB64UrlSegment;
98
112
  declare const index_decodePayloadFromBytes: typeof decodePayloadFromBytes;
99
113
  declare const index_getPlaintextBytes: typeof getPlaintextBytes;
114
+ declare const index_isAsymmetricJWK: typeof isAsymmetricJWK;
100
115
  declare const index_isCryptoKey: typeof isCryptoKey;
101
116
  declare const index_isCryptoKeyPair: typeof isCryptoKeyPair;
102
117
  declare const index_isJWK: typeof isJWK;
103
118
  declare const index_isJWKSet: typeof isJWKSet;
104
- declare const index_isJsonContent: typeof isJsonContent;
119
+ declare const index_isJWTContent: typeof isJWTContent;
120
+ declare const index_isPrivateJWK: typeof isPrivateJWK;
121
+ declare const index_isPublicJWK: typeof isPublicJWK;
122
+ declare const index_isSymmetricJWK: typeof isSymmetricJWK;
105
123
  declare const index_maybeArray: typeof maybeArray;
106
124
  declare const index_randomBytes: typeof randomBytes;
107
125
  declare const index_sanitizeObject: typeof sanitizeObject;
@@ -120,15 +138,19 @@ declare namespace index {
120
138
  index_base64UrlEncode as base64UrlEncode,
121
139
  index_computeJwtTimeClaims as computeJwtTimeClaims,
122
140
  index_concatUint8Arrays as concatUint8Arrays,
123
- index_decodeMaybeJsonString as decodeMaybeJsonString,
141
+ index_decodeMaybeJWTString as decodeMaybeJWTString,
124
142
  index_decodePayloadFromB64UrlSegment as decodePayloadFromB64UrlSegment,
125
143
  index_decodePayloadFromBytes as decodePayloadFromBytes,
126
144
  index_getPlaintextBytes as getPlaintextBytes,
145
+ index_isAsymmetricJWK as isAsymmetricJWK,
127
146
  index_isCryptoKey as isCryptoKey,
128
147
  index_isCryptoKeyPair as isCryptoKeyPair,
129
148
  index_isJWK as isJWK,
130
149
  index_isJWKSet as isJWKSet,
131
- index_isJsonContent as isJsonContent,
150
+ index_isJWTContent as isJWTContent,
151
+ index_isPrivateJWK as isPrivateJWK,
152
+ index_isPublicJWK as isPublicJWK,
153
+ index_isSymmetricJWK as isSymmetricJWK,
132
154
  index_maybeArray as maybeArray,
133
155
  index_randomBytes as randomBytes,
134
156
  index_sanitizeObject as sanitizeObject,
@@ -140,4 +162,4 @@ declare namespace index {
140
162
  };
141
163
  }
142
164
 
143
- export { textDecoder as a, base64Encode as b, base64UrlEncode as c, base64Decode as d, base64UrlDecode as e, concatUint8Arrays as f, isJWK as g, isJWKSet as h, index as i, assertCryptoKey as j, isCryptoKey as k, isCryptoKeyPair as l, maybeArray as m, applyTypCtyDefaults as n, isJsonContent as o, decodeMaybeJsonString as p, decodePayloadFromBytes as q, randomBytes as r, decodePayloadFromB64UrlSegment as s, textEncoder as t, getPlaintextBytes as u, computeJwtTimeClaims as v, validateJwtClaims as w, validateCriticalHeadersJWS as x, validateCriticalHeadersJWE as y, sanitizeObject as z };
165
+ export { validateJwtClaims as A, validateCriticalHeadersJWS as B, validateCriticalHeadersJWE as C, sanitizeObject as D, textDecoder as a, base64Encode as b, base64UrlEncode as c, base64Decode as d, base64UrlDecode as e, concatUint8Arrays as f, isJWK as g, isJWKSet as h, index as i, assertCryptoKey as j, isCryptoKey as k, isCryptoKeyPair as l, maybeArray as m, isSymmetricJWK as n, isAsymmetricJWK as o, isPrivateJWK as p, isPublicJWK as q, randomBytes as r, applyTypCtyDefaults as s, textEncoder as t, isJWTContent as u, decodeMaybeJWTString as v, decodePayloadFromBytes as w, decodePayloadFromB64UrlSegment as x, getPlaintextBytes as y, computeJwtTimeClaims as z };
@@ -1,4 +1,4 @@
1
- import { m as isCryptoKey, k as concatUint8Arrays, d as base64UrlEncode, q as base64Decode, u as base64Encode, s as sanitizeObject, t as textEncoder, r as randomBytes, a as isJWK, f as base64UrlDecode, g as isJWKSet } from './unjwt.mBSKL6g4.mjs';
1
+ import { m as isCryptoKey, k as concatUint8Arrays, d as base64UrlEncode, q as base64Decode, u as base64Encode, s as sanitizeObject, t as textEncoder, r as randomBytes, a as isJWK, f as base64UrlDecode, g as isJWKSet } from './unjwt.CRHfsi7v.mjs';
2
2
 
3
3
  /**
4
4
  * This is a fork of Jose library's utility functions.
@@ -830,7 +830,10 @@ function subtleMapping(jwk) {
830
830
  case "ECDH-ES+A128KW":
831
831
  case "ECDH-ES+A192KW":
832
832
  case "ECDH-ES+A256KW": {
833
- algorithm = { name: "ECDH", namedCurve: jwk.crv };
833
+ algorithm = {
834
+ name: "ECDH",
835
+ namedCurve: jwk.crv
836
+ };
834
837
  keyUsages = jwk.d ? ["deriveBits"] : [];
835
838
  break;
836
839
  }
@@ -1024,7 +1027,7 @@ async function _unwrap(alg, key, encryptedKey) {
1024
1027
  }
1025
1028
 
1026
1029
  async function generateKey(alg, options = {}) {
1027
- const exportToJWK = options.toJWK === true;
1030
+ const exportToJWK = options.toJWK !== void 0 && options.toJWK !== false;
1028
1031
  const defaultExtractable = options.extractable !== false;
1029
1032
  if (alg === "A128CBC-HS256" || alg === "A192CBC-HS384" || alg === "A256CBC-HS512") {
1030
1033
  const keyLength = bitLengthCEK(alg);
@@ -1041,12 +1044,19 @@ async function generateKey(alg, options = {}) {
1041
1044
  keyUsages
1042
1045
  );
1043
1046
  if (exportToJWK) {
1047
+ const {
1048
+ alg: _a,
1049
+ kty: _k,
1050
+ key_ops: _ko,
1051
+ ext: _e,
1052
+ ...additionalKeyParams
1053
+ } = typeof options.toJWK === "object" ? options.toJWK : {};
1044
1054
  if (key instanceof CryptoKey) {
1045
- return exportKey(key, { alg });
1055
+ return exportKey(key, { ...additionalKeyParams, alg });
1046
1056
  } else {
1047
1057
  const [publicKey, privateKey] = await Promise.all([
1048
- exportKey(key.publicKey, { alg }),
1049
- exportKey(key.privateKey, { alg })
1058
+ exportKey(key.publicKey, { ...additionalKeyParams, alg }),
1059
+ exportKey(key.privateKey, { ...additionalKeyParams, alg })
1050
1060
  ]);
1051
1061
  return { privateKey, publicKey };
1052
1062
  }
@@ -1118,7 +1128,7 @@ async function importKey(key, alg) {
1118
1128
  async function exportKey(key, jwk) {
1119
1129
  const exportedJwk = await keyToJWK(key);
1120
1130
  if (jwk) {
1121
- return { ...exportedJwk, ...sanitizeObject(jwk) };
1131
+ return { ...sanitizeObject(jwk), ...exportedJwk };
1122
1132
  }
1123
1133
  return exportedJwk;
1124
1134
  }
@@ -1,6 +1,6 @@
1
- import { i as importKey, g as getJWKFromSet } from './unjwt.CX8WRx20.mjs';
2
- import { s as sanitizeObject, a as isJWK, b as applyTypCtyDefaults, c as computeJwtTimeClaims, d as base64UrlEncode, t as textEncoder, e as textDecoder, f as base64UrlDecode, g as isJWKSet, h as decodePayloadFromB64UrlSegment, v as validateCriticalHeadersJWS, j as validateJwtClaims } from './unjwt.mBSKL6g4.mjs';
3
- import { s as sign$1, v as verify$1 } from './unjwt.DFAKwqBz.mjs';
1
+ import { i as importKey, g as getJWKFromSet } from './unjwt.D1MC63Yb.mjs';
2
+ import { s as sanitizeObject, a as isJWK, b as applyTypCtyDefaults, c as computeJwtTimeClaims, d as base64UrlEncode, t as textEncoder, e as textDecoder, f as base64UrlDecode, g as isJWKSet, h as decodePayloadFromB64UrlSegment, v as validateCriticalHeadersJWS, j as validateJwtClaims } from './unjwt.CRHfsi7v.mjs';
3
+ import { s as sign$1, v as verify$1 } from './unjwt.BpusjhPR.mjs';
4
4
 
5
5
  async function sign(payload, key, options = {}) {
6
6
  const { protectedHeader: additionalProtectedHeader } = options;
@@ -130,7 +130,7 @@ async function verify(jws, key, options = {}) {
130
130
  ...options.critical || [],
131
131
  ...options.requiredHeaders || []
132
132
  ]);
133
- if (payload && typeof payload === "object" && (protectedHeader.typ === "at+jwt" || protectedHeader.typ === "JWT") && !options.forceUint8Array) {
133
+ if (payload && typeof payload === "object" && options.validateJWT !== false && (options.validateJWT === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) {
134
134
  validateJwtClaims(payload, options);
135
135
  }
136
136
  return {
package/dist/utils.d.mts CHANGED
@@ -1,2 +1,2 @@
1
- import './shared/unjwt.CN1C8TlR.mjs';
2
- export { n as applyTypCtyDefaults, j as assertCryptoKey, d as base64Decode, b as base64Encode, e as base64UrlDecode, c as base64UrlEncode, v as computeJwtTimeClaims, f as concatUint8Arrays, p as decodeMaybeJsonString, s as decodePayloadFromB64UrlSegment, q as decodePayloadFromBytes, u as getPlaintextBytes, k as isCryptoKey, l as isCryptoKeyPair, g as isJWK, h as isJWKSet, o as isJsonContent, m as maybeArray, r as randomBytes, z as sanitizeObject, a as textDecoder, t as textEncoder, y as validateCriticalHeadersJWE, x as validateCriticalHeadersJWS, w as validateJwtClaims } from './shared/unjwt.Cay_cOOI.mjs';
1
+ import './shared/unjwt.BsgIQHVD.mjs';
2
+ export { s as applyTypCtyDefaults, j as assertCryptoKey, d as base64Decode, b as base64Encode, e as base64UrlDecode, c as base64UrlEncode, z as computeJwtTimeClaims, f as concatUint8Arrays, v as decodeMaybeJWTString, x as decodePayloadFromB64UrlSegment, w as decodePayloadFromBytes, y as getPlaintextBytes, o as isAsymmetricJWK, k as isCryptoKey, l as isCryptoKeyPair, g as isJWK, h as isJWKSet, u as isJWTContent, p as isPrivateJWK, q as isPublicJWK, n as isSymmetricJWK, m as maybeArray, r as randomBytes, D as sanitizeObject, a as textDecoder, t as textEncoder, C as validateCriticalHeadersJWE, B as validateCriticalHeadersJWS, A as validateJwtClaims } from './shared/unjwt.CfL4cArP.mjs';
package/dist/utils.mjs CHANGED
@@ -1 +1 @@
1
- export { b as applyTypCtyDefaults, l as assertCryptoKey, q as base64Decode, u as base64Encode, f as base64UrlDecode, d as base64UrlEncode, c as computeJwtTimeClaims, k as concatUint8Arrays, z as decodeMaybeJsonString, h as decodePayloadFromB64UrlSegment, o as decodePayloadFromBytes, n as getPlaintextBytes, m as isCryptoKey, x as isCryptoKeyPair, a as isJWK, g as isJWKSet, y as isJsonContent, w as maybeArray, r as randomBytes, s as sanitizeObject, e as textDecoder, t as textEncoder, p as validateCriticalHeadersJWE, v as validateCriticalHeadersJWS, j as validateJwtClaims } from './shared/unjwt.mBSKL6g4.mjs';
1
+ export { b as applyTypCtyDefaults, l as assertCryptoKey, q as base64Decode, u as base64Encode, f as base64UrlDecode, d as base64UrlEncode, c as computeJwtTimeClaims, k as concatUint8Arrays, D as decodeMaybeJWTString, h as decodePayloadFromB64UrlSegment, o as decodePayloadFromBytes, n as getPlaintextBytes, z as isAsymmetricJWK, m as isCryptoKey, x as isCryptoKeyPair, a as isJWK, g as isJWKSet, C as isJWTContent, A as isPrivateJWK, B as isPublicJWK, y as isSymmetricJWK, w as maybeArray, r as randomBytes, s as sanitizeObject, e as textDecoder, t as textEncoder, p as validateCriticalHeadersJWE, v as validateCriticalHeadersJWS, j as validateJwtClaims } from './shared/unjwt.CRHfsi7v.mjs';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.5.13",
3
+ "version": "0.5.15",
4
4
  "description": "Low-level JWT utilities",
5
5
  "repository": "sandros94/unjwt",
6
6
  "license": "MIT",
@@ -47,20 +47,21 @@
47
47
  "release": "pnpm test && pnpm prepack && changelogen --release --push --publish",
48
48
  "test": "pnpm lint && pnpm test:types && vitest run --slowTestThreshold=500",
49
49
  "test:coverage": "pnpm lint && pnpm test:types && vitest run --coverage --slowTestThreshold=500",
50
- "test:types": "tsc --noEmit --skipLibCheck"
50
+ "test:types": "tsc --noEmit --skipLibCheck",
51
+ "bench": "vitest bench --run"
51
52
  },
52
53
  "devDependencies": {
53
- "@types/node": "^22.13.14",
54
+ "@types/node": "^22",
54
55
  "@vitest/coverage-v8": "^3.2.4",
55
- "automd": "^0.4.0",
56
+ "automd": "^0.4.2",
56
57
  "changelogen": "^0.6.2",
57
- "eslint": "^9.34.0",
58
+ "eslint": "^9.36.0",
58
59
  "eslint-config-unjs": "^0.5.0",
59
- "jose": "^6.0.13",
60
+ "jose": "^6.1.0",
60
61
  "prettier": "^3.6.2",
61
62
  "typescript": "^5.9.2",
62
63
  "unbuild": "^3.6.1",
63
64
  "vitest": "^3.2.4"
64
65
  },
65
- "packageManager": "pnpm@10.15.0"
66
+ "packageManager": "pnpm@10.17.1"
66
67
  }