unjwt 0.6.1 → 0.7.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.
@@ -1,7 +1,9 @@
1
- import { a as deriveJWKFromPassword, c as importFromPEM, d as isSymmetricJWK, f as computeExpiresInSeconds, i as encrypt, l as isPrivateJWK, m as isJWTError, n as verify, o as exportToPEM, p as JWTError, r as decrypt, s as generateJWK, t as sign, u as isPublicJWK } from "../../_chunks/jws.mjs";
1
+ import { D as JWTError, O as isJWTError, c as isPrivateJWK, l as isPublicJWK, p as computeDurationInSeconds, u as isSymmetricJWK } from "../../_chunks/utils.mjs";
2
+ import { c as exportPEM, g as importPEM, i as deriveJWKFromPassword, l as exportToPEM, m as importFromPEM, u as generateJWK } from "../../_chunks/jwk.mjs";
3
+ import { n as verify, t as sign } from "../../_chunks/jws.mjs";
4
+ import { n as encrypt, t as decrypt } from "../../_chunks/jwe.mjs";
2
5
  import { isEvent, setCookie } from "h3";
3
6
  import { parse } from "cookie-es";
4
- //#region src/adapters/h3v1/session/jwe.ts
5
7
  const kGetSessionPromise$1 = Symbol("h3_jwe_getSession");
6
8
  const DEFAULT_NAME$1 = "h3-jwe";
7
9
  const DEFAULT_COOKIE$1 = {
@@ -9,9 +11,6 @@ const DEFAULT_COOKIE$1 = {
9
11
  secure: true,
10
12
  httpOnly: true
11
13
  };
12
- /**
13
- * Create a session manager for the current request.
14
- */
15
14
  async function useJWESession(event, config) {
16
15
  const sessionName = config.name || DEFAULT_NAME$1;
17
16
  await getJWESession(event, config);
@@ -42,20 +41,12 @@ async function useJWESession(event, config) {
42
41
  };
43
42
  return sessionManager;
44
43
  }
45
- /**
46
- * Get (and lazily initialize) the session for the current request.
47
- */
48
44
  async function getJWESession(event, config) {
49
45
  const sessionName = config.name || DEFAULT_NAME$1;
50
46
  if (!event.context.sessions) event.context.sessions = Object.create(null);
51
47
  const existingSession = event.context.sessions[sessionName];
52
48
  if (existingSession) {
53
49
  const session = existingSession[kGetSessionPromise$1] ? await existingSession[kGetSessionPromise$1] : existingSession;
54
- /**
55
- * We check if a session is expired before returning it. If it is expired we clear it and create a new one,
56
- * unless we have a read-only event in which case we just return it as it was valid at the time of reading
57
- * the cookie/header (like in a websocket upgrade)
58
- */
59
50
  if (session.expiresAt !== void 0 && session.expiresAt < Date.now()) {
60
51
  await config.hooks?.onExpire?.({
61
52
  session: {
@@ -84,7 +75,7 @@ async function getJWESession(event, config) {
84
75
  const freshSession = {
85
76
  id: void 0,
86
77
  createdAt: freshCreatedAt,
87
- expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeExpiresInSeconds(config.maxAge) * 1e3,
78
+ expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeDurationInSeconds(config.maxAge) * 1e3,
88
79
  data: Object.create(null),
89
80
  token: void 0
90
81
  };
@@ -103,7 +94,7 @@ async function getJWESession(event, config) {
103
94
  const session = {
104
95
  id: void 0,
105
96
  createdAt,
106
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
97
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3,
107
98
  data: Object.create(null),
108
99
  token: void 0
109
100
  };
@@ -176,9 +167,6 @@ function _getResHeader$1(event, name) {
176
167
  if (event.node) return event.node?.res.getHeader(name);
177
168
  if (event.response) return event.response.headers?.get(name);
178
169
  }
179
- /**
180
- * Update the session (optionally mutating the session data) and re-issue the JWE token.
181
- */
182
170
  async function updateJWESession(event, config, update) {
183
171
  const canWriteCookie = isEvent(event);
184
172
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot update session on read-only event.");
@@ -195,7 +183,7 @@ async function updateJWESession(event, config, update) {
195
183
  Object.assign(session, {
196
184
  id: config.generateId?.() || crypto.randomUUID(),
197
185
  createdAt,
198
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
186
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3
199
187
  });
200
188
  let sealed;
201
189
  try {
@@ -219,7 +207,7 @@ async function updateJWESession(event, config, update) {
219
207
  if (config.cookie !== false && canWriteCookie) setCookie(event, sessionName, sealed, {
220
208
  ...DEFAULT_COOKIE$1,
221
209
  ...config.cookie,
222
- expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3)
210
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeDurationInSeconds(config.maxAge) * 1e3)
223
211
  });
224
212
  await config.hooks?.onUpdate?.({
225
213
  session,
@@ -229,16 +217,6 @@ async function updateJWESession(event, config, update) {
229
217
  });
230
218
  return session;
231
219
  }
232
- /**
233
- * Produce a JWE for the current session.
234
- * Payload structure:
235
- * {
236
- * jti: string;
237
- * iat: number; (seconds)
238
- * exp?: number; (seconds)
239
- * data: Record<string, any>;
240
- * }
241
- */
242
220
  async function sealJWESession(event, config) {
243
221
  const key = getEncryptKey(config.key);
244
222
  const sessionName = config.name || DEFAULT_NAME$1;
@@ -264,9 +242,6 @@ async function sealJWESession(event, config) {
264
242
  }
265
243
  });
266
244
  }
267
- /**
268
- * Decrypt the JWE and return a Session-compatible object.
269
- */
270
245
  async function unsealJWESession(event, config, sealed) {
271
246
  const key = config.hooks?.onUnsealKeyLookup ? (header) => config.hooks.onUnsealKeyLookup({
272
247
  header,
@@ -305,9 +280,6 @@ async function unsealJWESession(event, config, sealed) {
305
280
  data: data && typeof data === "object" ? data : Object.create(null)
306
281
  };
307
282
  }
308
- /**
309
- * Clear the session (delete from context and drop cookie).
310
- */
311
283
  async function clearJWESession(event, config) {
312
284
  const canWriteCookie = isEvent(event);
313
285
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot clear session on read-only event.");
@@ -355,8 +327,6 @@ function findSetCookie$1(setCookie, name) {
355
327
  const match = setCookie.match(regex);
356
328
  return match ? match[1] : void 0;
357
329
  }
358
- //#endregion
359
- //#region src/adapters/h3v1/session/jws.ts
360
330
  const kGetSessionPromise = Symbol("h3_jws_getSession");
361
331
  const DEFAULT_NAME = "h3-jws";
362
332
  const DEFAULT_COOKIE = {
@@ -364,10 +334,6 @@ const DEFAULT_COOKIE = {
364
334
  secure: true,
365
335
  httpOnly: false
366
336
  };
367
- /**
368
- * Create a session manager for the current request using JWS (signed only, not encrypted).
369
- * NOTE: Contents are visible to clients (Base64URL-decoded JSON). Do not store secrets in session data.
370
- */
371
337
  async function useJWSSession(event, config) {
372
338
  const sessionName = config.name || DEFAULT_NAME;
373
339
  await getJWSSession(event, config);
@@ -398,20 +364,12 @@ async function useJWSSession(event, config) {
398
364
  };
399
365
  return sessionManager;
400
366
  }
401
- /**
402
- * Retrieve (and lazily initialize) the session.
403
- */
404
367
  async function getJWSSession(event, config) {
405
368
  const sessionName = config.name || DEFAULT_NAME;
406
369
  if (!event.context.sessions) event.context.sessions = Object.create(null);
407
370
  const existingSession = event.context.sessions[sessionName];
408
371
  if (existingSession) {
409
372
  const session = existingSession[kGetSessionPromise] ? await existingSession[kGetSessionPromise] : existingSession;
410
- /**
411
- * We check if a session is expired before returning it. If it is expired we clear it and create a new one,
412
- * unless we have a read-only event in which case we just return it as it was valid at the time of reading
413
- * the cookie/header (like in a websocket upgrade)
414
- */
415
373
  if (session.expiresAt !== void 0 && session.expiresAt < Date.now()) {
416
374
  await config.hooks?.onExpire?.({
417
375
  session: {
@@ -440,7 +398,7 @@ async function getJWSSession(event, config) {
440
398
  const freshSession = {
441
399
  id: void 0,
442
400
  createdAt: freshCreatedAt,
443
- expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeExpiresInSeconds(config.maxAge) * 1e3,
401
+ expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeDurationInSeconds(config.maxAge) * 1e3,
444
402
  data: Object.create(null),
445
403
  token: void 0
446
404
  };
@@ -459,7 +417,7 @@ async function getJWSSession(event, config) {
459
417
  const session = {
460
418
  id: void 0,
461
419
  createdAt,
462
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
420
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3,
463
421
  data: Object.create(null),
464
422
  token: void 0
465
423
  };
@@ -532,9 +490,6 @@ function _getResHeader(event, name) {
532
490
  if (event.node) return event.node?.res.getHeader(name);
533
491
  if (event.response) return event.response.headers?.get(name);
534
492
  }
535
- /**
536
- * Update session data (if provided) and reissue JWS.
537
- */
538
493
  async function updateJWSSession(event, config, update) {
539
494
  const canWriteCookie = isEvent(event);
540
495
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot update session on read-only event.");
@@ -551,7 +506,7 @@ async function updateJWSSession(event, config, update) {
551
506
  Object.assign(session, {
552
507
  id: config.generateId?.() || crypto.randomUUID(),
553
508
  createdAt,
554
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
509
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3
555
510
  });
556
511
  let token;
557
512
  try {
@@ -575,7 +530,7 @@ async function updateJWSSession(event, config, update) {
575
530
  if (config.cookie !== false && canWriteCookie) setCookie(event, sessionName, token, {
576
531
  ...DEFAULT_COOKIE,
577
532
  ...config.cookie,
578
- expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3)
533
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeDurationInSeconds(config.maxAge) * 1e3)
579
534
  });
580
535
  await config.hooks?.onUpdate?.({
581
536
  session,
@@ -585,11 +540,6 @@ async function updateJWSSession(event, config, update) {
585
540
  });
586
541
  return session;
587
542
  }
588
- /**
589
- * Sign current session as a compact JWS.
590
- * Payload claims:
591
- * jti, iat, exp?, data, plus optional extraClaims (cannot override reserved)
592
- */
593
543
  async function signJWSSession(event, config) {
594
544
  const key = getSignKey(config.key);
595
545
  const sessionName = config.name || DEFAULT_NAME;
@@ -615,14 +565,6 @@ async function signJWSSession(event, config) {
615
565
  }
616
566
  });
617
567
  }
618
- /**
619
- * Verify and parse a compact JWS into a Session structure.
620
- * Performs:
621
- * - cryptographic signature verification
622
- * - (optional) standard claim checks if validateJWT true
623
- * - ensures jti & iat presence
624
- * - enforces maxAge if provided
625
- */
626
568
  async function verifyJWSSession(event, config, token) {
627
569
  const alg = config.jws?.signOptions?.alg;
628
570
  const jwk = config.hooks?.onVerifyKeyLookup ? (header) => config.hooks.onVerifyKeyLookup({
@@ -656,9 +598,6 @@ async function verifyJWSSession(event, config, token) {
656
598
  data: data && typeof data === "object" ? data : Object.create(null)
657
599
  };
658
600
  }
659
- /**
660
- * Destroy the session (context + cookie).
661
- */
662
601
  async function clearJWSSession(event, config) {
663
602
  const canWriteCookie = isEvent(event);
664
603
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot clear session on read-only event.");
@@ -707,5 +646,4 @@ function findSetCookie(setCookie, name) {
707
646
  const match = setCookie.match(regex);
708
647
  return match ? match[1] : void 0;
709
648
  }
710
- //#endregion
711
- export { clearJWESession, clearJWSSession, deriveJWKFromPassword, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
649
+ export { clearJWESession, clearJWSSession, deriveJWKFromPassword, exportPEM, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, importPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
@@ -1,8 +1,7 @@
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";
1
+ import { $ as JWKAlgorithm, A as JWSSignOptions, At as JWK_RSA_Public, B as JWTClaimValidationOptions, C as JWSMultiSignOptions, Ct as JWK_Pair, D as JWSMultiVerifyOutcome, Dt as JWK_RSA_ENC, E as JWSMultiVerifyOptions, Et as JWK_RSA, Ft as KeyManagementAlgorithm, G as DeriveKeyOptions, H as JoseHeaderParameters, It as UnwrapKeyOptions, J as GenerateJWKReturn, K as DeriveKeyReturn, Lt as WrapKeyOptions, M as JWSVerifyResult, Mt as JWK_Symmetric, N as StrictOmit, Nt as JWK_Symmetric_Algorithm, O as JWSMultiVerifyResult, Ot as JWK_RSA_PSS, Pt as JWK_oct, Q as JWK, R as JOSEPayload, Rt as WrapKeyResult, S as JWSHeaderParameters, St as JWK_PBES2, T as JWSMultiVerifyAllOptions, Tt as JWK_Public, U as CompositeKey, V as JWTClaims, W as ContentEncryptionAlgorithm, X as GenerateKeyOptions, Y as GenerateKeyAlgorithm, Z as GenerateKeyReturn, _ as JWEProtectedHeader, _t as JWK_HMAC, a as JWEDecryptOptions, at as JWKSet, b as JWSGeneralSerialization, bt as JWK_OKP_Public, c as JWEFlattenedSerialization, ct as JWK_AES_GCM_KW, d as JWEHeaderParameters, dt as JWK_Asymmetric_Algorithm, et as JWKCacheAdapter, f as JWEKeyManagementHeaderParameters, ft as JWK_EC, g as JWEMultiRecipient, gt as JWK_EC_Public, h as JWEMultiEncryptOptions, ht as JWK_EC_Private, i as NotBeforeIn, it as JWKParameters, j as JWSVerifyOptions, jt as JWK_RSA_SIGN, k as JWSProtectedHeader, kt as JWK_RSA_Private, l as JWEGeneralRecipient, lt as JWK_AES_KW, m as JWEMultiDecryptResult, mt as JWK_ECDSA, n as ExpiresIn, nt as JWKLookupFunctionHeader, o as JWEDecryptResult, ot as JWK_AES_CBC_HMAC, p as JWEMultiDecryptOptions, pt as JWK_ECDH_ES, q as GenerateJWKOptions, r as MaxTokenAge, rt as JWKPEMAlgorithm, s as JWEEncryptOptions, st as JWK_AES_GCM, t as Duration, tt as JWKLookupFunction, u as JWEGeneralSerialization, ut as JWK_Asymmetric, v as JWSAlgorithm, vt as JWK_OKP, w as JWSMultiSigner, wt as JWK_Private, x as JWSGeneralSignature, xt as JWK_OKP_SIGN, y as JWSFlattenedSerialization, yt as JWK_OKP_Private, z as JWT } from "../../_chunks/index.mjs";
2
+ import { deriveJWKFromPassword, exportPEM, exportToPEM, generateJWK, importFromPEM, importPEM } from "../../core/jwk.mjs";
2
3
  import { H3Event, HTTPEvent } from "h3";
3
4
  import { CookieSerializeOptions } from "cookie-es";
4
-
5
- //#region src/adapters/h3v2/session/types.d.ts
6
5
  interface SessionClaims extends Required<Pick<JWTClaims, "jti" | "iat">>, StrictOmit<JWTClaims, "jti" | "iat"> {}
7
6
  type SessionData<T extends Record<string, any> = SessionClaims> = StrictOmit<T, "jti" | "iat" | "exp">;
8
7
  type SessionUpdate<T extends Record<string, any> = SessionClaims> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
@@ -15,12 +14,13 @@ interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMa
15
14
  update: (update?: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge>>;
16
15
  clear: () => Promise<SessionManager<T, ConfigMaxAge>>;
17
16
  }
18
- //#endregion
19
- //#region src/adapters/h3v2/session/jwe.d.ts
20
17
  declare const kGetSessionPromise$1: unique symbol;
21
18
  interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
19
+ /** Session ID — mirrors the JWT `jti` claim. `undefined` until the session is persisted. */
22
20
  id: string | undefined;
21
+ /** Session creation time in ms — mirrors the JWT `iat` claim (seconds). */
23
22
  createdAt: number;
23
+ /** Session expiry time in ms — mirrors the JWT `exp` claim (seconds). */
24
24
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
25
25
  data: SessionData<T>;
26
26
  token: string | undefined;
@@ -125,12 +125,13 @@ declare function unsealJWESession<T extends Record<string, any> = SessionClaims,
125
125
  * Clear the session (delete from context and drop cookie).
126
126
  */
127
127
  declare function clearJWESession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends HTTPEvent = HTTPEvent>(event: TEvent, config: SessionConfigJWE<T, MaxAge, TEvent>): Promise<void>;
128
- //#endregion
129
- //#region src/adapters/h3v2/session/jws.d.ts
130
128
  declare const kGetSessionPromise: unique symbol;
131
129
  interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
130
+ /** Session ID — mirrors the JWT `jti` claim. `undefined` until the session is persisted. */
132
131
  id: string | undefined;
132
+ /** Session creation time in ms — mirrors the JWT `iat` claim (seconds). */
133
133
  createdAt: number;
134
+ /** Session expiry time in ms — mirrors the JWT `exp` claim (seconds). */
134
135
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
135
136
  data: SessionData<T>;
136
137
  token: string | undefined;
@@ -209,5 +210,4 @@ declare function updateJWSSession<T extends Record<string, any> = SessionClaims,
209
210
  declare function signJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends HTTPEvent = HTTPEvent>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<string>;
210
211
  declare function verifyJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends HTTPEvent = HTTPEvent>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>, token: string): Promise<Partial<SessionJWS<T, MaxAge>>>;
211
212
  declare function clearJWSSession<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined, TEvent extends HTTPEvent = HTTPEvent>(event: TEvent, config: SessionConfigJWS<T, MaxAge, TEvent>): Promise<void>;
212
- //#endregion
213
- 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 };
213
+ export { CompositeKey, ContentEncryptionAlgorithm, DeriveKeyOptions, DeriveKeyReturn, Duration, ExpiresIn, GenerateJWKOptions, GenerateJWKReturn, GenerateKeyAlgorithm, GenerateKeyOptions, GenerateKeyReturn, JOSEPayload, JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralRecipient, JWEGeneralSerialization, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, 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, JWSFlattenedSerialization, JWSGeneralSerialization, JWSGeneralSignature, JWSHeaderParameters, JWSMultiSignOptions, JWSMultiSigner, JWSMultiVerifyAllOptions, JWSMultiVerifyOptions, JWSMultiVerifyOutcome, JWSMultiVerifyResult, JWSProtectedHeader, JWSSignOptions, JWSVerifyOptions, JWSVerifyResult, JWT, JWTClaimValidationOptions, JWTClaims, JoseHeaderParameters, KeyManagementAlgorithm, MaxTokenAge, NotBeforeIn, 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, exportPEM, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, importPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
@@ -1,7 +1,9 @@
1
- import { a as deriveJWKFromPassword, c as importFromPEM, d as isSymmetricJWK, f as computeExpiresInSeconds, i as encrypt, l as isPrivateJWK, m as isJWTError, n as verify, o as exportToPEM, p as JWTError, r as decrypt, s as generateJWK, t as sign, u as isPublicJWK } from "../../_chunks/jws.mjs";
1
+ import { D as JWTError, O as isJWTError, c as isPrivateJWK, l as isPublicJWK, p as computeDurationInSeconds, u as isSymmetricJWK } from "../../_chunks/utils.mjs";
2
+ import { c as exportPEM, g as importPEM, i as deriveJWKFromPassword, l as exportToPEM, m as importFromPEM, u as generateJWK } from "../../_chunks/jwk.mjs";
3
+ import { n as verify, t as sign } from "../../_chunks/jws.mjs";
4
+ import { n as encrypt, t as decrypt } from "../../_chunks/jwe.mjs";
5
+ import { t as NullProtoObj } from "../../_chunks/libs/rou3.mjs";
2
6
  import { getChunkedCookie, getEventContext, setChunkedCookie } from "h3";
3
- import { NullProtoObj } from "rou3";
4
- //#region src/adapters/h3v2/session/jwe.ts
5
7
  const kGetSessionPromise$1 = Symbol("h3_jwe_getSession");
6
8
  const DEFAULT_NAME$1 = "h3-jwe";
7
9
  const DEFAULT_COOKIE$1 = {
@@ -9,9 +11,6 @@ const DEFAULT_COOKIE$1 = {
9
11
  secure: true,
10
12
  httpOnly: true
11
13
  };
12
- /**
13
- * Create a session manager for the current request.
14
- */
15
14
  async function useJWESession(event, config) {
16
15
  const sessionName = config.name || DEFAULT_NAME$1;
17
16
  await getJWESession(event, config);
@@ -42,9 +41,6 @@ async function useJWESession(event, config) {
42
41
  };
43
42
  return sessionManager;
44
43
  }
45
- /**
46
- * Get the session for the current request.
47
- */
48
44
  async function getJWESession(event, config) {
49
45
  const sessionName = config.name || DEFAULT_NAME$1;
50
46
  const context = getEventContext(event);
@@ -52,11 +48,6 @@ async function getJWESession(event, config) {
52
48
  const existingSession = context.sessions[sessionName];
53
49
  if (existingSession) {
54
50
  const session = existingSession[kGetSessionPromise$1] ? await existingSession[kGetSessionPromise$1] : existingSession;
55
- /**
56
- * We check if a session is expired before returning it. If it is expired we clear it and create a new one,
57
- * unless we have a read-only event in which case we just return it as it was valid at the time of reading
58
- * the cookie/header (like in a websocket upgrade)
59
- */
60
51
  if (session.expiresAt !== void 0 && session.expiresAt < Date.now()) {
61
52
  await config.hooks?.onExpire?.({
62
53
  session: {
@@ -85,7 +76,7 @@ async function getJWESession(event, config) {
85
76
  const freshSession = {
86
77
  id: void 0,
87
78
  createdAt: freshCreatedAt,
88
- expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeExpiresInSeconds(config.maxAge) * 1e3,
79
+ expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeDurationInSeconds(config.maxAge) * 1e3,
89
80
  data: new NullProtoObj(),
90
81
  token: void 0
91
82
  };
@@ -104,7 +95,7 @@ async function getJWESession(event, config) {
104
95
  const session = {
105
96
  id: void 0,
106
97
  createdAt,
107
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
98
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3,
108
99
  data: new NullProtoObj(),
109
100
  token: void 0
110
101
  };
@@ -162,9 +153,6 @@ function getJWESessionToken(event, config) {
162
153
  if (!token) token = getChunkedCookie(event, sessionName);
163
154
  return token;
164
155
  }
165
- /**
166
- * Update the session data for the current request.
167
- */
168
156
  async function updateJWESession(event, config, update) {
169
157
  const canWriteCookie = hasWritableResponse$1(event);
170
158
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot update session on read-only event.");
@@ -181,7 +169,7 @@ async function updateJWESession(event, config, update) {
181
169
  Object.assign(session, {
182
170
  id: config.generateId?.() || crypto.randomUUID(),
183
171
  createdAt,
184
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
172
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3
185
173
  });
186
174
  let sealed;
187
175
  try {
@@ -204,7 +192,7 @@ async function updateJWESession(event, config, update) {
204
192
  session.token = sealed;
205
193
  if (config.cookie !== false && canWriteCookie) setChunkedCookie(event, sessionName, sealed, {
206
194
  ...DEFAULT_COOKIE$1,
207
- expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3),
195
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeDurationInSeconds(config.maxAge) * 1e3),
208
196
  ...config.cookie
209
197
  });
210
198
  await config.hooks?.onUpdate?.({
@@ -215,9 +203,6 @@ async function updateJWESession(event, config, update) {
215
203
  });
216
204
  return session;
217
205
  }
218
- /**
219
- * Produce a JWE for the current session.
220
- */
221
206
  async function sealJWESession(event, config) {
222
207
  const key = getEncryptKey(config.key);
223
208
  const sessionName = config.name || DEFAULT_NAME$1;
@@ -242,9 +227,6 @@ async function sealJWESession(event, config) {
242
227
  }
243
228
  });
244
229
  }
245
- /**
246
- * Decrypt the JWE and return a Session-compatible object.
247
- */
248
230
  async function unsealJWESession(event, config, sealed) {
249
231
  const key = config.hooks?.onUnsealKeyLookup ? (header) => config.hooks.onUnsealKeyLookup({
250
232
  header,
@@ -282,9 +264,6 @@ async function unsealJWESession(event, config, sealed) {
282
264
  data: data && typeof data === "object" ? data : new NullProtoObj()
283
265
  };
284
266
  }
285
- /**
286
- * Clear the session (delete from context and drop cookie).
287
- */
288
267
  async function clearJWESession(event, config) {
289
268
  const canWriteCookie = hasWritableResponse$1(event);
290
269
  if (config.cookie !== false && !canWriteCookie) throw new Error("[unjwt/h3] Cannot clear session on read-only event.");
@@ -339,8 +318,6 @@ function findSetCookie$1(setCookie, name) {
339
318
  const match = setCookie.match(regex);
340
319
  return match ? match[1] : void 0;
341
320
  }
342
- //#endregion
343
- //#region src/adapters/h3v2/session/jws.ts
344
321
  const kGetSessionPromise = Symbol("h3_jws_getSession");
345
322
  const DEFAULT_NAME = "h3-jws";
346
323
  const DEFAULT_COOKIE = {
@@ -413,7 +390,7 @@ async function getJWSSession(event, config) {
413
390
  const freshSession = {
414
391
  id: void 0,
415
392
  createdAt: freshCreatedAt,
416
- expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeExpiresInSeconds(config.maxAge) * 1e3,
393
+ expiresAt: config.maxAge === void 0 ? void 0 : freshCreatedAt + computeDurationInSeconds(config.maxAge) * 1e3,
417
394
  data: new NullProtoObj(),
418
395
  token: void 0
419
396
  };
@@ -432,7 +409,7 @@ async function getJWSSession(event, config) {
432
409
  const session = {
433
410
  id: void 0,
434
411
  createdAt,
435
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
412
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3,
436
413
  data: new NullProtoObj(),
437
414
  token: void 0
438
415
  };
@@ -506,7 +483,7 @@ async function updateJWSSession(event, config, update) {
506
483
  Object.assign(session, {
507
484
  id: config.generateId?.() || crypto.randomUUID(),
508
485
  createdAt,
509
- expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
486
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeDurationInSeconds(config.maxAge) * 1e3
510
487
  });
511
488
  let token;
512
489
  try {
@@ -529,7 +506,7 @@ async function updateJWSSession(event, config, update) {
529
506
  session.token = token;
530
507
  if (config.cookie !== false && canWriteCookie) setChunkedCookie(event, sessionName, token, {
531
508
  ...DEFAULT_COOKIE,
532
- expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3),
509
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeDurationInSeconds(config.maxAge) * 1e3),
533
510
  ...config.cookie
534
511
  });
535
512
  await config.hooks?.onUpdate?.({
@@ -659,5 +636,4 @@ function findSetCookie(setCookie, name) {
659
636
  const match = setCookie.match(regex);
660
637
  return match ? match[1] : void 0;
661
638
  }
662
- //#endregion
663
- export { clearJWESession, clearJWSSession, deriveJWKFromPassword, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
639
+ export { clearJWESession, clearJWSSession, deriveJWKFromPassword, exportPEM, exportToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, importFromPEM, importPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
@@ -1,40 +1,3 @@
1
- import { A as JWK, L as JWKSet, N as JWKLookupFunction, S as ContentEncryptionAlgorithm, a as JWEEncryptOptions, c as JWEProtectedHeader, g as JOSEPayload, i as JWEDecryptResult, nt as JWK_Private, o as JWEHeaderParameters, pt as KeyManagementAlgorithm, r as JWEDecryptOptions, s as JWEKeyManagementHeaderParameters, 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/jwe.d.ts
5
- /**
6
- * Encrypts a payload to produce a JWE Compact Serialization string.
7
- *
8
- * @param payload The payload to encrypt. Can be a string, Uint8Array, or a JSON-serializable object.
9
- * @param key The key encryption key (KEK). Can be a CryptoKey, JWK, or for PBES2, a password string/Uint8Array.
10
- * @param options JWE encryption options, including `alg` (key management algorithm) and `enc` (content encryption algorithm).
11
- * @returns A Promise resolving to the JWE Compact Serialization string.
12
- */
13
- declare function encrypt(payload: JOSEPayload, key: JWK | string | Uint8Array<ArrayBuffer>, options?: JWEEncryptOptions): Promise<string>;
14
- declare function encrypt(payload: JOSEPayload, key: CryptoKey | JWK_Symmetric | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
15
- alg: "dir";
16
- enc: ContentEncryptionAlgorithm;
17
- }): Promise<string>;
18
- declare function encrypt(payload: JOSEPayload, key: CryptoKey, options: JWEEncryptOptions & {
19
- alg: KeyManagementAlgorithm;
20
- enc: ContentEncryptionAlgorithm;
21
- }): Promise<string>;
22
- declare function encrypt(payload: JOSEPayload, key: CryptoKey | JWK | string | Uint8Array<ArrayBuffer>, options: JWEEncryptOptions & {
23
- alg: KeyManagementAlgorithm;
24
- enc: ContentEncryptionAlgorithm;
25
- }): Promise<string>;
26
- /**
27
- * Decrypts a JWE Compact Serialization string.
28
- *
29
- * @param jwe The JWE Compact Serialization string.
30
- * @param key The key decryption key (KEK) or a function to look up the key.
31
- * @param options JWE decryption options.
32
- * @returns A Promise resolving to an object containing the decrypted plaintext, protected header, CEK, and AAD.
33
- * @throws If JWE is invalid, decryption fails, or options are not met.
34
- */
35
- declare function decrypt<T extends JOSEPayload>(jwe: string, key: CryptoKey | JWKSet | JWK_Private | JWK_Symmetric | string | Uint8Array<ArrayBuffer> | JWKLookupFunction, options?: JWEDecryptOptions): Promise<JWEDecryptResult<T>>;
36
- declare function decrypt(jwe: string, key: CryptoKey | JWKSet | JWK_Private | JWK_Symmetric | string | Uint8Array<ArrayBuffer> | JWKLookupFunction, options: JWEDecryptOptions & {
37
- forceUint8Array: true;
38
- }): Promise<JWEDecryptResult<Uint8Array<ArrayBuffer>>>;
39
- //#endregion
40
- export { JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEProtectedHeader, JWTError, type JWTErrorCauseMap, type JWTErrorCode, decrypt, encrypt, isJWTError };
1
+ import { F as JWTErrorCauseMap, I as JWTErrorCode, L as isJWTError, P as JWTError, _ as JWEProtectedHeader, a as JWEDecryptOptions, c as JWEFlattenedSerialization, d as JWEHeaderParameters, f as JWEKeyManagementHeaderParameters, g as JWEMultiRecipient, h as JWEMultiEncryptOptions, l as JWEGeneralRecipient, m as JWEMultiDecryptResult, o as JWEDecryptResult, p as JWEMultiDecryptOptions, s as JWEEncryptOptions, u as JWEGeneralSerialization } from "../_chunks/index.mjs";
2
+ import { a as generalToFlattened, i as encryptMulti, n as encrypt, r as decryptMulti, t as decrypt } from "../_chunks/jwe.mjs";
3
+ export { JWEDecryptOptions, JWEDecryptResult, JWEEncryptOptions, JWEFlattenedSerialization, JWEGeneralRecipient, JWEGeneralSerialization, JWEHeaderParameters, JWEKeyManagementHeaderParameters, JWEMultiDecryptOptions, JWEMultiDecryptResult, JWEMultiEncryptOptions, JWEMultiRecipient, JWEProtectedHeader, JWTError, JWTErrorCauseMap, JWTErrorCode, decrypt, decryptMulti, encrypt, encryptMulti, generalToFlattened, isJWTError };