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,31 @@
1
+ import { L as JWKSet, N as JWKLookupFunction, d as JWSHeaderParameters, f as JWSProtectedHeader, g as JOSEPayload, h as JWSVerifyResult, m as JWSVerifyOptions, nt as JWK_Private, p as JWSSignOptions, rt as JWK_Public, u as JWSAlgorithm, ut as JWK_Symmetric } from "../_chunks/index.mjs";
2
+ import { i as isJWTError, n as JWTErrorCauseMap, r as JWTErrorCode, t as JWTError } from "../_chunks/error.mjs";
3
+
4
+ //#region src/core/jws.d.ts
5
+ /**
6
+ * Creates a JWS (JSON Web Signature) in Compact Serialization format.
7
+ *
8
+ * @param payload The payload to sign. Can be a string, Uint8Array, or an object (which will be JSON stringified).
9
+ * @param key The signing key (CryptoKey, JWK, or raw symmetric key as Uint8Array).
10
+ * @param options Signing options, including the algorithm (`alg`) and protected header parameters.
11
+ * @returns A Promise resolving to the JWS Compact Serialization string.
12
+ */
13
+ declare function sign(payload: JOSEPayload, key: JWK_Symmetric | JWK_Private, options?: JWSSignOptions): Promise<string>;
14
+ declare function sign(payload: JOSEPayload, key: CryptoKey | Uint8Array<ArrayBuffer>, options: JWSSignOptions & {
15
+ alg: JWSAlgorithm;
16
+ }): Promise<string>;
17
+ /**
18
+ * Verifies a JWS (JSON Web Signature) in Compact Serialization format.
19
+ *
20
+ * @param jws The JWS Compact Serialization string.
21
+ * @param key The verification key (CryptoKey, JWK, JWKSet, raw symmetric key as Uint8Array, or a function resolving the key or set).
22
+ * @param options Verification options, such as allowed algorithms.
23
+ * @returns A Promise resolving to an object containing the verified payload and protected header.
24
+ * @throws If the JWS is invalid, signature verification fails, or options are not met.
25
+ */
26
+ declare function verify<T extends JOSEPayload>(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWSVerifyOptions): Promise<JWSVerifyResult<T>>;
27
+ declare function verify(jws: string, key: CryptoKey | JWKSet | JWK_Public | JWK_Symmetric | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWSVerifyOptions & {
28
+ forceUint8Array: true;
29
+ }): Promise<JWSVerifyResult<Uint8Array<ArrayBuffer>>>;
30
+ //#endregion
31
+ export { JWSAlgorithm, JWSHeaderParameters, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWTError, type JWTErrorCauseMap, type JWTErrorCode, isJWTError, sign, verify };
@@ -0,0 +1,104 @@
1
+ import { A as validateCriticalHeadersJWS, M as JWTError, N as isJWTError, S as computeJwtTimeClaims, T as decodePayloadFromB64UrlSegment, _ as textDecoder, a as base64UrlEncode, b as applyTypCtyDefaults, d as isJWKSet, i as base64UrlDecode, j as validateJwtClaims, u as isJWK, v as textEncoder, y as sanitizeObject } from "../_chunks/utils.mjs";
2
+ import { D as verify$1, E as sign$1 } from "../_chunks/_crypto.mjs";
3
+ import { getJWKsFromSet, importKey } from "./jwk.mjs";
4
+ //#region src/core/jws.ts
5
+ async function sign(payload, key, options = {}) {
6
+ const { protectedHeader: userHeader } = options;
7
+ let { alg } = options;
8
+ if (!alg) if (isJWK(key) && key.alg) alg = key.alg;
9
+ else throw new TypeError("JWS \"alg\" (Algorithm) must be provided in options");
10
+ if (alg === "none") throw new JWTError("\"none\" is not a valid signing algorithm", "ERR_JWS_ALG_NOT_ALLOWED");
11
+ const signingKey = await _resolveSigningKey(alg, await importKey(key, alg), "sign");
12
+ const protectedHeader = _buildJWSHeader(alg, key, userHeader, payload);
13
+ const computedPayload = computeJwtTimeClaims(payload, protectedHeader.typ, options.expiresIn, options.currentDate);
14
+ const protectedHeaderEncoded = base64UrlEncode(JSON.stringify(protectedHeader));
15
+ let payloadBytes;
16
+ if (payload instanceof Uint8Array) payloadBytes = payload;
17
+ else if (typeof payload === "string") payloadBytes = textEncoder.encode(payload);
18
+ else if (typeof payload === "object" && payload !== null) payloadBytes = textEncoder.encode(JSON.stringify(computedPayload || payload));
19
+ else throw new TypeError("Payload must be a string, Uint8Array, or a JSON-serializable object.");
20
+ const signingInputString = `${protectedHeaderEncoded}.${protectedHeader.b64 !== false ? base64UrlEncode(payloadBytes) : textDecoder.decode(payloadBytes)}`;
21
+ const signingInputBytes = textEncoder.encode(signingInputString);
22
+ return `${signingInputString}.${base64UrlEncode(await sign$1(alg, signingKey, signingInputBytes))}`;
23
+ }
24
+ async function verify(jws, key, options = {}) {
25
+ const parts = jws.split(".");
26
+ if (parts.length !== 3) throw new JWTError("Invalid JWS: Must contain three parts separated by dots.", "ERR_JWS_INVALID");
27
+ const [protectedHeaderEncoded, payloadEncoded, signatureEncoded] = parts;
28
+ let protectedHeader;
29
+ try {
30
+ const protectedHeaderString = base64UrlDecode(protectedHeaderEncoded);
31
+ protectedHeader = sanitizeObject(JSON.parse(protectedHeaderString));
32
+ } catch {
33
+ throw new JWTError("Invalid JWS: Protected header could not be decoded.", "ERR_JWS_INVALID");
34
+ }
35
+ if (!protectedHeader || typeof protectedHeader !== "object" || !protectedHeader.alg) throw new JWTError("Invalid JWS: Protected header must be an object with an \"alg\" property.", "ERR_JWS_INVALID");
36
+ const alg = protectedHeader.alg;
37
+ if (options.algorithms && !options.algorithms.includes(alg)) throw new JWTError(`Algorithm not allowed: ${alg}`, "ERR_JWS_ALG_NOT_ALLOWED");
38
+ if (options.typ && protectedHeader.typ !== options.typ) throw new JWTError(`Invalid JWS: "typ" (Type) Header Parameter mismatch. Expected "${options.typ}", got "${protectedHeader.typ}".`, "ERR_JWS_INVALID");
39
+ let signatureBytes;
40
+ try {
41
+ signatureBytes = base64UrlDecode(signatureEncoded, false);
42
+ } catch {
43
+ throw new JWTError("Invalid JWS: Signature could not be decoded.", "ERR_JWS_INVALID");
44
+ }
45
+ const keyInput = typeof key === "function" ? await key(protectedHeader, jws) : key;
46
+ const signingInputBytes = textEncoder.encode(`${protectedHeaderEncoded}.${payloadEncoded}`);
47
+ if (isJWKSet(keyInput)) {
48
+ const candidates = getJWKsFromSet(keyInput, _buildJWSSetFilter(protectedHeader));
49
+ if (candidates.length === 0) throw new JWTError(`No key found in JWK Set${protectedHeader.kid ? ` with kid "${protectedHeader.kid}"` : ""}.`, "ERR_JWK_KEY_NOT_FOUND");
50
+ let verified = false;
51
+ for (const candidate of candidates) try {
52
+ if (await verify$1(alg, await _resolveSigningKey(alg, await importKey(candidate, alg), "verify"), signatureBytes, signingInputBytes)) {
53
+ verified = true;
54
+ break;
55
+ }
56
+ } catch {}
57
+ if (!verified) throw new JWTError("JWS signature verification failed.", "ERR_JWS_SIGNATURE_INVALID");
58
+ } else if (!await verify$1(alg, await _resolveSigningKey(alg, await importKey(keyInput, alg), "verify"), signatureBytes, signingInputBytes)) throw new JWTError("JWS signature verification failed.", "ERR_JWS_SIGNATURE_INVALID");
59
+ const useB64 = protectedHeader.b64 !== false;
60
+ let payload;
61
+ try {
62
+ payload = useB64 ? decodePayloadFromB64UrlSegment(payloadEncoded, protectedHeader, options.forceUint8Array) : options.forceUint8Array ? textEncoder.encode(payloadEncoded) : payloadEncoded;
63
+ } catch (error_) {
64
+ throw new JWTError(`Invalid JWS: Payload decoding failed (${error_ instanceof Error ? error_.message : error_})`, "ERR_JWS_INVALID");
65
+ }
66
+ validateCriticalHeadersJWS(protectedHeader, options.recognizedHeaders);
67
+ if (payload && typeof payload === "object" && options.validateClaims !== false && (options.validateClaims === true || protectedHeader.typ?.toLowerCase().includes("jwt")) && !options.forceUint8Array && !(payload instanceof Uint8Array)) validateJwtClaims(payload, options);
68
+ return {
69
+ payload,
70
+ protectedHeader
71
+ };
72
+ }
73
+ async function _resolveSigningKey(alg, key, usage) {
74
+ if (key instanceof Uint8Array) {
75
+ const minBytes = Number.parseInt(alg.slice(2), 10) / 8;
76
+ if (key.length < minBytes) throw new JWTError(`${alg} requires a key of at least ${minBytes} bytes`, "ERR_JWK_INVALID");
77
+ return crypto.subtle.importKey("raw", key, {
78
+ name: "HMAC",
79
+ hash: `SHA-${alg.slice(-3)}`
80
+ }, false, [usage]);
81
+ }
82
+ if (alg.startsWith("RS") || alg.startsWith("PS")) {
83
+ const { modulusLength } = key.algorithm;
84
+ if (typeof modulusLength !== "number" || modulusLength < 2048) throw new JWTError(`${alg} requires a key modulusLength of at least 2048 bits`, "ERR_JWK_INVALID");
85
+ }
86
+ return key;
87
+ }
88
+ function _buildJWSSetFilter(header) {
89
+ const { kid, alg } = header;
90
+ return (k) => (!kid || k.kid === kid) && (!k.alg || k.alg === alg);
91
+ }
92
+ function _buildJWSHeader(alg, key, userHeader, payload) {
93
+ const safeHeader = sanitizeObject(userHeader);
94
+ const header = {
95
+ ...isJWK(key) && key.kid ? { kid: key.kid } : {},
96
+ ...safeHeader,
97
+ alg
98
+ };
99
+ applyTypCtyDefaults(header, payload);
100
+ if (header.b64 === true) delete header.b64;
101
+ return header;
102
+ }
103
+ //#endregion
104
+ export { JWTError, isJWTError, sign, verify };
@@ -1,4 +1,4 @@
1
- import { $ as JWK_Private, A as JWK, P as JWKSet, et as JWK_Public, l as StrictOmit, lt as JWK_oct, t as ExpiresIn, v as JWTClaimValidationOptions, y as JWTClaims } from "./index.mjs";
1
+ import { A as JWK, L as JWKSet, ft as JWK_oct, l as StrictOmit, nt as JWK_Private, rt as JWK_Public, t as ExpiresIn, v as JWTClaimValidationOptions, y as JWTClaims } from "../../_chunks/index.mjs";
2
2
 
3
3
  //#region src/core/utils/jwt.d.ts
4
4
  /**
@@ -37,7 +37,7 @@ declare function decodePayloadFromB64UrlSegment<T = unknown>(payloadEncoded: str
37
37
  cty?: string;
38
38
  } | undefined, forceUint8Array?: boolean): T | Uint8Array<ArrayBuffer> | string;
39
39
  /** Convert plaintext input to bytes, shared by JWS & JWE when preparing payload. */
40
- declare function getPlaintextBytes(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>): Uint8Array<ArrayBuffer>;
40
+ declare function getPlaintextBytes(payload: string | Uint8Array<ArrayBuffer> | Record<string, unknown>): Uint8Array<ArrayBuffer>;
41
41
  declare function computeExpiresInSeconds(expiresIn: ExpiresIn): number;
42
42
  declare const computeMaxTokenAgeSeconds: (expiresIn: ExpiresIn) => number;
43
43
  /** Optionally compute iat/exp when signing JWTs. */
@@ -47,28 +47,29 @@ declare function validateJwtClaims(jwtClaims: JWTClaims, options?: JWTClaimValid
47
47
  /** Validate critical headers in JWS semantics. */
48
48
  declare function validateCriticalHeadersJWS(protectedHeader: {
49
49
  crit?: string[];
50
- } & Record<string, any>, requiredHeaders?: string[]): void;
50
+ } & Record<string, any>, recognizedHeaders?: string[]): void;
51
51
  /** Validate critical headers in JWE semantics. */
52
52
  declare function validateCriticalHeadersJWE(protectedHeader: {
53
53
  crit?: string[];
54
- } & Record<string, any>, understoodFromOptions?: string[]): void;
54
+ } & Record<string, any>, recognizedHeaders?: string[]): void;
55
55
  //#endregion
56
56
  //#region src/core/utils/sanitize.d.ts
57
57
  /**
58
- * Forked from unsecure@0.0.4
58
+ * Originally derived from unsecure@0.0.4
59
59
  * Licensed under the MIT License.
60
60
  *
61
61
  * Source: https://github.com/sandros94/unsecure/blob/913ed5580c551153a19154b4916c67a8efb2be85/src/sanitize.ts
62
62
  */
63
63
  /**
64
- * Remove prototype-pollution vectors from a plain record in-place.
65
- * This strips __proto__, prototype, and constructor own properties.
66
- * Returns the same reference for convenience.
64
+ * Returns a deep structural copy of `obj` with prototype-pollution vectors
65
+ * stripped at every level (`__proto__`, `prototype`, `constructor`).
66
+ *
67
+ * The input is never modified. Arrays are copied element-by-element;
68
+ * nested objects are recursed with a WeakSet cycle guard.
67
69
  */
68
70
  declare function sanitizeObject<T extends Record<string, unknown> | undefined>(obj: T): T;
69
- declare namespace index_d_exports {
70
- export { StrictOmit, applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
71
- }
71
+ //#endregion
72
+ //#region src/core/utils/index.d.ts
72
73
  declare const textEncoder: TextEncoder;
73
74
  declare const textDecoder: TextDecoder;
74
75
  declare function base64Encode(data: Uint8Array<ArrayBuffer> | string): string;
@@ -105,4 +106,4 @@ declare function isPrivateJWK(key: unknown): key is JWK_Private;
105
106
  */
106
107
  declare function isPublicJWK(key: unknown): key is JWK_Public;
107
108
  //#endregion
108
- export { validateCriticalHeadersJWE as A, computeJwtTimeClaims as C, decodePayloadFromBytes as D, decodePayloadFromB64UrlSegment as E, validateJwtClaims as M, getPlaintextBytes as O, computeExpiresInSeconds as S, decodeMaybeJWTString as T, randomBytes as _, base64UrlEncode as a, sanitizeObject as b, isAsymmetricJWK as c, isJWK as d, isJWKSet as f, maybeArray as g, isSymmetricJWK as h, base64UrlDecode as i, validateCriticalHeadersJWS as j, isJWTContent as k, isCryptoKey as l, isPublicJWK as m, base64Decode as n, concatUint8Arrays as o, isPrivateJWK as p, base64Encode as r, index_d_exports as s, assertCryptoKey as t, isCryptoKeyPair as u, textDecoder as v, computeMaxTokenAgeSeconds as w, applyTypCtyDefaults as x, textEncoder as y };
109
+ export { StrictOmit, applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
@@ -0,0 +1,2 @@
1
+ import { A as validateCriticalHeadersJWS, C as computeMaxTokenAgeSeconds, D as getPlaintextBytes, E as decodePayloadFromBytes, O as isJWTContent, S as computeJwtTimeClaims, T as decodePayloadFromB64UrlSegment, _ as textDecoder, a as base64UrlEncode, b as applyTypCtyDefaults, c as isCryptoKey, d as isJWKSet, f as isPrivateJWK, g as randomBytes, h as maybeArray, i as base64UrlDecode, j as validateJwtClaims, k as validateCriticalHeadersJWE, l as isCryptoKeyPair, m as isSymmetricJWK, n as base64Decode, o as concatUint8Arrays, p as isPublicJWK, r as base64Encode, s as isAsymmetricJWK, t as assertCryptoKey, u as isJWK, v as textEncoder, w as decodeMaybeJWTString, x as computeExpiresInSeconds, y as sanitizeObject } from "../../_chunks/utils.mjs";
2
+ export { applyTypCtyDefaults, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeExpiresInSeconds, computeJwtTimeClaims, computeMaxTokenAgeSeconds, concatUint8Arrays, decodeMaybeJWTString, decodePayloadFromB64UrlSegment, decodePayloadFromBytes, getPlaintextBytes, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTContent, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, textDecoder, textEncoder, validateCriticalHeadersJWE, validateCriticalHeadersJWS, validateJwtClaims };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,7 @@
1
- import { $ as JWK_Private, A as JWK, B as JWK_Asymmetric_Algorithm, C as DeriveKeyOptions, D as GenerateKeyAlgorithm, E as GenerateJWKReturn, F as JWK_AES_CBC_HMAC, G as JWK_EC_Public, H as JWK_ECDH_ES, I as JWK_AES_GCM, J as JWK_OKP_Private, K as JWK_HMAC, L as JWK_AES_GCM_KW, M as JWKPEMAlgorithm, N as JWKParameters, O as GenerateKeyOptions, P as JWKSet, Q as JWK_Pair, R as JWK_AES_KW, S as ContentEncryptionAlgorithm, T as GenerateJWKOptions, U as JWK_ECDSA, V as JWK_EC, W as JWK_EC_Private, X as JWK_OKP_SIGN, Y as JWK_OKP_Public, Z as JWK_PBES2, _ as JWT, a as JWEEncryptOptions, at as JWK_RSA_Public, b as JoseHeaderParameters, c as JWEKeyManagementHeaderParameters, ct as JWK_Symmetric_Algorithm, d as JWSHeaderParameters, dt as UnwrapKeyOptions, et as JWK_Public, f as JWSKeyLookupFunction, ft as WrapKeyOptions, g as JWSVerifyResult, h as JWSVerifyOptions, i as JWEDecryptResult, it as JWK_RSA_Private, j as JWKAlgorithm, k as GenerateKeyReturn, lt as JWK_oct, m as JWSSignOptions, n as MaxTokenAge, nt as JWK_RSA_ENC, o as JWEHeaderParameters, ot as JWK_RSA_SIGN, p as JWSProtectedHeader, pt as WrapKeyResult, q as JWK_OKP, r as JWEDecryptOptions, rt as JWK_RSA_PSS, s as JWEKeyLookupFunction, st as JWK_Symmetric, t as ExpiresIn, tt as JWK_RSA, u as JWSAlgorithm, ut as KeyManagementAlgorithm, v as JWTClaimValidationOptions, w as DeriveKeyReturn, x as CompositeKey, y as JWTClaims, z as JWK_Asymmetric } from "./_chunks/index.mjs";
2
- import { s as index_d_exports } from "./_chunks/index2.mjs";
3
- import { r as jwe_d_exports } from "./_chunks/jwe.mjs";
4
- import { u as jwk_d_exports } from "./_chunks/jwk.mjs";
5
- import { t as jws_d_exports } from "./_chunks/jws.mjs";
6
- export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyLookupFunction, JWEKeyManagementHeaderParameters, JWK, JWKAlgorithm, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSHeaderParameters, JWSKeyLookupFunction, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, UnwrapKeyOptions, WrapKeyOptions, WrapKeyResult, jwe_d_exports as jwe, jwk_d_exports as jwk, jws_d_exports as jws, index_d_exports as utils };
1
+ import { $ as JWK_OKP_SIGN, A as JWK, B as JWK_AES_GCM_KW, C as DeriveKeyOptions, D as GenerateKeyAlgorithm, E as GenerateJWKReturn, F as JWKPEMAlgorithm, G as JWK_ECDH_ES, H as JWK_Asymmetric, I as JWKParameters, J as JWK_EC_Public, K as JWK_ECDSA, L as JWKSet, M as JWKCacheAdapter, N as JWKLookupFunction, O as GenerateKeyOptions, P as JWKLookupFunctionHeader, Q as JWK_OKP_Public, R as JWK_AES_CBC_HMAC, S as ContentEncryptionAlgorithm, T as GenerateJWKOptions, U as JWK_Asymmetric_Algorithm, V as JWK_AES_KW, W as JWK_EC, X as JWK_OKP, Y as JWK_HMAC, Z as JWK_OKP_Private, _ as JWT, a as JWEEncryptOptions, at as JWK_RSA_ENC, b as JoseHeaderParameters, c as JWEProtectedHeader, ct as JWK_RSA_Public, d as JWSHeaderParameters, dt as JWK_Symmetric_Algorithm, et as JWK_PBES2, f as JWSProtectedHeader, ft as JWK_oct, g as JOSEPayload, gt as WrapKeyResult, h as JWSVerifyResult, ht as WrapKeyOptions, i as JWEDecryptResult, it as JWK_RSA, j as JWKAlgorithm, k as GenerateKeyReturn, lt as JWK_RSA_SIGN, m as JWSVerifyOptions, mt as UnwrapKeyOptions, n as MaxTokenAge, nt as JWK_Private, o as JWEHeaderParameters, ot as JWK_RSA_PSS, p as JWSSignOptions, pt as KeyManagementAlgorithm, q as JWK_EC_Private, r as JWEDecryptOptions, rt as JWK_Public, s as JWEKeyManagementHeaderParameters, st as JWK_RSA_Private, t as ExpiresIn, tt as JWK_Pair, u as JWSAlgorithm, ut as JWK_Symmetric, v as JWTClaimValidationOptions, w as DeriveKeyReturn, x as CompositeKey, y as JWTClaims, z as JWK_AES_GCM } from "./_chunks/index.mjs";
2
+ import { i as isJWTError, n as JWTErrorCauseMap, r as JWTErrorCode, t as JWTError } from "./_chunks/error.mjs";
3
+ import { decrypt, encrypt } from "./core/jwe.mjs";
4
+ import { WeakMapJWKCache, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, unwrapKey, wrapKey } from "./core/jwk.mjs";
5
+ import { sign, verify } from "./core/jws.mjs";
6
+ import { assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, computeExpiresInSeconds, computeMaxTokenAgeSeconds, concatUint8Arrays, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, textDecoder, textEncoder, validateJwtClaims } from "./core/utils/index.mjs";
7
+ export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JOSEPayload, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEProtectedHeader, JWK, JWKAlgorithm, JWKCacheAdapter, JWKLookupFunction, JWKLookupFunctionHeader, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWSAlgorithm, JWSHeaderParameters, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JWTError, type JWTErrorCauseMap, type JWTErrorCode, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, UnwrapKeyOptions, WeakMapJWKCache, WrapKeyOptions, WrapKeyResult, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, clearJWKCache, computeExpiresInSeconds, computeMaxTokenAgeSeconds, concatUint8Arrays, configureJWKCache, decrypt, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, encrypt, exportKey, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTError, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, sign, textDecoder, textEncoder, unwrapKey, validateJwtClaims, verify, wrapKey };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { y as utils_exports } from "./_chunks/utils.mjs";
2
- import { u as jwk_exports } from "./_chunks/jwk.mjs";
3
- import { r as jwe_exports } from "./_chunks/jwe.mjs";
4
- import { t as jws_exports } from "./_chunks/jws.mjs";
5
- export { jwe_exports as jwe, jwk_exports as jwk, jws_exports as jws, utils_exports as utils };
1
+ import { C as computeMaxTokenAgeSeconds, M as JWTError, N as isJWTError, _ as textDecoder, a as base64UrlEncode, c as isCryptoKey, d as isJWKSet, f as isPrivateJWK, g as randomBytes, h as maybeArray, i as base64UrlDecode, j as validateJwtClaims, l as isCryptoKeyPair, m as isSymmetricJWK, n as base64Decode, o as concatUint8Arrays, p as isPublicJWK, r as base64Encode, s as isAsymmetricJWK, t as assertCryptoKey, u as isJWK, v as textEncoder, x as computeExpiresInSeconds, y as sanitizeObject } from "./_chunks/utils.mjs";
2
+ import { WeakMapJWKCache, clearJWKCache, configureJWKCache, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, exportKey, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, unwrapKey, wrapKey } from "./core/jwk.mjs";
3
+ import { sign, verify } from "./core/jws.mjs";
4
+ import { decrypt, encrypt } from "./core/jwe.mjs";
5
+ export { JWTError, WeakMapJWKCache, assertCryptoKey, base64Decode, base64Encode, base64UrlDecode, base64UrlEncode, clearJWKCache, computeExpiresInSeconds, computeMaxTokenAgeSeconds, concatUint8Arrays, configureJWKCache, decrypt, deriveJWKFromPassword, deriveKeyFromPassword, deriveSharedSecret, encrypt, exportKey, exportToPEM, generateJWK, generateKey, getJWKFromSet, getJWKsFromSet, importFromPEM, importKey, isAsymmetricJWK, isCryptoKey, isCryptoKeyPair, isJWK, isJWKSet, isJWTError, isPrivateJWK, isPublicJWK, isSymmetricJWK, maybeArray, randomBytes, sanitizeObject, sign, textDecoder, textEncoder, unwrapKey, validateJwtClaims, verify, wrapKey };
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.5.40",
3
+ "version": "0.6.0",
4
4
  "description": "Low-level JWT utilities",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "Sandro Circi",
8
8
  "email": "sandro.circi@digitoolmedia.com"
9
9
  },
10
- "repository": "sandros94/unjwt",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/sandros94/unjwt.git"
13
+ },
11
14
  "files": [
12
15
  "dist"
13
16
  ],
@@ -21,48 +24,35 @@
21
24
  "types": "./dist/index.d.mts",
22
25
  "default": "./dist/index.mjs"
23
26
  },
24
- "./jwe": {
25
- "types": "./dist/jwe.d.mts",
26
- "default": "./dist/jwe.mjs"
27
- },
28
27
  "./jwk": {
29
- "types": "./dist/jwk.d.mts",
30
- "default": "./dist/jwk.mjs"
28
+ "types": "./dist/core/jwk.d.mts",
29
+ "default": "./dist/core/jwk.mjs"
31
30
  },
32
31
  "./jws": {
33
- "types": "./dist/jws.d.mts",
34
- "default": "./dist/jws.mjs"
32
+ "types": "./dist/core/jws.d.mts",
33
+ "default": "./dist/core/jws.mjs"
34
+ },
35
+ "./jwe": {
36
+ "types": "./dist/core/jwe.d.mts",
37
+ "default": "./dist/core/jwe.mjs"
35
38
  },
36
39
  "./utils": {
37
- "types": "./dist/utils.d.mts",
38
- "default": "./dist/utils.mjs"
40
+ "types": "./dist/core/utils/index.d.mts",
41
+ "default": "./dist/core/utils/index.mjs"
39
42
  },
40
43
  "./adapters/h3": {
41
- "types": "./dist/adapters/h3.d.mts",
42
- "default": "./dist/adapters/h3.mjs"
44
+ "types": "./dist/adapters/h3v1/index.d.mts",
45
+ "default": "./dist/adapters/h3v1/index.mjs"
43
46
  },
44
47
  "./adapters/h3v1": {
45
- "types": "./dist/adapters/h3v1.d.mts",
46
- "default": "./dist/adapters/h3v1.mjs"
48
+ "types": "./dist/adapters/h3v1/index.d.mts",
49
+ "default": "./dist/adapters/h3v1/index.mjs"
47
50
  },
48
51
  "./adapters/h3v2": {
49
- "types": "./dist/adapters/h3v2.d.mts",
50
- "default": "./dist/adapters/h3v2.mjs"
52
+ "types": "./dist/adapters/h3v2/index.d.mts",
53
+ "default": "./dist/adapters/h3v2/index.mjs"
51
54
  }
52
55
  },
53
- "scripts": {
54
- "build": "obuild",
55
- "dev": "bun --watch ./playground/main.ts",
56
- "dev:prepare": "obuild --stub",
57
- "lint": "oxlint . && oxfmt --check .",
58
- "fmt": "automd && oxlint --fix . && oxfmt .",
59
- "prepack": "pnpm build",
60
- "release": "pnpm test && pnpm prepack && changelogen --release --push",
61
- "test": "vitest run --slowTestThreshold=1000",
62
- "test:coverage": "vitest run --coverage --slowTestThreshold=1000",
63
- "typecheck": "tsgo --noEmit",
64
- "bench": "vitest bench --run"
65
- },
66
56
  "devDependencies": {
67
57
  "@types/node": "^24",
68
58
  "@types/supertest": "^6.0.3",
@@ -72,20 +62,22 @@
72
62
  "changelogen": "^0.6.2",
73
63
  "cookie-esv1": "npm:cookie-es@^1.2.2",
74
64
  "cookie-esv2": "npm:cookie-es@^2.0.0",
75
- "h3v1": "npm:h3@^1.15.4",
76
- "h3v2": "npm:h3@^2.0.1-rc.14",
65
+ "h3v1": "npm:h3@^1.15.10",
66
+ "h3v2": "npm:h3@^2.0.1-rc.19",
77
67
  "jose": "^6.2.0",
68
+ "nano-staged": "^0.9.0",
78
69
  "obuild": "^0.4.32",
79
- "oxfmt": "^0.41.0",
70
+ "oxfmt": "^0.43.0",
80
71
  "oxlint": "^1.51.0",
81
- "rolldown": "1.0.0-rc.11",
72
+ "rolldown": "1.0.0-rc.12",
73
+ "simple-git-hooks": "^2.13.1",
82
74
  "supertest": "^7.2.2",
83
75
  "typescript": "^5.9.3",
84
76
  "vitest": "^4.0.18"
85
77
  },
86
78
  "peerDependencies": {
87
79
  "cookie-es": "^1.2.2 || ^2.0.0",
88
- "h3": "^1.15.4 || >=2.0.1-rc.5 || ^2.0.1",
80
+ "h3": "^1.15.4 || >=2.0.1-rc.18 || ^2.0.1",
89
81
  "rou3": "^0.7.10"
90
82
  },
91
83
  "peerDependenciesMeta": {
@@ -99,8 +91,25 @@
99
91
  "optional": true
100
92
  }
101
93
  },
102
- "engines": {
103
- "node": "^22.12 || >=24"
94
+ "simple-git-hooks": {
95
+ "pre-commit": "./node_modules/.bin/nano-staged"
104
96
  },
105
- "packageManager": "pnpm@10.30.3"
106
- }
97
+ "nano-staged": {
98
+ "*.{js,ts,mjs,cjs,json,yml,md}": [
99
+ "pnpm run fmt"
100
+ ]
101
+ },
102
+ "scripts": {
103
+ "postinstall": "obuild --stub && pnpm simple-git-hooks",
104
+ "build": "obuild",
105
+ "dev": "bun --watch ./playground/main.ts",
106
+ "dev:prepare": "obuild --stub",
107
+ "lint": "oxlint . && oxfmt --check .",
108
+ "fmt": "automd && oxlint --fix . && oxfmt .",
109
+ "release": "pnpm test && pnpm prepack && changelogen --release --push",
110
+ "test": "vitest run --slowTestThreshold=1000",
111
+ "test:coverage": "vitest run --coverage --slowTestThreshold=1000",
112
+ "typecheck": "tsgo --noEmit",
113
+ "bench": "vitest bench --run"
114
+ }
115
+ }
@@ -1,13 +0,0 @@
1
- //#region \0rolldown/runtime.js
2
- var __defProp = Object.defineProperty;
3
- var __exportAll = (all, no_symbols) => {
4
- let target = {};
5
- for (var name in all) __defProp(target, name, {
6
- get: all[name],
7
- enumerable: true
8
- });
9
- if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
- return target;
11
- };
12
- //#endregion
13
- export { __exportAll as t };
@@ -1,43 +0,0 @@
1
- import { $ as JWK_Private, A as JWK, B as JWK_Asymmetric_Algorithm, C as DeriveKeyOptions, D as GenerateKeyAlgorithm, E as GenerateJWKReturn, F as JWK_AES_CBC_HMAC, G as JWK_EC_Public, H as JWK_ECDH_ES, I as JWK_AES_GCM, J as JWK_OKP_Private, K as JWK_HMAC, L as JWK_AES_GCM_KW, M as JWKPEMAlgorithm, N as JWKParameters, O as GenerateKeyOptions, P as JWKSet, Q as JWK_Pair, R as JWK_AES_KW, S as ContentEncryptionAlgorithm, T as GenerateJWKOptions, U as JWK_ECDSA, V as JWK_EC, W as JWK_EC_Private, X as JWK_OKP_SIGN, Y as JWK_OKP_Public, Z as JWK_PBES2, _ as JWT, a as JWEEncryptOptions, at as JWK_RSA_Public, b as JoseHeaderParameters, c as JWEKeyManagementHeaderParameters, ct as JWK_Symmetric_Algorithm, dt as UnwrapKeyOptions, et as JWK_Public, ft as WrapKeyOptions, i as JWEDecryptResult, it as JWK_RSA_Private, j as JWKAlgorithm, k as GenerateKeyReturn, lt as JWK_oct, nt as JWK_RSA_ENC, o as JWEHeaderParameters, ot as JWK_RSA_SIGN, pt as WrapKeyResult, q as JWK_OKP, r as JWEDecryptOptions, rt as JWK_RSA_PSS, s as JWEKeyLookupFunction, st as JWK_Symmetric, tt as JWK_RSA, ut as KeyManagementAlgorithm, v as JWTClaimValidationOptions, w as DeriveKeyReturn, x as CompositeKey, y as JWTClaims, z as JWK_Asymmetric } from "./index.mjs";
2
-
3
- //#region src/core/jwe.d.ts
4
- declare namespace jwe_d_exports {
5
- export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyLookupFunction, JWEKeyManagementHeaderParameters, JWK, JWKAlgorithm, JWKPEMAlgorithm, JWKParameters, JWKSet, JWK_AES_CBC_HMAC, JWK_AES_GCM, JWK_AES_GCM_KW, JWK_AES_KW, JWK_Asymmetric, JWK_Asymmetric_Algorithm, JWK_EC, JWK_ECDH_ES, JWK_ECDSA, JWK_EC_Private, JWK_EC_Public, JWK_HMAC, JWK_OKP, JWK_OKP_Private, JWK_OKP_Public, JWK_OKP_SIGN, JWK_PBES2, JWK_Pair, JWK_Private, JWK_Public, JWK_RSA, JWK_RSA_ENC, JWK_RSA_PSS, JWK_RSA_Private, JWK_RSA_Public, JWK_RSA_SIGN, JWK_Symmetric, JWK_Symmetric_Algorithm, JWK_oct, JWT, JWTClaimValidationOptions, JWTClaims, JoseHeaderParameters, KeyManagementAlgorithm, UnwrapKeyOptions, WrapKeyOptions, WrapKeyResult, decrypt, encrypt };
6
- }
7
- /**
8
- * Encrypts a payload to produce a JWE Compact Serialization string.
9
- *
10
- * @param payload The payload to encrypt. Can be a string, Uint8Array, or a JSON-serializable object.
11
- * @param key The key encryption key (KEK). Can be a CryptoKey, JWK, or for PBES2, a password string/Uint8Array.
12
- * @param options JWE encryption options, including `alg` (key management algorithm) and `enc` (content encryption algorithm).
13
- * @returns A Promise resolving to the JWE Compact Serialization string.
14
- */
15
- declare function encrypt(payload: JWTClaims, key: JWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
16
- declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: JWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
17
- declare function encrypt(payload: JWTClaims, key: CryptoKey, options: JWEEncryptOptions & {
18
- alg: KeyManagementAlgorithm;
19
- enc: ContentEncryptionAlgorithm;
20
- }): Promise<string>;
21
- declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: CryptoKey, options: JWEEncryptOptions & {
22
- alg: KeyManagementAlgorithm;
23
- enc: ContentEncryptionAlgorithm;
24
- }): Promise<string>;
25
- declare function encrypt(payload: string | Uint8Array<ArrayBuffer> | Record<string, any>, key: CryptoKey | JWK | string | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
26
- alg: KeyManagementAlgorithm;
27
- enc: ContentEncryptionAlgorithm;
28
- }): Promise<string>;
29
- /**
30
- * Decrypts a JWE Compact Serialization string.
31
- *
32
- * @param jwe The JWE Compact Serialization string.
33
- * @param key The key decryption key (KEK) or a function to look up the key.
34
- * @param options JWE decryption options.
35
- * @returns A Promise resolving to an object containing the decrypted plaintext, protected header, CEK, and AAD.
36
- * @throws If JWE is invalid, decryption fails, or options are not met.
37
- */
38
- declare function decrypt<T extends JWTClaims | Uint8Array<ArrayBuffer> | string>(jwe: string, key: CryptoKey | JWK_Symmetric | JWK_Private | string | Uint8Array<ArrayBuffer> | JWEKeyLookupFunction, options?: JWEDecryptOptions): Promise<JWEDecryptResult<T>>;
39
- declare function decrypt(jwe: string, key: CryptoKey | JWK_Symmetric | JWK_Private | string | Uint8Array<ArrayBuffer> | JWEKeyLookupFunction, options: JWEDecryptOptions & {
40
- forceUint8Array: true;
41
- }): Promise<JWEDecryptResult<Uint8Array<ArrayBuffer>>>;
42
- //#endregion
43
- export { encrypt as n, jwe_d_exports as r, decrypt as t };