unjwt 0.5.36 → 0.5.37

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.
@@ -120,9 +120,6 @@ async function getJWESession(event, config) {
120
120
  "Cannot initialize a new session. Use `useSession(event)` within the main handler."
121
121
  );
122
122
  }
123
- session.id = config.generateId?.() ?? crypto.randomUUID();
124
- session.createdAt = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
125
- session.expiresAt = config.maxAge === void 0 ? void 0 : session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3;
126
123
  await updateJWESession(event, config);
127
124
  }
128
125
  await config.hooks?.onRead?.({
@@ -198,6 +195,13 @@ async function updateJWESession(event, config, update) {
198
195
  config
199
196
  });
200
197
  }
198
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
199
+ const createdAt = now - now % 1e3;
200
+ Object.assign(session, {
201
+ id: config.generateId?.() || crypto.randomUUID(),
202
+ createdAt,
203
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
204
+ });
201
205
  if (config.cookie !== false) {
202
206
  const sealed = await sealJWESession(event, config);
203
207
  setCookie(event, sessionName, sealed, {
@@ -215,8 +219,8 @@ async function sealJWESession(event, config) {
215
219
  const key = getEncryptKey(config.key || config.secret);
216
220
  const sessionName = config.name || DEFAULT_NAME$1;
217
221
  const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
218
- const iat = Math.floor(Date.now() / 1e3);
219
- const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
222
+ const iat = Math.floor(session.createdAt / 1e3);
223
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
220
224
  const payload = {
221
225
  ...session.data,
222
226
  jti: session.id,
@@ -469,9 +473,6 @@ async function getJWSSession(event, config) {
469
473
  "Cannot initialize a new session outside main handler. Use `useJWSSession(event)` properly."
470
474
  );
471
475
  }
472
- session.id = config.generateId?.() ?? crypto.randomUUID();
473
- session.createdAt = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
474
- session.expiresAt = config.maxAge === void 0 ? void 0 : session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3;
475
476
  await updateJWSSession(event, config);
476
477
  }
477
478
  await config.hooks?.onRead?.({
@@ -547,6 +548,13 @@ async function updateJWSSession(event, config, update) {
547
548
  config
548
549
  });
549
550
  }
551
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
552
+ const createdAt = now - now % 1e3;
553
+ Object.assign(session, {
554
+ id: config.generateId?.() || crypto.randomUUID(),
555
+ createdAt,
556
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
557
+ });
550
558
  if (config.cookie !== false) {
551
559
  const token = await signJWSSession(event, config);
552
560
  setCookie(event, sessionName, token, {
@@ -563,8 +571,8 @@ async function signJWSSession(event, config) {
563
571
  const key = getSignKey(config.key);
564
572
  const sessionName = config.name || DEFAULT_NAME;
565
573
  const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
566
- const iat = Math.floor(Date.now() / 1e3);
567
- const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
574
+ const iat = Math.floor(session.createdAt / 1e3);
575
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
568
576
  const payload = {
569
577
  ...session.data,
570
578
  jti: session.id,
@@ -108,9 +108,6 @@ async function getJWESession(event, config) {
108
108
  await promise;
109
109
  }
110
110
  if (!session.id) {
111
- session.id = config.generateId?.() ?? crypto.randomUUID();
112
- session.createdAt = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
113
- session.expiresAt = config.maxAge === void 0 ? void 0 : session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3;
114
111
  await updateJWESession(event, config);
115
112
  }
116
113
  await config.hooks?.onRead?.({
@@ -158,6 +155,13 @@ async function updateJWESession(event, config, update) {
158
155
  config
159
156
  });
160
157
  }
158
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
159
+ const createdAt = now - now % 1e3;
160
+ Object.assign(session, {
161
+ id: config.generateId?.() || crypto.randomUUID(),
162
+ createdAt,
163
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
164
+ });
161
165
  if (config.cookie !== false && hasWritableResponse$1(event)) {
162
166
  const sealed = await sealJWESession(event, config);
163
167
  setChunkedCookie(event, sessionName, sealed, {
@@ -175,8 +179,8 @@ async function sealJWESession(event, config) {
175
179
  const sessionName = config.name || DEFAULT_NAME$1;
176
180
  const context = getEventContext(event);
177
181
  const session = context.sessions?.[sessionName] || await getJWESession(event, config);
178
- const iat = Math.floor(Date.now() / 1e3);
179
- const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
182
+ const iat = Math.floor(session.createdAt / 1e3);
183
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
180
184
  const payload = {
181
185
  ...session.data,
182
186
  jti: session.id,
@@ -421,9 +425,6 @@ async function getJWSSession(event, config) {
421
425
  await promise;
422
426
  }
423
427
  if (!session.id) {
424
- session.id = config.generateId?.() ?? crypto.randomUUID();
425
- session.createdAt = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
426
- session.expiresAt = config.maxAge === void 0 ? void 0 : session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3;
427
428
  await updateJWSSession(event, config);
428
429
  }
429
430
  await config.hooks?.onRead?.({
@@ -471,6 +472,13 @@ async function updateJWSSession(event, config, update) {
471
472
  config
472
473
  });
473
474
  }
475
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
476
+ const createdAt = now - now % 1e3;
477
+ Object.assign(session, {
478
+ id: config.generateId?.() || crypto.randomUUID(),
479
+ createdAt,
480
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
481
+ });
474
482
  if (config.cookie !== false && hasWritableResponse(event)) {
475
483
  const token = await signJWSSession(event, config);
476
484
  setChunkedCookie(event, sessionName, token, {
@@ -488,8 +496,8 @@ async function signJWSSession(event, config) {
488
496
  const sessionName = config.name || DEFAULT_NAME;
489
497
  const context = getEventContext(event);
490
498
  const session = context.sessions?.[sessionName] || await getJWSSession(event, config);
491
- const iat = Math.floor(Date.now() / 1e3);
492
- const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
499
+ const iat = Math.floor(session.createdAt / 1e3);
500
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
493
501
  const payload = {
494
502
  ...session.data,
495
503
  jti: session.id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.5.36",
3
+ "version": "0.5.37",
4
4
  "description": "Low-level JWT utilities",
5
5
  "repository": "sandros94/unjwt",
6
6
  "license": "MIT",