unjwt 0.5.37 → 0.5.38

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.
package/README.md CHANGED
@@ -605,6 +605,9 @@ console.log(rsaPublicSpki);
605
605
 
606
606
  ### Adapters
607
607
 
608
+ > [!WARNING]
609
+ > While these adapters try to be compatible with h3's `useSession` API, the key difference is that new sessions are not automatically created when calling `useJWESession` or `useJWSSession`, marking their `id` as `undefined` until you explicitly call `session.update()`. This is to comply with various specs that do integrate JWT one (such as OAuth) where sessions are only created upon valid operations (like user authorization).
610
+
608
611
  #### H3 v1 (Nuxt v4, Nitro v2)
609
612
 
610
613
  The `h3v1` adapter bundles session helpers that store data inside signed or encrypted JWTs.
@@ -18,7 +18,9 @@ interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMa
18
18
  readonly expiresAt: ConfigMaxAge extends ExpiresIn ? number : "exp" extends keyof T ? T["exp"] : number | undefined;
19
19
  readonly data: SessionData<T>;
20
20
  readonly token: string | undefined;
21
- update: (update: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge>>;
21
+ update: (update: SessionUpdate<T>) => Promise<SessionManager<T, ConfigMaxAge> & {
22
+ readonly id: string;
23
+ }>;
22
24
  clear: () => Promise<SessionManager<T, ConfigMaxAge>>;
23
25
  }
24
26
 
@@ -30,7 +32,7 @@ interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMa
30
32
 
31
33
  declare const kGetSessionPromise$1: unique symbol;
32
34
  interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
33
- id: string;
35
+ id: string | undefined;
34
36
  createdAt: number;
35
37
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
36
38
  data: SessionData<T>;
@@ -151,7 +153,7 @@ declare function clearJWESession<T extends Record<string, any> = SessionClaims,
151
153
 
152
154
  declare const kGetSessionPromise: unique symbol;
153
155
  interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
154
- id: string;
156
+ id: string | undefined;
155
157
  createdAt: number;
156
158
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
157
159
  data: SessionData<T>;
@@ -80,10 +80,12 @@ async function getJWESession(event, config) {
80
80
  });
81
81
  return session2;
82
82
  }
83
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
84
+ const createdAt = now - now % 1e3;
83
85
  const session = {
84
- id: "",
85
- createdAt: 0,
86
- expiresAt: void 0,
86
+ id: void 0,
87
+ createdAt,
88
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
87
89
  data: /* @__PURE__ */ Object.create(null)
88
90
  };
89
91
  event.context.sessions[sessionName] = session;
@@ -114,14 +116,6 @@ async function getJWESession(event, config) {
114
116
  event.context.sessions[sessionName][kGetSessionPromise$1] = promise;
115
117
  await promise;
116
118
  }
117
- if (!session.id) {
118
- if (!isEvent(event)) {
119
- throw new Error(
120
- "Cannot initialize a new session. Use `useSession(event)` within the main handler."
121
- );
122
- }
123
- await updateJWESession(event, config);
124
- }
125
119
  await config.hooks?.onRead?.({
126
120
  event,
127
121
  session,
@@ -180,6 +174,11 @@ function _getResHeader$1(event, name) {
180
174
  }
181
175
  }
182
176
  async function updateJWESession(event, config, update) {
177
+ if (!isEvent(event)) {
178
+ throw new Error(
179
+ "Cannot update a new session. Use `useSession(event)` within the main handler."
180
+ );
181
+ }
183
182
  const sessionName = config.name || DEFAULT_NAME$1;
184
183
  const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
185
184
  if (typeof update === "function") {
@@ -433,10 +432,12 @@ async function getJWSSession(event, config) {
433
432
  });
434
433
  return session2;
435
434
  }
435
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
436
+ const createdAt = now - now % 1e3;
436
437
  const session = {
437
- id: "",
438
- createdAt: 0,
439
- expiresAt: void 0,
438
+ id: void 0,
439
+ createdAt,
440
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
440
441
  data: /* @__PURE__ */ Object.create(null)
441
442
  };
442
443
  event.context.sessions[sessionName] = session;
@@ -467,14 +468,6 @@ async function getJWSSession(event, config) {
467
468
  session[kGetSessionPromise] = promise;
468
469
  await promise;
469
470
  }
470
- if (!session.id) {
471
- if (!isEvent(event)) {
472
- throw new Error(
473
- "Cannot initialize a new session outside main handler. Use `useJWSSession(event)` properly."
474
- );
475
- }
476
- await updateJWSSession(event, config);
477
- }
478
471
  await config.hooks?.onRead?.({
479
472
  event,
480
473
  session,
@@ -533,6 +526,11 @@ function _getResHeader(event, name) {
533
526
  }
534
527
  }
535
528
  async function updateJWSSession(event, config, update) {
529
+ if (!isEvent(event)) {
530
+ throw new Error(
531
+ "Cannot update a new session. Use `useSession(event)` within the main handler."
532
+ );
533
+ }
536
534
  const sessionName = config.name || DEFAULT_NAME;
537
535
  const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
538
536
  if (typeof update === "function") {
@@ -24,7 +24,7 @@ interface SessionManager<T extends Record<string, any> = SessionClaims, ConfigMa
24
24
 
25
25
  declare const kGetSessionPromise$1: unique symbol;
26
26
  interface SessionJWE<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
27
- id: string;
27
+ id: string | undefined;
28
28
  createdAt: number;
29
29
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
30
30
  data: SessionData<T>;
@@ -116,7 +116,7 @@ declare function clearJWESession<T extends Record<string, any> = SessionClaims,
116
116
 
117
117
  declare const kGetSessionPromise: unique symbol;
118
118
  interface SessionJWS<T extends Record<string, any> = SessionClaims, MaxAge extends ExpiresIn | undefined = ExpiresIn | undefined> {
119
- id: string;
119
+ id: string | undefined;
120
120
  createdAt: number;
121
121
  expiresAt: MaxAge extends ExpiresIn ? number : T["exp"];
122
122
  data: SessionData<T>;
@@ -70,10 +70,12 @@ async function getJWESession(event, config) {
70
70
  });
71
71
  return session2;
72
72
  }
73
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
74
+ const createdAt = now - now % 1e3;
73
75
  const session = {
74
- id: "",
75
- createdAt: 0,
76
- expiresAt: void 0,
76
+ id: void 0,
77
+ createdAt,
78
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
77
79
  data: new NullProtoObj()
78
80
  };
79
81
  context.sessions[sessionName] = session;
@@ -107,9 +109,6 @@ async function getJWESession(event, config) {
107
109
  session[kGetSessionPromise$1] = promise;
108
110
  await promise;
109
111
  }
110
- if (!session.id) {
111
- await updateJWESession(event, config);
112
- }
113
112
  await config.hooks?.onRead?.({
114
113
  event,
115
114
  session,
@@ -390,10 +389,12 @@ async function getJWSSession(event, config) {
390
389
  });
391
390
  return session2;
392
391
  }
392
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
393
+ const createdAt = now - now % 1e3;
393
394
  const session = {
394
- id: "",
395
- createdAt: 0,
396
- expiresAt: void 0,
395
+ id: void 0,
396
+ createdAt,
397
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
397
398
  data: new NullProtoObj()
398
399
  };
399
400
  context.sessions[sessionName] = session;
@@ -424,9 +425,6 @@ async function getJWSSession(event, config) {
424
425
  session[kGetSessionPromise] = promise;
425
426
  await promise;
426
427
  }
427
- if (!session.id) {
428
- await updateJWSSession(event, config);
429
- }
430
428
  await config.hooks?.onRead?.({
431
429
  event,
432
430
  session,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unjwt",
3
- "version": "0.5.37",
3
+ "version": "0.5.38",
4
4
  "description": "Low-level JWT utilities",
5
5
  "repository": "sandros94/unjwt",
6
6
  "license": "MIT",