unjwt 0.5.22 → 0.5.24

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,13 +1,13 @@
1
- export { k as deriveJWKFromPassword, h as exportJWKToPEM, c as generateJWK, f as importJWKFromPEM } from './shared/unjwt.Vg02PZWw.mjs';
1
+ export { k as deriveJWKFromPassword, h as exportJWKToPEM, c as generateJWK, f as importJWKFromPEM } from '../shared/unjwt.epPK1KFr.mjs';
2
2
  import { isEvent, setCookie } from 'h3';
3
3
  import { parse } from 'cookie-es';
4
- import { e as encrypt, d as decrypt } from './shared/unjwt.CXl9FD-V.mjs';
5
- import { p as isSymmetricJWK, q as isPrivateJWK, u as isAsymmetricJWK } from './shared/unjwt.DCiTNyCM.mjs';
6
- import { s as sign, v as verify } from './shared/unjwt.DmRAjMp8.mjs';
4
+ import { e as encrypt, d as decrypt } from '../shared/unjwt.CiBEuvtQ.mjs';
5
+ import { p as isSymmetricJWK, q as isPrivateJWK, u as isPublicJWK } from '../shared/unjwt.C86hA0o-.mjs';
6
+ import { s as sign, v as verify } from '../shared/unjwt.D-FATaw1.mjs';
7
7
 
8
8
  /**
9
9
  * This is a fork of h3 library's session utility functions.
10
- * @source https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/test/session.test.ts
10
+ * @source https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/src/utils/session.ts
11
11
  * @license MIT https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/LICENSE
12
12
  */
13
13
  const kGetSessionPromise$1 = Symbol("h3_jwe_getSession");
@@ -57,22 +57,15 @@ async function getJWESession(event, config) {
57
57
  }
58
58
  const existingSession = event.context.sessions[sessionName];
59
59
  if (existingSession) {
60
- if (existingSession.expiresAt === void 0) {
61
- const session2 = existingSession[kGetSessionPromise$1] || existingSession;
62
- await config.hooks?.onRead?.(await session2, event);
63
- return session2;
64
- } else {
65
- if (existingSession.expiresAt < Date.now() && isEvent(event)) {
66
- await config.hooks?.onExpire?.(event, void 0);
67
- return clearJWESession(event, config).then(
68
- () => getJWESession(event, config)
69
- );
70
- } else {
71
- const session2 = existingSession[kGetSessionPromise$1] || existingSession;
72
- await config.hooks?.onRead?.(await session2, event);
73
- return session2;
74
- }
60
+ const session2 = existingSession[kGetSessionPromise$1] ? await existingSession[kGetSessionPromise$1] : existingSession;
61
+ if (session2.expiresAt !== void 0 && session2.expiresAt < Date.now() && isEvent(event)) {
62
+ await config.hooks?.onExpire?.(event, void 0, config);
63
+ return clearJWESession(event, config).then(
64
+ () => getJWESession(event, config)
65
+ );
75
66
  }
67
+ await config.hooks?.onRead?.(session2, event, config);
68
+ return session2;
76
69
  }
77
70
  const session = {
78
71
  id: "",
@@ -97,14 +90,11 @@ async function getJWESession(event, config) {
97
90
  }
98
91
  if (jweToken) {
99
92
  const promise = unsealJWESession(event, config, jweToken).catch(async (error_) => {
100
- if (error_ instanceof Error) {
101
- const message = error_.message;
102
- if (message.includes("Token has expired") || message.includes("Token is too old")) {
103
- await config.hooks?.onExpire?.(event, error_);
104
- return void 0;
105
- }
93
+ if (error_ instanceof Error && (error_.message.includes("Token has expired") || error_.message.includes("Token is too old"))) {
94
+ await config.hooks?.onExpire?.(event, error_, config);
95
+ return void 0;
106
96
  }
107
- await config.hooks?.onError?.(event, error_);
97
+ await config.hooks?.onError?.(event, error_, config);
108
98
  return void 0;
109
99
  }).then((unsealed) => {
110
100
  if (unsealed) {
@@ -122,13 +112,12 @@ async function getJWESession(event, config) {
122
112
  "Cannot initialize a new session. Use `useSession(event)` within the main handler."
123
113
  );
124
114
  }
125
- session.id = config.generateId?.() ?? (config.crypto || crypto).randomUUID();
126
- session.createdAt = config.jwe?.encryptOptions?.currentDate === void 0 ? Date.now() : config.jwe.encryptOptions.currentDate.getTime();
115
+ session.id = config.generateId?.() ?? crypto.randomUUID();
116
+ session.createdAt = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
127
117
  session.expiresAt = config.maxAge ? session.createdAt + config.maxAge * 1e3 : void 0;
128
- session.data = /* @__PURE__ */ Object.create(null);
129
118
  await updateJWESession(event, config);
130
119
  }
131
- await config.hooks?.onRead?.(session, event);
120
+ await config.hooks?.onRead?.(session, event, config);
132
121
  return session;
133
122
  }
134
123
  function _getReqHeader$1(event, name) {
@@ -150,7 +139,7 @@ async function updateJWESession(event, config, update) {
150
139
  }
151
140
  if (update) {
152
141
  Object.assign(session.data, update);
153
- await config.hooks?.onUpdate?.(session, event);
142
+ await config.hooks?.onUpdate?.(session, event, config);
154
143
  }
155
144
  if (config.cookie !== false) {
156
145
  const sealed = await sealJWESession(event, config);
@@ -166,12 +155,7 @@ async function updateJWESession(event, config, update) {
166
155
  return session;
167
156
  }
168
157
  async function sealJWESession(event, config) {
169
- const key = getEncryptKey(config.secret);
170
- if (typeof key !== "string" && !isSymmetricJWK(key) && !isPrivateJWK(key)) {
171
- throw new Error(
172
- "Session: JWE secret must be a password string or a private JWK."
173
- );
174
- }
158
+ const key = getEncryptKey(config.key || config.secret);
175
159
  const sessionName = config.name || DEFAULT_NAME$1;
176
160
  const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
177
161
  const iatSeconds = Math.floor(session.createdAt / 1e3);
@@ -184,25 +168,30 @@ async function sealJWESession(event, config) {
184
168
  if (expSeconds) {
185
169
  payload.exp = expSeconds;
186
170
  }
171
+ let typ = void 0;
172
+ if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
173
+ typ = config.jwe.encryptOptions.protectedHeader.typ;
174
+ }
187
175
  const token = await encrypt(payload, key, {
188
176
  ...config.jwe?.encryptOptions,
177
+ expiresIn: void 0,
178
+ // controlled via 'exp' claim
189
179
  protectedHeader: {
190
180
  ...config.jwe?.encryptOptions?.protectedHeader,
191
- typ: "JWT",
181
+ kid: typeof key === "string" ? void 0 : key.kid,
182
+ typ: typ || "JWT",
192
183
  cty: "application/json"
193
184
  }
194
185
  });
195
186
  return token;
196
187
  }
197
188
  async function unsealJWESession(_event, config, sealed) {
198
- const key = getDecryptKey(config.secret);
199
- if (typeof key !== "string" && !isSymmetricJWK(key) && !isPrivateJWK(key)) {
200
- throw new Error(
201
- "Session: JWE secret must be a password string or a private JWK."
202
- );
203
- }
189
+ const key = getDecryptKey(config.key || config.secret);
204
190
  const alg = config.jwe?.encryptOptions?.alg;
205
191
  const enc = config.jwe?.encryptOptions?.enc;
192
+ if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
193
+ config.jwe.encryptOptions.protectedHeader.typ;
194
+ }
206
195
  const { payload } = await decrypt(sealed, key, {
207
196
  ...config.jwe?.decryptOptions,
208
197
  requiredClaims: [
@@ -226,12 +215,12 @@ async function unsealJWESession(_event, config, sealed) {
226
215
  }
227
216
  throw new Error(error_);
228
217
  });
229
- const { jti, iat, ...data } = payload;
218
+ const { jti, iat, exp, ...data } = payload;
230
219
  return {
231
220
  id: jti,
232
221
  createdAt: iat * 1e3,
233
222
  // Convert back to ms
234
- expiresAt: payload.exp ? payload.exp * 1e3 : void 0,
223
+ expiresAt: exp ? exp * 1e3 : void 0,
235
224
  data: data && typeof data === "object" ? data : /* @__PURE__ */ Object.create(null)
236
225
  };
237
226
  }
@@ -243,32 +232,57 @@ async function clearJWESession(event, config) {
243
232
  setCookie(event, sessionName, "", {
244
233
  ...DEFAULT_COOKIE$1,
245
234
  ...config.cookie,
246
- // Optionally set expires in the past
247
235
  expires: /* @__PURE__ */ new Date(0),
248
236
  maxAge: void 0
249
237
  });
250
- await config.hooks?.onClear?.(event);
238
+ await config.hooks?.onClear?.(event, config);
251
239
  }
252
- function getEncryptKey(secret) {
253
- if (typeof secret === "string") {
254
- return secret;
255
- } else if (isSymmetricJWK(secret)) {
256
- return secret;
240
+ function getEncryptKey(key) {
241
+ if (!key) {
242
+ throw new Error("Session: JWE key is required.");
243
+ }
244
+ let _key;
245
+ if (typeof key === "string") {
246
+ _key = key;
247
+ } else if (isSymmetricJWK(key) || isPrivateJWK(key)) {
248
+ _key = key;
249
+ } else if ("publicKey" in key && isPublicJWK(key.publicKey)) {
250
+ _key = key.publicKey;
251
+ } else if ("privateKey" in key && isPrivateJWK(key.privateKey)) {
252
+ _key = key.privateKey;
253
+ }
254
+ if (!_key) {
255
+ throw new Error(
256
+ "Session: Invalid JWE key. It must be a password string or valid JWK.",
257
+ { cause: key }
258
+ );
257
259
  }
258
- return secret.publicKey || secret.privateKey;
260
+ return _key;
259
261
  }
260
- function getDecryptKey(secret) {
261
- if (typeof secret === "string") {
262
- return secret;
263
- } else if ("privateKey" in secret) {
264
- return secret.privateKey;
262
+ function getDecryptKey(key) {
263
+ if (!key) {
264
+ throw new Error("Session: JWE key is required.");
265
+ }
266
+ let _key = void 0;
267
+ if (typeof key === "string") {
268
+ _key = key;
269
+ } else if (isSymmetricJWK(key)) {
270
+ _key = key;
271
+ } else if ("privateKey" in key) {
272
+ _key = key.privateKey;
273
+ }
274
+ if (!_key) {
275
+ throw new Error(
276
+ "Session: Invalid JWE key. It must be a password string or a valid private JWK.",
277
+ { cause: key }
278
+ );
265
279
  }
266
- return secret;
280
+ return _key;
267
281
  }
268
282
 
269
283
  /**
270
284
  * This is a fork of h3 library's session utility functions.
271
- * @source https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/test/session.test.ts
285
+ * @source https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/src/utils/session.ts
272
286
  * @license MIT https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/LICENSE
273
287
  */
274
288
  const kGetSessionPromise = Symbol("h3_jws_getSession");
@@ -318,22 +332,15 @@ async function getJWSSession(event, config) {
318
332
  }
319
333
  const existingSession = event.context.sessions[sessionName];
320
334
  if (existingSession) {
321
- if (existingSession.expiresAt === void 0) {
322
- const session2 = existingSession[kGetSessionPromise] || existingSession;
323
- await config.hooks?.onRead?.(await session2, event);
324
- return session2;
325
- } else {
326
- if (existingSession.expiresAt < Date.now() && isEvent(event)) {
327
- await config.hooks?.onExpire?.(event, void 0);
328
- return clearJWSSession(event, config).then(
329
- () => getJWSSession(event, config)
330
- );
331
- } else {
332
- const session2 = existingSession[kGetSessionPromise] || existingSession;
333
- await config.hooks?.onRead?.(await session2, event);
334
- return session2;
335
- }
335
+ const session2 = existingSession[kGetSessionPromise] ? await existingSession[kGetSessionPromise] : existingSession;
336
+ if (session2.expiresAt !== void 0 && session2.expiresAt < Date.now() && isEvent(event)) {
337
+ await config.hooks?.onExpire?.(event, void 0, config);
338
+ return clearJWSSession(event, config).then(
339
+ () => getJWSSession(event, config)
340
+ );
336
341
  }
342
+ await config.hooks?.onRead?.(session2, event, config);
343
+ return session2;
337
344
  }
338
345
  const session = {
339
346
  id: "",
@@ -358,14 +365,11 @@ async function getJWSSession(event, config) {
358
365
  }
359
366
  if (token) {
360
367
  const promise = verifyJWSSession(event, config, token).catch(async (error_) => {
361
- if (error_ instanceof Error) {
362
- const message = error_.message;
363
- if (message.includes("Token has expired") || message.includes("Token is too old")) {
364
- await config.hooks?.onExpire?.(event, error_);
365
- return void 0;
366
- }
368
+ if (error_ instanceof Error && (error_.message.includes("Token has expired") || error_.message.includes("Token is too old"))) {
369
+ await config.hooks?.onExpire?.(event, error_, config);
370
+ return void 0;
367
371
  }
368
- await config.hooks?.onError?.(event, error_);
372
+ await config.hooks?.onError?.(event, error_, config);
369
373
  return void 0;
370
374
  }).then((unsealed) => {
371
375
  if (unsealed) {
@@ -383,13 +387,12 @@ async function getJWSSession(event, config) {
383
387
  "Cannot initialize a new session outside main handler. Use `useJWSSession(event)` properly."
384
388
  );
385
389
  }
386
- session.id = config.generateId?.() ?? (config.crypto || globalThis.crypto).randomUUID();
387
- session.createdAt = config.jws?.signOptions?.currentDate === void 0 ? Date.now() : config.jws.signOptions.currentDate.getTime();
390
+ session.id = config.generateId?.() ?? crypto.randomUUID();
391
+ session.createdAt = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
388
392
  session.expiresAt = config.maxAge ? session.createdAt + config.maxAge * 1e3 : void 0;
389
- session.data = /* @__PURE__ */ Object.create(null);
390
393
  await updateJWSSession(event, config);
391
394
  }
392
- await config.hooks?.onRead?.(session, event);
395
+ await config.hooks?.onRead?.(session, event, config);
393
396
  return session;
394
397
  }
395
398
  function _getReqHeader(event, name) {
@@ -411,7 +414,7 @@ async function updateJWSSession(event, config, update) {
411
414
  }
412
415
  if (update) {
413
416
  Object.assign(session.data, update);
414
- await config.hooks?.onUpdate?.(session, event);
417
+ await config.hooks?.onUpdate?.(session, event, config);
415
418
  }
416
419
  if (config.cookie !== false) {
417
420
  const token = await signJWSSession(event, config);
@@ -425,9 +428,6 @@ async function updateJWSSession(event, config, update) {
425
428
  }
426
429
  async function signJWSSession(event, config) {
427
430
  const key = getSignKey(config.key);
428
- if (isAsymmetricJWK(key) && !isPrivateJWK(key)) {
429
- throw new Error("Session: JWS key cannot be a public asymmetric JWK.");
430
- }
431
431
  const sessionName = config.name || DEFAULT_NAME;
432
432
  const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
433
433
  const iatSeconds = Math.floor(session.createdAt / 1e3);
@@ -440,11 +440,19 @@ async function signJWSSession(event, config) {
440
440
  if (expSeconds) {
441
441
  payload.exp = expSeconds;
442
442
  }
443
+ let typ = void 0;
444
+ if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
445
+ typ = config.jws.signOptions.protectedHeader.typ;
446
+ }
443
447
  const token = await sign(payload, key, {
444
448
  ...config.jws?.signOptions,
449
+ expiresIn: void 0,
450
+ // controlled via 'exp' claim
445
451
  protectedHeader: {
446
452
  ...config.jws?.signOptions?.protectedHeader,
447
- typ: "JWT"
453
+ kid: key.kid,
454
+ typ: typ || "JWT",
455
+ cty: "application/json"
448
456
  }
449
457
  });
450
458
  return token;
@@ -452,8 +460,9 @@ async function signJWSSession(event, config) {
452
460
  async function verifyJWSSession(_event, config, token) {
453
461
  const alg = config.jws?.signOptions?.alg;
454
462
  const jwk = getVerifyKey(config.key);
455
- if (isAsymmetricJWK(jwk) && isPrivateJWK(jwk)) {
456
- throw new Error("Session: JWS key cannot be a private asymmetric JWK.");
463
+ let typ = void 0;
464
+ if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
465
+ typ = config.jws.signOptions.protectedHeader.typ;
457
466
  }
458
467
  const { payload } = await verify(token, jwk, {
459
468
  ...config.jws?.verifyOptions,
@@ -464,7 +473,7 @@ async function verifyJWSSession(_event, config, token) {
464
473
  "jti",
465
474
  "iat"
466
475
  ],
467
- typ: "JWT",
476
+ typ: typ || "JWT",
468
477
  algorithms: alg ? [alg] : void 0,
469
478
  forceUint8Array: false,
470
479
  validateJWT: true
@@ -472,12 +481,12 @@ async function verifyJWSSession(_event, config, token) {
472
481
  const message = error_ instanceof Error ? error_.message : String(error_);
473
482
  throw new Error(`Invalid session token: ${message}`);
474
483
  });
475
- const { jti, iat, ...data } = payload;
484
+ const { jti, iat, exp, ...data } = payload;
476
485
  return {
477
486
  id: jti,
478
487
  createdAt: iat * 1e3,
479
488
  // Convert back to ms
480
- expiresAt: payload.exp ? payload.exp * 1e3 : void 0,
489
+ expiresAt: exp ? exp * 1e3 : void 0,
481
490
  data: data && typeof data === "object" ? data : /* @__PURE__ */ Object.create(null)
482
491
  };
483
492
  }
@@ -492,19 +501,48 @@ async function clearJWSSession(event, config) {
492
501
  expires: /* @__PURE__ */ new Date(0),
493
502
  maxAge: void 0
494
503
  });
495
- await config.hooks?.onClear?.(event);
504
+ await config.hooks?.onClear?.(event, config);
496
505
  }
497
506
  function getSignKey(key) {
498
- if ("privateKey" in key) {
499
- return key.privateKey;
507
+ if (!key) {
508
+ throw new Error("Session: JWS key is required.");
509
+ }
510
+ let _key = void 0;
511
+ if (isSymmetricJWK(key)) {
512
+ _key = key;
513
+ } else if ("privateKey" in key && isPrivateJWK(key.privateKey)) {
514
+ _key = key.privateKey;
500
515
  }
501
- return key;
516
+ if (!_key) {
517
+ throw new Error(
518
+ "Session: Invalid JWS key. It must be a symmetric JWK or a private JWK.",
519
+ { cause: key }
520
+ );
521
+ }
522
+ return _key;
502
523
  }
503
524
  function getVerifyKey(key) {
504
- if ("publicKey" in key) {
505
- return Array.isArray(key.publicKey) ? { keys: key.publicKey } : key.publicKey;
525
+ if (!key) {
526
+ throw new Error("Session: JWS key is required.");
527
+ }
528
+ let _key = void 0;
529
+ if (isSymmetricJWK(key)) {
530
+ _key = key;
531
+ } else if ("publicKey" in key && isPublicJWK(key.publicKey)) {
532
+ _key = key.publicKey;
533
+ } else if ("publicKey" in key && Array.isArray(key.publicKey)) {
534
+ const keys = key.publicKey.filter((k) => isPublicJWK(k));
535
+ if (keys && keys.length > 0) {
536
+ _key = { keys };
537
+ }
538
+ }
539
+ if (!_key) {
540
+ throw new Error(
541
+ "Session: Invalid JWS key. It must be a symmetric JWK or a public JWK/set.",
542
+ { cause: key }
543
+ );
506
544
  }
507
- return key;
545
+ return _key;
508
546
  }
509
547
 
510
548
  export { clearJWESession, clearJWSSession, getJWESession, getJWSSession, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
@@ -0,0 +1,7 @@
1
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters } from '../shared/unjwt.9mL1vSFP.mjs';
2
+ export { C as CompositeKey, f as ContentEncryptionAlgorithm, D as DeriveKeyOptions, e as DeriveKeyReturn, c as GenerateJWKOptions, d as GenerateJWKReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, v as JWK, S as JWKAlgorithm, T as JWKPEMAlgorithm, J as JWKParameters, w as JWKSet, I as JWK_AES_CBC_HMAC, L as JWK_AES_GCM, H as JWK_AES_GCM_KW, F as JWK_AES_KW, s as JWK_Asymmetric, R as JWK_Asymmetric_Algorithm, j as JWK_EC, O as JWK_ECDH_ES, A as JWK_ECDSA, i as JWK_EC_Private, h as JWK_EC_Public, x as JWK_HMAC, p as JWK_OKP, o as JWK_OKP_Private, n as JWK_OKP_Public, P as JWK_OKP_SIGN, N as JWK_PBES2, u as JWK_Private, t as JWK_Public, m as JWK_RSA, B as JWK_RSA_ENC, z as JWK_RSA_PSS, l as JWK_RSA_Private, k as JWK_RSA_Public, y as JWK_RSA_SIGN, r as JWK_Symmetric, Q as JWK_Symmetric_Algorithm, q as JWK_oct, Y as JWT, Z as JWTClaimValidationOptions, X as JWTClaims, V as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, g as WrapKeyResult } from '../shared/unjwt.CtHrClHc.mjs';
3
+ export { SessionConfigJWE, SessionConfigJWS, SessionData, SessionHooksJWE, SessionHooksJWS, SessionJWE, SessionJWEConfig, SessionJWS, SessionJWSConfig, SessionManager, clearJWESession, clearJWSSession, getJWESession, getJWSSession, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession } from './h3.mjs';
4
+ export { d as deriveJWKFromPassword, e as exportJWKToPEM, g as generateJWK, i as importJWKFromPEM } from '../shared/unjwt.BjP_dGnX.mjs';
5
+ import 'cookie-es';
6
+ import 'h3';
7
+ import '../shared/unjwt.BsLgL0DQ.mjs';
@@ -0,0 +1,7 @@
1
+ export { k as deriveJWKFromPassword, h as exportJWKToPEM, c as generateJWK, f as importJWKFromPEM } from '../shared/unjwt.epPK1KFr.mjs';
2
+ export { clearJWESession, clearJWSSession, getJWESession, getJWSSession, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession } from './h3.mjs';
3
+ import '../shared/unjwt.C86hA0o-.mjs';
4
+ import 'h3';
5
+ import 'cookie-es';
6
+ import '../shared/unjwt.CiBEuvtQ.mjs';
7
+ import '../shared/unjwt.D-FATaw1.mjs';
@@ -0,0 +1,129 @@
1
+ import { b as JWEEncryptOptions } from '../shared/unjwt.9mL1vSFP.mjs';
2
+ export { d as JWEDecryptOptions, e as JWEDecryptResult, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters } from '../shared/unjwt.9mL1vSFP.mjs';
3
+ import { X as JWTClaims, r as JWK_Symmetric, u as JWK_Private, t as JWK_Public, Z as JWTClaimValidationOptions, w as JWKSet } from '../shared/unjwt.CtHrClHc.mjs';
4
+ export { C as CompositeKey, f as ContentEncryptionAlgorithm, D as DeriveKeyOptions, e as DeriveKeyReturn, c as GenerateJWKOptions, d as GenerateJWKReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn, v as JWK, S as JWKAlgorithm, T as JWKPEMAlgorithm, J as JWKParameters, I as JWK_AES_CBC_HMAC, L as JWK_AES_GCM, H as JWK_AES_GCM_KW, F as JWK_AES_KW, s as JWK_Asymmetric, R as JWK_Asymmetric_Algorithm, j as JWK_EC, O as JWK_ECDH_ES, A as JWK_ECDSA, i as JWK_EC_Private, h as JWK_EC_Public, x as JWK_HMAC, p as JWK_OKP, o as JWK_OKP_Private, n as JWK_OKP_Public, P as JWK_OKP_SIGN, N as JWK_PBES2, m as JWK_RSA, B as JWK_RSA_ENC, z as JWK_RSA_PSS, l as JWK_RSA_Private, k as JWK_RSA_Public, y as JWK_RSA_SIGN, Q as JWK_Symmetric_Algorithm, q as JWK_oct, Y as JWT, V as JoseHeaderParameters, K as KeyManagementAlgorithm, U as UnwrapKeyOptions, W as WrapKeyOptions, g as WrapKeyResult } from '../shared/unjwt.CtHrClHc.mjs';
5
+ import { HTTPEvent } from 'h3';
6
+ import { CookieSerializeOptions } from 'cookie-es';
7
+ import { c as JWSSignOptions } from '../shared/unjwt.BsLgL0DQ.mjs';
8
+ export { d as deriveJWKFromPassword, e as exportJWKToPEM, g as generateJWK, i as importJWKFromPEM } from '../shared/unjwt.BjP_dGnX.mjs';
9
+
10
+ type SessionDataT$1 = Omit<JWTClaims, "jti" | "iat" | "exp">;
11
+ type SessionData<T extends SessionDataT$1 = SessionDataT$1> = T;
12
+ declare const kGetSessionPromise$1: unique symbol;
13
+ interface SessionJWE<T extends SessionDataT$1 = SessionDataT$1> {
14
+ id: string;
15
+ createdAt: number;
16
+ expiresAt?: number;
17
+ data: SessionData<T>;
18
+ [kGetSessionPromise$1]?: Promise<SessionJWE<T>>;
19
+ }
20
+ interface SessionHooksJWE {
21
+ onRead?: (session: SessionJWE, event: HTTPEvent, config: SessionConfigJWE) => void | Promise<void>;
22
+ onUpdate?: (session: SessionJWE, event: HTTPEvent, config: SessionConfigJWE) => void | Promise<void>;
23
+ onClear?: (event: HTTPEvent, config: Partial<SessionConfigJWE>) => void | Promise<void>;
24
+ onExpire?: (event: HTTPEvent, error: any | undefined, config: SessionConfigJWE) => void | Promise<void>;
25
+ onError?: (event: HTTPEvent, error: any, config: SessionConfigJWE) => void | Promise<void>;
26
+ }
27
+ interface SessionManager<T extends SessionDataT$1 = SessionDataT$1> {
28
+ readonly id: string | undefined;
29
+ readonly createdAt: number;
30
+ readonly expiresAt: number | undefined;
31
+ readonly data: SessionData<T>;
32
+ update: (update: SessionUpdate$1<T>) => Promise<SessionManager<T>>;
33
+ clear: () => Promise<SessionManager<T>>;
34
+ }
35
+ interface SessionConfigJWE {
36
+ /** Shared secret used, string for PBES2 or Json Web Key (JWK) */
37
+ key: string | JWK_Symmetric | {
38
+ privateKey: JWK_Private | JWK_Symmetric;
39
+ publicKey?: JWK_Public;
40
+ };
41
+ /** Session lifetime in seconds (used to derive exp from iat) */
42
+ maxAge?: number;
43
+ /** Default is "h3" */
44
+ name?: string;
45
+ /** Default is secure, httpOnly, path="/" */
46
+ cookie?: false | (CookieSerializeOptions & {
47
+ chunkMaxLength?: number;
48
+ });
49
+ /** Default is x-h3-session / x-{name}-session */
50
+ sessionHeader?: false | string;
51
+ /** Default is crypto.randomUUID */
52
+ generateId?: () => string;
53
+ /** JWE configuration overrides */
54
+ jwe?: {
55
+ encryptOptions?: Omit<JWEEncryptOptions, "expiresIn">;
56
+ decryptOptions?: JWTClaimValidationOptions;
57
+ };
58
+ hooks?: SessionHooksJWE;
59
+ }
60
+ type SessionUpdate$1<T extends SessionDataT$1 = SessionDataT$1> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
61
+ /**
62
+ * Create a session manager for the current request.
63
+ */
64
+ declare function useJWESession<T extends SessionDataT$1 = SessionDataT$1>(event: HTTPEvent, config: SessionConfigJWE): Promise<SessionManager<T>>;
65
+ /**
66
+ * Get the session for the current request.
67
+ */
68
+ declare function getJWESession<T extends SessionDataT$1 = SessionDataT$1>(event: HTTPEvent, config: SessionConfigJWE): Promise<SessionJWE<T>>;
69
+ /**
70
+ * Update the session data for the current request.
71
+ */
72
+ declare function updateJWESession<T extends SessionDataT$1 = SessionDataT$1>(event: HTTPEvent, config: SessionConfigJWE, update?: SessionUpdate$1<T>): Promise<SessionJWE<T>>;
73
+ /**
74
+ * Produce a JWE for the current session.
75
+ */
76
+ declare function sealJWESession<T extends SessionDataT$1 = SessionDataT$1>(event: HTTPEvent, config: SessionConfigJWE): Promise<string>;
77
+ /**
78
+ * Decrypt the JWE and return a Session-compatible object.
79
+ */
80
+ declare function unsealJWESession(_event: HTTPEvent, config: SessionConfigJWE, sealed: string): Promise<Partial<SessionJWE>>;
81
+ /**
82
+ * Clear the session (delete from context and drop cookie).
83
+ */
84
+ declare function clearJWESession(event: HTTPEvent, config: Partial<SessionConfigJWE>): Promise<void>;
85
+
86
+ type SessionDataT = Omit<JWTClaims, "jti" | "iat" | "exp">;
87
+ declare const kGetSessionPromise: unique symbol;
88
+ interface SessionJWS<T extends SessionDataT = SessionDataT> {
89
+ id: string;
90
+ createdAt: number;
91
+ expiresAt?: number;
92
+ data: SessionData<T>;
93
+ [kGetSessionPromise]?: Promise<SessionJWS<T>>;
94
+ }
95
+ interface SessionHooksJWS {
96
+ onRead?: (session: SessionJWS, event: HTTPEvent, config: SessionConfigJWS) => void | Promise<void>;
97
+ onUpdate?: (session: SessionJWS, event: HTTPEvent, config: SessionConfigJWS) => void | Promise<void>;
98
+ onClear?: (event: HTTPEvent, config: Partial<SessionConfigJWS>) => void | Promise<void>;
99
+ onExpire?: (event: HTTPEvent, error: any | undefined, config: SessionConfigJWS) => void | Promise<void>;
100
+ onError?: (event: HTTPEvent, error: any, config: SessionConfigJWS) => void | Promise<void>;
101
+ }
102
+ interface SessionConfigJWS {
103
+ key: JWK_Symmetric | {
104
+ privateKey: JWK_Private;
105
+ publicKey: JWK_Public | JWK_Public[] | JWKSet;
106
+ };
107
+ maxAge?: number;
108
+ name?: string;
109
+ cookie?: false | (CookieSerializeOptions & {
110
+ chunkMaxLength?: number;
111
+ });
112
+ sessionHeader?: false | string;
113
+ generateId?: () => string;
114
+ jws?: {
115
+ signOptions?: Omit<JWSSignOptions, "expiresIn">;
116
+ verifyOptions?: JWTClaimValidationOptions;
117
+ };
118
+ hooks?: SessionHooksJWS;
119
+ }
120
+ type SessionUpdate<T extends SessionDataT = SessionDataT> = Partial<SessionData<T>> | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined);
121
+ declare function useJWSSession<T extends SessionDataT = SessionDataT>(event: HTTPEvent, config: SessionConfigJWS): Promise<SessionManager<T>>;
122
+ declare function getJWSSession<T extends SessionDataT = SessionDataT>(event: HTTPEvent, config: SessionConfigJWS): Promise<SessionJWS<T>>;
123
+ declare function updateJWSSession<T extends SessionDataT = SessionDataT>(event: HTTPEvent, config: SessionConfigJWS, update?: SessionUpdate<T>): Promise<SessionJWS<T>>;
124
+ declare function signJWSSession<T extends SessionDataT = SessionDataT>(event: HTTPEvent, config: SessionConfigJWS): Promise<string>;
125
+ declare function verifyJWSSession(_event: HTTPEvent, config: SessionConfigJWS, token: string): Promise<Partial<SessionJWS>>;
126
+ declare function clearJWSSession(event: HTTPEvent, config: Partial<SessionConfigJWS>): Promise<void>;
127
+
128
+ export { JWEEncryptOptions, JWKSet, JWK_Private, JWK_Public, JWK_Symmetric, JWTClaimValidationOptions, JWTClaims, clearJWESession, clearJWSSession, getJWESession, getJWSSession, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
129
+ export type { SessionConfigJWE, SessionConfigJWS, SessionData, SessionHooksJWE, SessionHooksJWS, SessionJWE, SessionJWS, SessionManager };