unjwt 0.5.37 → 0.5.39

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.
Files changed (41) hide show
  1. package/README.md +142 -663
  2. package/dist/_chunks/chunk.mjs +13 -0
  3. package/dist/_chunks/h3v1.mjs +598 -0
  4. package/dist/_chunks/index.d.mts +250 -0
  5. package/dist/_chunks/index2.d.mts +108 -0
  6. package/dist/_chunks/jwe.d.mts +43 -0
  7. package/dist/_chunks/jwe.mjs +132 -0
  8. package/dist/_chunks/jwk.d.mts +575 -0
  9. package/dist/_chunks/jwk.mjs +1592 -0
  10. package/dist/_chunks/jws.d.mts +46 -0
  11. package/dist/_chunks/jws.mjs +2069 -0
  12. package/dist/_chunks/utils.mjs +374 -0
  13. package/dist/adapters/h3.d.mts +3 -265
  14. package/dist/adapters/h3.mjs +3 -706
  15. package/dist/adapters/h3v1.d.mts +3 -8
  16. package/dist/adapters/h3v1.mjs +3 -7
  17. package/dist/adapters/h3v2.d.mts +145 -146
  18. package/dist/adapters/h3v2.mjs +505 -596
  19. package/dist/index.d.mts +6 -8
  20. package/dist/index.mjs +5 -4
  21. package/dist/jwe.d.mts +3 -4
  22. package/dist/jwe.mjs +4 -3
  23. package/dist/jwk.d.mts +3 -2
  24. package/dist/jwk.mjs +3 -2
  25. package/dist/jws.d.mts +3 -3
  26. package/dist/jws.mjs +4 -3
  27. package/dist/utils.d.mts +3 -3
  28. package/dist/utils.mjs +2 -1
  29. package/package.json +33 -31
  30. package/dist/shared/unjwt.4JQCoID_.d.mts +0 -161
  31. package/dist/shared/unjwt.B-v62plj.mjs +0 -249
  32. package/dist/shared/unjwt.BCi3FEWr.d.mts +0 -328
  33. package/dist/shared/unjwt.BYDTEWa-.d.mts +0 -269
  34. package/dist/shared/unjwt.Be8NxwFK.mjs +0 -438
  35. package/dist/shared/unjwt.BqdNUKDr.d.mts +0 -64
  36. package/dist/shared/unjwt.CMNtw8ek.d.mts +0 -155
  37. package/dist/shared/unjwt.Cy2MlkUe.d.mts +0 -174
  38. package/dist/shared/unjwt.D0_ccT7g.d.mts +0 -166
  39. package/dist/shared/unjwt.DdLN9vwI.mjs +0 -163
  40. package/dist/shared/unjwt.Dpl__ZAR.mjs +0 -2121
  41. package/dist/shared/unjwt.JdKqVANs.d.mts +0 -5
@@ -1,652 +1,561 @@
1
- export { k as deriveJWKFromPassword, h as exportJWKToPEM, c as generateJWK, f as importJWKFromPEM } from '../shared/unjwt.Dpl__ZAR.mjs';
2
- import { getEventContext, getChunkedCookie, setChunkedCookie } from 'h3';
3
- import { NullProtoObj } from 'rou3';
4
- import { e as encrypt, d as decrypt } from '../shared/unjwt.B-v62plj.mjs';
5
- import { p as computeExpiresInSeconds, q as isSymmetricJWK, u as isPrivateJWK, w as isPublicJWK } from '../shared/unjwt.Be8NxwFK.mjs';
6
- import { s as sign, v as verify } from '../shared/unjwt.DdLN9vwI.mjs';
7
-
1
+ import { a as deriveJWKFromPassword, c as importJWKFromPEM, d as isSymmetricJWK, f as computeExpiresInSeconds, i as encrypt, l as isPrivateJWK, n as verify, o as exportJWKToPEM, r as decrypt, s as generateJWK, t as sign, u as isPublicJWK } from "../_chunks/jws.mjs";
2
+ import { getChunkedCookie, getEventContext, setChunkedCookie } from "h3";
3
+ import { NullProtoObj } from "rou3";
4
+ //#region src/adapters/h3v2/session/jwe.ts
8
5
  const kGetSessionPromise$1 = Symbol("h3_jwe_getSession");
6
+ const kSessionToken$1 = Symbol("h3_jwe_sessionToken");
9
7
  const DEFAULT_NAME$1 = "h3-jwe";
10
8
  const DEFAULT_COOKIE$1 = {
11
- path: "/",
12
- secure: true,
13
- httpOnly: true
9
+ path: "/",
10
+ secure: true,
11
+ httpOnly: true
14
12
  };
13
+ /**
14
+ * Create a session manager for the current request.
15
+ */
15
16
  async function useJWESession(event, config) {
16
- const sessionName = config.name || DEFAULT_NAME$1;
17
- await getJWESession(event, config);
18
- const sessionManager = {
19
- get id() {
20
- return getSessionFromContext$1(event, sessionName)?.id;
21
- },
22
- get createdAt() {
23
- return getSessionFromContext$1(event, sessionName)?.createdAt ?? Date.now();
24
- },
25
- get expiresAt() {
26
- return getSessionFromContext$1(event, sessionName)?.expiresAt;
27
- },
28
- get data() {
29
- return getSessionFromContext$1(event, sessionName)?.data || {};
30
- },
31
- get token() {
32
- return getJWESessionToken(event, config);
33
- },
34
- update: async (update) => {
35
- await updateJWESession(event, config, update);
36
- return sessionManager;
37
- },
38
- clear: async () => {
39
- await clearJWESession(event, config);
40
- return sessionManager;
41
- }
42
- };
43
- return sessionManager;
17
+ const sessionName = config.name || DEFAULT_NAME$1;
18
+ await getJWESession(event, config);
19
+ const sessionManager = {
20
+ get id() {
21
+ return getSessionFromContext$1(event, sessionName)?.id;
22
+ },
23
+ get createdAt() {
24
+ return getSessionFromContext$1(event, sessionName)?.createdAt ?? Date.now();
25
+ },
26
+ get expiresAt() {
27
+ return getSessionFromContext$1(event, sessionName)?.expiresAt;
28
+ },
29
+ get data() {
30
+ return getSessionFromContext$1(event, sessionName)?.data || {};
31
+ },
32
+ get token() {
33
+ return getSessionFromContext$1(event, sessionName)?.[kSessionToken$1] ?? getJWESessionToken(event, config);
34
+ },
35
+ update: async (update) => {
36
+ await updateJWESession(event, config, update);
37
+ return sessionManager;
38
+ },
39
+ clear: async () => {
40
+ await clearJWESession(event, config);
41
+ return sessionManager;
42
+ }
43
+ };
44
+ return sessionManager;
44
45
  }
46
+ /**
47
+ * Get the session for the current request.
48
+ */
45
49
  async function getJWESession(event, config) {
46
- const sessionName = config.name || DEFAULT_NAME$1;
47
- const context = getEventContext(event);
48
- if (!context.sessions) {
49
- context.sessions = new NullProtoObj();
50
- }
51
- const existingSession = context.sessions[sessionName];
52
- if (existingSession) {
53
- const session2 = existingSession[kGetSessionPromise$1] ? await existingSession[kGetSessionPromise$1] : existingSession;
54
- if (session2.expiresAt !== void 0 && session2.expiresAt < Date.now() && hasWritableResponse$1(event)) {
55
- await config.hooks?.onExpire?.({
56
- event,
57
- error: new Error(
58
- `JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${new Date(session2.expiresAt * 1e3).toISOString()})`
59
- ),
60
- config
61
- });
62
- return clearJWESession(event, config).then(
63
- () => getJWESession(event, config)
64
- );
65
- }
66
- await config.hooks?.onRead?.({
67
- event,
68
- session: session2,
69
- config
70
- });
71
- return session2;
72
- }
73
- const session = {
74
- id: "",
75
- createdAt: 0,
76
- expiresAt: void 0,
77
- data: new NullProtoObj()
78
- };
79
- context.sessions[sessionName] = session;
80
- const token = getJWESessionToken(event, config);
81
- if (token) {
82
- const promise = unsealJWESession(event, config, token).catch(async (error_) => {
83
- if (error_ instanceof Error) {
84
- const message = error_.message;
85
- if (message.includes("Token has expired") || message.includes("Token is too old")) {
86
- await config.hooks?.onExpire?.({
87
- event,
88
- error: error_,
89
- config
90
- });
91
- return void 0;
92
- }
93
- }
94
- await config.hooks?.onError?.({
95
- event,
96
- error: error_,
97
- config
98
- });
99
- return void 0;
100
- }).then((unsealed) => {
101
- if (unsealed) {
102
- Object.assign(session, unsealed);
103
- }
104
- delete session[kGetSessionPromise$1];
105
- return session;
106
- });
107
- session[kGetSessionPromise$1] = promise;
108
- await promise;
109
- }
110
- if (!session.id) {
111
- await updateJWESession(event, config);
112
- }
113
- await config.hooks?.onRead?.({
114
- event,
115
- session,
116
- config
117
- });
118
- return session;
50
+ const sessionName = config.name || DEFAULT_NAME$1;
51
+ const context = getEventContext(event);
52
+ if (!context.sessions) context.sessions = new NullProtoObj();
53
+ const existingSession = context.sessions[sessionName];
54
+ if (existingSession) {
55
+ const session = existingSession[kGetSessionPromise$1] ? await existingSession[kGetSessionPromise$1] : existingSession;
56
+ /**
57
+ * We check if a session is expired before returning it. If it is expired we clear it and create a new one,
58
+ * unless we have a read-only event in which case we just return it as it was valid at the time of reading
59
+ * the cookie/header (like in a websocket upgrade)
60
+ */
61
+ if (session.expiresAt !== void 0 && session.expiresAt < Date.now() && hasWritableResponse$1(event)) {
62
+ await config.hooks?.onExpire?.({
63
+ event,
64
+ error: /* @__PURE__ */ new Error(`JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${(/* @__PURE__ */ new Date(session.expiresAt * 1e3)).toISOString()})`),
65
+ config
66
+ });
67
+ return clearJWESession(event, config).then(() => getJWESession(event, config));
68
+ }
69
+ await config.hooks?.onRead?.({
70
+ event,
71
+ session,
72
+ config
73
+ });
74
+ return session;
75
+ }
76
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
77
+ const createdAt = now - now % 1e3;
78
+ const session = {
79
+ id: void 0,
80
+ createdAt,
81
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
82
+ data: new NullProtoObj()
83
+ };
84
+ context.sessions[sessionName] = session;
85
+ const token = getJWESessionToken(event, config);
86
+ if (token) {
87
+ session[kSessionToken$1] = token;
88
+ const promise = unsealJWESession(event, config, token).catch(async (error_) => {
89
+ if (error_ instanceof Error) {
90
+ const message = error_.message;
91
+ if (message.includes("Token has expired") || message.includes("Token is too old")) {
92
+ await config.hooks?.onExpire?.({
93
+ event,
94
+ error: error_,
95
+ config
96
+ });
97
+ return;
98
+ }
99
+ }
100
+ await config.hooks?.onError?.({
101
+ event,
102
+ error: error_,
103
+ config
104
+ });
105
+ }).then((unsealed) => {
106
+ if (unsealed) Object.assign(session, unsealed);
107
+ delete session[kGetSessionPromise$1];
108
+ return session;
109
+ });
110
+ session[kGetSessionPromise$1] = promise;
111
+ await promise;
112
+ }
113
+ await config.hooks?.onRead?.({
114
+ event,
115
+ session,
116
+ config
117
+ });
118
+ return session;
119
119
  }
120
120
  function getJWESessionToken(event, config) {
121
- const sessionName = config.name || DEFAULT_NAME$1;
122
- let token;
123
- if (config.sessionHeader !== false) {
124
- const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
125
- const headerValue = event.req.headers.get(headerName);
126
- if (typeof headerValue === "string") {
127
- token = headerValue.startsWith("Bearer ") ? headerValue.slice(7).trim() : headerValue;
128
- }
129
- }
130
- if (config.cookie !== false && hasWritableResponse$1(event)) {
131
- const setCookie = event.res.headers.get("set-cookie");
132
- if (typeof setCookie === "string") {
133
- token = findSetCookie$1(setCookie, sessionName);
134
- }
135
- }
136
- if (!token) {
137
- token = getChunkedCookie(event, sessionName);
138
- }
139
- return token;
121
+ const sessionName = config.name || DEFAULT_NAME$1;
122
+ let token;
123
+ if (config.sessionHeader !== false) {
124
+ const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
125
+ const headerValue = event.req.headers.get(headerName);
126
+ if (typeof headerValue === "string") token = headerValue.startsWith("Bearer ") ? headerValue.slice(7).trim() : headerValue;
127
+ }
128
+ if (config.cookie !== false && hasWritableResponse$1(event)) {
129
+ const setCookie = event.res.headers.get("set-cookie");
130
+ if (typeof setCookie === "string") token = findSetCookie$1(setCookie, sessionName);
131
+ }
132
+ if (!token) token = getChunkedCookie(event, sessionName);
133
+ return token;
140
134
  }
135
+ /**
136
+ * Update the session data for the current request.
137
+ */
141
138
  async function updateJWESession(event, config, update) {
142
- const sessionName = config.name || DEFAULT_NAME$1;
143
- const context = getEventContext(event);
144
- const session = context.sessions?.[sessionName] || await getJWESession(event, config);
145
- if (typeof update === "function") {
146
- update = update(session.data);
147
- }
148
- if (update) {
149
- const oldSession = { ...session, data: { ...session.data } };
150
- Object.assign(session.data, update);
151
- await config.hooks?.onUpdate?.({
152
- oldSession,
153
- session,
154
- event,
155
- config
156
- });
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
- });
165
- if (config.cookie !== false && hasWritableResponse$1(event)) {
166
- const sealed = await sealJWESession(event, config);
167
- setChunkedCookie(event, sessionName, sealed, {
168
- ...DEFAULT_COOKIE$1,
169
- expires: config.maxAge === void 0 ? void 0 : new Date(
170
- session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
171
- ),
172
- ...config.cookie
173
- });
174
- }
175
- return session;
139
+ const sessionName = config.name || DEFAULT_NAME$1;
140
+ const session = getEventContext(event).sessions?.[sessionName] || await getJWESession(event, config);
141
+ if (typeof update === "function") update = update(session.data);
142
+ if (update) {
143
+ const oldSession = {
144
+ ...session,
145
+ data: { ...session.data }
146
+ };
147
+ Object.assign(session.data, update);
148
+ await config.hooks?.onUpdate?.({
149
+ oldSession,
150
+ session,
151
+ event,
152
+ config
153
+ });
154
+ }
155
+ const now = config.jwe?.encryptOptions?.currentDate?.getTime() ?? Date.now();
156
+ const createdAt = now - now % 1e3;
157
+ Object.assign(session, {
158
+ id: config.generateId?.() || crypto.randomUUID(),
159
+ createdAt,
160
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
161
+ });
162
+ if (config.cookie !== false && hasWritableResponse$1(event)) {
163
+ const sealed = await sealJWESession(event, config);
164
+ session[kSessionToken$1] = sealed;
165
+ setChunkedCookie(event, sessionName, sealed, {
166
+ ...DEFAULT_COOKIE$1,
167
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3),
168
+ ...config.cookie
169
+ });
170
+ }
171
+ return session;
176
172
  }
173
+ /**
174
+ * Produce a JWE for the current session.
175
+ */
177
176
  async function sealJWESession(event, config) {
178
- const key = getEncryptKey(config.key);
179
- const sessionName = config.name || DEFAULT_NAME$1;
180
- const context = getEventContext(event);
181
- const session = context.sessions?.[sessionName] || await getJWESession(event, config);
182
- const iat = Math.floor(session.createdAt / 1e3);
183
- const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
184
- const payload = {
185
- ...session.data,
186
- jti: session.id,
187
- iat
188
- };
189
- if (exp) {
190
- payload.exp = exp;
191
- }
192
- let typ = void 0;
193
- if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
194
- typ = config.jwe.encryptOptions.protectedHeader.typ;
195
- }
196
- const token = await encrypt(payload, key, {
197
- ...config.jwe?.encryptOptions,
198
- expiresIn: void 0,
199
- // controlled via 'exp' claim
200
- protectedHeader: {
201
- ...config.jwe?.encryptOptions?.protectedHeader,
202
- typ: typ || "JWT",
203
- cty: "application/json"
204
- }
205
- });
206
- return token;
177
+ const key = getEncryptKey(config.key);
178
+ const sessionName = config.name || DEFAULT_NAME$1;
179
+ const session = getEventContext(event).sessions?.[sessionName] || await getJWESession(event, config);
180
+ const iat = Math.floor(session.createdAt / 1e3);
181
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
182
+ const payload = {
183
+ ...session.data,
184
+ jti: session.id,
185
+ iat
186
+ };
187
+ if (exp) payload.exp = exp;
188
+ let typ = void 0;
189
+ if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) typ = config.jwe.encryptOptions.protectedHeader.typ;
190
+ return await encrypt(payload, key, {
191
+ ...config.jwe?.encryptOptions,
192
+ expiresIn: void 0,
193
+ protectedHeader: {
194
+ ...config.jwe?.encryptOptions?.protectedHeader,
195
+ typ: typ || "JWT",
196
+ cty: "application/json"
197
+ }
198
+ });
207
199
  }
200
+ /**
201
+ * Decrypt the JWE and return a Session-compatible object.
202
+ */
208
203
  async function unsealJWESession(event, config, sealed) {
209
- const key = config.hooks?.onUnsealKeyLookup ? (header) => config.hooks.onUnsealKeyLookup({
210
- header,
211
- event,
212
- config
213
- }) : getDecryptKey(config.key);
214
- const alg = config.jwe?.encryptOptions?.alg;
215
- const enc = config.jwe?.encryptOptions?.enc;
216
- if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
217
- config.jwe.encryptOptions.protectedHeader.typ;
218
- }
219
- const { payload } = await decrypt(sealed, key, {
220
- ...config.jwe?.decryptOptions,
221
- requiredClaims: [
222
- .../* @__PURE__ */ new Set([
223
- ...config.jwe?.decryptOptions?.requiredClaims || [],
224
- "jti",
225
- "iat"
226
- ])
227
- ],
228
- maxTokenAge: config.maxAge,
229
- algorithms: alg ? [alg] : void 0,
230
- encryptionAlgorithms: enc ? [enc] : void 0,
231
- unwrappedKeyAlgorithm: void 0,
232
- keyUsage: void 0,
233
- forceUint8Array: false,
234
- validateJWT: true
235
- }).catch((error_) => {
236
- if (error_ instanceof Error) {
237
- throw error_;
238
- }
239
- throw new Error(String(error_));
240
- });
241
- const { jti, iat, exp, ...data } = payload;
242
- return {
243
- id: jti,
244
- createdAt: iat * 1e3,
245
- expiresAt: exp ? exp * 1e3 : void 0,
246
- data: data && typeof data === "object" ? data : new NullProtoObj()
247
- };
204
+ const key = config.hooks?.onUnsealKeyLookup ? (header) => config.hooks.onUnsealKeyLookup({
205
+ header,
206
+ event,
207
+ config
208
+ }) : getDecryptKey(config.key);
209
+ const alg = config.jwe?.encryptOptions?.alg;
210
+ const enc = config.jwe?.encryptOptions?.enc;
211
+ let typ = void 0;
212
+ if (config.jwe?.encryptOptions?.protectedHeader?.typ && typeof config.jwe.encryptOptions.protectedHeader.typ === "string" && config.jwe.encryptOptions.protectedHeader.typ.toLowerCase().includes("jwt")) typ = config.jwe.encryptOptions.protectedHeader.typ;
213
+ const { payload } = await decrypt(sealed, key, {
214
+ ...config.jwe?.decryptOptions,
215
+ requiredClaims: [...new Set([
216
+ ...config.jwe?.decryptOptions?.requiredClaims || [],
217
+ "jti",
218
+ "iat"
219
+ ])],
220
+ typ: typ || "JWT",
221
+ maxTokenAge: config.maxAge,
222
+ algorithms: alg ? [alg] : void 0,
223
+ encryptionAlgorithms: enc ? [enc] : void 0,
224
+ unwrappedKeyAlgorithm: void 0,
225
+ keyUsage: void 0,
226
+ forceUint8Array: false,
227
+ validateJWT: true
228
+ }).catch((error_) => {
229
+ if (error_ instanceof Error) throw error_;
230
+ throw new Error(String(error_));
231
+ });
232
+ const { jti, iat, exp, ...data } = payload;
233
+ return {
234
+ id: jti,
235
+ createdAt: iat * 1e3,
236
+ expiresAt: exp ? exp * 1e3 : void 0,
237
+ data: data && typeof data === "object" ? data : new NullProtoObj()
238
+ };
248
239
  }
240
+ /**
241
+ * Clear the session (delete from context and drop cookie).
242
+ */
249
243
  async function clearJWESession(event, config) {
250
- const context = getEventContext(event);
251
- const sessionName = config.name || DEFAULT_NAME$1;
252
- let session = context.sessions?.[sessionName];
253
- if (session && session[kGetSessionPromise$1]) {
254
- session = await session[kGetSessionPromise$1];
255
- }
256
- if (context.sessions?.[sessionName]) {
257
- delete context.sessions[sessionName];
258
- }
259
- if (config.cookie !== false && hasWritableResponse$1(event)) {
260
- setChunkedCookie(event, sessionName, "", {
261
- ...DEFAULT_COOKIE$1,
262
- ...config.cookie,
263
- expires: /* @__PURE__ */ new Date(0),
264
- maxAge: void 0
265
- });
266
- await config.hooks?.onClear?.({
267
- session,
268
- event,
269
- config
270
- });
271
- }
244
+ const context = getEventContext(event);
245
+ const sessionName = config.name || DEFAULT_NAME$1;
246
+ let session = context.sessions?.[sessionName];
247
+ if (session && session[kGetSessionPromise$1]) session = await session[kGetSessionPromise$1];
248
+ if (context.sessions?.[sessionName]) delete context.sessions[sessionName];
249
+ if (config.cookie !== false && hasWritableResponse$1(event)) {
250
+ setChunkedCookie(event, sessionName, "", {
251
+ ...DEFAULT_COOKIE$1,
252
+ ...config.cookie,
253
+ expires: /* @__PURE__ */ new Date(0),
254
+ maxAge: void 0
255
+ });
256
+ await config.hooks?.onClear?.({
257
+ session,
258
+ event,
259
+ config
260
+ });
261
+ }
272
262
  }
273
263
  function getSessionFromContext$1(event, sessionName) {
274
- const context = getEventContext(event);
275
- return context.sessions?.[sessionName];
264
+ return getEventContext(event).sessions?.[sessionName];
276
265
  }
277
266
  function hasWritableResponse$1(event) {
278
- return Boolean(event.res);
267
+ return Boolean(event.res);
279
268
  }
280
269
  function getEncryptKey(key) {
281
- if (!key) {
282
- throw new Error("Session: JWE key is required.");
283
- }
284
- let _key;
285
- if (typeof key === "string") {
286
- _key = key;
287
- } else if (isSymmetricJWK(key) || isPrivateJWK(key)) {
288
- _key = key;
289
- } else if ("publicKey" in key && isPublicJWK(key.publicKey)) {
290
- _key = key.publicKey;
291
- } else if ("privateKey" in key && isPrivateJWK(key.privateKey)) {
292
- _key = key.privateKey;
293
- }
294
- if (!_key) {
295
- throw new Error(
296
- "Session: Invalid JWE key. It must be a password string or valid JWK.",
297
- { cause: key }
298
- );
299
- }
300
- return _key;
270
+ if (!key) throw new Error("Session: JWE key is required.");
271
+ let _key;
272
+ if (typeof key === "string") _key = key;
273
+ else if (isSymmetricJWK(key) || isPrivateJWK(key)) _key = key;
274
+ else if ("publicKey" in key && isPublicJWK(key.publicKey)) _key = key.publicKey;
275
+ else if ("privateKey" in key && isPrivateJWK(key.privateKey)) _key = key.privateKey;
276
+ if (!_key) throw new Error("Session: Invalid JWE key. It must be a password string or valid JWK.", { cause: key });
277
+ return _key;
301
278
  }
302
279
  function getDecryptKey(key) {
303
- if (!key) {
304
- throw new Error("Session: JWE key is required.");
305
- }
306
- let _key = void 0;
307
- if (typeof key === "string") {
308
- _key = key;
309
- } else if (isSymmetricJWK(key)) {
310
- _key = key;
311
- } else if ("privateKey" in key) {
312
- _key = key.privateKey;
313
- }
314
- if (!_key) {
315
- throw new Error(
316
- "Session: Invalid JWE key. It must be a password string or a valid private JWK.",
317
- { cause: key }
318
- );
319
- }
320
- return _key;
280
+ if (!key) throw new Error("Session: JWE key is required.");
281
+ let _key = void 0;
282
+ if (typeof key === "string") _key = key;
283
+ else if (isSymmetricJWK(key)) _key = key;
284
+ else if ("privateKey" in key) _key = key.privateKey;
285
+ if (!_key) throw new Error("Session: Invalid JWE key. It must be a password string or a valid private JWK.", { cause: key });
286
+ return _key;
321
287
  }
322
288
  function findSetCookie$1(setCookie, name) {
323
- const regex = new RegExp(`(?:^|,\\s*)${name}=([^;]+)`);
324
- const match = setCookie.match(regex);
325
- return match ? match[1] : void 0;
289
+ const regex = new RegExp(`(?:^|,\\s*)${name}=([^;]+)`);
290
+ const match = setCookie.match(regex);
291
+ return match ? match[1] : void 0;
326
292
  }
327
-
293
+ //#endregion
294
+ //#region src/adapters/h3v2/session/jws.ts
328
295
  const kGetSessionPromise = Symbol("h3_jws_getSession");
296
+ const kSessionToken = Symbol("h3_jws_sessionToken");
329
297
  const DEFAULT_NAME = "h3-jws";
330
298
  const DEFAULT_COOKIE = {
331
- path: "/",
332
- secure: true,
333
- httpOnly: false
299
+ path: "/",
300
+ secure: true,
301
+ httpOnly: false
334
302
  };
335
303
  async function useJWSSession(event, config) {
336
- const sessionName = config.name || DEFAULT_NAME;
337
- await getJWSSession(event, config);
338
- const sessionManager = {
339
- get id() {
340
- return getSessionFromContext(event, sessionName)?.id;
341
- },
342
- get createdAt() {
343
- return getSessionFromContext(event, sessionName)?.createdAt ?? Date.now();
344
- },
345
- get expiresAt() {
346
- return getSessionFromContext(event, sessionName)?.expiresAt;
347
- },
348
- get data() {
349
- return getSessionFromContext(event, sessionName)?.data || {};
350
- },
351
- get token() {
352
- return getJWSSessionToken(event, config);
353
- },
354
- update: async (update) => {
355
- await updateJWSSession(event, config, update);
356
- return sessionManager;
357
- },
358
- clear: async () => {
359
- await clearJWSSession(event, config);
360
- return sessionManager;
361
- }
362
- };
363
- return sessionManager;
304
+ const sessionName = config.name || DEFAULT_NAME;
305
+ await getJWSSession(event, config);
306
+ const sessionManager = {
307
+ get id() {
308
+ return getSessionFromContext(event, sessionName)?.id;
309
+ },
310
+ get createdAt() {
311
+ return getSessionFromContext(event, sessionName)?.createdAt ?? Date.now();
312
+ },
313
+ get expiresAt() {
314
+ return getSessionFromContext(event, sessionName)?.expiresAt;
315
+ },
316
+ get data() {
317
+ return getSessionFromContext(event, sessionName)?.data || {};
318
+ },
319
+ get token() {
320
+ return getSessionFromContext(event, sessionName)?.[kSessionToken] ?? getJWSSessionToken(event, config);
321
+ },
322
+ update: async (update) => {
323
+ await updateJWSSession(event, config, update);
324
+ return sessionManager;
325
+ },
326
+ clear: async () => {
327
+ await clearJWSSession(event, config);
328
+ return sessionManager;
329
+ }
330
+ };
331
+ return sessionManager;
364
332
  }
365
333
  async function getJWSSession(event, config) {
366
- const sessionName = config.name || DEFAULT_NAME;
367
- const context = getEventContext(event);
368
- if (!context.sessions) {
369
- context.sessions = new NullProtoObj();
370
- }
371
- const existingSession = context.sessions[sessionName];
372
- if (existingSession) {
373
- const session2 = existingSession[kGetSessionPromise] ? await existingSession[kGetSessionPromise] : existingSession;
374
- if (session2.expiresAt !== void 0 && session2.expiresAt < Date.now() && hasWritableResponse(event)) {
375
- await config.hooks?.onExpire?.({
376
- event,
377
- error: new Error(
378
- `JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${new Date(session2.expiresAt * 1e3).toISOString()})`
379
- ),
380
- config
381
- });
382
- return clearJWSSession(event, config).then(
383
- () => getJWSSession(event, config)
384
- );
385
- }
386
- await config.hooks?.onRead?.({
387
- event,
388
- session: session2,
389
- config
390
- });
391
- return session2;
392
- }
393
- const session = {
394
- id: "",
395
- createdAt: 0,
396
- expiresAt: void 0,
397
- data: new NullProtoObj()
398
- };
399
- context.sessions[sessionName] = session;
400
- const token = getJWSSessionToken(event, config);
401
- if (token) {
402
- const promise = verifyJWSSession(event, config, token).catch(async (error_) => {
403
- if (error_ instanceof Error && (error_.message.includes("Token has expired") || error_.message.includes("Token is too old"))) {
404
- await config.hooks?.onExpire?.({
405
- event,
406
- error: error_,
407
- config
408
- });
409
- return void 0;
410
- }
411
- await config.hooks?.onError?.({
412
- event,
413
- error: error_,
414
- config
415
- });
416
- return void 0;
417
- }).then((unsealed) => {
418
- if (unsealed) {
419
- Object.assign(session, unsealed);
420
- }
421
- delete session[kGetSessionPromise];
422
- return session;
423
- });
424
- session[kGetSessionPromise] = promise;
425
- await promise;
426
- }
427
- if (!session.id) {
428
- await updateJWSSession(event, config);
429
- }
430
- await config.hooks?.onRead?.({
431
- event,
432
- session,
433
- config
434
- });
435
- return session;
334
+ const sessionName = config.name || DEFAULT_NAME;
335
+ const context = getEventContext(event);
336
+ if (!context.sessions) context.sessions = new NullProtoObj();
337
+ const existingSession = context.sessions[sessionName];
338
+ if (existingSession) {
339
+ const session = existingSession[kGetSessionPromise] ? await existingSession[kGetSessionPromise] : existingSession;
340
+ if (session.expiresAt !== void 0 && session.expiresAt < Date.now() && hasWritableResponse(event)) {
341
+ await config.hooks?.onExpire?.({
342
+ event,
343
+ error: /* @__PURE__ */ new Error(`JWT "exp" (Expiration Time) Claim validation failed: Token has expired (exp: ${(/* @__PURE__ */ new Date(session.expiresAt * 1e3)).toISOString()})`),
344
+ config
345
+ });
346
+ return clearJWSSession(event, config).then(() => getJWSSession(event, config));
347
+ }
348
+ await config.hooks?.onRead?.({
349
+ event,
350
+ session,
351
+ config
352
+ });
353
+ return session;
354
+ }
355
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
356
+ const createdAt = now - now % 1e3;
357
+ const session = {
358
+ id: void 0,
359
+ createdAt,
360
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3,
361
+ data: new NullProtoObj()
362
+ };
363
+ context.sessions[sessionName] = session;
364
+ const token = getJWSSessionToken(event, config);
365
+ if (token) {
366
+ session[kSessionToken] = token;
367
+ const promise = verifyJWSSession(event, config, token).catch(async (error_) => {
368
+ if (error_ instanceof Error && (error_.message.includes("Token has expired") || error_.message.includes("Token is too old"))) {
369
+ await config.hooks?.onExpire?.({
370
+ event,
371
+ error: error_,
372
+ config
373
+ });
374
+ return;
375
+ }
376
+ await config.hooks?.onError?.({
377
+ event,
378
+ error: error_,
379
+ config
380
+ });
381
+ }).then((unsealed) => {
382
+ if (unsealed) Object.assign(session, unsealed);
383
+ delete session[kGetSessionPromise];
384
+ return session;
385
+ });
386
+ session[kGetSessionPromise] = promise;
387
+ await promise;
388
+ }
389
+ await config.hooks?.onRead?.({
390
+ event,
391
+ session,
392
+ config
393
+ });
394
+ return session;
436
395
  }
437
396
  function getJWSSessionToken(event, config) {
438
- const sessionName = config.name || DEFAULT_NAME;
439
- let token;
440
- if (config.sessionHeader !== false) {
441
- const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
442
- const headerValue = event.req.headers.get(headerName);
443
- if (typeof headerValue === "string") {
444
- token = headerValue.startsWith("Bearer ") ? headerValue.slice(7).trim() : headerValue;
445
- }
446
- }
447
- if (config.cookie !== false && hasWritableResponse(event)) {
448
- const setCookie = event.res.headers.get("set-cookie");
449
- if (typeof setCookie === "string") {
450
- token = findSetCookie(setCookie, sessionName);
451
- }
452
- }
453
- if (!token) {
454
- token = getChunkedCookie(event, sessionName);
455
- }
456
- return token;
397
+ const sessionName = config.name || DEFAULT_NAME;
398
+ let token;
399
+ if (config.sessionHeader !== false) {
400
+ const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
401
+ const headerValue = event.req.headers.get(headerName);
402
+ if (typeof headerValue === "string") token = headerValue.startsWith("Bearer ") ? headerValue.slice(7).trim() : headerValue;
403
+ }
404
+ if (config.cookie !== false && hasWritableResponse(event)) {
405
+ const setCookie = event.res.headers.get("set-cookie");
406
+ if (typeof setCookie === "string") token = findSetCookie(setCookie, sessionName);
407
+ }
408
+ if (!token) token = getChunkedCookie(event, sessionName);
409
+ return token;
457
410
  }
458
411
  async function updateJWSSession(event, config, update) {
459
- const sessionName = config.name || DEFAULT_NAME;
460
- const context = getEventContext(event);
461
- const session = context.sessions?.[sessionName] || await getJWSSession(event, config);
462
- if (typeof update === "function") {
463
- update = update(session.data);
464
- }
465
- if (update) {
466
- const oldSession = { ...session, data: { ...session.data } };
467
- Object.assign(session.data, update);
468
- await config.hooks?.onUpdate?.({
469
- session,
470
- oldSession,
471
- event,
472
- config
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
- });
482
- if (config.cookie !== false && hasWritableResponse(event)) {
483
- const token = await signJWSSession(event, config);
484
- setChunkedCookie(event, sessionName, token, {
485
- ...DEFAULT_COOKIE,
486
- expires: config.maxAge === void 0 ? void 0 : new Date(
487
- session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
488
- ),
489
- ...config.cookie
490
- });
491
- }
492
- return session;
412
+ const sessionName = config.name || DEFAULT_NAME;
413
+ const session = getEventContext(event).sessions?.[sessionName] || await getJWSSession(event, config);
414
+ if (typeof update === "function") update = update(session.data);
415
+ if (update) {
416
+ const oldSession = {
417
+ ...session,
418
+ data: { ...session.data }
419
+ };
420
+ Object.assign(session.data, update);
421
+ await config.hooks?.onUpdate?.({
422
+ session,
423
+ oldSession,
424
+ event,
425
+ config
426
+ });
427
+ }
428
+ const now = config.jws?.signOptions?.currentDate?.getTime() ?? Date.now();
429
+ const createdAt = now - now % 1e3;
430
+ Object.assign(session, {
431
+ id: config.generateId?.() || crypto.randomUUID(),
432
+ createdAt,
433
+ expiresAt: config.maxAge === void 0 ? void 0 : createdAt + computeExpiresInSeconds(config.maxAge) * 1e3
434
+ });
435
+ if (config.cookie !== false && hasWritableResponse(event)) {
436
+ const token = await signJWSSession(event, config);
437
+ session[kSessionToken] = token;
438
+ setChunkedCookie(event, sessionName, token, {
439
+ ...DEFAULT_COOKIE,
440
+ expires: config.maxAge === void 0 ? void 0 : new Date(session.createdAt + computeExpiresInSeconds(config.maxAge) * 1e3),
441
+ ...config.cookie
442
+ });
443
+ }
444
+ return session;
493
445
  }
494
446
  async function signJWSSession(event, config) {
495
- const key = getSignKey(config.key);
496
- const sessionName = config.name || DEFAULT_NAME;
497
- const context = getEventContext(event);
498
- const session = context.sessions?.[sessionName] || await getJWSSession(event, config);
499
- const iat = Math.floor(session.createdAt / 1e3);
500
- const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
501
- const payload = {
502
- ...session.data,
503
- jti: session.id,
504
- iat
505
- };
506
- if (exp) {
507
- payload.exp = exp;
508
- }
509
- let typ;
510
- if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
511
- typ = config.jws.signOptions.protectedHeader.typ;
512
- }
513
- const token = await sign(payload, key, {
514
- ...config.jws?.signOptions,
515
- expiresIn: void 0,
516
- // controlled via 'exp' claim
517
- protectedHeader: {
518
- ...config.jws?.signOptions?.protectedHeader,
519
- kid: "kid" in key ? key.kid : void 0,
520
- typ: typ || "JWT",
521
- cty: "application/json"
522
- }
523
- });
524
- return token;
447
+ const key = getSignKey(config.key);
448
+ const sessionName = config.name || DEFAULT_NAME;
449
+ const session = getEventContext(event).sessions?.[sessionName] || await getJWSSession(event, config);
450
+ const iat = Math.floor(session.createdAt / 1e3);
451
+ const exp = session.expiresAt ? Math.floor(session.expiresAt / 1e3) : void 0;
452
+ const payload = {
453
+ ...session.data,
454
+ jti: session.id,
455
+ iat
456
+ };
457
+ if (exp) payload.exp = exp;
458
+ let typ;
459
+ if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) typ = config.jws.signOptions.protectedHeader.typ;
460
+ return await sign(payload, key, {
461
+ ...config.jws?.signOptions,
462
+ expiresIn: void 0,
463
+ protectedHeader: {
464
+ ...config.jws?.signOptions?.protectedHeader,
465
+ kid: "kid" in key ? key.kid : void 0,
466
+ typ: typ || "JWT",
467
+ cty: "application/json"
468
+ }
469
+ });
525
470
  }
526
471
  async function verifyJWSSession(event, config, token) {
527
- const alg = config.jws?.signOptions?.alg;
528
- const jwk = config.hooks?.onVerifyKeyLookup ? (header) => config.hooks.onVerifyKeyLookup({
529
- header,
530
- event,
531
- config
532
- }) : getVerifyKey(config.key);
533
- let typ;
534
- if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) {
535
- typ = config.jws.signOptions.protectedHeader.typ;
536
- }
537
- const { payload } = await verify(token, jwk, {
538
- ...config.jws?.verifyOptions,
539
- requiredClaims: [
540
- .../* @__PURE__ */ new Set([
541
- ...config.jws?.verifyOptions?.requiredClaims || [],
542
- "jti",
543
- "iat"
544
- ])
545
- ],
546
- typ: typ || "JWT",
547
- algorithms: alg ? [alg] : void 0,
548
- forceUint8Array: false,
549
- validateJWT: true
550
- }).catch((error_) => {
551
- const message = error_ instanceof Error ? error_.message : String(error_);
552
- throw new Error(`Invalid session token: ${message}`);
553
- });
554
- const { jti, iat, exp, ...data } = payload;
555
- return {
556
- id: jti,
557
- createdAt: iat * 1e3,
558
- expiresAt: exp ? exp * 1e3 : void 0,
559
- data: data && typeof data === "object" ? data : new NullProtoObj()
560
- };
472
+ const alg = config.jws?.signOptions?.alg;
473
+ const jwk = config.hooks?.onVerifyKeyLookup ? (header) => config.hooks.onVerifyKeyLookup({
474
+ header,
475
+ event,
476
+ config
477
+ }) : getVerifyKey(config.key);
478
+ let typ;
479
+ if (config.jws?.signOptions?.protectedHeader?.typ && typeof config.jws.signOptions.protectedHeader.typ === "string" && config.jws.signOptions.protectedHeader.typ.toLowerCase().includes("jwt")) typ = config.jws.signOptions.protectedHeader.typ;
480
+ const { payload } = await verify(token, jwk, {
481
+ ...config.jws?.verifyOptions,
482
+ requiredClaims: [...new Set([
483
+ ...config.jws?.verifyOptions?.requiredClaims || [],
484
+ "jti",
485
+ "iat"
486
+ ])],
487
+ typ: typ || "JWT",
488
+ algorithms: alg ? [alg] : void 0,
489
+ forceUint8Array: false,
490
+ validateJWT: true
491
+ }).catch((error_) => {
492
+ const message = error_ instanceof Error ? error_.message : String(error_);
493
+ throw new Error(`Invalid session token: ${message}`);
494
+ });
495
+ const { jti, iat, exp, ...data } = payload;
496
+ return {
497
+ id: jti,
498
+ createdAt: iat * 1e3,
499
+ expiresAt: exp ? exp * 1e3 : void 0,
500
+ data: data && typeof data === "object" ? data : new NullProtoObj()
501
+ };
561
502
  }
562
503
  async function clearJWSSession(event, config) {
563
- const context = getEventContext(event);
564
- const sessionName = config.name || DEFAULT_NAME;
565
- let session = context.sessions?.[sessionName];
566
- if (session && session[kGetSessionPromise]) {
567
- session = await session[kGetSessionPromise];
568
- }
569
- if (context.sessions?.[sessionName]) {
570
- delete context.sessions[sessionName];
571
- }
572
- if (config.cookie !== false && hasWritableResponse(event)) {
573
- setChunkedCookie(event, sessionName, "", {
574
- ...DEFAULT_COOKIE,
575
- ...config.cookie,
576
- expires: /* @__PURE__ */ new Date(0),
577
- maxAge: void 0
578
- });
579
- await config.hooks?.onClear?.({
580
- session,
581
- event,
582
- config
583
- });
584
- }
504
+ const context = getEventContext(event);
505
+ const sessionName = config.name || DEFAULT_NAME;
506
+ let session = context.sessions?.[sessionName];
507
+ if (session && session[kGetSessionPromise]) session = await session[kGetSessionPromise];
508
+ if (context.sessions?.[sessionName]) delete context.sessions[sessionName];
509
+ if (config.cookie !== false && hasWritableResponse(event)) {
510
+ setChunkedCookie(event, sessionName, "", {
511
+ ...DEFAULT_COOKIE,
512
+ ...config.cookie,
513
+ expires: /* @__PURE__ */ new Date(0),
514
+ maxAge: void 0
515
+ });
516
+ await config.hooks?.onClear?.({
517
+ session,
518
+ event,
519
+ config
520
+ });
521
+ }
585
522
  }
586
523
  function getSessionFromContext(event, sessionName) {
587
- const context = getEventContext(event);
588
- return context.sessions?.[sessionName];
524
+ return getEventContext(event).sessions?.[sessionName];
589
525
  }
590
526
  function hasWritableResponse(event) {
591
- return Boolean(event.res);
527
+ return Boolean(event.res);
592
528
  }
593
529
  function getSignKey(key) {
594
- if (!key) {
595
- throw new Error("Session: JWS key is required.");
596
- }
597
- let _key;
598
- if (isSymmetricJWK(key)) {
599
- _key = key;
600
- } else if ("privateKey" in key && isPrivateJWK(key.privateKey)) {
601
- _key = key.privateKey;
602
- }
603
- if (!_key) {
604
- throw new Error(
605
- "Session: Invalid JWS key. It must be a symmetric JWK or a private JWK.",
606
- { cause: key }
607
- );
608
- }
609
- return _key;
530
+ if (!key) throw new Error("Session: JWS key is required.");
531
+ let _key;
532
+ if (isSymmetricJWK(key)) _key = key;
533
+ else if ("privateKey" in key && isPrivateJWK(key.privateKey)) _key = key.privateKey;
534
+ if (!_key) throw new Error("Session: Invalid JWS key. It must be a symmetric JWK or a private JWK.", { cause: key });
535
+ return _key;
610
536
  }
611
537
  function getVerifyKey(key) {
612
- if (!key) {
613
- throw new Error("Session: JWS key is required.");
614
- }
615
- let _key;
616
- if (isSymmetricJWK(key)) {
617
- _key = key;
618
- } else if ("publicKey" in key) {
619
- const publicKey = key.publicKey;
620
- if (isPublicJWK(publicKey)) {
621
- _key = publicKey;
622
- } else if (Array.isArray(publicKey)) {
623
- const keys = publicKey.filter(
624
- (candidate) => isPublicJWK(candidate)
625
- );
626
- if (keys.length > 0) {
627
- _key = { keys };
628
- }
629
- } else if (publicKey && typeof publicKey === "object" && Array.isArray(publicKey.keys)) {
630
- const keys = publicKey.keys.filter(
631
- (candidate) => isPublicJWK(candidate)
632
- );
633
- if (keys.length > 0) {
634
- _key = { keys };
635
- }
636
- }
637
- }
638
- if (!_key) {
639
- throw new Error(
640
- "Session: Invalid JWS key. It must be a symmetric JWK or a public JWK/set.",
641
- { cause: key }
642
- );
643
- }
644
- return _key;
538
+ if (!key) throw new Error("Session: JWS key is required.");
539
+ let _key;
540
+ if (isSymmetricJWK(key)) _key = key;
541
+ else if ("publicKey" in key) {
542
+ const publicKey = key.publicKey;
543
+ if (isPublicJWK(publicKey)) _key = publicKey;
544
+ else if (Array.isArray(publicKey)) {
545
+ const keys = publicKey.filter((candidate) => isPublicJWK(candidate));
546
+ if (keys.length > 0) _key = { keys };
547
+ } else if (publicKey && typeof publicKey === "object" && Array.isArray(publicKey.keys)) {
548
+ const keys = publicKey.keys.filter((candidate) => isPublicJWK(candidate));
549
+ if (keys.length > 0) _key = { keys };
550
+ }
551
+ }
552
+ if (!_key) throw new Error("Session: Invalid JWS key. It must be a symmetric JWK or a public JWK/set.", { cause: key });
553
+ return _key;
645
554
  }
646
555
  function findSetCookie(setCookie, name) {
647
- const regex = new RegExp(`(?:^|,\\s*)${name}=([^;]+)`);
648
- const match = setCookie.match(regex);
649
- return match ? match[1] : void 0;
556
+ const regex = new RegExp(`(?:^|,\\s*)${name}=([^;]+)`);
557
+ const match = setCookie.match(regex);
558
+ return match ? match[1] : void 0;
650
559
  }
651
-
652
- export { clearJWESession, clearJWSSession, getJWESession, getJWESessionToken, getJWSSession, getJWSSessionToken, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
560
+ //#endregion
561
+ export { clearJWESession, clearJWSSession, deriveJWKFromPassword, exportJWKToPEM, generateJWK, getJWESession, getJWESessionToken, getJWSSession, getJWSSessionToken, importJWKFromPEM, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };