unjwt 0.5.20 → 0.5.21
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 +55 -0
- package/dist/h3.d.mts +185 -0
- package/dist/h3.mjs +460 -0
- package/dist/index.d.mts +7 -5
- package/dist/index.mjs +4 -4
- package/dist/jwe.d.mts +3 -2
- package/dist/jwe.mjs +3 -3
- package/dist/jwk.d.mts +2 -2
- package/dist/jwk.mjs +2 -2
- package/dist/jws.d.mts +3 -2
- package/dist/jws.mjs +3 -3
- package/dist/shared/unjwt.9mL1vSFP.d.mts +154 -0
- package/dist/shared/{unjwt.ChCsF4q6.d.mts → unjwt.BjP_dGnX.d.mts} +6 -2
- package/dist/shared/{unjwt.BuQdWrN5.d.mts → unjwt.BrNGm4On.d.mts} +1 -1
- package/dist/shared/unjwt.BsLgL0DQ.d.mts +64 -0
- package/dist/shared/unjwt.C5jNR_1K.d.mts +166 -0
- package/dist/shared/{unjwt.CaZJRuRa.mjs → unjwt.CXl9FD-V.mjs} +2 -2
- package/dist/shared/{unjwt.Dy68jE0N.d.mts → unjwt.CtHrClHc.d.mts} +5 -1
- package/dist/shared/{unjwt.K_6rgVTy.mjs → unjwt.DCiTNyCM.mjs} +1 -1
- package/dist/shared/{unjwt.Dc_YDjvO.mjs → unjwt.DmRAjMp8.mjs} +3 -5
- package/dist/shared/unjwt.VUfJDcCJ.d.mts +161 -0
- package/dist/shared/{unjwt.D7ya3PwS.mjs → unjwt.Vg02PZWw.mjs} +10 -21
- package/dist/utils.d.mts +2 -2
- package/dist/utils.mjs +1 -1
- package/package.json +19 -1
- package/dist/shared/unjwt.BFtNZVqt.d.mts +0 -252
- package/dist/shared/unjwt.DANsaNYC.d.mts +0 -166
package/dist/h3.mjs
ADDED
|
@@ -0,0 +1,460 @@
|
|
|
1
|
+
export { k as deriveJWKFromPassword, h as exportJWKToPEM, c as generateJWK, f as importJWKFromPEM } from './shared/unjwt.Vg02PZWw.mjs';
|
|
2
|
+
import { isEvent, setCookie } from 'h3';
|
|
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';
|
|
7
|
+
|
|
8
|
+
/**
|
|
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
|
|
11
|
+
* @license MIT https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/LICENSE
|
|
12
|
+
*/
|
|
13
|
+
const kGetSessionPromise$1 = Symbol("h3_jwe_getSession");
|
|
14
|
+
const DEFAULT_NAME$1 = "h3-jwe";
|
|
15
|
+
const DEFAULT_COOKIE$1 = {
|
|
16
|
+
path: "/",
|
|
17
|
+
secure: true,
|
|
18
|
+
httpOnly: true
|
|
19
|
+
};
|
|
20
|
+
async function useJWESession(event, config) {
|
|
21
|
+
const sessionName = config.name || DEFAULT_NAME$1;
|
|
22
|
+
await getJWESession(event, config);
|
|
23
|
+
const sessionManager = {
|
|
24
|
+
get id() {
|
|
25
|
+
return event.context.sessions?.[sessionName]?.id;
|
|
26
|
+
},
|
|
27
|
+
get createdAt() {
|
|
28
|
+
return event.context.sessions?.[sessionName]?.createdAt || Date.now();
|
|
29
|
+
},
|
|
30
|
+
get expiresAt() {
|
|
31
|
+
return event.context.sessions?.[sessionName]?.expiresAt;
|
|
32
|
+
},
|
|
33
|
+
get data() {
|
|
34
|
+
return event.context.sessions?.[sessionName]?.data || {};
|
|
35
|
+
},
|
|
36
|
+
update: async (update) => {
|
|
37
|
+
if (!isEvent(event)) {
|
|
38
|
+
throw new Error("[h3] Cannot update read-only session.");
|
|
39
|
+
}
|
|
40
|
+
await updateJWESession(event, config, update);
|
|
41
|
+
return sessionManager;
|
|
42
|
+
},
|
|
43
|
+
clear: () => {
|
|
44
|
+
if (!isEvent(event)) {
|
|
45
|
+
throw new Error("[h3] Cannot clear read-only session.");
|
|
46
|
+
}
|
|
47
|
+
clearJWESession(event, config);
|
|
48
|
+
return Promise.resolve(sessionManager);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
return sessionManager;
|
|
52
|
+
}
|
|
53
|
+
async function getJWESession(event, config) {
|
|
54
|
+
const sessionName = config.name || DEFAULT_NAME$1;
|
|
55
|
+
if (!event.context.sessions) {
|
|
56
|
+
event.context.sessions = /* @__PURE__ */ Object.create(null);
|
|
57
|
+
}
|
|
58
|
+
const existingSession = event.context.sessions[sessionName];
|
|
59
|
+
if (existingSession) {
|
|
60
|
+
return existingSession.expiresAt === void 0 ? existingSession[kGetSessionPromise$1] || existingSession : existingSession.expiresAt < Date.now() && isEvent(event) ? clearJWESession(event, config).then(
|
|
61
|
+
() => getJWESession(event, config)
|
|
62
|
+
) : existingSession[kGetSessionPromise$1] || existingSession;
|
|
63
|
+
}
|
|
64
|
+
const session = {
|
|
65
|
+
id: "",
|
|
66
|
+
createdAt: 0,
|
|
67
|
+
expiresAt: void 0,
|
|
68
|
+
data: /* @__PURE__ */ Object.create(null)
|
|
69
|
+
};
|
|
70
|
+
event.context.sessions[sessionName] = session;
|
|
71
|
+
let jweToken;
|
|
72
|
+
if (config.sessionHeader !== false) {
|
|
73
|
+
const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
|
|
74
|
+
const headerValue = _getReqHeader$1(event, headerName);
|
|
75
|
+
if (typeof headerValue === "string") {
|
|
76
|
+
jweToken = headerValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (!jweToken) {
|
|
80
|
+
const cookieHeader = _getReqHeader$1(event, "cookie");
|
|
81
|
+
if (cookieHeader) {
|
|
82
|
+
jweToken = parse(String(cookieHeader))[sessionName];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (jweToken) {
|
|
86
|
+
const promise = unsealJWESession(event, config, jweToken).catch(() => {
|
|
87
|
+
}).then((unsealed) => {
|
|
88
|
+
if (unsealed) {
|
|
89
|
+
Object.assign(session, unsealed);
|
|
90
|
+
}
|
|
91
|
+
delete event.context.sessions[sessionName][kGetSessionPromise$1];
|
|
92
|
+
return session;
|
|
93
|
+
});
|
|
94
|
+
event.context.sessions[sessionName][kGetSessionPromise$1] = promise;
|
|
95
|
+
await promise;
|
|
96
|
+
}
|
|
97
|
+
if (!session.id) {
|
|
98
|
+
if (!isEvent(event)) {
|
|
99
|
+
throw new Error(
|
|
100
|
+
"Cannot initialize a new session. Use `useSession(event)` within the main handler."
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
session.id = config.generateId?.() ?? (config.crypto || crypto).randomUUID();
|
|
104
|
+
session.createdAt = config.jwe?.encryptOptions?.currentDate === void 0 ? Date.now() : config.jwe.encryptOptions.currentDate.getTime();
|
|
105
|
+
session.expiresAt = config.maxAge ? session.createdAt + config.maxAge * 1e3 : void 0;
|
|
106
|
+
session.data = /* @__PURE__ */ Object.create(null);
|
|
107
|
+
await updateJWESession(event, config);
|
|
108
|
+
}
|
|
109
|
+
return session;
|
|
110
|
+
}
|
|
111
|
+
function _getReqHeader$1(event, name) {
|
|
112
|
+
if (event.node) {
|
|
113
|
+
return event.node?.req.headers[name];
|
|
114
|
+
}
|
|
115
|
+
if (event.request) {
|
|
116
|
+
return event.request.headers?.get(name);
|
|
117
|
+
}
|
|
118
|
+
if (event.headers) {
|
|
119
|
+
return event.headers.get(name);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function updateJWESession(event, config, update) {
|
|
123
|
+
const sessionName = config.name || DEFAULT_NAME$1;
|
|
124
|
+
const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
|
|
125
|
+
if (typeof update === "function") {
|
|
126
|
+
update = update(session.data);
|
|
127
|
+
}
|
|
128
|
+
if (update) {
|
|
129
|
+
Object.assign(session.data, update);
|
|
130
|
+
}
|
|
131
|
+
if (config.cookie !== false) {
|
|
132
|
+
const sealed = await sealJWESession(event, config);
|
|
133
|
+
setCookie(event, sessionName, sealed, {
|
|
134
|
+
...DEFAULT_COOKIE$1,
|
|
135
|
+
...config.cookie,
|
|
136
|
+
expires: config.maxAge ? new Date(
|
|
137
|
+
// createdAt is ms, maxAge is seconds
|
|
138
|
+
session.createdAt + config.maxAge * 1e3
|
|
139
|
+
) : void 0
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return session;
|
|
143
|
+
}
|
|
144
|
+
async function sealJWESession(event, config) {
|
|
145
|
+
const key = getEncryptKey(config.secret);
|
|
146
|
+
if (typeof key !== "string" && !isSymmetricJWK(key) && !isPrivateJWK(key)) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
"Session: JWE secret must be a password string or a private JWK."
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
const sessionName = config.name || DEFAULT_NAME$1;
|
|
152
|
+
const session = event.context.sessions?.[sessionName] || await getJWESession(event, config);
|
|
153
|
+
const iatSeconds = Math.floor(session.createdAt / 1e3);
|
|
154
|
+
const expSeconds = config.maxAge == null ? void 0 : iatSeconds + config.maxAge;
|
|
155
|
+
const payload = {
|
|
156
|
+
...session.data,
|
|
157
|
+
jti: session.id,
|
|
158
|
+
iat: iatSeconds
|
|
159
|
+
};
|
|
160
|
+
if (expSeconds) {
|
|
161
|
+
payload.exp = expSeconds;
|
|
162
|
+
}
|
|
163
|
+
const token = await encrypt(payload, key, {
|
|
164
|
+
...config.jwe?.encryptOptions,
|
|
165
|
+
protectedHeader: {
|
|
166
|
+
...config.jwe?.encryptOptions?.protectedHeader,
|
|
167
|
+
typ: "JWT",
|
|
168
|
+
cty: "application/json"
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return token;
|
|
172
|
+
}
|
|
173
|
+
async function unsealJWESession(_event, config, sealed) {
|
|
174
|
+
const key = getDecryptKey(config.secret);
|
|
175
|
+
if (typeof key !== "string" && !isSymmetricJWK(key) && !isPrivateJWK(key)) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
"Session: JWE secret must be a password string or a private JWK."
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const alg = config.jwe?.encryptOptions?.alg;
|
|
181
|
+
const enc = config.jwe?.encryptOptions?.enc;
|
|
182
|
+
const { payload } = await decrypt(sealed, key, {
|
|
183
|
+
...config.jwe?.decryptOptions,
|
|
184
|
+
requiredClaims: [
|
|
185
|
+
...config.jwe?.decryptOptions?.requiredClaims?.filter(
|
|
186
|
+
(claim) => claim !== "jti" && claim !== "iat"
|
|
187
|
+
) || [],
|
|
188
|
+
"jti",
|
|
189
|
+
"iat"
|
|
190
|
+
],
|
|
191
|
+
maxTokenAge: config.maxAge,
|
|
192
|
+
algorithms: alg ? [alg] : void 0,
|
|
193
|
+
encryptionAlgorithms: enc ? [enc] : void 0,
|
|
194
|
+
unwrappedKeyAlgorithm: void 0,
|
|
195
|
+
keyUsage: void 0,
|
|
196
|
+
forceUint8Array: false,
|
|
197
|
+
validateJWT: true
|
|
198
|
+
}).catch((error_) => {
|
|
199
|
+
if (error_ instanceof Error) {
|
|
200
|
+
const message = error_.message;
|
|
201
|
+
throw new Error(`Invalid session token: ${message}`);
|
|
202
|
+
}
|
|
203
|
+
throw new Error(error_);
|
|
204
|
+
});
|
|
205
|
+
const { jti, iat, ...data } = payload;
|
|
206
|
+
return {
|
|
207
|
+
id: jti,
|
|
208
|
+
createdAt: iat * 1e3,
|
|
209
|
+
// Convert back to ms
|
|
210
|
+
expiresAt: payload.exp ? payload.exp * 1e3 : void 0,
|
|
211
|
+
data: data && typeof data === "object" ? data : /* @__PURE__ */ Object.create(null)
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function clearJWESession(event, config) {
|
|
215
|
+
const sessionName = config.name || DEFAULT_NAME$1;
|
|
216
|
+
if (event.context.sessions?.[sessionName]) {
|
|
217
|
+
delete event.context.sessions[sessionName];
|
|
218
|
+
}
|
|
219
|
+
setCookie(event, sessionName, "", {
|
|
220
|
+
...DEFAULT_COOKIE$1,
|
|
221
|
+
...config.cookie,
|
|
222
|
+
// Optionally set expires in the past
|
|
223
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
224
|
+
});
|
|
225
|
+
return Promise.resolve();
|
|
226
|
+
}
|
|
227
|
+
function getEncryptKey(secret) {
|
|
228
|
+
if (typeof secret === "string") {
|
|
229
|
+
return secret;
|
|
230
|
+
} else if (isSymmetricJWK(secret)) {
|
|
231
|
+
return secret;
|
|
232
|
+
}
|
|
233
|
+
return secret.publicKey || secret.privateKey;
|
|
234
|
+
}
|
|
235
|
+
function getDecryptKey(secret) {
|
|
236
|
+
if (typeof secret === "string") {
|
|
237
|
+
return secret;
|
|
238
|
+
} else if ("privateKey" in secret) {
|
|
239
|
+
return secret.privateKey;
|
|
240
|
+
}
|
|
241
|
+
return secret;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* This is a fork of h3 library's session utility functions.
|
|
246
|
+
* @source https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/test/session.test.ts
|
|
247
|
+
* @license MIT https://github.com/h3js/h3/blob/b4dce71c256911335f3402d09f30ffad120ad61a/LICENSE
|
|
248
|
+
*/
|
|
249
|
+
const kGetSessionPromise = Symbol("h3_jws_getSession");
|
|
250
|
+
const DEFAULT_NAME = "h3-jws";
|
|
251
|
+
const DEFAULT_COOKIE = {
|
|
252
|
+
path: "/",
|
|
253
|
+
secure: true,
|
|
254
|
+
httpOnly: false
|
|
255
|
+
};
|
|
256
|
+
async function useJWSSession(event, config) {
|
|
257
|
+
const sessionName = config.name || DEFAULT_NAME;
|
|
258
|
+
await getJWSSession(event, config);
|
|
259
|
+
const sessionManager = {
|
|
260
|
+
get id() {
|
|
261
|
+
return event.context.sessions?.[sessionName]?.id;
|
|
262
|
+
},
|
|
263
|
+
get createdAt() {
|
|
264
|
+
return event.context.sessions?.[sessionName]?.createdAt || Date.now();
|
|
265
|
+
},
|
|
266
|
+
get expiresAt() {
|
|
267
|
+
return event.context.sessions?.[sessionName]?.expiresAt;
|
|
268
|
+
},
|
|
269
|
+
get data() {
|
|
270
|
+
return event.context.sessions?.[sessionName]?.data || {};
|
|
271
|
+
},
|
|
272
|
+
update: async (update) => {
|
|
273
|
+
if (!isEvent(event)) {
|
|
274
|
+
throw new Error("[h3] Cannot update read-only session.");
|
|
275
|
+
}
|
|
276
|
+
await updateJWSSession(event, config, update);
|
|
277
|
+
return sessionManager;
|
|
278
|
+
},
|
|
279
|
+
clear: () => {
|
|
280
|
+
if (!isEvent(event)) {
|
|
281
|
+
throw new Error("[h3] Cannot clear read-only session.");
|
|
282
|
+
}
|
|
283
|
+
clearJWSSession(event, config);
|
|
284
|
+
return Promise.resolve(sessionManager);
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
return sessionManager;
|
|
288
|
+
}
|
|
289
|
+
async function getJWSSession(event, config) {
|
|
290
|
+
const sessionName = config.name || DEFAULT_NAME;
|
|
291
|
+
if (!event.context.sessions) {
|
|
292
|
+
event.context.sessions = /* @__PURE__ */ Object.create(null);
|
|
293
|
+
}
|
|
294
|
+
const existingSession = event.context.sessions[sessionName];
|
|
295
|
+
if (existingSession) {
|
|
296
|
+
return existingSession.expiresAt === void 0 ? existingSession[kGetSessionPromise] || existingSession : existingSession.expiresAt < Date.now() && isEvent(event) ? clearJWSSession(event, config).then(
|
|
297
|
+
() => getJWSSession(event, config)
|
|
298
|
+
) : existingSession[kGetSessionPromise] || existingSession;
|
|
299
|
+
}
|
|
300
|
+
const session = {
|
|
301
|
+
id: "",
|
|
302
|
+
createdAt: 0,
|
|
303
|
+
expiresAt: void 0,
|
|
304
|
+
data: /* @__PURE__ */ Object.create(null)
|
|
305
|
+
};
|
|
306
|
+
event.context.sessions[sessionName] = session;
|
|
307
|
+
let token;
|
|
308
|
+
if (config.sessionHeader !== false) {
|
|
309
|
+
const headerName = typeof config.sessionHeader === "string" ? config.sessionHeader.toLowerCase() : `x-${sessionName.toLowerCase()}-session`;
|
|
310
|
+
const headerValue = _getReqHeader(event, headerName);
|
|
311
|
+
if (typeof headerValue === "string") {
|
|
312
|
+
token = headerValue;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (!token) {
|
|
316
|
+
const cookieHeader = _getReqHeader(event, "cookie");
|
|
317
|
+
if (cookieHeader) {
|
|
318
|
+
token = parse(String(cookieHeader))[sessionName];
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (token) {
|
|
322
|
+
const promise = verifyJWSSession(event, config, token).catch(() => {
|
|
323
|
+
}).then((unsealed) => {
|
|
324
|
+
if (unsealed) {
|
|
325
|
+
Object.assign(session, unsealed);
|
|
326
|
+
}
|
|
327
|
+
delete event.context.sessions[sessionName][kGetSessionPromise];
|
|
328
|
+
return session;
|
|
329
|
+
});
|
|
330
|
+
session[kGetSessionPromise] = promise;
|
|
331
|
+
await promise;
|
|
332
|
+
}
|
|
333
|
+
if (!session.id) {
|
|
334
|
+
if (!isEvent(event)) {
|
|
335
|
+
throw new Error(
|
|
336
|
+
"Cannot initialize a new session outside main handler. Use `useJWSSession(event)` properly."
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
session.id = config.generateId?.() ?? (config.crypto || globalThis.crypto).randomUUID();
|
|
340
|
+
session.createdAt = config.jws?.signOptions?.currentDate === void 0 ? Date.now() : config.jws.signOptions.currentDate.getTime();
|
|
341
|
+
session.expiresAt = config.maxAge ? session.createdAt + config.maxAge * 1e3 : void 0;
|
|
342
|
+
session.data = /* @__PURE__ */ Object.create(null);
|
|
343
|
+
await updateJWSSession(event, config);
|
|
344
|
+
}
|
|
345
|
+
return session;
|
|
346
|
+
}
|
|
347
|
+
function _getReqHeader(event, name) {
|
|
348
|
+
if (event.node) {
|
|
349
|
+
return event.node.req.headers[name];
|
|
350
|
+
}
|
|
351
|
+
if (event.request) {
|
|
352
|
+
return event.request.headers.get(name);
|
|
353
|
+
}
|
|
354
|
+
if (event.headers) {
|
|
355
|
+
return event.headers.get(name);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async function updateJWSSession(event, config, update) {
|
|
359
|
+
const sessionName = config.name || DEFAULT_NAME;
|
|
360
|
+
const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
|
|
361
|
+
if (typeof update === "function") {
|
|
362
|
+
update = update(session.data);
|
|
363
|
+
}
|
|
364
|
+
if (update) {
|
|
365
|
+
Object.assign(session.data, update);
|
|
366
|
+
}
|
|
367
|
+
if (config.cookie !== false) {
|
|
368
|
+
const token = await signJWSSession(event, config);
|
|
369
|
+
setCookie(event, sessionName, token, {
|
|
370
|
+
...DEFAULT_COOKIE,
|
|
371
|
+
...config.cookie,
|
|
372
|
+
expires: config.maxAge ? new Date(session.createdAt + config.maxAge * 1e3) : void 0
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
return session;
|
|
376
|
+
}
|
|
377
|
+
async function signJWSSession(event, config) {
|
|
378
|
+
const key = getSignKey(config.key);
|
|
379
|
+
if (isAsymmetricJWK(key) && !isPrivateJWK(key)) {
|
|
380
|
+
throw new Error("Session: JWS key cannot be a public asymmetric JWK.");
|
|
381
|
+
}
|
|
382
|
+
const sessionName = config.name || DEFAULT_NAME;
|
|
383
|
+
const session = event.context.sessions?.[sessionName] || await getJWSSession(event, config);
|
|
384
|
+
const iatSeconds = Math.floor(session.createdAt / 1e3);
|
|
385
|
+
const expSeconds = config.maxAge == null ? void 0 : iatSeconds + config.maxAge;
|
|
386
|
+
const payload = {
|
|
387
|
+
...session.data,
|
|
388
|
+
jti: session.id,
|
|
389
|
+
iat: iatSeconds
|
|
390
|
+
};
|
|
391
|
+
if (expSeconds) {
|
|
392
|
+
payload.exp = expSeconds;
|
|
393
|
+
}
|
|
394
|
+
const token = await sign(payload, key, {
|
|
395
|
+
...config.jws?.signOptions,
|
|
396
|
+
protectedHeader: {
|
|
397
|
+
...config.jws?.signOptions?.protectedHeader,
|
|
398
|
+
typ: "JWT"
|
|
399
|
+
}
|
|
400
|
+
});
|
|
401
|
+
return token;
|
|
402
|
+
}
|
|
403
|
+
async function verifyJWSSession(_event, config, token) {
|
|
404
|
+
const alg = config.jws?.signOptions?.alg;
|
|
405
|
+
const jwk = getVerifyKey(config.key);
|
|
406
|
+
if (isAsymmetricJWK(jwk) && isPrivateJWK(jwk)) {
|
|
407
|
+
throw new Error("Session: JWS key cannot be a private asymmetric JWK.");
|
|
408
|
+
}
|
|
409
|
+
const { payload } = await verify(token, jwk, {
|
|
410
|
+
...config.jws?.verifyOptions,
|
|
411
|
+
requiredClaims: [
|
|
412
|
+
...config.jws?.verifyOptions?.requiredClaims?.filter(
|
|
413
|
+
(claim) => claim !== "jti" && claim !== "iat"
|
|
414
|
+
) || [],
|
|
415
|
+
"jti",
|
|
416
|
+
"iat"
|
|
417
|
+
],
|
|
418
|
+
typ: "JWT",
|
|
419
|
+
algorithms: alg ? [alg] : void 0,
|
|
420
|
+
forceUint8Array: false,
|
|
421
|
+
validateJWT: true
|
|
422
|
+
}).catch((error_) => {
|
|
423
|
+
const message = error_ instanceof Error ? error_.message : String(error_);
|
|
424
|
+
throw new Error(`Invalid session token: ${message}`);
|
|
425
|
+
});
|
|
426
|
+
const { jti, iat, ...data } = payload;
|
|
427
|
+
return {
|
|
428
|
+
id: jti,
|
|
429
|
+
createdAt: iat * 1e3,
|
|
430
|
+
// Convert back to ms
|
|
431
|
+
expiresAt: payload.exp ? payload.exp * 1e3 : void 0,
|
|
432
|
+
data: data && typeof data === "object" ? data : /* @__PURE__ */ Object.create(null)
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function clearJWSSession(event, config) {
|
|
436
|
+
const sessionName = config.name || DEFAULT_NAME;
|
|
437
|
+
if (event.context.sessions?.[sessionName]) {
|
|
438
|
+
delete event.context.sessions[sessionName];
|
|
439
|
+
}
|
|
440
|
+
setCookie(event, sessionName, "", {
|
|
441
|
+
...DEFAULT_COOKIE,
|
|
442
|
+
...config.cookie,
|
|
443
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
444
|
+
});
|
|
445
|
+
return Promise.resolve();
|
|
446
|
+
}
|
|
447
|
+
function getSignKey(key) {
|
|
448
|
+
if ("privateKey" in key) {
|
|
449
|
+
return key.privateKey;
|
|
450
|
+
}
|
|
451
|
+
return key;
|
|
452
|
+
}
|
|
453
|
+
function getVerifyKey(key) {
|
|
454
|
+
if ("publicKey" in key) {
|
|
455
|
+
return Array.isArray(key.publicKey) ? { keys: key.publicKey } : key.publicKey;
|
|
456
|
+
}
|
|
457
|
+
return key;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export { clearJWESession, clearJWSSession, getJWESession, getJWSSession, sealJWESession, signJWSSession, unsealJWESession, updateJWESession, updateJWSSession, useJWESession, useJWSSession, verifyJWSSession };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
export { C as CompositeKey, f as ContentEncryptionAlgorithm, D as DeriveKeyOptions, e as DeriveKeyReturn, E as ExpiresIn, c as GenerateJWKOptions, d as GenerateJWKReturn, G as GenerateKeyAlgorithm, a as GenerateKeyOptions, b as GenerateKeyReturn,
|
|
2
|
-
export { i as utils } from './shared/unjwt.
|
|
3
|
-
export {
|
|
4
|
-
export { j as jwk } from './shared/unjwt.
|
|
5
|
-
export {
|
|
1
|
+
export { C as CompositeKey, f as ContentEncryptionAlgorithm, D as DeriveKeyOptions, e as DeriveKeyReturn, E as ExpiresIn, 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, M as MaxTokenAge, U as UnwrapKeyOptions, W as WrapKeyOptions, g as WrapKeyResult } from './shared/unjwt.CtHrClHc.mjs';
|
|
2
|
+
export { i as utils } from './shared/unjwt.BrNGm4On.mjs';
|
|
3
|
+
export { j as jwe } from './shared/unjwt.VUfJDcCJ.mjs';
|
|
4
|
+
export { j as jwk } from './shared/unjwt.BjP_dGnX.mjs';
|
|
5
|
+
export { j as jws } from './shared/unjwt.C5jNR_1K.mjs';
|
|
6
|
+
export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult } from './shared/unjwt.BsLgL0DQ.mjs';
|
|
7
|
+
export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters } from './shared/unjwt.9mL1vSFP.mjs';
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { i as utils } from './shared/unjwt.
|
|
2
|
-
export { j as jwe } from './shared/unjwt.
|
|
3
|
-
export { j as jwk } from './shared/unjwt.
|
|
4
|
-
export { j as jws } from './shared/unjwt.
|
|
1
|
+
export { i as utils } from './shared/unjwt.DCiTNyCM.mjs';
|
|
2
|
+
export { j as jwe } from './shared/unjwt.CXl9FD-V.mjs';
|
|
3
|
+
export { j as jwk } from './shared/unjwt.Vg02PZWw.mjs';
|
|
4
|
+
export { j as jws } from './shared/unjwt.DmRAjMp8.mjs';
|
package/dist/jwe.d.mts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
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,
|
|
2
|
-
export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters
|
|
1
|
+
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';
|
|
2
|
+
export { d as JWEDecryptOptions, e as JWEDecryptResult, b as JWEEncryptOptions, J as JWEHeaderParameters, c as JWEKeyLookupFunction, a as JWEKeyManagementHeaderParameters } from './shared/unjwt.9mL1vSFP.mjs';
|
|
3
|
+
export { d as decrypt, e as encrypt } from './shared/unjwt.VUfJDcCJ.mjs';
|
package/dist/jwe.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import './shared/unjwt.
|
|
2
|
-
import './shared/unjwt.
|
|
3
|
-
export { d as decrypt, e as encrypt } from './shared/unjwt.
|
|
1
|
+
import './shared/unjwt.Vg02PZWw.mjs';
|
|
2
|
+
import './shared/unjwt.DCiTNyCM.mjs';
|
|
3
|
+
export { d as decrypt, e as encrypt } from './shared/unjwt.CXl9FD-V.mjs';
|
package/dist/jwk.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
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,
|
|
2
|
-
export {
|
|
1
|
+
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';
|
|
2
|
+
export { d as deriveJWKFromPassword, b as deriveKeyFromPassword, e as exportJWKToPEM, f as exportKey, g as generateJWK, a as generateKey, h as getJWKFromSet, i as importJWKFromPEM, c as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.BjP_dGnX.mjs';
|
package/dist/jwk.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import './shared/unjwt.
|
|
2
|
-
export { k as deriveJWKFromPassword,
|
|
1
|
+
import './shared/unjwt.DCiTNyCM.mjs';
|
|
2
|
+
export { k as deriveJWKFromPassword, m as deriveKeyFromPassword, h as exportJWKToPEM, n as exportKey, c as generateJWK, l as generateKey, b as getJWKFromSet, f as importJWKFromPEM, i as importKey, u as unwrapKey, w as wrapKey } from './shared/unjwt.Vg02PZWw.mjs';
|
package/dist/jws.d.mts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
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,
|
|
2
|
-
export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult
|
|
1
|
+
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';
|
|
2
|
+
export { J as JWSAlgorithm, a as JWSHeaderParameters, f as JWSKeyLookupFunction, b as JWSProtectedHeader, c as JWSSignOptions, e as JWSVerifyOptions, d as JWSVerifyResult } from './shared/unjwt.BsLgL0DQ.mjs';
|
|
3
|
+
export { s as sign, v as verify } from './shared/unjwt.C5jNR_1K.mjs';
|
package/dist/jws.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import './shared/unjwt.
|
|
2
|
-
import './shared/unjwt.
|
|
3
|
-
export { s as sign, v as verify } from './shared/unjwt.
|
|
1
|
+
import './shared/unjwt.Vg02PZWw.mjs';
|
|
2
|
+
import './shared/unjwt.DCiTNyCM.mjs';
|
|
3
|
+
export { s as sign, v as verify } from './shared/unjwt.DmRAjMp8.mjs';
|