unjwt 0.5.34 → 0.5.36

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.
@@ -44,6 +44,7 @@ interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge
44
44
  }) => void | Promise<void>;
45
45
  onUpdate?: (args: {
46
46
  session: SessionJWE<T, MaxAge>;
47
+ oldSession: SessionJWE<T, MaxAge>;
47
48
  event: H3Event;
48
49
  config: SessionConfigJWE<T, MaxAge>;
49
50
  }) => void | Promise<void>;
@@ -164,6 +165,7 @@ interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge
164
165
  }) => void | Promise<void>;
165
166
  onUpdate?: (args: {
166
167
  session: SessionJWS<T, MaxAge>;
168
+ oldSession: SessionJWS<T, MaxAge>;
167
169
  event: H3Event;
168
170
  config: SessionConfigJWS<T, MaxAge>;
169
171
  }) => void | Promise<void>;
@@ -189,10 +189,12 @@ async function updateJWESession(event, config, update) {
189
189
  update = update(session.data);
190
190
  }
191
191
  if (update) {
192
+ const oldSession = { ...session, data: { ...session.data } };
192
193
  Object.assign(session.data, update);
193
194
  await config.hooks?.onUpdate?.({
194
- event,
195
195
  session,
196
+ oldSession,
197
+ event,
196
198
  config
197
199
  });
198
200
  }
@@ -213,15 +215,15 @@ async function sealJWESession(event, config) {
213
215
  const key = getEncryptKey(config.key || config.secret);
214
216
  const sessionName = config.name || DEFAULT_NAME$1;
215
217
  const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
216
- const iatSeconds = Math.floor(session.createdAt / 1e3);
217
- const expSeconds = config.maxAge === void 0 ? void 0 : iatSeconds + computeExpiresInSeconds(config.maxAge);
218
+ const iat = Math.floor(Date.now() / 1e3);
219
+ const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
218
220
  const payload = {
219
221
  ...session.data,
220
222
  jti: session.id,
221
- iat: iatSeconds
223
+ iat
222
224
  };
223
- if (expSeconds) {
224
- payload.exp = expSeconds;
225
+ if (exp) {
226
+ payload.exp = exp;
225
227
  }
226
228
  let typ = void 0;
227
229
  if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
@@ -536,10 +538,12 @@ async function updateJWSSession(event, config, update) {
536
538
  update = update(session.data);
537
539
  }
538
540
  if (update) {
541
+ const oldSession = { ...session, data: { ...session.data } };
539
542
  Object.assign(session.data, update);
540
543
  await config.hooks?.onUpdate?.({
541
- event,
542
544
  session,
545
+ oldSession,
546
+ event,
543
547
  config
544
548
  });
545
549
  }
@@ -559,15 +563,15 @@ async function signJWSSession(event, config) {
559
563
  const key = getSignKey(config.key);
560
564
  const sessionName = config.name || DEFAULT_NAME;
561
565
  const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
562
- const iatSeconds = Math.floor(session.createdAt / 1e3);
563
- const expSeconds = config.maxAge === void 0 ? void 0 : iatSeconds + computeExpiresInSeconds(config.maxAge);
566
+ const iat = Math.floor(Date.now() / 1e3);
567
+ const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
564
568
  const payload = {
565
569
  ...session.data,
566
570
  jti: session.id,
567
- iat: iatSeconds
571
+ iat
568
572
  };
569
- if (expSeconds) {
570
- payload.exp = expSeconds;
573
+ if (exp) {
574
+ payload.exp = exp;
571
575
  }
572
576
  let typ = void 0;
573
577
  if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
@@ -38,6 +38,7 @@ interface SessionHooksJWE<T extends Record<string, any> = SessionClaims, MaxAge
38
38
  }) => void | Promise<void>;
39
39
  onUpdate?: (args: {
40
40
  session: SessionJWE<T, MaxAge>;
41
+ oldSession: SessionJWE<T, MaxAge>;
41
42
  event: HTTPEvent;
42
43
  config: SessionConfigJWE<T, MaxAge>;
43
44
  }) => void | Promise<void>;
@@ -129,6 +130,7 @@ interface SessionHooksJWS<T extends Record<string, any> = SessionClaims, MaxAge
129
130
  }) => void | Promise<void>;
130
131
  onUpdate?: (args: {
131
132
  session: SessionJWS<T, MaxAge>;
133
+ oldSession: SessionJWS<T, MaxAge>;
132
134
  event: HTTPEvent;
133
135
  config: SessionConfigJWS<T, MaxAge>;
134
136
  }) => void | Promise<void>;
@@ -149,10 +149,12 @@ async function updateJWESession(event, config, update) {
149
149
  update = update(session.data);
150
150
  }
151
151
  if (update) {
152
+ const oldSession = { ...session, data: { ...session.data } };
152
153
  Object.assign(session.data, update);
153
154
  await config.hooks?.onUpdate?.({
154
- event,
155
+ oldSession,
155
156
  session,
157
+ event,
156
158
  config
157
159
  });
158
160
  }
@@ -173,15 +175,15 @@ async function sealJWESession(event, config) {
173
175
  const sessionName = config.name || DEFAULT_NAME$1;
174
176
  const context = getEventContext(event);
175
177
  const session = context.sessions?.[sessionName] || await getJWESession(event, config);
176
- const iatSeconds = Math.floor(session.createdAt / 1e3);
177
- const expSeconds = config.maxAge === void 0 ? void 0 : iatSeconds + computeExpiresInSeconds(config.maxAge);
178
+ const iat = Math.floor(Date.now() / 1e3);
179
+ const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
178
180
  const payload = {
179
181
  ...session.data,
180
182
  jti: session.id,
181
- iat: iatSeconds
183
+ iat
182
184
  };
183
- if (expSeconds) {
184
- payload.exp = expSeconds;
185
+ if (exp) {
186
+ payload.exp = exp;
185
187
  }
186
188
  let typ = void 0;
187
189
  if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
@@ -460,10 +462,12 @@ async function updateJWSSession(event, config, update) {
460
462
  update = update(session.data);
461
463
  }
462
464
  if (update) {
465
+ const oldSession = { ...session, data: { ...session.data } };
463
466
  Object.assign(session.data, update);
464
467
  await config.hooks?.onUpdate?.({
465
- event,
466
468
  session,
469
+ oldSession,
470
+ event,
467
471
  config
468
472
  });
469
473
  }
@@ -484,15 +488,15 @@ async function signJWSSession(event, config) {
484
488
  const sessionName = config.name || DEFAULT_NAME;
485
489
  const context = getEventContext(event);
486
490
  const session = context.sessions?.[sessionName] || await getJWSSession(event, config);
487
- const iatSeconds = Math.floor(session.createdAt / 1e3);
488
- const expSeconds = config.maxAge === void 0 ? void 0 : iatSeconds + computeExpiresInSeconds(config.maxAge);
491
+ const iat = Math.floor(Date.now() / 1e3);
492
+ const exp = config.maxAge === void 0 ? void 0 : iat + computeExpiresInSeconds(config.maxAge);
489
493
  const payload = {
490
494
  ...session.data,
491
495
  jti: session.id,
492
- iat: iatSeconds
496
+ iat
493
497
  };
494
- if (expSeconds) {
495
- payload.exp = expSeconds;
498
+ if (exp) {
499
+ payload.exp = exp;
496
500
  }
497
501
  let typ;
498
502
  if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.5.34",
3
+ "version": "0.5.36",
4
4
  "description": "Low-level JWT utilities",
5
5
  "repository": "sandros94/unjwt",
6
6
  "license": "MIT",