iron-session 7.0.6 → 8.0.0-beta.0
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/LICENSE.md +20 -0
- package/README.md +364 -632
- package/dist/index.cjs +785 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +230 -0
- package/dist/index.d.ts +204 -40
- package/dist/index.js +722 -136
- package/dist/index.js.map +1 -1
- package/dist/index.node.cjs +791 -0
- package/dist/index.node.cjs.map +1 -0
- package/dist/index.node.js +776 -0
- package/dist/index.node.js.map +1 -0
- package/package.json +68 -170
- package/LICENSE +0 -21
- package/dist/index.mjs +0 -159
- package/dist/index.mjs.map +0 -1
- package/express/dist/index.d.ts +0 -6
- package/express/dist/index.js +0 -61
- package/express/dist/index.js.map +0 -1
- package/express/dist/index.mjs +0 -37
- package/express/dist/index.mjs.map +0 -1
- package/express/index.d.ts +0 -6
- package/express/index.js +0 -61
- package/express/index.js.map +0 -1
- package/express/index.mjs +0 -37
- package/express/index.mjs.map +0 -1
- package/express/index.test.ts +0 -41
- package/express/index.ts +0 -19
- package/express/tsconfig.json +0 -8
- package/next/dist/index.d.ts +0 -14
- package/next/dist/index.js +0 -82
- package/next/dist/index.js.map +0 -1
- package/next/dist/index.mjs +0 -57
- package/next/dist/index.mjs.map +0 -1
- package/next/index.d.ts +0 -14
- package/next/index.js +0 -82
- package/next/index.js.map +0 -1
- package/next/index.mjs +0 -57
- package/next/index.mjs.map +0 -1
- package/next/index.test.ts +0 -253
- package/next/index.ts +0 -87
- package/next/tsconfig.json +0 -8
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
10
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
|
|
29
|
+
// node_modules/.pnpm/cookie@0.6.0/node_modules/cookie/index.js
|
|
30
|
+
var require_cookie = __commonJS({
|
|
31
|
+
"node_modules/.pnpm/cookie@0.6.0/node_modules/cookie/index.js"(exports) {
|
|
32
|
+
exports.parse = parse2;
|
|
33
|
+
exports.serialize = serialize2;
|
|
34
|
+
var __toString = Object.prototype.toString;
|
|
35
|
+
var fieldContentRegExp = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/;
|
|
36
|
+
function parse2(str, options) {
|
|
37
|
+
if (typeof str !== "string") {
|
|
38
|
+
throw new TypeError("argument str must be a string");
|
|
39
|
+
}
|
|
40
|
+
var obj = {};
|
|
41
|
+
var opt = options || {};
|
|
42
|
+
var dec = opt.decode || decode;
|
|
43
|
+
var index = 0;
|
|
44
|
+
while (index < str.length) {
|
|
45
|
+
var eqIdx = str.indexOf("=", index);
|
|
46
|
+
if (eqIdx === -1) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
var endIdx = str.indexOf(";", index);
|
|
50
|
+
if (endIdx === -1) {
|
|
51
|
+
endIdx = str.length;
|
|
52
|
+
} else if (endIdx < eqIdx) {
|
|
53
|
+
index = str.lastIndexOf(";", eqIdx - 1) + 1;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
var key = str.slice(index, eqIdx).trim();
|
|
57
|
+
if (void 0 === obj[key]) {
|
|
58
|
+
var val = str.slice(eqIdx + 1, endIdx).trim();
|
|
59
|
+
if (val.charCodeAt(0) === 34) {
|
|
60
|
+
val = val.slice(1, -1);
|
|
61
|
+
}
|
|
62
|
+
obj[key] = tryDecode(val, dec);
|
|
63
|
+
}
|
|
64
|
+
index = endIdx + 1;
|
|
65
|
+
}
|
|
66
|
+
return obj;
|
|
67
|
+
}
|
|
68
|
+
function serialize2(name, val, options) {
|
|
69
|
+
var opt = options || {};
|
|
70
|
+
var enc = opt.encode || encode;
|
|
71
|
+
if (typeof enc !== "function") {
|
|
72
|
+
throw new TypeError("option encode is invalid");
|
|
73
|
+
}
|
|
74
|
+
if (!fieldContentRegExp.test(name)) {
|
|
75
|
+
throw new TypeError("argument name is invalid");
|
|
76
|
+
}
|
|
77
|
+
var value = enc(val);
|
|
78
|
+
if (value && !fieldContentRegExp.test(value)) {
|
|
79
|
+
throw new TypeError("argument val is invalid");
|
|
80
|
+
}
|
|
81
|
+
var str = name + "=" + value;
|
|
82
|
+
if (null != opt.maxAge) {
|
|
83
|
+
var maxAge = opt.maxAge - 0;
|
|
84
|
+
if (isNaN(maxAge) || !isFinite(maxAge)) {
|
|
85
|
+
throw new TypeError("option maxAge is invalid");
|
|
86
|
+
}
|
|
87
|
+
str += "; Max-Age=" + Math.floor(maxAge);
|
|
88
|
+
}
|
|
89
|
+
if (opt.domain) {
|
|
90
|
+
if (!fieldContentRegExp.test(opt.domain)) {
|
|
91
|
+
throw new TypeError("option domain is invalid");
|
|
92
|
+
}
|
|
93
|
+
str += "; Domain=" + opt.domain;
|
|
94
|
+
}
|
|
95
|
+
if (opt.path) {
|
|
96
|
+
if (!fieldContentRegExp.test(opt.path)) {
|
|
97
|
+
throw new TypeError("option path is invalid");
|
|
98
|
+
}
|
|
99
|
+
str += "; Path=" + opt.path;
|
|
100
|
+
}
|
|
101
|
+
if (opt.expires) {
|
|
102
|
+
var expires = opt.expires;
|
|
103
|
+
if (!isDate(expires) || isNaN(expires.valueOf())) {
|
|
104
|
+
throw new TypeError("option expires is invalid");
|
|
105
|
+
}
|
|
106
|
+
str += "; Expires=" + expires.toUTCString();
|
|
107
|
+
}
|
|
108
|
+
if (opt.httpOnly) {
|
|
109
|
+
str += "; HttpOnly";
|
|
110
|
+
}
|
|
111
|
+
if (opt.secure) {
|
|
112
|
+
str += "; Secure";
|
|
113
|
+
}
|
|
114
|
+
if (opt.partitioned) {
|
|
115
|
+
str += "; Partitioned";
|
|
116
|
+
}
|
|
117
|
+
if (opt.priority) {
|
|
118
|
+
var priority = typeof opt.priority === "string" ? opt.priority.toLowerCase() : opt.priority;
|
|
119
|
+
switch (priority) {
|
|
120
|
+
case "low":
|
|
121
|
+
str += "; Priority=Low";
|
|
122
|
+
break;
|
|
123
|
+
case "medium":
|
|
124
|
+
str += "; Priority=Medium";
|
|
125
|
+
break;
|
|
126
|
+
case "high":
|
|
127
|
+
str += "; Priority=High";
|
|
128
|
+
break;
|
|
129
|
+
default:
|
|
130
|
+
throw new TypeError("option priority is invalid");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (opt.sameSite) {
|
|
134
|
+
var sameSite = typeof opt.sameSite === "string" ? opt.sameSite.toLowerCase() : opt.sameSite;
|
|
135
|
+
switch (sameSite) {
|
|
136
|
+
case true:
|
|
137
|
+
str += "; SameSite=Strict";
|
|
138
|
+
break;
|
|
139
|
+
case "lax":
|
|
140
|
+
str += "; SameSite=Lax";
|
|
141
|
+
break;
|
|
142
|
+
case "strict":
|
|
143
|
+
str += "; SameSite=Strict";
|
|
144
|
+
break;
|
|
145
|
+
case "none":
|
|
146
|
+
str += "; SameSite=None";
|
|
147
|
+
break;
|
|
148
|
+
default:
|
|
149
|
+
throw new TypeError("option sameSite is invalid");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return str;
|
|
153
|
+
}
|
|
154
|
+
function decode(str) {
|
|
155
|
+
return str.indexOf("%") !== -1 ? decodeURIComponent(str) : str;
|
|
156
|
+
}
|
|
157
|
+
function encode(val) {
|
|
158
|
+
return encodeURIComponent(val);
|
|
159
|
+
}
|
|
160
|
+
function isDate(val) {
|
|
161
|
+
return __toString.call(val) === "[object Date]" || val instanceof Date;
|
|
162
|
+
}
|
|
163
|
+
function tryDecode(str, decode2) {
|
|
164
|
+
try {
|
|
165
|
+
return decode2(str);
|
|
166
|
+
} catch (e) {
|
|
167
|
+
return str;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// src/core.ts
|
|
174
|
+
var import_cookie = __toESM(require_cookie(), 1);
|
|
175
|
+
|
|
176
|
+
// node_modules/.pnpm/iron-webcrypto@1.0.0/node_modules/iron-webcrypto/dist/index.js
|
|
177
|
+
var alphabetByEncoding = {};
|
|
178
|
+
var alphabetByValue = new Array(64);
|
|
179
|
+
for (let i = 0, start = "A".charCodeAt(0), limit = "Z".charCodeAt(0); i + start <= limit; i++) {
|
|
180
|
+
const char = String.fromCharCode(i + start);
|
|
181
|
+
alphabetByEncoding[char] = i;
|
|
182
|
+
alphabetByValue[i] = char;
|
|
183
|
+
}
|
|
184
|
+
for (let i = 0, start = "a".charCodeAt(0), limit = "z".charCodeAt(0); i + start <= limit; i++) {
|
|
185
|
+
const char = String.fromCharCode(i + start);
|
|
186
|
+
const index = i + 26;
|
|
187
|
+
alphabetByEncoding[char] = index;
|
|
188
|
+
alphabetByValue[index] = char;
|
|
189
|
+
}
|
|
190
|
+
for (let i = 0; i < 10; i++) {
|
|
191
|
+
alphabetByEncoding[i.toString(10)] = i + 52;
|
|
192
|
+
const char = i.toString(10);
|
|
193
|
+
const index = i + 52;
|
|
194
|
+
alphabetByEncoding[char] = index;
|
|
195
|
+
alphabetByValue[index] = char;
|
|
196
|
+
}
|
|
197
|
+
alphabetByEncoding["+"] = 62;
|
|
198
|
+
alphabetByValue[62] = "+";
|
|
199
|
+
alphabetByEncoding["/"] = 63;
|
|
200
|
+
alphabetByValue[63] = "/";
|
|
201
|
+
var bitsPerLetter = 6;
|
|
202
|
+
var bitsPerByte = 8;
|
|
203
|
+
var maxLetterValue = 63;
|
|
204
|
+
var fromBase64 = (input) => {
|
|
205
|
+
let totalByteLength = input.length / 4 * 3;
|
|
206
|
+
if (input.slice(-2) === "==") {
|
|
207
|
+
totalByteLength -= 2;
|
|
208
|
+
} else if (input.slice(-1) === "=") {
|
|
209
|
+
totalByteLength--;
|
|
210
|
+
}
|
|
211
|
+
const out = new ArrayBuffer(totalByteLength);
|
|
212
|
+
const dataView = new DataView(out);
|
|
213
|
+
for (let i = 0; i < input.length; i += 4) {
|
|
214
|
+
let bits = 0;
|
|
215
|
+
let bitLength = 0;
|
|
216
|
+
for (let j = i, limit = i + 3; j <= limit; j++) {
|
|
217
|
+
if (input[j] !== "=") {
|
|
218
|
+
if (!(input[j] in alphabetByEncoding)) {
|
|
219
|
+
throw new TypeError(`Invalid character ${input[j]} in base64 string.`);
|
|
220
|
+
}
|
|
221
|
+
bits |= alphabetByEncoding[input[j]] << (limit - j) * bitsPerLetter;
|
|
222
|
+
bitLength += bitsPerLetter;
|
|
223
|
+
} else {
|
|
224
|
+
bits >>= bitsPerLetter;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
const chunkOffset = i / 4 * 3;
|
|
228
|
+
bits >>= bitLength % bitsPerByte;
|
|
229
|
+
const byteLength = Math.floor(bitLength / bitsPerByte);
|
|
230
|
+
for (let k = 0; k < byteLength; k++) {
|
|
231
|
+
const offset = (byteLength - k - 1) * bitsPerByte;
|
|
232
|
+
dataView.setUint8(chunkOffset + k, (bits & 255 << offset) >> offset);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return new Uint8Array(out);
|
|
236
|
+
};
|
|
237
|
+
function toBase64(input) {
|
|
238
|
+
let str = "";
|
|
239
|
+
for (let i = 0; i < input.length; i += 3) {
|
|
240
|
+
let bits = 0;
|
|
241
|
+
let bitLength = 0;
|
|
242
|
+
for (let j = i, limit = Math.min(i + 3, input.length); j < limit; j++) {
|
|
243
|
+
bits |= input[j] << (limit - j - 1) * bitsPerByte;
|
|
244
|
+
bitLength += bitsPerByte;
|
|
245
|
+
}
|
|
246
|
+
const bitClusterCount = Math.ceil(bitLength / bitsPerLetter);
|
|
247
|
+
bits <<= bitClusterCount * bitsPerLetter - bitLength;
|
|
248
|
+
for (let k = 1; k <= bitClusterCount; k++) {
|
|
249
|
+
const offset = (bitClusterCount - k) * bitsPerLetter;
|
|
250
|
+
str += alphabetByValue[(bits & maxLetterValue << offset) >> offset];
|
|
251
|
+
}
|
|
252
|
+
str += "==".slice(0, 4 - bitClusterCount);
|
|
253
|
+
}
|
|
254
|
+
return str;
|
|
255
|
+
}
|
|
256
|
+
var stringToBuffer = (value) => {
|
|
257
|
+
return new TextEncoder().encode(value);
|
|
258
|
+
};
|
|
259
|
+
var bufferToString = (value) => {
|
|
260
|
+
return new TextDecoder().decode(value);
|
|
261
|
+
};
|
|
262
|
+
var base64urlEncode = (value) => toBase64(typeof value === "string" ? stringToBuffer(value) : value).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
263
|
+
var base64urlDecode = (value) => fromBase64(
|
|
264
|
+
value.replace(/-/g, "+").replace(/_/g, "/") + Array((4 - value.length % 4) % 4 + 1).join("=")
|
|
265
|
+
);
|
|
266
|
+
var defaults = {
|
|
267
|
+
encryption: { saltBits: 256, algorithm: "aes-256-cbc", iterations: 1, minPasswordlength: 32 },
|
|
268
|
+
integrity: { saltBits: 256, algorithm: "sha256", iterations: 1, minPasswordlength: 32 },
|
|
269
|
+
ttl: 0,
|
|
270
|
+
timestampSkewSec: 60,
|
|
271
|
+
localtimeOffsetMsec: 0
|
|
272
|
+
};
|
|
273
|
+
var clone = (options) => ({
|
|
274
|
+
...options,
|
|
275
|
+
encryption: { ...options.encryption },
|
|
276
|
+
integrity: { ...options.integrity }
|
|
277
|
+
});
|
|
278
|
+
var algorithms = {
|
|
279
|
+
"aes-128-ctr": { keyBits: 128, ivBits: 128, name: "AES-CTR" },
|
|
280
|
+
"aes-256-cbc": { keyBits: 256, ivBits: 128, name: "AES-CBC" },
|
|
281
|
+
sha256: { keyBits: 256, name: "SHA-256" }
|
|
282
|
+
};
|
|
283
|
+
var macFormatVersion = "2";
|
|
284
|
+
var macPrefix = `Fe26.${macFormatVersion}`;
|
|
285
|
+
var randomBytes = (_crypto, size) => {
|
|
286
|
+
const bytes = new Uint8Array(size);
|
|
287
|
+
_crypto.getRandomValues(bytes);
|
|
288
|
+
return bytes;
|
|
289
|
+
};
|
|
290
|
+
var randomBits = (_crypto, bits) => {
|
|
291
|
+
if (bits < 1)
|
|
292
|
+
throw Error("Invalid random bits count");
|
|
293
|
+
const bytes = Math.ceil(bits / 8);
|
|
294
|
+
return randomBytes(_crypto, bytes);
|
|
295
|
+
};
|
|
296
|
+
var pbkdf2 = async (_crypto, password, salt, iterations, keyLength, hash) => {
|
|
297
|
+
const passwordBuffer = stringToBuffer(password);
|
|
298
|
+
const importedKey = await _crypto.subtle.importKey("raw", passwordBuffer, "PBKDF2", false, [
|
|
299
|
+
"deriveBits"
|
|
300
|
+
]);
|
|
301
|
+
const saltBuffer = stringToBuffer(salt);
|
|
302
|
+
const params = { name: "PBKDF2", hash, salt: saltBuffer, iterations };
|
|
303
|
+
const derivation = await _crypto.subtle.deriveBits(params, importedKey, keyLength * 8);
|
|
304
|
+
return derivation;
|
|
305
|
+
};
|
|
306
|
+
var generateKey = async (_crypto, password, options) => {
|
|
307
|
+
var _a;
|
|
308
|
+
if (!(password == null ? void 0 : password.length))
|
|
309
|
+
throw new Error("Empty password");
|
|
310
|
+
if (options == null || typeof options !== "object")
|
|
311
|
+
throw new Error("Bad options");
|
|
312
|
+
if (!(options.algorithm in algorithms))
|
|
313
|
+
throw new Error(`Unknown algorithm: ${options.algorithm}`);
|
|
314
|
+
const algorithm = algorithms[options.algorithm];
|
|
315
|
+
const result = {};
|
|
316
|
+
const hmac = (_a = options.hmac) != null ? _a : false;
|
|
317
|
+
const id = hmac ? { name: "HMAC", hash: algorithm.name } : { name: algorithm.name };
|
|
318
|
+
const usage = hmac ? ["sign", "verify"] : ["encrypt", "decrypt"];
|
|
319
|
+
if (typeof password === "string") {
|
|
320
|
+
if (password.length < options.minPasswordlength)
|
|
321
|
+
throw new Error(
|
|
322
|
+
`Password string too short (min ${options.minPasswordlength} characters required)`
|
|
323
|
+
);
|
|
324
|
+
let { salt = "" } = options;
|
|
325
|
+
if (!salt) {
|
|
326
|
+
const { saltBits = 0 } = options;
|
|
327
|
+
if (!saltBits)
|
|
328
|
+
throw new Error("Missing salt and saltBits options");
|
|
329
|
+
const randomSalt = randomBits(_crypto, saltBits);
|
|
330
|
+
salt = [...new Uint8Array(randomSalt)].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
331
|
+
}
|
|
332
|
+
const derivedKey = await pbkdf2(
|
|
333
|
+
_crypto,
|
|
334
|
+
password,
|
|
335
|
+
salt,
|
|
336
|
+
options.iterations,
|
|
337
|
+
algorithm.keyBits / 8,
|
|
338
|
+
"SHA-1"
|
|
339
|
+
);
|
|
340
|
+
const importedEncryptionKey = await _crypto.subtle.importKey(
|
|
341
|
+
"raw",
|
|
342
|
+
derivedKey,
|
|
343
|
+
id,
|
|
344
|
+
false,
|
|
345
|
+
usage
|
|
346
|
+
);
|
|
347
|
+
result.key = importedEncryptionKey;
|
|
348
|
+
result.salt = salt;
|
|
349
|
+
} else {
|
|
350
|
+
if (password.length < algorithm.keyBits / 8)
|
|
351
|
+
throw new Error("Key buffer (password) too small");
|
|
352
|
+
result.key = await _crypto.subtle.importKey("raw", password, id, false, usage);
|
|
353
|
+
result.salt = "";
|
|
354
|
+
}
|
|
355
|
+
if (options.iv)
|
|
356
|
+
result.iv = options.iv;
|
|
357
|
+
else if ("ivBits" in algorithm)
|
|
358
|
+
result.iv = randomBits(_crypto, algorithm.ivBits);
|
|
359
|
+
return result;
|
|
360
|
+
};
|
|
361
|
+
var encrypt = async (_crypto, password, options, data) => {
|
|
362
|
+
const key = await generateKey(_crypto, password, options);
|
|
363
|
+
const textBuffer = stringToBuffer(data);
|
|
364
|
+
const encrypted = await _crypto.subtle.encrypt(
|
|
365
|
+
{ name: algorithms[options.algorithm].name, iv: key.iv },
|
|
366
|
+
key.key,
|
|
367
|
+
textBuffer
|
|
368
|
+
);
|
|
369
|
+
return { encrypted: new Uint8Array(encrypted), key };
|
|
370
|
+
};
|
|
371
|
+
var decrypt = async (_crypto, password, options, data) => {
|
|
372
|
+
const key = await generateKey(_crypto, password, options);
|
|
373
|
+
const decrypted = await _crypto.subtle.decrypt(
|
|
374
|
+
{ name: algorithms[options.algorithm].name, iv: key.iv },
|
|
375
|
+
key.key,
|
|
376
|
+
typeof data === "string" ? stringToBuffer(data) : data
|
|
377
|
+
);
|
|
378
|
+
return bufferToString(new Uint8Array(decrypted));
|
|
379
|
+
};
|
|
380
|
+
var hmacWithPassword = async (_crypto, password, options, data) => {
|
|
381
|
+
const key = await generateKey(_crypto, password, { ...options, hmac: true });
|
|
382
|
+
const textBuffer = stringToBuffer(data);
|
|
383
|
+
const signed = await _crypto.subtle.sign({ name: "HMAC" }, key.key, textBuffer);
|
|
384
|
+
const digest = base64urlEncode(new Uint8Array(signed));
|
|
385
|
+
return { digest, salt: key.salt };
|
|
386
|
+
};
|
|
387
|
+
var normalizePassword = (password) => {
|
|
388
|
+
if (typeof password === "string" || password instanceof Uint8Array)
|
|
389
|
+
return { encryption: password, integrity: password };
|
|
390
|
+
if ("secret" in password)
|
|
391
|
+
return { id: password.id, encryption: password.secret, integrity: password.secret };
|
|
392
|
+
return { id: password.id, encryption: password.encryption, integrity: password.integrity };
|
|
393
|
+
};
|
|
394
|
+
var seal = async (_crypto, object, password, options) => {
|
|
395
|
+
if (!password)
|
|
396
|
+
throw Error("Empty password");
|
|
397
|
+
const opts = clone(options);
|
|
398
|
+
const now = Date.now() + (opts.localtimeOffsetMsec || 0);
|
|
399
|
+
const objectString = JSON.stringify(object);
|
|
400
|
+
const pass = normalizePassword(password);
|
|
401
|
+
const { id = "" } = pass;
|
|
402
|
+
if (id && !/^\w+$/.test(id))
|
|
403
|
+
throw new Error("Invalid password id");
|
|
404
|
+
const { encrypted, key } = await encrypt(_crypto, pass.encryption, opts.encryption, objectString);
|
|
405
|
+
const encryptedB64 = base64urlEncode(new Uint8Array(encrypted));
|
|
406
|
+
const iv = base64urlEncode(key.iv);
|
|
407
|
+
const expiration = opts.ttl ? now + opts.ttl : "";
|
|
408
|
+
const macBaseString = `${macPrefix}*${id}*${key.salt}*${iv}*${encryptedB64}*${expiration}`;
|
|
409
|
+
const mac = await hmacWithPassword(_crypto, pass.integrity, opts.integrity, macBaseString);
|
|
410
|
+
const sealed = `${macBaseString}*${mac.salt}*${mac.digest}`;
|
|
411
|
+
return sealed;
|
|
412
|
+
};
|
|
413
|
+
var fixedTimeComparison = (a, b) => {
|
|
414
|
+
let mismatch = a.length === b.length ? 0 : 1;
|
|
415
|
+
if (mismatch)
|
|
416
|
+
b = a;
|
|
417
|
+
for (let i = 0; i < a.length; i += 1)
|
|
418
|
+
mismatch |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
419
|
+
return mismatch === 0;
|
|
420
|
+
};
|
|
421
|
+
var unseal = async (_crypto, sealed, password, options) => {
|
|
422
|
+
if (!password)
|
|
423
|
+
throw Error("Empty password");
|
|
424
|
+
const opts = clone(options);
|
|
425
|
+
const now = Date.now() + (opts.localtimeOffsetMsec || 0);
|
|
426
|
+
const parts = sealed.split("*");
|
|
427
|
+
if (parts.length !== 8)
|
|
428
|
+
throw new Error("Incorrect number of sealed components");
|
|
429
|
+
const prefix = parts[0];
|
|
430
|
+
let passwordId = parts[1];
|
|
431
|
+
const encryptionSalt = parts[2];
|
|
432
|
+
const encryptionIv = parts[3];
|
|
433
|
+
const encryptedB64 = parts[4];
|
|
434
|
+
const expiration = parts[5];
|
|
435
|
+
const hmacSalt = parts[6];
|
|
436
|
+
const hmac = parts[7];
|
|
437
|
+
const macBaseString = `${prefix}*${passwordId}*${encryptionSalt}*${encryptionIv}*${encryptedB64}*${expiration}`;
|
|
438
|
+
if (macPrefix !== prefix)
|
|
439
|
+
throw new Error("Wrong mac prefix");
|
|
440
|
+
if (expiration) {
|
|
441
|
+
if (!/^\d+$/.exec(expiration))
|
|
442
|
+
throw new Error("Invalid expiration");
|
|
443
|
+
const exp = parseInt(expiration, 10);
|
|
444
|
+
if (exp <= now - opts.timestampSkewSec * 1e3)
|
|
445
|
+
throw new Error("Expired seal");
|
|
446
|
+
}
|
|
447
|
+
if (typeof password === "undefined" || typeof password === "string" && password.length === 0)
|
|
448
|
+
throw new Error("Empty password");
|
|
449
|
+
let pass = "";
|
|
450
|
+
passwordId = passwordId || "default";
|
|
451
|
+
if (typeof password === "string" || password instanceof Uint8Array)
|
|
452
|
+
pass = password;
|
|
453
|
+
else if (!(passwordId in password))
|
|
454
|
+
throw new Error(`Cannot find password: ${passwordId}`);
|
|
455
|
+
else
|
|
456
|
+
pass = password[passwordId];
|
|
457
|
+
pass = normalizePassword(pass);
|
|
458
|
+
const macOptions = opts.integrity;
|
|
459
|
+
macOptions.salt = hmacSalt;
|
|
460
|
+
const mac = await hmacWithPassword(_crypto, pass.integrity, macOptions, macBaseString);
|
|
461
|
+
if (!fixedTimeComparison(mac.digest, hmac))
|
|
462
|
+
throw new Error("Bad hmac value");
|
|
463
|
+
const encrypted = base64urlDecode(encryptedB64);
|
|
464
|
+
const decryptOptions = opts.encryption;
|
|
465
|
+
decryptOptions.salt = encryptionSalt;
|
|
466
|
+
decryptOptions.iv = base64urlDecode(encryptionIv);
|
|
467
|
+
const decrypted = await decrypt(_crypto, pass.encryption, decryptOptions, encrypted);
|
|
468
|
+
if (decrypted)
|
|
469
|
+
return JSON.parse(decrypted);
|
|
470
|
+
return null;
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
// src/core.ts
|
|
474
|
+
var timestampSkewSec = 60;
|
|
475
|
+
var fourteenDaysInSeconds = 14 * 24 * 3600;
|
|
476
|
+
var currentMajorVersion = 2;
|
|
477
|
+
var versionDelimiter = "~";
|
|
478
|
+
var defaultOptions = {
|
|
479
|
+
ttl: fourteenDaysInSeconds,
|
|
480
|
+
cookieOptions: { httpOnly: true, secure: true, sameSite: "lax", path: "/" }
|
|
481
|
+
};
|
|
482
|
+
function normalizeStringPasswordToMap(password) {
|
|
483
|
+
return typeof password === "string" ? { 1: password } : password;
|
|
484
|
+
}
|
|
485
|
+
function parseSeal(seal2) {
|
|
486
|
+
const [sealWithoutVersion, tokenVersionAsString] = seal2.split(versionDelimiter);
|
|
487
|
+
const tokenVersion = tokenVersionAsString == null ? null : parseInt(tokenVersionAsString, 10);
|
|
488
|
+
return { sealWithoutVersion, tokenVersion };
|
|
489
|
+
}
|
|
490
|
+
function computeCookieMaxAge(ttl) {
|
|
491
|
+
if (ttl === 0) {
|
|
492
|
+
return 2147483647;
|
|
493
|
+
}
|
|
494
|
+
return ttl - timestampSkewSec;
|
|
495
|
+
}
|
|
496
|
+
function getCookie(req, cookieName) {
|
|
497
|
+
return (0, import_cookie.parse)(
|
|
498
|
+
("headers" in req && typeof req.headers.get === "function" ? req.headers.get("cookie") : req.headers.cookie) ?? ""
|
|
499
|
+
)[cookieName] ?? "";
|
|
500
|
+
}
|
|
501
|
+
function getServerActionCookie(cookieName, cookieHandler) {
|
|
502
|
+
const cookieObject = cookieHandler.get(cookieName);
|
|
503
|
+
const cookie = cookieObject?.value;
|
|
504
|
+
if (typeof cookie === "string") {
|
|
505
|
+
return cookie;
|
|
506
|
+
}
|
|
507
|
+
return "";
|
|
508
|
+
}
|
|
509
|
+
function setCookie(res, cookieValue) {
|
|
510
|
+
if ("headers" in res && typeof res.headers.append === "function") {
|
|
511
|
+
res.headers.append("set-cookie", cookieValue);
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
let existingSetCookie = res.getHeader("set-cookie") ?? [];
|
|
515
|
+
if (!Array.isArray(existingSetCookie)) {
|
|
516
|
+
existingSetCookie = [existingSetCookie.toString()];
|
|
517
|
+
}
|
|
518
|
+
res.setHeader("set-cookie", [
|
|
519
|
+
...existingSetCookie,
|
|
520
|
+
cookieValue
|
|
521
|
+
]);
|
|
522
|
+
}
|
|
523
|
+
function createSealData(_crypto = globalThis.crypto) {
|
|
524
|
+
return async function sealData2(data, {
|
|
525
|
+
password,
|
|
526
|
+
ttl = fourteenDaysInSeconds
|
|
527
|
+
}) {
|
|
528
|
+
const passwordsMap = normalizeStringPasswordToMap(password);
|
|
529
|
+
const mostRecentPasswordId = Math.max(
|
|
530
|
+
...Object.keys(passwordsMap).map(Number)
|
|
531
|
+
);
|
|
532
|
+
const passwordForSeal = {
|
|
533
|
+
id: mostRecentPasswordId.toString(),
|
|
534
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
535
|
+
secret: passwordsMap[mostRecentPasswordId]
|
|
536
|
+
};
|
|
537
|
+
const seal2 = await seal(_crypto, data, passwordForSeal, {
|
|
538
|
+
...defaults,
|
|
539
|
+
ttl: ttl * 1e3
|
|
540
|
+
});
|
|
541
|
+
return `${seal2}${versionDelimiter}${currentMajorVersion}`;
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
function createUnsealData(_crypto = globalThis.crypto) {
|
|
545
|
+
return async function unsealData2(seal2, {
|
|
546
|
+
password,
|
|
547
|
+
ttl = fourteenDaysInSeconds
|
|
548
|
+
}) {
|
|
549
|
+
const passwordsMap = normalizeStringPasswordToMap(password);
|
|
550
|
+
const { sealWithoutVersion, tokenVersion } = parseSeal(seal2);
|
|
551
|
+
try {
|
|
552
|
+
const data = await unseal(_crypto, sealWithoutVersion, passwordsMap, {
|
|
553
|
+
...defaults,
|
|
554
|
+
ttl: ttl * 1e3
|
|
555
|
+
}) ?? {};
|
|
556
|
+
if (tokenVersion === 2) {
|
|
557
|
+
return data;
|
|
558
|
+
}
|
|
559
|
+
return { ...data.persistent };
|
|
560
|
+
} catch (error) {
|
|
561
|
+
if (error instanceof Error && /^(Expired seal|Bad hmac value|Cannot find password|Incorrect number of sealed components)/.test(
|
|
562
|
+
error.message
|
|
563
|
+
)) {
|
|
564
|
+
return {};
|
|
565
|
+
}
|
|
566
|
+
throw error;
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function mergeOptions(userSessionOptions, overrides) {
|
|
571
|
+
const options = {
|
|
572
|
+
...defaultOptions,
|
|
573
|
+
...userSessionOptions,
|
|
574
|
+
...overrides,
|
|
575
|
+
cookieOptions: {
|
|
576
|
+
...defaultOptions.cookieOptions,
|
|
577
|
+
...userSessionOptions.cookieOptions,
|
|
578
|
+
...overrides?.cookieOptions
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
if (userSessionOptions.cookieOptions && "maxAge" in userSessionOptions.cookieOptions) {
|
|
582
|
+
if (userSessionOptions.cookieOptions.maxAge === void 0) {
|
|
583
|
+
options.ttl = 0;
|
|
584
|
+
}
|
|
585
|
+
} else {
|
|
586
|
+
options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);
|
|
587
|
+
}
|
|
588
|
+
return options;
|
|
589
|
+
}
|
|
590
|
+
function createGetIronSession(sealData2, unsealData2) {
|
|
591
|
+
return async function getIronSession2(req, res, userSessionOptions) {
|
|
592
|
+
if (!req) {
|
|
593
|
+
throw new Error("iron-session: Bad usage. Missing request parameter.");
|
|
594
|
+
}
|
|
595
|
+
if (!res) {
|
|
596
|
+
throw new Error("iron-session: Bad usage. Missing response parameter.");
|
|
597
|
+
}
|
|
598
|
+
if (!userSessionOptions) {
|
|
599
|
+
throw new Error("iron-session: Bad usage. Missing options.");
|
|
600
|
+
}
|
|
601
|
+
if (!userSessionOptions.cookieName) {
|
|
602
|
+
throw new Error("iron-session: Bad usage. Missing cookie name.");
|
|
603
|
+
}
|
|
604
|
+
if (!userSessionOptions.password) {
|
|
605
|
+
throw new Error("iron-session: Bad usage. Missing password.");
|
|
606
|
+
}
|
|
607
|
+
const passwordsMap = normalizeStringPasswordToMap(
|
|
608
|
+
userSessionOptions.password
|
|
609
|
+
);
|
|
610
|
+
if (Object.values(passwordsMap).some((password) => password.length < 32)) {
|
|
611
|
+
throw new Error(
|
|
612
|
+
"iron-session: Bad usage. Password must be at least 32 characters long."
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
const options = mergeOptions(userSessionOptions);
|
|
616
|
+
const sealFromCookies = getCookie(req, options.cookieName);
|
|
617
|
+
const session = sealFromCookies ? await unsealData2(sealFromCookies, {
|
|
618
|
+
password: passwordsMap,
|
|
619
|
+
ttl: options.ttl
|
|
620
|
+
}) : {};
|
|
621
|
+
Object.defineProperties(session, {
|
|
622
|
+
save: {
|
|
623
|
+
value: async function save(saveOptions) {
|
|
624
|
+
if ("headersSent" in res && res.headersSent) {
|
|
625
|
+
throw new Error(
|
|
626
|
+
"iron-session: Cannot set session cookie: session.save() was called after headers were sent. Make sure to call it before any res.send() or res.end()"
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
const mergedOptions = mergeOptions(userSessionOptions, saveOptions);
|
|
630
|
+
const seal2 = await sealData2(session, {
|
|
631
|
+
password: passwordsMap,
|
|
632
|
+
ttl: mergedOptions.ttl
|
|
633
|
+
});
|
|
634
|
+
const cookieValue = (0, import_cookie.serialize)(
|
|
635
|
+
mergedOptions.cookieName,
|
|
636
|
+
seal2,
|
|
637
|
+
mergedOptions.cookieOptions
|
|
638
|
+
);
|
|
639
|
+
if (cookieValue.length > 4096) {
|
|
640
|
+
throw new Error(
|
|
641
|
+
`iron-session: Cookie length is too big (${cookieValue.length} bytes), browsers will refuse it. Try to remove some data.`
|
|
642
|
+
);
|
|
643
|
+
}
|
|
644
|
+
setCookie(res, cookieValue);
|
|
645
|
+
}
|
|
646
|
+
},
|
|
647
|
+
destroy: {
|
|
648
|
+
value: async function destroy(destroyOptions) {
|
|
649
|
+
Object.keys(session).forEach((key) => {
|
|
650
|
+
delete session[key];
|
|
651
|
+
});
|
|
652
|
+
const mergedOptions = mergeOptions(
|
|
653
|
+
userSessionOptions,
|
|
654
|
+
destroyOptions
|
|
655
|
+
);
|
|
656
|
+
const cookieValue = (0, import_cookie.serialize)(mergedOptions.cookieName, "", {
|
|
657
|
+
...mergedOptions.cookieOptions,
|
|
658
|
+
maxAge: 0
|
|
659
|
+
});
|
|
660
|
+
setCookie(res, cookieValue);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
});
|
|
664
|
+
return session;
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
function createGetServerActionIronSession(sealData2, unsealData2) {
|
|
668
|
+
return async function getServerActionIronSession2(userSessionOptions, cookieHandler) {
|
|
669
|
+
if (!userSessionOptions) {
|
|
670
|
+
throw new Error("iron-session: Bad usage. Missing options.");
|
|
671
|
+
}
|
|
672
|
+
if (!cookieHandler) {
|
|
673
|
+
throw new Error(
|
|
674
|
+
"iron-session: Bad usage. Missing NextJS cookies() handler."
|
|
675
|
+
);
|
|
676
|
+
}
|
|
677
|
+
if (!userSessionOptions.cookieName) {
|
|
678
|
+
throw new Error("iron-session: Bad usage. Missing cookie name.");
|
|
679
|
+
}
|
|
680
|
+
if (!userSessionOptions.password) {
|
|
681
|
+
throw new Error("iron-session: Bad usage. Missing password.");
|
|
682
|
+
}
|
|
683
|
+
const passwordsMap = normalizeStringPasswordToMap(
|
|
684
|
+
userSessionOptions.password
|
|
685
|
+
);
|
|
686
|
+
if (Object.values(passwordsMap).some((password) => password.length < 32)) {
|
|
687
|
+
throw new Error(
|
|
688
|
+
"iron-session: Bad usage. Password must be at least 32 characters long."
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
const options = mergeOptions(userSessionOptions);
|
|
692
|
+
const sealFromCookies = getServerActionCookie(
|
|
693
|
+
options.cookieName,
|
|
694
|
+
cookieHandler
|
|
695
|
+
);
|
|
696
|
+
const session = sealFromCookies ? await unsealData2(sealFromCookies, {
|
|
697
|
+
password: passwordsMap,
|
|
698
|
+
ttl: options.ttl
|
|
699
|
+
}) : {};
|
|
700
|
+
Object.defineProperties(session, {
|
|
701
|
+
save: {
|
|
702
|
+
value: async function save(saveOptions) {
|
|
703
|
+
const mergedOptions = mergeOptions(userSessionOptions, saveOptions);
|
|
704
|
+
const seal2 = await sealData2(session, {
|
|
705
|
+
password: passwordsMap,
|
|
706
|
+
ttl: mergedOptions.ttl
|
|
707
|
+
});
|
|
708
|
+
const cookieLength = mergedOptions.cookieName.length + seal2.length + JSON.stringify(mergedOptions.cookieOptions).length;
|
|
709
|
+
if (cookieLength > 4096) {
|
|
710
|
+
throw new Error(
|
|
711
|
+
`iron-session: Cookie length is too big (${cookieLength} bytes), browsers will refuse it. Try to remove some data.`
|
|
712
|
+
);
|
|
713
|
+
}
|
|
714
|
+
cookieHandler.set(
|
|
715
|
+
mergedOptions.cookieName,
|
|
716
|
+
seal2,
|
|
717
|
+
mergedOptions.cookieOptions
|
|
718
|
+
);
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
destroy: {
|
|
722
|
+
value: async function destroy(destroyOptions) {
|
|
723
|
+
Object.keys(session).forEach((key) => {
|
|
724
|
+
delete session[key];
|
|
725
|
+
});
|
|
726
|
+
const mergedOptions = mergeOptions(
|
|
727
|
+
userSessionOptions,
|
|
728
|
+
destroyOptions
|
|
729
|
+
);
|
|
730
|
+
const cookieOptions = { ...mergedOptions.cookieOptions, maxAge: 0 };
|
|
731
|
+
cookieHandler.set(mergedOptions.cookieName, "", cookieOptions);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
return session;
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function mergeHeaders(...headersList) {
|
|
739
|
+
const mergedHeaders = new Headers();
|
|
740
|
+
headersList.forEach((headers) => {
|
|
741
|
+
new Headers(headers).forEach((value, key) => {
|
|
742
|
+
mergedHeaders.append(key, value);
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
return mergedHeaders;
|
|
746
|
+
}
|
|
747
|
+
function createResponse(originalResponse, bodyString, options) {
|
|
748
|
+
return new Response(bodyString, {
|
|
749
|
+
status: options?.status ?? originalResponse.status,
|
|
750
|
+
statusText: options?.statusText ?? originalResponse.statusText,
|
|
751
|
+
headers: mergeHeaders(options?.headers, originalResponse.headers)
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
// src/index.ts
|
|
756
|
+
var sealData = createSealData();
|
|
757
|
+
var unsealData = createUnsealData();
|
|
758
|
+
var getIronSession = createGetIronSession(sealData, unsealData);
|
|
759
|
+
var getServerActionIronSession = createGetServerActionIronSession(
|
|
760
|
+
sealData,
|
|
761
|
+
unsealData
|
|
762
|
+
);
|
|
763
|
+
/*! Bundled license information:
|
|
764
|
+
|
|
765
|
+
cookie/index.js:
|
|
766
|
+
(*!
|
|
767
|
+
* cookie
|
|
768
|
+
* Copyright(c) 2012-2014 Roman Shtylman
|
|
769
|
+
* Copyright(c) 2015 Douglas Christopher Wilson
|
|
770
|
+
* MIT Licensed
|
|
771
|
+
*)
|
|
772
|
+
*/
|
|
773
|
+
|
|
774
|
+
exports.createGetIronSession = createGetIronSession;
|
|
775
|
+
exports.createGetServerActionIronSession = createGetServerActionIronSession;
|
|
776
|
+
exports.createResponse = createResponse;
|
|
777
|
+
exports.createSealData = createSealData;
|
|
778
|
+
exports.createUnsealData = createUnsealData;
|
|
779
|
+
exports.getIronSession = getIronSession;
|
|
780
|
+
exports.getServerActionIronSession = getServerActionIronSession;
|
|
781
|
+
exports.mergeHeaders = mergeHeaders;
|
|
782
|
+
exports.sealData = sealData;
|
|
783
|
+
exports.unsealData = unsealData;
|
|
784
|
+
//# sourceMappingURL=out.js.map
|
|
785
|
+
//# sourceMappingURL=index.cjs.map
|