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
@@ -1,4 +1,36 @@
1
- import { t as __exportAll } from "./chunk.mjs";
1
+ //#region src/core/error.ts
2
+ /**
3
+ * Base error class for all unjwt errors. Every error thrown by the library is
4
+ * an instance of `JWTError`, so a single `instanceof` check is sufficient to
5
+ * distinguish library errors from other thrown values. Use the `code` property
6
+ * (or the `isJWTError` type guard) to narrow to a specific error condition.
7
+ *
8
+ * @example
9
+ * import { JWTError, isJWTError } from "unjwt/jws";
10
+ *
11
+ * try {
12
+ * await verify(token, key);
13
+ * } catch (err) {
14
+ * if (isJWTError(err, "ERR_JWT_EXPIRED")) {
15
+ * console.log(err.cause.jti); // string | undefined
16
+ * }
17
+ * }
18
+ */
19
+ var JWTError = class extends Error {
20
+ code;
21
+ cause;
22
+ constructor(message, code, cause) {
23
+ super(message);
24
+ this.name = "JWTError";
25
+ this.code = code;
26
+ this.cause = cause;
27
+ }
28
+ };
29
+ function isJWTError(error, code) {
30
+ if (!(error instanceof JWTError)) return false;
31
+ return code === void 0 || error.code === code;
32
+ }
33
+ //#endregion
2
34
  //#region src/core/utils/jwt.ts
3
35
  /**
4
36
  * Apply default typ/cty semantics shared by JWS & JWE.
@@ -107,23 +139,31 @@ function validateJwtClaims(jwtClaims, options = {}) {
107
139
  if (options.subject) allRequiredClaims.add("sub");
108
140
  if (options.maxTokenAge) allRequiredClaims.add("iat");
109
141
  for (const claimName of allRequiredClaims) if (!(claimName in jwtClaims)) missingClaims.add(claimName);
110
- if (missingClaims.size > 0) throw new Error(`Missing required JWT Claims: ${[...missingClaims].join(", ")}`);
142
+ if (missingClaims.size > 0) throw new JWTError(`Missing required JWT Claims: ${[...missingClaims].join(", ")}`, "ERR_JWT_CLAIM_MISSING");
111
143
  if (options.issuer) {
112
144
  const expectedIssuers = maybeArray(options.issuer);
113
- if (!jwtClaims.iss || !expectedIssuers.includes(jwtClaims.iss)) throw new Error(`Invalid JWT "iss" (Issuer) Claim: Expected ${expectedIssuers.join(" or ")}, got ${jwtClaims.iss}`);
145
+ if (!jwtClaims.iss || !expectedIssuers.includes(jwtClaims.iss)) throw new JWTError(`Invalid JWT "iss" (Issuer) Claim: Expected ${expectedIssuers.join(" or ")}, got ${jwtClaims.iss}`, "ERR_JWT_CLAIM_INVALID");
114
146
  }
115
- if (options.subject && jwtClaims.sub !== options.subject) throw new Error(`Invalid JWT "sub" (Subject) Claim: Expected ${options.subject}, got ${jwtClaims.sub}`);
147
+ if (options.subject && jwtClaims.sub !== options.subject) throw new JWTError(`Invalid JWT "sub" (Subject) Claim: Expected ${options.subject}, got ${jwtClaims.sub}`, "ERR_JWT_CLAIM_INVALID");
116
148
  if (options.audience) {
117
149
  const expectedAudiences = maybeArray(options.audience);
118
150
  const claimAudience = maybeArray(jwtClaims.aud || []);
119
- if (!claimAudience.some((aud) => expectedAudiences.includes(aud))) throw new Error(`Invalid JWT "aud" (Audience) Claim: Expected ${expectedAudiences.join(" or ")}, got ${claimAudience.join(", ")}`);
151
+ if (!claimAudience.some((aud) => expectedAudiences.includes(aud))) throw new JWTError(`Invalid JWT "aud" (Audience) Claim: Expected ${expectedAudiences.join(" or ")}, got ${claimAudience.join(", ")}`, "ERR_JWT_CLAIM_INVALID");
120
152
  }
121
- if (typeof jwtClaims.nbf === "number" && jwtClaims.nbf > currentTime + clockTolerance) throw new Error(`JWT "nbf" (Not Before) Claim validation failed: Token is not yet valid (nbf: ${(/* @__PURE__ */ new Date(jwtClaims.nbf * 1e3)).toISOString()})`);
122
- if (typeof jwtClaims.exp === "number" && jwtClaims.exp <= currentTime - clockTolerance) throw new Error(`JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${(/* @__PURE__ */ new Date(jwtClaims.exp * 1e3)).toISOString()})`);
153
+ if (typeof jwtClaims.nbf === "number" && jwtClaims.nbf > currentTime + clockTolerance) throw new JWTError(`JWT "nbf" (Not Before) Claim validation failed: Token is not yet valid (nbf: ${(/* @__PURE__ */ new Date(jwtClaims.nbf * 1e3)).toISOString()})`, "ERR_JWT_NBF");
154
+ if (typeof jwtClaims.exp === "number" && jwtClaims.exp <= currentTime - clockTolerance) throw new JWTError(`JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${(/* @__PURE__ */ new Date(jwtClaims.exp * 1e3)).toISOString()})`, "ERR_JWT_EXPIRED", {
155
+ jti: jwtClaims.jti,
156
+ iat: jwtClaims.iat,
157
+ exp: jwtClaims.exp
158
+ });
123
159
  if (options.maxTokenAge) {
124
- if (typeof jwtClaims.iat !== "number") throw new TypeError("JWT \"iat\" (Issued At) Claim must be a number when maxTokenAge is set.");
125
- if (jwtClaims.iat > currentTime + clockTolerance) throw new Error(`JWT "iat" (Issued At) Claim validation failed: Token was issued in the future (iat: ${(/* @__PURE__ */ new Date(jwtClaims.iat * 1e3)).toISOString()})`);
126
- if (jwtClaims.iat < currentTime - computeMaxTokenAgeSeconds(options.maxTokenAge) - clockTolerance) throw new Error(`JWT "iat" (Issued At) Claim validation failed: Token is too old (maxTokenAge: ${options.maxTokenAge}s, iat: ${(/* @__PURE__ */ new Date(jwtClaims.iat * 1e3)).toISOString()})`);
160
+ if (typeof jwtClaims.iat !== "number") throw new JWTError("JWT \"iat\" (Issued At) Claim must be a number when maxTokenAge is set.", "ERR_JWT_CLAIM_INVALID");
161
+ if (jwtClaims.iat > currentTime + clockTolerance) throw new JWTError(`JWT "iat" (Issued At) Claim validation failed: Token was issued in the future (iat: ${(/* @__PURE__ */ new Date(jwtClaims.iat * 1e3)).toISOString()})`, "ERR_JWT_CLAIM_INVALID");
162
+ if (jwtClaims.iat < currentTime - computeMaxTokenAgeSeconds(options.maxTokenAge) - clockTolerance) throw new JWTError(`JWT "iat" (Issued At) Claim validation failed: Token is too old (maxTokenAge: ${options.maxTokenAge}s, iat: ${(/* @__PURE__ */ new Date(jwtClaims.iat * 1e3)).toISOString()})`, "ERR_JWT_EXPIRED", {
163
+ jti: jwtClaims.jti,
164
+ iat: jwtClaims.iat,
165
+ exp: jwtClaims.exp
166
+ });
127
167
  }
128
168
  }
129
169
  const BASE_HEADER_PARAMS = [
@@ -138,11 +178,11 @@ const BASE_HEADER_PARAMS = [
138
178
  "x5u"
139
179
  ];
140
180
  /** Validate critical headers in JWS semantics. */
141
- function validateCriticalHeadersJWS(protectedHeader, requiredHeaders = []) {
181
+ function validateCriticalHeadersJWS(protectedHeader, recognizedHeaders = []) {
142
182
  if (!protectedHeader.crit) return;
143
183
  const missingHeaderParams = /* @__PURE__ */ new Set();
144
184
  const recognizedParams = new Set([
145
- ...requiredHeaders,
185
+ ...recognizedHeaders,
146
186
  ...BASE_HEADER_PARAMS,
147
187
  "b64"
148
188
  ]);
@@ -150,14 +190,14 @@ function validateCriticalHeadersJWS(protectedHeader, requiredHeaders = []) {
150
190
  if (recognizedParams.has(param) && (param in protectedHeader || param === "b64")) continue;
151
191
  missingHeaderParams.add(param);
152
192
  }
153
- if (missingHeaderParams.size > 0) throw new Error(`Missing critical header parameters: ${[...missingHeaderParams].join(", ")}`);
193
+ if (missingHeaderParams.size > 0) throw new JWTError(`Missing critical header parameters: ${[...missingHeaderParams].join(", ")}`, "ERR_JWS_INVALID");
154
194
  }
155
195
  /** Validate critical headers in JWE semantics. */
156
- function validateCriticalHeadersJWE(protectedHeader, understoodFromOptions) {
157
- if (protectedHeader.crit && !understoodFromOptions) throw new Error(`Unprocessed critical header parameters: ${protectedHeader.crit.join(", ")}`);
196
+ function validateCriticalHeadersJWE(protectedHeader, recognizedHeaders) {
197
+ if (protectedHeader.crit && !recognizedHeaders) throw new JWTError(`Unprocessed critical header parameters: ${protectedHeader.crit.join(", ")}`, "ERR_JWE_INVALID");
158
198
  if (!protectedHeader.crit) return;
159
199
  const understoodParams = new Set([
160
- ...understoodFromOptions || [],
200
+ ...recognizedHeaders || [],
161
201
  ...BASE_HEADER_PARAMS,
162
202
  "enc",
163
203
  "iv",
@@ -169,77 +209,41 @@ function validateCriticalHeadersJWE(protectedHeader, understoodFromOptions) {
169
209
  "apv"
170
210
  ]);
171
211
  for (const critParam of protectedHeader.crit) {
172
- if (!Object.prototype.hasOwnProperty.call(protectedHeader, critParam)) throw new Error(`Critical header parameter "${critParam}" listed in "crit" but not present in the protected header.`);
173
- if (!understoodParams.has(critParam)) throw new Error(`Unprocessed critical header parameters: ${critParam}`);
212
+ if (!Object.prototype.hasOwnProperty.call(protectedHeader, critParam)) throw new JWTError(`Critical header parameter "${critParam}" listed in "crit" but not present in the protected header.`, "ERR_JWE_INVALID");
213
+ if (!understoodParams.has(critParam)) throw new JWTError(`Unprocessed critical header parameters: ${critParam}`, "ERR_JWE_INVALID");
174
214
  }
175
215
  }
176
216
  //#endregion
177
217
  //#region src/core/utils/sanitize.ts
178
218
  /**
179
- * Forked from unsecure@0.0.4
219
+ * Originally derived from unsecure@0.0.4
180
220
  * Licensed under the MIT License.
181
221
  *
182
222
  * Source: https://github.com/sandros94/unsecure/blob/913ed5580c551153a19154b4916c67a8efb2be85/src/sanitize.ts
183
223
  */
184
224
  /**
185
- * Remove prototype-pollution vectors from a plain record in-place.
186
- * This strips __proto__, prototype, and constructor own properties.
187
- * Returns the same reference for convenience.
225
+ * Returns a deep structural copy of `obj` with prototype-pollution vectors
226
+ * stripped at every level (`__proto__`, `prototype`, `constructor`).
227
+ *
228
+ * The input is never modified. Arrays are copied element-by-element;
229
+ * nested objects are recursed with a WeakSet cycle guard.
188
230
  */
189
231
  function sanitizeObject(obj) {
190
232
  if (!obj || typeof obj !== "object") return obj;
191
- const seen = /* @__PURE__ */ new WeakSet();
192
- seen.add(obj);
193
- sanitizeKeys(obj, seen);
194
- return obj;
233
+ return _sanitizeCopy(obj, /* @__PURE__ */ new WeakSet());
195
234
  }
196
- function sanitizeKeys(current, seen) {
197
- if (Object.prototype.hasOwnProperty.call(current, "__proto__")) delete current["__proto__"];
198
- if (Object.prototype.hasOwnProperty.call(current, "prototype")) delete current["prototype"];
199
- if (Object.prototype.hasOwnProperty.call(current, "constructor")) delete current["constructor"];
200
- const items = Array.isArray(current) ? current : Object.values(current);
201
- for (const v of items) if (v && typeof v === "object") {
202
- if (!seen.has(v)) {
203
- seen.add(v);
204
- sanitizeKeys(v, seen);
205
- }
235
+ function _sanitizeCopy(current, seen) {
236
+ seen.add(current);
237
+ if (Array.isArray(current)) return current.map((item) => item && typeof item === "object" && !seen.has(item) ? _sanitizeCopy(item, seen) : item);
238
+ const result = {};
239
+ for (const [key, value] of Object.entries(current)) {
240
+ if (key === "__proto__" || key === "prototype" || key === "constructor") continue;
241
+ result[key] = value && typeof value === "object" && !seen.has(value) ? _sanitizeCopy(value, seen) : value;
206
242
  }
243
+ return result;
207
244
  }
208
245
  //#endregion
209
246
  //#region src/core/utils/index.ts
210
- var utils_exports = /* @__PURE__ */ __exportAll({
211
- applyTypCtyDefaults: () => applyTypCtyDefaults,
212
- assertCryptoKey: () => assertCryptoKey,
213
- base64Decode: () => base64Decode,
214
- base64Encode: () => base64Encode,
215
- base64UrlDecode: () => base64UrlDecode,
216
- base64UrlEncode: () => base64UrlEncode,
217
- computeExpiresInSeconds: () => computeExpiresInSeconds,
218
- computeJwtTimeClaims: () => computeJwtTimeClaims,
219
- computeMaxTokenAgeSeconds: () => computeMaxTokenAgeSeconds,
220
- concatUint8Arrays: () => concatUint8Arrays,
221
- decodeMaybeJWTString: () => decodeMaybeJWTString,
222
- decodePayloadFromB64UrlSegment: () => decodePayloadFromB64UrlSegment,
223
- decodePayloadFromBytes: () => decodePayloadFromBytes,
224
- getPlaintextBytes: () => getPlaintextBytes,
225
- isAsymmetricJWK: () => isAsymmetricJWK,
226
- isCryptoKey: () => isCryptoKey,
227
- isCryptoKeyPair: () => isCryptoKeyPair,
228
- isJWK: () => isJWK,
229
- isJWKSet: () => isJWKSet,
230
- isJWTContent: () => isJWTContent,
231
- isPrivateJWK: () => isPrivateJWK,
232
- isPublicJWK: () => isPublicJWK,
233
- isSymmetricJWK: () => isSymmetricJWK,
234
- maybeArray: () => maybeArray,
235
- randomBytes: () => randomBytes,
236
- sanitizeObject: () => sanitizeObject,
237
- textDecoder: () => textDecoder,
238
- textEncoder: () => textEncoder,
239
- validateCriticalHeadersJWE: () => validateCriticalHeadersJWE,
240
- validateCriticalHeadersJWS: () => validateCriticalHeadersJWS,
241
- validateJwtClaims: () => validateJwtClaims
242
- });
243
247
  const textEncoder = /* @__PURE__ */ new TextEncoder();
244
248
  const textDecoder = /* @__PURE__ */ new TextDecoder();
245
249
  const _Buffer = /* @__PURE__ */ (() => {
@@ -372,4 +376,4 @@ function isPublicJWK(key) {
372
376
  return false;
373
377
  }
374
378
  //#endregion
375
- 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, textDecoder as _, base64UrlEncode as a, sanitizeObject as b, isCryptoKey as c, isJWKSet as d, isPrivateJWK as f, randomBytes as g, maybeArray as h, base64UrlDecode as i, validateCriticalHeadersJWS as j, isJWTContent as k, isCryptoKeyPair as l, isSymmetricJWK as m, base64Decode as n, concatUint8Arrays as o, isPublicJWK as p, base64Encode as r, isAsymmetricJWK as s, assertCryptoKey as t, isJWK as u, textEncoder as v, computeMaxTokenAgeSeconds as w, applyTypCtyDefaults as x, utils_exports as y };
379
+ export { validateCriticalHeadersJWS as A, computeMaxTokenAgeSeconds as C, getPlaintextBytes as D, decodePayloadFromBytes as E, JWTError as M, isJWTError as N, isJWTContent as O, computeJwtTimeClaims as S, decodePayloadFromB64UrlSegment as T, textDecoder as _, base64UrlEncode as a, applyTypCtyDefaults as b, isCryptoKey as c, isJWKSet as d, isPrivateJWK as f, randomBytes as g, maybeArray as h, base64UrlDecode as i, validateJwtClaims as j, validateCriticalHeadersJWE as k, isCryptoKeyPair as l, isSymmetricJWK as m, base64Decode as n, concatUint8Arrays as o, isPublicJWK as p, base64Encode as r, isAsymmetricJWK as s, assertCryptoKey as t, isJWK as u, textEncoder as v, decodeMaybeJWTString as w, computeExpiresInSeconds as x, sanitizeObject as y };
@@ -1,4 +1,4 @@
1
- import { C as JWTClaims, R as JWKSet, S as JWTClaimValidationOptions, _ as JWSProtectedHeader, a as ExpiresIn, dt as JWK_Symmetric, it as JWK_Public, l as JWEEncryptOptions, p as StrictOmit, rt as JWK_Private, u as JWEHeaderParameters, v as JWSSignOptions } from "./jwk.mjs";
1
+ import { $ as JWK_HMAC, A as GenerateJWKReturn, B as JWKParameters, C as JWTClaims, D as DeriveKeyOptions, E as ContentEncryptionAlgorithm, F as JWKAlgorithm, G as JWK_AES_KW, H as JWK_AES_CBC_HMAC, I as JWKCacheAdapter, J as JWK_EC, K as JWK_Asymmetric, L as JWKLookupFunction, M as GenerateKeyOptions, N as GenerateKeyReturn, O as DeriveKeyReturn, P as JWK, Q as JWK_EC_Public, R as JWKLookupFunctionHeader, S as JWTClaimValidationOptions, T as CompositeKey, U as JWK_AES_GCM, V as JWKSet, W as JWK_AES_GCM_KW, X as JWK_ECDSA, Y as JWK_ECDH_ES, Z as JWK_EC_Private, _ as JWSSignOptions, _t as KeyManagementAlgorithm, a as ExpiresIn, at as JWK_Pair, b as JOSEPayload, bt as WrapKeyResult, c as JWEDecryptResult, ct as JWK_RSA, d as JWEKeyManagementHeaderParameters, dt as JWK_RSA_Private, et as JWK_OKP, f as JWEProtectedHeader, ft as JWK_RSA_Public, g as JWSProtectedHeader, gt as JWK_oct, h as JWSHeaderParameters, ht as JWK_Symmetric_Algorithm, i as importFromPEM, it as JWK_PBES2, j as GenerateKeyAlgorithm, k as GenerateJWKOptions, l as JWEEncryptOptions, lt as JWK_RSA_ENC, m as JWSAlgorithm, mt as JWK_Symmetric, n as exportToPEM, nt as JWK_OKP_Public, o as MaxTokenAge, ot as JWK_Private, p as StrictOmit, pt as JWK_RSA_SIGN, q as JWK_Asymmetric_Algorithm, r as generateJWK, rt as JWK_OKP_SIGN, s as JWEDecryptOptions, st as JWK_Public, t as deriveJWKFromPassword, tt as JWK_OKP_Private, u as JWEHeaderParameters, ut as JWK_RSA_PSS, v as JWSVerifyOptions, vt as UnwrapKeyOptions, w as JoseHeaderParameters, x as JWT, y as JWSVerifyResult, yt as WrapKeyOptions, z as JWKPEMAlgorithm } from "../../_chunks/jwk.mjs";
2
2
  import { H3Event } from "h3";
3
3
  import { CookieSerializeOptions } from "cookie-es";
4
4
 
@@ -14,8 +14,13 @@ interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMa
14
14
  readonly token: string | undefined;
15
15
  update: (update?: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge> & {
16
16
  readonly id: string;
17
+ readonly token: string;
18
+ }>;
19
+ clear: () => Promise<SessionManager<T, ConfigMaxAge> & {
20
+ readonly id: undefined;
21
+ readonly createdAt: number;
22
+ readonly expiresAt: undefined;
17
23
  }>;
18
- clear: () => Promise<SessionManager<T, ConfigMaxAge>>;
19
24
  }
20
25
  //#endregion
21
26
  //#region src/adapters/h3v1/session/jwe.d.ts
@@ -25,47 +30,59 @@ interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge exten
25
30
  createdAt: number;
26
31
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
27
32
  data: SessionData<T>;
33
+ token: string | undefined;
28
34
  [kGetSessionPromise$1]?: Promise<SessionJWE<T, MaxAge>>;
29
35
  }
30
- interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
36
+ interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event> {
31
37
  onRead?: (args: {
32
- session: SessionJWE<T, MaxAge>;
33
- event: H3Event;
34
- config: SessionConfigJWE<T, MaxAge>;
38
+ session: SessionJWE<T, MaxAge> & {
39
+ id: string;
40
+ token: string;
41
+ };
42
+ event: TEvent;
43
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
35
44
  }) => void | Promise<void>;
36
45
  onUpdate?: (args: {
37
- session: SessionJWE<T, MaxAge>;
46
+ /** Session after it has been updated.. */session: SessionJWE<T, MaxAge> & {
47
+ id: string;
48
+ token: string;
49
+ }; /** Snapshot of the session before was updated. */
38
50
  oldSession: SessionJWE<T, MaxAge>;
39
- event: H3Event;
40
- config: SessionConfigJWE<T, MaxAge>;
51
+ event: TEvent;
52
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
41
53
  }) => void | Promise<void>;
42
54
  onClear?: (args: {
43
- session: SessionJWE<T, MaxAge> | undefined;
44
- event: H3Event;
45
- config: Partial<SessionConfigJWE<T, MaxAge>>;
55
+ oldSession: SessionJWE<T, MaxAge> | undefined;
56
+ event: TEvent;
57
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
46
58
  }) => void | Promise<void>;
47
59
  onExpire?: (args: {
48
- event: H3Event;
60
+ session: {
61
+ id: string | undefined;
62
+ createdAt: number | undefined;
63
+ expiresAt: number | undefined;
64
+ token: string;
65
+ };
66
+ event: TEvent;
49
67
  error: Error;
50
- config: SessionConfigJWE<T, MaxAge>;
68
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
51
69
  }) => void | Promise<void>;
52
70
  onError?: (args: {
53
- event: H3Event;
71
+ /**
72
+ * The session involved in the error.
73
+ */
74
+ session: SessionJWE<T, MaxAge>;
75
+ event: TEvent;
54
76
  error: any;
55
- config: SessionConfigJWE<T, MaxAge>;
77
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
56
78
  }) => void | Promise<void>;
57
79
  onUnsealKeyLookup?: (args: {
58
80
  header: JWEHeaderParameters;
59
- event: H3Event;
60
- config: SessionConfigJWE<T, MaxAge>;
81
+ event: TEvent;
82
+ config: SessionConfigJWE<T, MaxAge, TEvent>;
61
83
  }) => JWK_Symmetric | JWK_Private | Promise<JWK_Symmetric | JWK_Private>;
62
84
  }
63
- interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
64
- /** @deprecated use `key` instead */
65
- secret?: string | JWK_Symmetric | {
66
- privateKey: JWK_Private | JWK_Symmetric;
67
- publicKey?: JWK_Public;
68
- };
85
+ interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event> {
69
86
  /** Shared key used, string for PBES2 or Json Web Key (JWK) */
70
87
  key: string | JWK_Symmetric | JWK_Private | {
71
88
  privateKey: JWK_Private | JWK_Symmetric;
@@ -86,7 +103,7 @@ interface SessionConfigJWE<T extends Record<string, any> = SessionClaims, MaxAge
86
103
  encryptOptions?: Omit<JWEEncryptOptions, "expiresIn">;
87
104
  decryptOptions?: JWTClaimValidationOptions;
88
105
  };
89
- hooks?: SessionHooksJWE<T, MaxAge>;
106
+ hooks?: SessionHooksJWE<T, MaxAge, TEvent>;
90
107
  }
91
108
  type CompatEvent$1 = {
92
109
  request: {
@@ -100,16 +117,19 @@ type CompatEvent$1 = {
100
117
  /**
101
118
  * Create a session manager for the current request.
102
119
  */
103
- declare function useJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<SessionManager<T, MaxAge>>;
120
+ declare function useJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
104
121
  /**
105
122
  * Get (and lazily initialize) the session for the current request.
106
123
  */
107
- declare function getJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<SessionJWE<T, MaxAge>>;
108
- declare function getJWESessionToken<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): string | undefined;
124
+ declare function getJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<SessionJWE<T, MaxAge>>;
125
+ declare function getJWESessionToken<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): string | undefined;
109
126
  /**
110
127
  * Update the session (optionally mutating the session data) and re-issue the JWE token.
111
128
  */
112
- declare function updateJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: SessionConfigJWE<T, MaxAge>, update?: SessionUpdate<T>): Promise<SessionJWE<T, MaxAge>>;
129
+ declare function updateJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, update?: SessionUpdate<T>): Promise<SessionJWE<T, MaxAge> & {
130
+ id: string;
131
+ token: string;
132
+ }>;
113
133
  /**
114
134
  * Produce a JWE for the current session.
115
135
  * Payload structure:
@@ -120,15 +140,15 @@ declare function updateJWESession<T extends Record<string, any> = SessionClaims,
120
140
  * data: Record<string, any>;
121
141
  * }
122
142
  */
123
- declare function sealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>): Promise<string>;
143
+ declare function sealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<string>;
124
144
  /**
125
145
  * Decrypt the JWE and return a Session-compatible object.
126
146
  */
127
- declare function unsealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent$1, config: SessionConfigJWE<T, MaxAge>, sealed: string): Promise<Partial<SessionJWE<T, MaxAge>>>;
147
+ declare function unsealJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>, sealed: string): Promise<Partial<SessionJWE<T, MaxAge>>>;
128
148
  /**
129
149
  * Clear the session (delete from context and drop cookie).
130
150
  */
131
- declare function clearJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: Partial<SessionConfigJWE<T, MaxAge>>): Promise<void>;
151
+ declare function clearJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent$1 | H3Event = CompatEvent$1 | H3Event>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<void>;
132
152
  //#endregion
133
153
  //#region src/adapters/h3v1/session/jws.d.ts
134
154
  declare const kGetSessionPromise: unique symbol;
@@ -137,42 +157,59 @@ interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge exten
137
157
  createdAt: number;
138
158
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
139
159
  data: SessionData<T>;
160
+ token: string | undefined;
140
161
  [kGetSessionPromise]?: Promise<SessionJWS<T, MaxAge>>;
141
162
  }
142
- interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
163
+ interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> {
143
164
  onRead?: (args: {
144
- session: SessionJWS<T, MaxAge>;
145
- event: H3Event;
146
- config: SessionConfigJWS<T, MaxAge>;
165
+ session: SessionJWS<T, MaxAge> & {
166
+ id: string;
167
+ token: string;
168
+ };
169
+ event: TEvent;
170
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
147
171
  }) => void | Promise<void>;
148
172
  onUpdate?: (args: {
149
- session: SessionJWS<T, MaxAge>;
173
+ /** Session after it has been updated.. */session: SessionJWS<T, MaxAge> & {
174
+ id: string;
175
+ token: string;
176
+ }; /** Snapshot of the session before was updated. */
150
177
  oldSession: SessionJWS<T, MaxAge>;
151
- event: H3Event;
152
- config: SessionConfigJWS<T, MaxAge>;
178
+ event: TEvent;
179
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
153
180
  }) => void | Promise<void>;
154
181
  onClear?: (args: {
155
- session: SessionJWS<T, MaxAge> | undefined;
156
- event: H3Event;
157
- config: Partial<SessionConfigJWS<T, MaxAge>>;
182
+ oldSession: SessionJWS<T, MaxAge> | undefined;
183
+ event: TEvent;
184
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
158
185
  }) => void | Promise<void>;
159
186
  onExpire?: (args: {
160
- event: H3Event;
187
+ session: {
188
+ id: string | undefined;
189
+ createdAt: number | undefined;
190
+ expiresAt: number | undefined;
191
+ token: string;
192
+ };
193
+ event: TEvent;
161
194
  error: Error;
162
- config: SessionConfigJWS<T, MaxAge>;
195
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
163
196
  }) => void | Promise<void>;
164
197
  onError?: (args: {
165
- event: H3Event;
198
+ /**
199
+ * The session involved in the error.
200
+ */
201
+ session: SessionJWS<T, MaxAge>;
202
+ event: TEvent;
166
203
  error: any;
167
- config: SessionConfigJWS<T, MaxAge>;
204
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
168
205
  }) => void | Promise<void>;
169
206
  onVerifyKeyLookup?: (args: {
170
- header: JWSProtectedHeader;
171
- event: H3Event;
172
- config: SessionConfigJWS<T, MaxAge>;
207
+ header: JWKLookupFunctionHeader;
208
+ event: TEvent;
209
+ config: SessionConfigJWS<T, MaxAge, TEvent>;
173
210
  }) => JWKSet | JWK_Symmetric | JWK_Public | Promise<JWKSet | JWK_Symmetric | JWK_Public>;
174
211
  }
175
- interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
212
+ interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event> {
176
213
  /**
177
214
  * JWK (private for signing with RS/ES/PS, or symmetric oct) used for signing.
178
215
  */
@@ -195,7 +232,7 @@ interface SessionConfigJWS<T extends Record<string, any> = SessionClaims, MaxAge
195
232
  signOptions?: Omit<JWSSignOptions, "expiresIn">;
196
233
  verifyOptions?: JWTClaimValidationOptions;
197
234
  };
198
- hooks?: SessionHooksJWS<T, MaxAge>;
235
+ hooks?: SessionHooksJWS<T, MaxAge, TEvent>;
199
236
  }
200
237
  type CompatEvent = {
201
238
  request: {
@@ -210,21 +247,24 @@ type CompatEvent = {
210
247
  * Create a session manager for the current request using JWS (signed only, not encrypted).
211
248
  * NOTE: Contents are visible to clients (Base64URL-decoded JSON). Do not store secrets in session data.
212
249
  */
213
- declare function useJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<SessionManager<T, MaxAge>>;
250
+ declare function useJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionManager<T, MaxAge>>;
214
251
  /**
215
252
  * Retrieve (and lazily initialize) the session.
216
253
  */
217
- declare function getJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<SessionJWS<T, MaxAge>>;
254
+ declare function getJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<SessionJWS<T, MaxAge>>;
218
255
  /**
219
256
  * Update session data (if provided) and reissue JWS.
220
257
  */
221
- declare function updateJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: SessionConfigJWS<T, MaxAge>, update?: SessionUpdate<T>): Promise<SessionJWS<T, MaxAge>>;
258
+ declare function updateJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, update?: SessionUpdate<T>): Promise<SessionJWS<T, MaxAge> & {
259
+ id: string;
260
+ token: string;
261
+ }>;
222
262
  /**
223
263
  * Sign current session as a compact JWS.
224
264
  * Payload claims:
225
265
  * jti, iat, exp?, data, plus optional extraClaims (cannot override reserved)
226
266
  */
227
- declare function signJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>): Promise<string>;
267
+ declare function signJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<string>;
228
268
  /**
229
269
  * Verify and parse a compact JWS into a Session structure.
230
270
  * Performs:
@@ -233,10 +273,10 @@ declare function signJWSSession<T extends Record<string, any> = SessionClaims, M
233
273
  * - ensures jti & iat presence
234
274
  * - enforces maxAge if provided
235
275
  */
236
- declare function verifyJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event | CompatEvent, config: SessionConfigJWS<T, MaxAge>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
276
+ declare function verifyJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
237
277
  /**
238
278
  * Destroy the session (context + cookie).
239
279
  */
240
- declare function clearJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined>(event: H3Event, config: Partial<SessionConfigJWS<T, MaxAge>>): Promise<void>;
280
+ declare function clearJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends CompatEvent | H3Event = CompatEvent | H3Event>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<void>;
241
281
  //#endregion
242
- export { SessionUpdate as C, SessionManager as S, unsealJWESession as _, getJWSSession as a, SessionClaims as b, useJWSSession as c, SessionHooksJWE as d, SessionJWE as f, sealJWESession as g, getJWESessionToken as h, clearJWSSession as i, verifyJWSSession as l, getJWESession as m, SessionHooksJWS as n, signJWSSession as o, clearJWESession as p, SessionJWS as r, updateJWSSession as s, SessionConfigJWS as t, SessionConfigJWE as u, updateJWESession as v, SessionData as x, useJWESession as y };
282
+ 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, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, type SessionClaims, type SessionConfigJWE, type SessionConfigJWS, type SessionData, type SessionHooksJWE, type SessionHooksJWS, type SessionJWE, type SessionJWS, type SessionManager, type SessionUpdate, UnwrapKeyOptions, WrapKeyOptions, WrapKeyResult, clearJWESession, clearJWSSession, deriveJWKFromPassword, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };