iron-session 6.2.0-beta.0 → 6.2.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/README.md +59 -3
- package/dist/index.js +48 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +43 -12
- package/dist/index.mjs.map +1 -1
- package/edge/dist/index.d.ts +4 -66
- package/edge/dist/index.js +51 -20
- package/edge/dist/index.js.map +1 -1
- package/edge/dist/index.mjs +46 -16
- package/edge/dist/index.mjs.map +1 -1
- package/edge/index.d.ts +4 -66
- package/edge/index.js +51 -20
- package/edge/index.js.map +1 -1
- package/edge/index.mjs +46 -16
- package/edge/index.mjs.map +1 -1
- package/edge/index.ts +3 -1
- package/express/dist/index.js +6 -1
- package/express/dist/index.js.map +1 -1
- package/express/dist/index.mjs +5 -1
- package/express/dist/index.mjs.map +1 -1
- package/express/index.js +6 -1
- package/express/index.js.map +1 -1
- package/express/index.mjs +5 -1
- package/express/index.mjs.map +1 -1
- package/next/dist/index.js +16 -3
- package/next/dist/index.js.map +1 -1
- package/next/dist/index.mjs +15 -3
- package/next/dist/index.mjs.map +1 -1
- package/next/index.js +16 -3
- package/next/index.js.map +1 -1
- package/next/index.mjs +15 -3
- package/next/index.mjs.map +1 -1
- package/package.json +17 -14
package/edge/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -16,15 +17,15 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
}
|
|
17
18
|
return to;
|
|
18
19
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
20
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
25
|
|
|
22
26
|
// edge/index.ts
|
|
23
27
|
var edge_exports = {};
|
|
24
28
|
__export(edge_exports, {
|
|
25
|
-
createGetIronSession: () => createGetIronSession,
|
|
26
|
-
createSealData: () => createSealData,
|
|
27
|
-
createUnsealData: () => createUnsealData,
|
|
28
29
|
getIronSession: () => getIronSession,
|
|
29
30
|
sealData: () => sealData,
|
|
30
31
|
unsealData: () => unsealData
|
|
@@ -50,12 +51,20 @@ var defaultOptions = {
|
|
|
50
51
|
function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
51
52
|
return async (req, res, userSessionOptions) => {
|
|
52
53
|
if (!req || !res || !userSessionOptions || !userSessionOptions.cookieName || !userSessionOptions.password) {
|
|
53
|
-
throw new Error(
|
|
54
|
+
throw new Error(
|
|
55
|
+
`iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: "...", password: "...". Check the usage here: https://github.com/vvo/iron-session`
|
|
56
|
+
);
|
|
54
57
|
}
|
|
55
|
-
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
56
|
-
|
|
58
|
+
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
59
|
+
userSessionOptions.password
|
|
60
|
+
);
|
|
61
|
+
Object.values(
|
|
62
|
+
normalizeStringPasswordToMap(userSessionOptions.password)
|
|
63
|
+
).forEach((password) => {
|
|
57
64
|
if (password.length < 32) {
|
|
58
|
-
throw new Error(
|
|
65
|
+
throw new Error(
|
|
66
|
+
`iron-session: Bad usage. Password must be at least 32 characters long.`
|
|
67
|
+
);
|
|
59
68
|
}
|
|
60
69
|
});
|
|
61
70
|
const options = {
|
|
@@ -73,12 +82,16 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
73
82
|
if (userSessionOptions.cookieOptions.maxAge === void 0) {
|
|
74
83
|
options.ttl = 0;
|
|
75
84
|
} else {
|
|
76
|
-
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
85
|
+
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
86
|
+
userSessionOptions.cookieOptions.maxAge
|
|
87
|
+
);
|
|
77
88
|
}
|
|
78
89
|
} else {
|
|
79
90
|
options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);
|
|
80
91
|
}
|
|
81
|
-
const sealFromCookies = import_cookie.default.parse(
|
|
92
|
+
const sealFromCookies = import_cookie.default.parse(
|
|
93
|
+
"credentials" in req ? req.headers.get("cookie") || "" : req.headers.cookie || ""
|
|
94
|
+
)[options.cookieName];
|
|
82
95
|
const session = sealFromCookies === void 0 ? {} : await unsealData2(sealFromCookies, {
|
|
83
96
|
password: passwordsAsMap,
|
|
84
97
|
ttl: options.ttl
|
|
@@ -87,15 +100,23 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
87
100
|
save: {
|
|
88
101
|
value: async function save() {
|
|
89
102
|
if ("headersSent" in res && res.headersSent === true) {
|
|
90
|
-
throw new Error(
|
|
103
|
+
throw new Error(
|
|
104
|
+
`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()`
|
|
105
|
+
);
|
|
91
106
|
}
|
|
92
107
|
const seal2 = await sealData2(session, {
|
|
93
108
|
password: passwordsAsMap,
|
|
94
109
|
ttl: options.ttl
|
|
95
110
|
});
|
|
96
|
-
const cookieValue = import_cookie.default.serialize(
|
|
111
|
+
const cookieValue = import_cookie.default.serialize(
|
|
112
|
+
options.cookieName,
|
|
113
|
+
seal2,
|
|
114
|
+
options.cookieOptions
|
|
115
|
+
);
|
|
97
116
|
if (cookieValue.length > 4096) {
|
|
98
|
-
throw new Error(
|
|
117
|
+
throw new Error(
|
|
118
|
+
`iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`
|
|
119
|
+
);
|
|
99
120
|
}
|
|
100
121
|
addToCookies(cookieValue, res);
|
|
101
122
|
}
|
|
@@ -139,7 +160,12 @@ function createUnsealData(_crypto2) {
|
|
|
139
160
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
140
161
|
const { sealWithoutVersion, tokenVersion } = parseSeal(seal2);
|
|
141
162
|
try {
|
|
142
|
-
const data = await Iron.unseal(
|
|
163
|
+
const data = await Iron.unseal(
|
|
164
|
+
_crypto2,
|
|
165
|
+
sealWithoutVersion,
|
|
166
|
+
passwordsAsMap,
|
|
167
|
+
{ ...Iron.defaults, ttl: ttl * 1e3 }
|
|
168
|
+
);
|
|
143
169
|
if (tokenVersion === 2) {
|
|
144
170
|
return data;
|
|
145
171
|
}
|
|
@@ -172,7 +198,9 @@ function createSealData(_crypto2) {
|
|
|
172
198
|
ttl = fourteenDaysInSeconds
|
|
173
199
|
}) => {
|
|
174
200
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
175
|
-
const mostRecentPasswordId = Math.max(
|
|
201
|
+
const mostRecentPasswordId = Math.max(
|
|
202
|
+
...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10))
|
|
203
|
+
);
|
|
176
204
|
const passwordForSeal = {
|
|
177
205
|
id: mostRecentPasswordId.toString(),
|
|
178
206
|
secret: passwordsAsMap[mostRecentPasswordId]
|
|
@@ -195,17 +223,20 @@ var getCrypto = () => {
|
|
|
195
223
|
return globalThis.crypto;
|
|
196
224
|
if (typeof ((_c = (_b = globalThis.crypto) == null ? void 0 : _b.webcrypto) == null ? void 0 : _c.subtle) === "object")
|
|
197
225
|
return globalThis.crypto.webcrypto;
|
|
198
|
-
throw new Error(
|
|
226
|
+
throw new Error(
|
|
227
|
+
"no native implementation of WebCrypto is available in current context"
|
|
228
|
+
);
|
|
199
229
|
};
|
|
200
230
|
var _crypto = getCrypto();
|
|
201
231
|
var unsealData = createUnsealData(_crypto);
|
|
202
232
|
var sealData = createSealData(_crypto);
|
|
203
|
-
var getIronSession = createGetIronSession(
|
|
233
|
+
var getIronSession = createGetIronSession(
|
|
234
|
+
_crypto,
|
|
235
|
+
unsealData,
|
|
236
|
+
sealData
|
|
237
|
+
);
|
|
204
238
|
// Annotate the CommonJS export names for ESM import in node:
|
|
205
239
|
0 && (module.exports = {
|
|
206
|
-
createGetIronSession,
|
|
207
|
-
createSealData,
|
|
208
|
-
createUnsealData,
|
|
209
240
|
getIronSession,
|
|
210
241
|
sealData,
|
|
211
242
|
unsealData
|
package/edge/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../index.ts","../../src/core.ts"],"sourcesContent":["import {\n createGetIronSession,\n createSealData,\n createUnsealData,\n} from \"../src/core\";\n\nconst getCrypto = (): Crypto => {\n if (typeof globalThis.crypto?.subtle === \"object\") return globalThis.crypto;\n // @ts-ignore crypto.webcrypto is not available in dom, but is there in newer node versions\n if (typeof globalThis.crypto?.webcrypto?.subtle === \"object\")\n // @ts-ignore same as above\n return globalThis.crypto.webcrypto;\n throw new Error(\n \"no native implementation of WebCrypto is available in current context\",\n );\n};\n\nconst _crypto = getCrypto();\n\nexport * from \"../src/core\";\nexport const unsealData = createUnsealData(_crypto);\nexport const sealData = createSealData(_crypto);\nexport const getIronSession = createGetIronSession(\n _crypto,\n unsealData,\n sealData,\n);\n","import * as Iron from \"iron-webcrypto\";\nimport type { CookieSerializeOptions } from \"cookie\";\nimport cookie from \"cookie\";\nimport type { IncomingMessage, ServerResponse } from \"http\";\n\n// default time allowed to check for iron seal validity when ttl passed\n// see https://hapi.dev/family/iron/api/?v=6.0.0#options\nconst timestampSkewSec = 60;\n\ntype passwordsMap = { [id: string]: string };\ntype password = string | passwordsMap;\n\nconst fourteenDaysInSeconds = 15 * 24 * 3600;\n\n// We store a token major version to handle data format changes when any. So that when you upgrade the cookies\n// can be kept alive between upgrades, no need to disconnect everyone.\nconst currentMajorVersion = 2;\nconst versionDelimiter = \"~\";\n\nconst defaultOptions: {\n ttl: number;\n cookieOptions: CookieSerializeOptions;\n} = {\n ttl: fourteenDaysInSeconds,\n cookieOptions: {\n httpOnly: true,\n secure: true,\n sameSite: \"lax\",\n path: \"/\",\n },\n};\n\nexport interface IronSessionOptions {\n /**\n * This is the cookie name that will be used inside the browser. You should make sure it's unique given\n * your application. Example: vercel-session\n */\n cookieName: string;\n\n /**\n * This is the password(s) that will be used to encrypt the cookie. It can be either a string or an object\n * like {1: \"password\", 2: password}.\n *\n * When you provide multiple passwords then all of them will be used to decrypt the cookie and only the most\n * recent (= highest key, 2 in this example) password will be used to encrypt the cookie. This allow you\n * to use password rotation (security)\n */\n password: password;\n\n /**\n * This is the time in seconds that the session will be valid for. This also set the max-age attribute of\n * the cookie automatically (minus 60 seconds so that the cookie always expire before the session).\n */\n ttl?: number;\n\n /**\n * This is the options that will be passed to the cookie library.\n * You can see all of them here: https://github.com/jshttp/cookie#options-1.\n *\n * If you want to use \"session cookies\" (cookies that are deleted when the browser is closed) then you need\n * to pass cookieOptions: { maxAge: undefined }.\n */\n cookieOptions?: CookieSerializeOptions;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface IronSessionData {\n // If we allow for any keys, then there's no more type check on unknown properties\n // which is not good\n // If we allow for any keys, the later delete will work but I prefer to disable the\n // check at this stage and\n // provide good type checking instead\n // [key: string]: unknown;\n}\n\nexport type IronSession = IronSessionData & {\n /**\n * Destroys the session data and removes the cookie.\n */\n destroy: () => void;\n\n /**\n * Encrypts the session data and sets the cookie.\n */\n save: () => Promise<void>;\n};\n\ndeclare module \"http\" {\n interface IncomingMessage {\n session: IronSession;\n }\n}\n\ntype RequestType = IncomingMessage | Request;\ntype ResponseType = ServerResponse | Response;\n\nexport function createGetIronSession(\n _crypto: Crypto,\n unsealData: ReturnType<typeof createUnsealData>,\n sealData: ReturnType<typeof createSealData>,\n) {\n return async (\n req: RequestType,\n res: ResponseType,\n userSessionOptions: IronSessionOptions,\n ): Promise<IronSession> => {\n if (\n !req ||\n !res ||\n !userSessionOptions ||\n !userSessionOptions.cookieName ||\n !userSessionOptions.password\n ) {\n throw new Error(\n `iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: \"...\", password: \"...\". Check the usage here: https://github.com/vvo/iron-session`,\n );\n }\n\n const passwordsAsMap = normalizeStringPasswordToMap(\n userSessionOptions.password,\n );\n\n Object.values(\n normalizeStringPasswordToMap(userSessionOptions.password),\n ).forEach((password) => {\n if (password.length < 32) {\n throw new Error(\n `iron-session: Bad usage. Password must be at least 32 characters long.`,\n );\n }\n });\n\n const options: Required<IronSessionOptions> = {\n ...defaultOptions,\n ...userSessionOptions,\n cookieOptions: {\n ...defaultOptions.cookieOptions,\n ...(userSessionOptions.cookieOptions || {}),\n },\n };\n\n if (options.ttl === 0) {\n // ttl = 0 means no expiration\n // but in reality cookies have to expire (can't have no max-age)\n // 2147483647 is the max value for max-age in cookies\n // see https://stackoverflow.com/a/11685301/147079\n options.ttl = 2147483647;\n }\n\n if (\n userSessionOptions.cookieOptions &&\n \"maxAge\" in userSessionOptions.cookieOptions\n ) {\n // session cookie, do not set maxAge, consider token as infinite\n if (userSessionOptions.cookieOptions.maxAge === undefined) {\n options.ttl = 0;\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(\n userSessionOptions.cookieOptions.maxAge,\n );\n }\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);\n }\n\n const sealFromCookies = cookie.parse(\n \"credentials\" in req\n ? req.headers.get(\"cookie\") || \"\"\n : req.headers.cookie || \"\",\n )[options.cookieName];\n\n const session =\n sealFromCookies === undefined\n ? {}\n : await unsealData<IronSessionData>(sealFromCookies, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n\n Object.defineProperties(session, {\n save: {\n value: async function save() {\n if (\"headersSent\" in res && res.headersSent === true) {\n throw new Error(\n `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()`,\n );\n }\n const seal = await sealData(session, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n const cookieValue = cookie.serialize(\n options.cookieName,\n seal,\n options.cookieOptions,\n );\n\n if (cookieValue.length > 4096) {\n throw new Error(\n `iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`,\n );\n }\n\n addToCookies(cookieValue, res);\n },\n },\n destroy: {\n value: function destroy() {\n Object.keys(session).forEach((key) => {\n // @ts-ignore See comment on the IronSessionData interface\n delete session[key];\n });\n\n const cookieValue = cookie.serialize(options.cookieName, \"\", {\n ...options.cookieOptions,\n maxAge: 0,\n });\n addToCookies(cookieValue, res);\n },\n },\n });\n\n return session as IronSession;\n };\n}\n\nfunction addToCookies(cookieValue: string, res: ResponseType) {\n if (\"headers\" in res) {\n res.headers.append(\"set-cookie\", cookieValue);\n return;\n }\n\n let existingSetCookie =\n (res.getHeader(\"set-cookie\") as string[] | string) ?? [];\n if (typeof existingSetCookie === \"string\") {\n existingSetCookie = [existingSetCookie];\n }\n res.setHeader(\"set-cookie\", [...existingSetCookie, cookieValue]);\n}\n\nfunction computeCookieMaxAge(ttl: number) {\n // The next line makes sure browser will expire cookies before seals are considered expired by the server.\n // It also allows for clock difference of 60 seconds maximum between server and clients.\n // It also makes sure to expire the cookie immediately when value is 0\n return ttl - timestampSkewSec;\n}\n\nexport function createUnsealData(_crypto: Crypto) {\n return async <T = Record<string, unknown>>(\n seal: string,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ): Promise<T> => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n const { sealWithoutVersion, tokenVersion } = parseSeal(seal);\n\n try {\n const data = await Iron.unseal(\n _crypto,\n sealWithoutVersion,\n passwordsAsMap,\n { ...Iron.defaults, ttl: ttl * 1000 },\n );\n\n if (tokenVersion === 2) {\n return data as T;\n }\n\n return {\n // @ts-expect-error `persistent` does not exist on newer tokens\n ...data.persistent,\n };\n } catch (error) {\n if (error instanceof Error) {\n if (\n error.message === \"Expired seal\" ||\n error.message === \"Bad hmac value\" ||\n error.message === \"Cannot find password: \" ||\n error.message === \"Incorrect number of sealed components\"\n ) {\n // if seal expired or\n // if seal is not valid (encrypted using a different password, when passwords are badly rotated) or\n // if we can't find back the password in the seal\n // then we just start a new session over\n return {} as T;\n }\n }\n\n throw error;\n }\n };\n}\n\nfunction parseSeal(seal: string): {\n sealWithoutVersion: string;\n tokenVersion: number | null;\n} {\n if (seal[seal.length - 2] === versionDelimiter) {\n const [sealWithoutVersion, tokenVersionAsString] =\n seal.split(versionDelimiter);\n return {\n sealWithoutVersion,\n tokenVersion: parseInt(tokenVersionAsString, 10),\n };\n }\n\n return { sealWithoutVersion: seal, tokenVersion: null };\n}\n\nexport function createSealData(_crypto: Crypto) {\n return async (\n data: unknown,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ) => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n\n const mostRecentPasswordId = Math.max(\n ...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10)),\n );\n\n const passwordForSeal = {\n id: mostRecentPasswordId.toString(),\n secret: passwordsAsMap[mostRecentPasswordId],\n };\n\n const seal = await Iron.seal(_crypto, data, passwordForSeal, {\n ...Iron.defaults,\n ttl: ttl * 1000,\n });\n\n return `${seal}${versionDelimiter}${currentMajorVersion}`;\n };\n}\n\nfunction normalizeStringPasswordToMap(password: password) {\n return typeof password === \"string\" ? { 1: password } : password;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,WAAsB;AAEtB,oBAAmB;AAKnB,IAAM,mBAAmB;AAKzB,IAAM,wBAAwB,KAAK,KAAK;AAIxC,IAAM,sBAAsB;AAC5B,IAAM,mBAAmB;AAEzB,IAAM,iBAGF;AAAA,EACF,KAAK;AAAA,EACL,eAAe;AAAA,IACb,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,UAAU;AAAA,IAC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,OAAO,kBAAW,WAAX,mBAAmB,YAAW;AAAU,WAAO,WAAW;AAErE,MAAI,OAAO,wBAAW,WAAX,mBAAmB,cAAnB,mBAA8B,YAAW;AAElD,WAAO,WAAW,OAAO;AAC3B,QAAM,IAAI,MACR,uEACF;AACF;AAEA,IAAM,UAAU,UAAU;AAGnB,IAAM,aAAa,iBAAiB,OAAO;AAC3C,IAAM,WAAW,eAAe,OAAO;AACvC,IAAM,iBAAiB,qBAC5B,SACA,YACA,QACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../index.ts","../../src/core.ts"],"sourcesContent":["// This allows the types to be linked from the main module instead of duplicated\nexport type { IronSessionOptions, IronSessionData } from \"iron-session\";\n\nimport {\n createGetIronSession,\n createSealData,\n createUnsealData,\n} from \"../src/core\";\n\nconst getCrypto = (): Crypto => {\n if (typeof globalThis.crypto?.subtle === \"object\") return globalThis.crypto;\n // @ts-ignore crypto.webcrypto is not available in dom, but is there in newer node versions\n if (typeof globalThis.crypto?.webcrypto?.subtle === \"object\")\n // @ts-ignore same as above\n return globalThis.crypto.webcrypto;\n throw new Error(\n \"no native implementation of WebCrypto is available in current context\",\n );\n};\n\nconst _crypto = getCrypto();\n\nexport const unsealData = createUnsealData(_crypto);\nexport const sealData = createSealData(_crypto);\nexport const getIronSession = createGetIronSession(\n _crypto,\n unsealData,\n sealData,\n);\n","import * as Iron from \"iron-webcrypto\";\nimport type { CookieSerializeOptions } from \"cookie\";\nimport cookie from \"cookie\";\nimport type { IncomingMessage, ServerResponse } from \"http\";\n\n// default time allowed to check for iron seal validity when ttl passed\n// see https://hapi.dev/family/iron/api/?v=6.0.0#options\nconst timestampSkewSec = 60;\n\ntype passwordsMap = { [id: string]: string };\ntype password = string | passwordsMap;\n\nconst fourteenDaysInSeconds = 15 * 24 * 3600;\n\n// We store a token major version to handle data format changes when any. So that when you upgrade the cookies\n// can be kept alive between upgrades, no need to disconnect everyone.\nconst currentMajorVersion = 2;\nconst versionDelimiter = \"~\";\n\nconst defaultOptions: {\n ttl: number;\n cookieOptions: CookieSerializeOptions;\n} = {\n ttl: fourteenDaysInSeconds,\n cookieOptions: {\n httpOnly: true,\n secure: true,\n sameSite: \"lax\",\n path: \"/\",\n },\n};\n\nexport interface IronSessionOptions {\n /**\n * This is the cookie name that will be used inside the browser. You should make sure it's unique given\n * your application. Example: vercel-session\n */\n cookieName: string;\n\n /**\n * This is the password(s) that will be used to encrypt the cookie. It can be either a string or an object\n * like {1: \"password\", 2: password}.\n *\n * When you provide multiple passwords then all of them will be used to decrypt the cookie and only the most\n * recent (= highest key, 2 in this example) password will be used to encrypt the cookie. This allow you\n * to use password rotation (security)\n */\n password: password;\n\n /**\n * This is the time in seconds that the session will be valid for. This also set the max-age attribute of\n * the cookie automatically (minus 60 seconds so that the cookie always expire before the session).\n */\n ttl?: number;\n\n /**\n * This is the options that will be passed to the cookie library.\n * You can see all of them here: https://github.com/jshttp/cookie#options-1.\n *\n * If you want to use \"session cookies\" (cookies that are deleted when the browser is closed) then you need\n * to pass cookieOptions: { maxAge: undefined }.\n */\n cookieOptions?: CookieSerializeOptions;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface IronSessionData {\n // If we allow for any keys, then there's no more type check on unknown properties\n // which is not good\n // If we allow for any keys, the later delete will work but I prefer to disable the\n // check at this stage and\n // provide good type checking instead\n // [key: string]: unknown;\n}\n\nexport type IronSession = IronSessionData & {\n /**\n * Destroys the session data and removes the cookie.\n */\n destroy: () => void;\n\n /**\n * Encrypts the session data and sets the cookie.\n */\n save: () => Promise<void>;\n};\n\ndeclare module \"http\" {\n interface IncomingMessage {\n session: IronSession;\n }\n}\n\ntype RequestType = IncomingMessage | Request;\ntype ResponseType = ServerResponse | Response;\n\nexport function createGetIronSession(\n _crypto: Crypto,\n unsealData: ReturnType<typeof createUnsealData>,\n sealData: ReturnType<typeof createSealData>,\n) {\n return async (\n req: RequestType,\n res: ResponseType,\n userSessionOptions: IronSessionOptions,\n ): Promise<IronSession> => {\n if (\n !req ||\n !res ||\n !userSessionOptions ||\n !userSessionOptions.cookieName ||\n !userSessionOptions.password\n ) {\n throw new Error(\n `iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: \"...\", password: \"...\". Check the usage here: https://github.com/vvo/iron-session`,\n );\n }\n\n const passwordsAsMap = normalizeStringPasswordToMap(\n userSessionOptions.password,\n );\n\n Object.values(\n normalizeStringPasswordToMap(userSessionOptions.password),\n ).forEach((password) => {\n if (password.length < 32) {\n throw new Error(\n `iron-session: Bad usage. Password must be at least 32 characters long.`,\n );\n }\n });\n\n const options: Required<IronSessionOptions> = {\n ...defaultOptions,\n ...userSessionOptions,\n cookieOptions: {\n ...defaultOptions.cookieOptions,\n ...(userSessionOptions.cookieOptions || {}),\n },\n };\n\n if (options.ttl === 0) {\n // ttl = 0 means no expiration\n // but in reality cookies have to expire (can't have no max-age)\n // 2147483647 is the max value for max-age in cookies\n // see https://stackoverflow.com/a/11685301/147079\n options.ttl = 2147483647;\n }\n\n if (\n userSessionOptions.cookieOptions &&\n \"maxAge\" in userSessionOptions.cookieOptions\n ) {\n // session cookie, do not set maxAge, consider token as infinite\n if (userSessionOptions.cookieOptions.maxAge === undefined) {\n options.ttl = 0;\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(\n userSessionOptions.cookieOptions.maxAge,\n );\n }\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);\n }\n\n const sealFromCookies = cookie.parse(\n \"credentials\" in req\n ? req.headers.get(\"cookie\") || \"\"\n : req.headers.cookie || \"\",\n )[options.cookieName];\n\n const session =\n sealFromCookies === undefined\n ? {}\n : await unsealData<IronSessionData>(sealFromCookies, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n\n Object.defineProperties(session, {\n save: {\n value: async function save() {\n if (\"headersSent\" in res && res.headersSent === true) {\n throw new Error(\n `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()`,\n );\n }\n const seal = await sealData(session, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n const cookieValue = cookie.serialize(\n options.cookieName,\n seal,\n options.cookieOptions,\n );\n\n if (cookieValue.length > 4096) {\n throw new Error(\n `iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`,\n );\n }\n\n addToCookies(cookieValue, res);\n },\n },\n destroy: {\n value: function destroy() {\n Object.keys(session).forEach((key) => {\n // @ts-ignore See comment on the IronSessionData interface\n delete session[key];\n });\n\n const cookieValue = cookie.serialize(options.cookieName, \"\", {\n ...options.cookieOptions,\n maxAge: 0,\n });\n addToCookies(cookieValue, res);\n },\n },\n });\n\n return session as IronSession;\n };\n}\n\nfunction addToCookies(cookieValue: string, res: ResponseType) {\n if (\"headers\" in res) {\n res.headers.append(\"set-cookie\", cookieValue);\n return;\n }\n\n let existingSetCookie =\n (res.getHeader(\"set-cookie\") as string[] | string) ?? [];\n if (typeof existingSetCookie === \"string\") {\n existingSetCookie = [existingSetCookie];\n }\n res.setHeader(\"set-cookie\", [...existingSetCookie, cookieValue]);\n}\n\nfunction computeCookieMaxAge(ttl: number) {\n // The next line makes sure browser will expire cookies before seals are considered expired by the server.\n // It also allows for clock difference of 60 seconds maximum between server and clients.\n // It also makes sure to expire the cookie immediately when value is 0\n return ttl - timestampSkewSec;\n}\n\nexport function createUnsealData(_crypto: Crypto) {\n return async <T = Record<string, unknown>>(\n seal: string,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ): Promise<T> => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n const { sealWithoutVersion, tokenVersion } = parseSeal(seal);\n\n try {\n const data = await Iron.unseal(\n _crypto,\n sealWithoutVersion,\n passwordsAsMap,\n { ...Iron.defaults, ttl: ttl * 1000 },\n );\n\n if (tokenVersion === 2) {\n return data as T;\n }\n\n return {\n // @ts-expect-error `persistent` does not exist on newer tokens\n ...data.persistent,\n };\n } catch (error) {\n if (error instanceof Error) {\n if (\n error.message === \"Expired seal\" ||\n error.message === \"Bad hmac value\" ||\n error.message === \"Cannot find password: \" ||\n error.message === \"Incorrect number of sealed components\"\n ) {\n // if seal expired or\n // if seal is not valid (encrypted using a different password, when passwords are badly rotated) or\n // if we can't find back the password in the seal\n // then we just start a new session over\n return {} as T;\n }\n }\n\n throw error;\n }\n };\n}\n\nfunction parseSeal(seal: string): {\n sealWithoutVersion: string;\n tokenVersion: number | null;\n} {\n if (seal[seal.length - 2] === versionDelimiter) {\n const [sealWithoutVersion, tokenVersionAsString] =\n seal.split(versionDelimiter);\n return {\n sealWithoutVersion,\n tokenVersion: parseInt(tokenVersionAsString, 10),\n };\n }\n\n return { sealWithoutVersion: seal, tokenVersion: null };\n}\n\nexport function createSealData(_crypto: Crypto) {\n return async (\n data: unknown,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ) => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n\n const mostRecentPasswordId = Math.max(\n ...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10)),\n );\n\n const passwordForSeal = {\n id: mostRecentPasswordId.toString(),\n secret: passwordsAsMap[mostRecentPasswordId],\n };\n\n const seal = await Iron.seal(_crypto, data, passwordForSeal, {\n ...Iron.defaults,\n ttl: ttl * 1000,\n });\n\n return `${seal}${versionDelimiter}${currentMajorVersion}`;\n };\n}\n\nfunction normalizeStringPasswordToMap(password: password) {\n return typeof password === \"string\" ? { 1: password } : password;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,WAAsB;AAEtB,oBAAmB;AAKnB,IAAM,mBAAmB;AAKzB,IAAM,wBAAwB,KAAK,KAAK;AAIxC,IAAM,sBAAsB;AAC5B,IAAM,mBAAmB;AAEzB,IAAM,iBAGF;AAAA,EACF,KAAK;AAAA,EACL,eAAe;AAAA,IACb,UAAU;AAAA,IACV,QAAQ;AAAA,IACR,UAAU;AAAA,IACV,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zB;AAEA,UAAMI,QAAO,MAAW,UAAKJ,UAAS,MAAM,iBAAiB;AAAA,MAC3D,GAAQ;AAAA,MACR,KAAK,MAAM;AAAA,IACb,CAAC;AAED,WAAO,GAAGI,QAAO,mBAAmB;AAAA,EACtC;AACF;AAEA,SAAS,6BAA6B,UAAoB;AACxD,SAAO,OAAO,aAAa,WAAW,EAAE,GAAG,SAAS,IAAI;AAC1D;;;AD5UA,IAAM,YAAY,MAAc;AAThC;AAUE,MAAI,SAAO,gBAAW,WAAX,mBAAmB,YAAW;AAAU,WAAO,WAAW;AAErE,MAAI,SAAO,sBAAW,WAAX,mBAAmB,cAAnB,mBAA8B,YAAW;AAElD,WAAO,WAAW,OAAO;AAC3B,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;AAEA,IAAM,UAAU,UAAU;AAEnB,IAAM,aAAa,iBAAiB,OAAO;AAC3C,IAAM,WAAW,eAAe,OAAO;AACvC,IAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AACF;","names":["_crypto","unsealData","sealData","cookie","seal"]}
|
package/edge/dist/index.mjs
CHANGED
|
@@ -17,12 +17,20 @@ var defaultOptions = {
|
|
|
17
17
|
function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
18
18
|
return async (req, res, userSessionOptions) => {
|
|
19
19
|
if (!req || !res || !userSessionOptions || !userSessionOptions.cookieName || !userSessionOptions.password) {
|
|
20
|
-
throw new Error(
|
|
20
|
+
throw new Error(
|
|
21
|
+
`iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: "...", password: "...". Check the usage here: https://github.com/vvo/iron-session`
|
|
22
|
+
);
|
|
21
23
|
}
|
|
22
|
-
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
23
|
-
|
|
24
|
+
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
25
|
+
userSessionOptions.password
|
|
26
|
+
);
|
|
27
|
+
Object.values(
|
|
28
|
+
normalizeStringPasswordToMap(userSessionOptions.password)
|
|
29
|
+
).forEach((password) => {
|
|
24
30
|
if (password.length < 32) {
|
|
25
|
-
throw new Error(
|
|
31
|
+
throw new Error(
|
|
32
|
+
`iron-session: Bad usage. Password must be at least 32 characters long.`
|
|
33
|
+
);
|
|
26
34
|
}
|
|
27
35
|
});
|
|
28
36
|
const options = {
|
|
@@ -40,12 +48,16 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
40
48
|
if (userSessionOptions.cookieOptions.maxAge === void 0) {
|
|
41
49
|
options.ttl = 0;
|
|
42
50
|
} else {
|
|
43
|
-
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
51
|
+
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
52
|
+
userSessionOptions.cookieOptions.maxAge
|
|
53
|
+
);
|
|
44
54
|
}
|
|
45
55
|
} else {
|
|
46
56
|
options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);
|
|
47
57
|
}
|
|
48
|
-
const sealFromCookies = cookie.parse(
|
|
58
|
+
const sealFromCookies = cookie.parse(
|
|
59
|
+
"credentials" in req ? req.headers.get("cookie") || "" : req.headers.cookie || ""
|
|
60
|
+
)[options.cookieName];
|
|
49
61
|
const session = sealFromCookies === void 0 ? {} : await unsealData2(sealFromCookies, {
|
|
50
62
|
password: passwordsAsMap,
|
|
51
63
|
ttl: options.ttl
|
|
@@ -54,15 +66,23 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
54
66
|
save: {
|
|
55
67
|
value: async function save() {
|
|
56
68
|
if ("headersSent" in res && res.headersSent === true) {
|
|
57
|
-
throw new Error(
|
|
69
|
+
throw new Error(
|
|
70
|
+
`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()`
|
|
71
|
+
);
|
|
58
72
|
}
|
|
59
73
|
const seal2 = await sealData2(session, {
|
|
60
74
|
password: passwordsAsMap,
|
|
61
75
|
ttl: options.ttl
|
|
62
76
|
});
|
|
63
|
-
const cookieValue = cookie.serialize(
|
|
77
|
+
const cookieValue = cookie.serialize(
|
|
78
|
+
options.cookieName,
|
|
79
|
+
seal2,
|
|
80
|
+
options.cookieOptions
|
|
81
|
+
);
|
|
64
82
|
if (cookieValue.length > 4096) {
|
|
65
|
-
throw new Error(
|
|
83
|
+
throw new Error(
|
|
84
|
+
`iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`
|
|
85
|
+
);
|
|
66
86
|
}
|
|
67
87
|
addToCookies(cookieValue, res);
|
|
68
88
|
}
|
|
@@ -106,7 +126,12 @@ function createUnsealData(_crypto2) {
|
|
|
106
126
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
107
127
|
const { sealWithoutVersion, tokenVersion } = parseSeal(seal2);
|
|
108
128
|
try {
|
|
109
|
-
const data = await Iron.unseal(
|
|
129
|
+
const data = await Iron.unseal(
|
|
130
|
+
_crypto2,
|
|
131
|
+
sealWithoutVersion,
|
|
132
|
+
passwordsAsMap,
|
|
133
|
+
{ ...Iron.defaults, ttl: ttl * 1e3 }
|
|
134
|
+
);
|
|
110
135
|
if (tokenVersion === 2) {
|
|
111
136
|
return data;
|
|
112
137
|
}
|
|
@@ -139,7 +164,9 @@ function createSealData(_crypto2) {
|
|
|
139
164
|
ttl = fourteenDaysInSeconds
|
|
140
165
|
}) => {
|
|
141
166
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
142
|
-
const mostRecentPasswordId = Math.max(
|
|
167
|
+
const mostRecentPasswordId = Math.max(
|
|
168
|
+
...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10))
|
|
169
|
+
);
|
|
143
170
|
const passwordForSeal = {
|
|
144
171
|
id: mostRecentPasswordId.toString(),
|
|
145
172
|
secret: passwordsAsMap[mostRecentPasswordId]
|
|
@@ -162,16 +189,19 @@ var getCrypto = () => {
|
|
|
162
189
|
return globalThis.crypto;
|
|
163
190
|
if (typeof ((_c = (_b = globalThis.crypto) == null ? void 0 : _b.webcrypto) == null ? void 0 : _c.subtle) === "object")
|
|
164
191
|
return globalThis.crypto.webcrypto;
|
|
165
|
-
throw new Error(
|
|
192
|
+
throw new Error(
|
|
193
|
+
"no native implementation of WebCrypto is available in current context"
|
|
194
|
+
);
|
|
166
195
|
};
|
|
167
196
|
var _crypto = getCrypto();
|
|
168
197
|
var unsealData = createUnsealData(_crypto);
|
|
169
198
|
var sealData = createSealData(_crypto);
|
|
170
|
-
var getIronSession = createGetIronSession(
|
|
199
|
+
var getIronSession = createGetIronSession(
|
|
200
|
+
_crypto,
|
|
201
|
+
unsealData,
|
|
202
|
+
sealData
|
|
203
|
+
);
|
|
171
204
|
export {
|
|
172
|
-
createGetIronSession,
|
|
173
|
-
createSealData,
|
|
174
|
-
createUnsealData,
|
|
175
205
|
getIronSession,
|
|
176
206
|
sealData,
|
|
177
207
|
unsealData
|
package/edge/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core.ts","../index.ts"],"sourcesContent":["import * as Iron from \"iron-webcrypto\";\nimport type { CookieSerializeOptions } from \"cookie\";\nimport cookie from \"cookie\";\nimport type { IncomingMessage, ServerResponse } from \"http\";\n\n// default time allowed to check for iron seal validity when ttl passed\n// see https://hapi.dev/family/iron/api/?v=6.0.0#options\nconst timestampSkewSec = 60;\n\ntype passwordsMap = { [id: string]: string };\ntype password = string | passwordsMap;\n\nconst fourteenDaysInSeconds = 15 * 24 * 3600;\n\n// We store a token major version to handle data format changes when any. So that when you upgrade the cookies\n// can be kept alive between upgrades, no need to disconnect everyone.\nconst currentMajorVersion = 2;\nconst versionDelimiter = \"~\";\n\nconst defaultOptions: {\n ttl: number;\n cookieOptions: CookieSerializeOptions;\n} = {\n ttl: fourteenDaysInSeconds,\n cookieOptions: {\n httpOnly: true,\n secure: true,\n sameSite: \"lax\",\n path: \"/\",\n },\n};\n\nexport interface IronSessionOptions {\n /**\n * This is the cookie name that will be used inside the browser. You should make sure it's unique given\n * your application. Example: vercel-session\n */\n cookieName: string;\n\n /**\n * This is the password(s) that will be used to encrypt the cookie. It can be either a string or an object\n * like {1: \"password\", 2: password}.\n *\n * When you provide multiple passwords then all of them will be used to decrypt the cookie and only the most\n * recent (= highest key, 2 in this example) password will be used to encrypt the cookie. This allow you\n * to use password rotation (security)\n */\n password: password;\n\n /**\n * This is the time in seconds that the session will be valid for. This also set the max-age attribute of\n * the cookie automatically (minus 60 seconds so that the cookie always expire before the session).\n */\n ttl?: number;\n\n /**\n * This is the options that will be passed to the cookie library.\n * You can see all of them here: https://github.com/jshttp/cookie#options-1.\n *\n * If you want to use \"session cookies\" (cookies that are deleted when the browser is closed) then you need\n * to pass cookieOptions: { maxAge: undefined }.\n */\n cookieOptions?: CookieSerializeOptions;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface IronSessionData {\n // If we allow for any keys, then there's no more type check on unknown properties\n // which is not good\n // If we allow for any keys, the later delete will work but I prefer to disable the\n // check at this stage and\n // provide good type checking instead\n // [key: string]: unknown;\n}\n\nexport type IronSession = IronSessionData & {\n /**\n * Destroys the session data and removes the cookie.\n */\n destroy: () => void;\n\n /**\n * Encrypts the session data and sets the cookie.\n */\n save: () => Promise<void>;\n};\n\ndeclare module \"http\" {\n interface IncomingMessage {\n session: IronSession;\n }\n}\n\ntype RequestType = IncomingMessage | Request;\ntype ResponseType = ServerResponse | Response;\n\nexport function createGetIronSession(\n _crypto: Crypto,\n unsealData: ReturnType<typeof createUnsealData>,\n sealData: ReturnType<typeof createSealData>,\n) {\n return async (\n req: RequestType,\n res: ResponseType,\n userSessionOptions: IronSessionOptions,\n ): Promise<IronSession> => {\n if (\n !req ||\n !res ||\n !userSessionOptions ||\n !userSessionOptions.cookieName ||\n !userSessionOptions.password\n ) {\n throw new Error(\n `iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: \"...\", password: \"...\". Check the usage here: https://github.com/vvo/iron-session`,\n );\n }\n\n const passwordsAsMap = normalizeStringPasswordToMap(\n userSessionOptions.password,\n );\n\n Object.values(\n normalizeStringPasswordToMap(userSessionOptions.password),\n ).forEach((password) => {\n if (password.length < 32) {\n throw new Error(\n `iron-session: Bad usage. Password must be at least 32 characters long.`,\n );\n }\n });\n\n const options: Required<IronSessionOptions> = {\n ...defaultOptions,\n ...userSessionOptions,\n cookieOptions: {\n ...defaultOptions.cookieOptions,\n ...(userSessionOptions.cookieOptions || {}),\n },\n };\n\n if (options.ttl === 0) {\n // ttl = 0 means no expiration\n // but in reality cookies have to expire (can't have no max-age)\n // 2147483647 is the max value for max-age in cookies\n // see https://stackoverflow.com/a/11685301/147079\n options.ttl = 2147483647;\n }\n\n if (\n userSessionOptions.cookieOptions &&\n \"maxAge\" in userSessionOptions.cookieOptions\n ) {\n // session cookie, do not set maxAge, consider token as infinite\n if (userSessionOptions.cookieOptions.maxAge === undefined) {\n options.ttl = 0;\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(\n userSessionOptions.cookieOptions.maxAge,\n );\n }\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);\n }\n\n const sealFromCookies = cookie.parse(\n \"credentials\" in req\n ? req.headers.get(\"cookie\") || \"\"\n : req.headers.cookie || \"\",\n )[options.cookieName];\n\n const session =\n sealFromCookies === undefined\n ? {}\n : await unsealData<IronSessionData>(sealFromCookies, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n\n Object.defineProperties(session, {\n save: {\n value: async function save() {\n if (\"headersSent\" in res && res.headersSent === true) {\n throw new Error(\n `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()`,\n );\n }\n const seal = await sealData(session, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n const cookieValue = cookie.serialize(\n options.cookieName,\n seal,\n options.cookieOptions,\n );\n\n if (cookieValue.length > 4096) {\n throw new Error(\n `iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`,\n );\n }\n\n addToCookies(cookieValue, res);\n },\n },\n destroy: {\n value: function destroy() {\n Object.keys(session).forEach((key) => {\n // @ts-ignore See comment on the IronSessionData interface\n delete session[key];\n });\n\n const cookieValue = cookie.serialize(options.cookieName, \"\", {\n ...options.cookieOptions,\n maxAge: 0,\n });\n addToCookies(cookieValue, res);\n },\n },\n });\n\n return session as IronSession;\n };\n}\n\nfunction addToCookies(cookieValue: string, res: ResponseType) {\n if (\"headers\" in res) {\n res.headers.append(\"set-cookie\", cookieValue);\n return;\n }\n\n let existingSetCookie =\n (res.getHeader(\"set-cookie\") as string[] | string) ?? [];\n if (typeof existingSetCookie === \"string\") {\n existingSetCookie = [existingSetCookie];\n }\n res.setHeader(\"set-cookie\", [...existingSetCookie, cookieValue]);\n}\n\nfunction computeCookieMaxAge(ttl: number) {\n // The next line makes sure browser will expire cookies before seals are considered expired by the server.\n // It also allows for clock difference of 60 seconds maximum between server and clients.\n // It also makes sure to expire the cookie immediately when value is 0\n return ttl - timestampSkewSec;\n}\n\nexport function createUnsealData(_crypto: Crypto) {\n return async <T = Record<string, unknown>>(\n seal: string,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ): Promise<T> => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n const { sealWithoutVersion, tokenVersion } = parseSeal(seal);\n\n try {\n const data = await Iron.unseal(\n _crypto,\n sealWithoutVersion,\n passwordsAsMap,\n { ...Iron.defaults, ttl: ttl * 1000 },\n );\n\n if (tokenVersion === 2) {\n return data as T;\n }\n\n return {\n // @ts-expect-error `persistent` does not exist on newer tokens\n ...data.persistent,\n };\n } catch (error) {\n if (error instanceof Error) {\n if (\n error.message === \"Expired seal\" ||\n error.message === \"Bad hmac value\" ||\n error.message === \"Cannot find password: \" ||\n error.message === \"Incorrect number of sealed components\"\n ) {\n // if seal expired or\n // if seal is not valid (encrypted using a different password, when passwords are badly rotated) or\n // if we can't find back the password in the seal\n // then we just start a new session over\n return {} as T;\n }\n }\n\n throw error;\n }\n };\n}\n\nfunction parseSeal(seal: string): {\n sealWithoutVersion: string;\n tokenVersion: number | null;\n} {\n if (seal[seal.length - 2] === versionDelimiter) {\n const [sealWithoutVersion, tokenVersionAsString] =\n seal.split(versionDelimiter);\n return {\n sealWithoutVersion,\n tokenVersion: parseInt(tokenVersionAsString, 10),\n };\n }\n\n return { sealWithoutVersion: seal, tokenVersion: null };\n}\n\nexport function createSealData(_crypto: Crypto) {\n return async (\n data: unknown,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ) => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n\n const mostRecentPasswordId = Math.max(\n ...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10)),\n );\n\n const passwordForSeal = {\n id: mostRecentPasswordId.toString(),\n secret: passwordsAsMap[mostRecentPasswordId],\n };\n\n const seal = await Iron.seal(_crypto, data, passwordForSeal, {\n ...Iron.defaults,\n ttl: ttl * 1000,\n });\n\n return `${seal}${versionDelimiter}${currentMajorVersion}`;\n };\n}\n\nfunction normalizeStringPasswordToMap(password: password) {\n return typeof password === \"string\" ? { 1: password } : password;\n}\n","import {\n createGetIronSession,\n createSealData,\n createUnsealData,\n} from \"../src/core\";\n\nconst getCrypto = (): Crypto => {\n if (typeof globalThis.crypto?.subtle === \"object\") return globalThis.crypto;\n // @ts-ignore crypto.webcrypto is not available in dom, but is there in newer node versions\n if (typeof globalThis.crypto?.webcrypto?.subtle === \"object\")\n // @ts-ignore same as above\n return globalThis.crypto.webcrypto;\n throw new Error(\n \"no native implementation of WebCrypto is available in current context\",\n );\n};\n\nconst _crypto = getCrypto();\n\nexport * from \"../src/core\";\nexport const unsealData = createUnsealData(_crypto);\nexport const sealData = createSealData(_crypto);\nexport const getIronSession = createGetIronSession(\n _crypto,\n unsealData,\n sealData,\n);\n"],"mappings":";AAAA;AAEA;AAKA,IAAM,mBAAmB;AAKzB,IAAM,wBAAwB,KAAK,KAAK;AAIxC,IAAM,sBAAsB;AAC5B,IAAM,mBAAmB;AAEzB,IAAM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AAO,OAAO,aAAa,WAAW,EAAE,GAAG,SAAS,IAAI;AAC1D;;;AC/UA,IAAM,YAAY,MAAc;AANhC;AAOE,MAAI,OAAO,kBAAW,WAAX,mBAAmB,YAAW;AAAU,WAAO,WAAW;AAErE,MAAI,OAAO,wBAAW,WAAX,mBAAmB,cAAnB,mBAA8B,YAAW;AAElD,WAAO,WAAW,OAAO;AAC3B,QAAM,IAAI,MACR,uEACF;AACF;AAEA,IAAM,UAAU,UAAU;AAGnB,IAAM,aAAa,iBAAiB,OAAO;AAC3C,IAAM,WAAW,eAAe,OAAO;AACvC,IAAM,iBAAiB,qBAC5B,SACA,YACA,QACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/core.ts","../index.ts"],"sourcesContent":["import * as Iron from \"iron-webcrypto\";\nimport type { CookieSerializeOptions } from \"cookie\";\nimport cookie from \"cookie\";\nimport type { IncomingMessage, ServerResponse } from \"http\";\n\n// default time allowed to check for iron seal validity when ttl passed\n// see https://hapi.dev/family/iron/api/?v=6.0.0#options\nconst timestampSkewSec = 60;\n\ntype passwordsMap = { [id: string]: string };\ntype password = string | passwordsMap;\n\nconst fourteenDaysInSeconds = 15 * 24 * 3600;\n\n// We store a token major version to handle data format changes when any. So that when you upgrade the cookies\n// can be kept alive between upgrades, no need to disconnect everyone.\nconst currentMajorVersion = 2;\nconst versionDelimiter = \"~\";\n\nconst defaultOptions: {\n ttl: number;\n cookieOptions: CookieSerializeOptions;\n} = {\n ttl: fourteenDaysInSeconds,\n cookieOptions: {\n httpOnly: true,\n secure: true,\n sameSite: \"lax\",\n path: \"/\",\n },\n};\n\nexport interface IronSessionOptions {\n /**\n * This is the cookie name that will be used inside the browser. You should make sure it's unique given\n * your application. Example: vercel-session\n */\n cookieName: string;\n\n /**\n * This is the password(s) that will be used to encrypt the cookie. It can be either a string or an object\n * like {1: \"password\", 2: password}.\n *\n * When you provide multiple passwords then all of them will be used to decrypt the cookie and only the most\n * recent (= highest key, 2 in this example) password will be used to encrypt the cookie. This allow you\n * to use password rotation (security)\n */\n password: password;\n\n /**\n * This is the time in seconds that the session will be valid for. This also set the max-age attribute of\n * the cookie automatically (minus 60 seconds so that the cookie always expire before the session).\n */\n ttl?: number;\n\n /**\n * This is the options that will be passed to the cookie library.\n * You can see all of them here: https://github.com/jshttp/cookie#options-1.\n *\n * If you want to use \"session cookies\" (cookies that are deleted when the browser is closed) then you need\n * to pass cookieOptions: { maxAge: undefined }.\n */\n cookieOptions?: CookieSerializeOptions;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-interface\nexport interface IronSessionData {\n // If we allow for any keys, then there's no more type check on unknown properties\n // which is not good\n // If we allow for any keys, the later delete will work but I prefer to disable the\n // check at this stage and\n // provide good type checking instead\n // [key: string]: unknown;\n}\n\nexport type IronSession = IronSessionData & {\n /**\n * Destroys the session data and removes the cookie.\n */\n destroy: () => void;\n\n /**\n * Encrypts the session data and sets the cookie.\n */\n save: () => Promise<void>;\n};\n\ndeclare module \"http\" {\n interface IncomingMessage {\n session: IronSession;\n }\n}\n\ntype RequestType = IncomingMessage | Request;\ntype ResponseType = ServerResponse | Response;\n\nexport function createGetIronSession(\n _crypto: Crypto,\n unsealData: ReturnType<typeof createUnsealData>,\n sealData: ReturnType<typeof createSealData>,\n) {\n return async (\n req: RequestType,\n res: ResponseType,\n userSessionOptions: IronSessionOptions,\n ): Promise<IronSession> => {\n if (\n !req ||\n !res ||\n !userSessionOptions ||\n !userSessionOptions.cookieName ||\n !userSessionOptions.password\n ) {\n throw new Error(\n `iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: \"...\", password: \"...\". Check the usage here: https://github.com/vvo/iron-session`,\n );\n }\n\n const passwordsAsMap = normalizeStringPasswordToMap(\n userSessionOptions.password,\n );\n\n Object.values(\n normalizeStringPasswordToMap(userSessionOptions.password),\n ).forEach((password) => {\n if (password.length < 32) {\n throw new Error(\n `iron-session: Bad usage. Password must be at least 32 characters long.`,\n );\n }\n });\n\n const options: Required<IronSessionOptions> = {\n ...defaultOptions,\n ...userSessionOptions,\n cookieOptions: {\n ...defaultOptions.cookieOptions,\n ...(userSessionOptions.cookieOptions || {}),\n },\n };\n\n if (options.ttl === 0) {\n // ttl = 0 means no expiration\n // but in reality cookies have to expire (can't have no max-age)\n // 2147483647 is the max value for max-age in cookies\n // see https://stackoverflow.com/a/11685301/147079\n options.ttl = 2147483647;\n }\n\n if (\n userSessionOptions.cookieOptions &&\n \"maxAge\" in userSessionOptions.cookieOptions\n ) {\n // session cookie, do not set maxAge, consider token as infinite\n if (userSessionOptions.cookieOptions.maxAge === undefined) {\n options.ttl = 0;\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(\n userSessionOptions.cookieOptions.maxAge,\n );\n }\n } else {\n options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);\n }\n\n const sealFromCookies = cookie.parse(\n \"credentials\" in req\n ? req.headers.get(\"cookie\") || \"\"\n : req.headers.cookie || \"\",\n )[options.cookieName];\n\n const session =\n sealFromCookies === undefined\n ? {}\n : await unsealData<IronSessionData>(sealFromCookies, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n\n Object.defineProperties(session, {\n save: {\n value: async function save() {\n if (\"headersSent\" in res && res.headersSent === true) {\n throw new Error(\n `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()`,\n );\n }\n const seal = await sealData(session, {\n password: passwordsAsMap,\n ttl: options.ttl,\n });\n const cookieValue = cookie.serialize(\n options.cookieName,\n seal,\n options.cookieOptions,\n );\n\n if (cookieValue.length > 4096) {\n throw new Error(\n `iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`,\n );\n }\n\n addToCookies(cookieValue, res);\n },\n },\n destroy: {\n value: function destroy() {\n Object.keys(session).forEach((key) => {\n // @ts-ignore See comment on the IronSessionData interface\n delete session[key];\n });\n\n const cookieValue = cookie.serialize(options.cookieName, \"\", {\n ...options.cookieOptions,\n maxAge: 0,\n });\n addToCookies(cookieValue, res);\n },\n },\n });\n\n return session as IronSession;\n };\n}\n\nfunction addToCookies(cookieValue: string, res: ResponseType) {\n if (\"headers\" in res) {\n res.headers.append(\"set-cookie\", cookieValue);\n return;\n }\n\n let existingSetCookie =\n (res.getHeader(\"set-cookie\") as string[] | string) ?? [];\n if (typeof existingSetCookie === \"string\") {\n existingSetCookie = [existingSetCookie];\n }\n res.setHeader(\"set-cookie\", [...existingSetCookie, cookieValue]);\n}\n\nfunction computeCookieMaxAge(ttl: number) {\n // The next line makes sure browser will expire cookies before seals are considered expired by the server.\n // It also allows for clock difference of 60 seconds maximum between server and clients.\n // It also makes sure to expire the cookie immediately when value is 0\n return ttl - timestampSkewSec;\n}\n\nexport function createUnsealData(_crypto: Crypto) {\n return async <T = Record<string, unknown>>(\n seal: string,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ): Promise<T> => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n const { sealWithoutVersion, tokenVersion } = parseSeal(seal);\n\n try {\n const data = await Iron.unseal(\n _crypto,\n sealWithoutVersion,\n passwordsAsMap,\n { ...Iron.defaults, ttl: ttl * 1000 },\n );\n\n if (tokenVersion === 2) {\n return data as T;\n }\n\n return {\n // @ts-expect-error `persistent` does not exist on newer tokens\n ...data.persistent,\n };\n } catch (error) {\n if (error instanceof Error) {\n if (\n error.message === \"Expired seal\" ||\n error.message === \"Bad hmac value\" ||\n error.message === \"Cannot find password: \" ||\n error.message === \"Incorrect number of sealed components\"\n ) {\n // if seal expired or\n // if seal is not valid (encrypted using a different password, when passwords are badly rotated) or\n // if we can't find back the password in the seal\n // then we just start a new session over\n return {} as T;\n }\n }\n\n throw error;\n }\n };\n}\n\nfunction parseSeal(seal: string): {\n sealWithoutVersion: string;\n tokenVersion: number | null;\n} {\n if (seal[seal.length - 2] === versionDelimiter) {\n const [sealWithoutVersion, tokenVersionAsString] =\n seal.split(versionDelimiter);\n return {\n sealWithoutVersion,\n tokenVersion: parseInt(tokenVersionAsString, 10),\n };\n }\n\n return { sealWithoutVersion: seal, tokenVersion: null };\n}\n\nexport function createSealData(_crypto: Crypto) {\n return async (\n data: unknown,\n {\n password,\n ttl = fourteenDaysInSeconds,\n }: { password: password; ttl?: number },\n ) => {\n const passwordsAsMap = normalizeStringPasswordToMap(password);\n\n const mostRecentPasswordId = Math.max(\n ...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10)),\n );\n\n const passwordForSeal = {\n id: mostRecentPasswordId.toString(),\n secret: passwordsAsMap[mostRecentPasswordId],\n };\n\n const seal = await Iron.seal(_crypto, data, passwordForSeal, {\n ...Iron.defaults,\n ttl: ttl * 1000,\n });\n\n return `${seal}${versionDelimiter}${currentMajorVersion}`;\n };\n}\n\nfunction normalizeStringPasswordToMap(password: password) {\n return typeof password === \"string\" ? { 1: password } : password;\n}\n","// This allows the types to be linked from the main module instead of duplicated\nexport type { IronSessionOptions, IronSessionData } from \"iron-session\";\n\nimport {\n createGetIronSession,\n createSealData,\n createUnsealData,\n} from \"../src/core\";\n\nconst getCrypto = (): Crypto => {\n if (typeof globalThis.crypto?.subtle === \"object\") return globalThis.crypto;\n // @ts-ignore crypto.webcrypto is not available in dom, but is there in newer node versions\n if (typeof globalThis.crypto?.webcrypto?.subtle === \"object\")\n // @ts-ignore same as above\n return globalThis.crypto.webcrypto;\n throw new Error(\n \"no native implementation of WebCrypto is available in current context\",\n );\n};\n\nconst _crypto = getCrypto();\n\nexport const unsealData = createUnsealData(_crypto);\nexport const sealData = createSealData(_crypto);\nexport const getIronSession = createGetIronSession(\n _crypto,\n unsealData,\n sealData,\n);\n"],"mappings":";AAAA,YAAY,UAAU;AAEtB,OAAO,YAAY;AAKnB,IAAM,mBAAmB;AAKzB,IAAM,wBAAwB,KAAK,KAAK;AAIxC,IAAM,sBAAsB;AAC5B,IAAM,mBAAmB;AAEzB,IAAM,iBAGF;AAAA,EACF,KAAK;AAAA,EACL,eAAe;AAAA,IACb,UAAU;AAAA,IACV,QA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AAA,IACzB;AAEA,UAAMG,QAAO,MAAW,UAAKH,UAAS,MAAM,iBAAiB;AAAA,MAC3D,GAAQ;AAAA,MACR,KAAK,MAAM;AAAA,IACb,CAAC;AAED,WAAO,GAAGG,QAAO,mBAAmB;AAAA,EACtC;AACF;AAEA,SAAS,6BAA6B,UAAoB;AACxD,SAAO,OAAO,aAAa,WAAW,EAAE,GAAG,SAAS,IAAI;AAC1D;;;AC5UA,IAAM,YAAY,MAAc;AAThC;AAUE,MAAI,SAAO,gBAAW,WAAX,mBAAmB,YAAW;AAAU,WAAO,WAAW;AAErE,MAAI,SAAO,sBAAW,WAAX,mBAAmB,cAAnB,mBAA8B,YAAW;AAElD,WAAO,WAAW,OAAO;AAC3B,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;AAEA,IAAM,UAAU,UAAU;AAEnB,IAAM,aAAa,iBAAiB,OAAO;AAC3C,IAAM,WAAW,eAAe,OAAO;AACvC,IAAM,iBAAiB;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AACF;","names":["_crypto","unsealData","sealData","seal"]}
|
package/edge/index.d.ts
CHANGED
|
@@ -1,68 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as iron_session from 'iron-session';
|
|
2
|
+
export { IronSessionData, IronSessionOptions } from 'iron-session';
|
|
2
3
|
import * as http from 'http';
|
|
3
|
-
import { IncomingMessage, ServerResponse } from 'http';
|
|
4
|
-
|
|
5
|
-
declare type passwordsMap = {
|
|
6
|
-
[id: string]: string;
|
|
7
|
-
};
|
|
8
|
-
declare type password = string | passwordsMap;
|
|
9
|
-
interface IronSessionOptions {
|
|
10
|
-
/**
|
|
11
|
-
* This is the cookie name that will be used inside the browser. You should make sure it's unique given
|
|
12
|
-
* your application. Example: vercel-session
|
|
13
|
-
*/
|
|
14
|
-
cookieName: string;
|
|
15
|
-
/**
|
|
16
|
-
* This is the password(s) that will be used to encrypt the cookie. It can be either a string or an object
|
|
17
|
-
* like {1: "password", 2: password}.
|
|
18
|
-
*
|
|
19
|
-
* When you provide multiple passwords then all of them will be used to decrypt the cookie and only the most
|
|
20
|
-
* recent (= highest key, 2 in this example) password will be used to encrypt the cookie. This allow you
|
|
21
|
-
* to use password rotation (security)
|
|
22
|
-
*/
|
|
23
|
-
password: password;
|
|
24
|
-
/**
|
|
25
|
-
* This is the time in seconds that the session will be valid for. This also set the max-age attribute of
|
|
26
|
-
* the cookie automatically (minus 60 seconds so that the cookie always expire before the session).
|
|
27
|
-
*/
|
|
28
|
-
ttl?: number;
|
|
29
|
-
/**
|
|
30
|
-
* This is the options that will be passed to the cookie library.
|
|
31
|
-
* You can see all of them here: https://github.com/jshttp/cookie#options-1.
|
|
32
|
-
*
|
|
33
|
-
* If you want to use "session cookies" (cookies that are deleted when the browser is closed) then you need
|
|
34
|
-
* to pass cookieOptions: { maxAge: undefined }.
|
|
35
|
-
*/
|
|
36
|
-
cookieOptions?: CookieSerializeOptions;
|
|
37
|
-
}
|
|
38
|
-
interface IronSessionData {
|
|
39
|
-
}
|
|
40
|
-
declare type IronSession = IronSessionData & {
|
|
41
|
-
/**
|
|
42
|
-
* Destroys the session data and removes the cookie.
|
|
43
|
-
*/
|
|
44
|
-
destroy: () => void;
|
|
45
|
-
/**
|
|
46
|
-
* Encrypts the session data and sets the cookie.
|
|
47
|
-
*/
|
|
48
|
-
save: () => Promise<void>;
|
|
49
|
-
};
|
|
50
|
-
declare module "http" {
|
|
51
|
-
interface IncomingMessage {
|
|
52
|
-
session: IronSession;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
declare type RequestType = IncomingMessage | Request;
|
|
56
|
-
declare type ResponseType = ServerResponse | Response;
|
|
57
|
-
declare function createGetIronSession(_crypto: Crypto, unsealData: ReturnType<typeof createUnsealData>, sealData: ReturnType<typeof createSealData>): (req: RequestType, res: ResponseType, userSessionOptions: IronSessionOptions) => Promise<IronSession>;
|
|
58
|
-
declare function createUnsealData(_crypto: Crypto): <T = Record<string, unknown>>(seal: string, { password, ttl, }: {
|
|
59
|
-
password: password;
|
|
60
|
-
ttl?: number | undefined;
|
|
61
|
-
}) => Promise<T>;
|
|
62
|
-
declare function createSealData(_crypto: Crypto): (data: unknown, { password, ttl, }: {
|
|
63
|
-
password: password;
|
|
64
|
-
ttl?: number | undefined;
|
|
65
|
-
}) => Promise<string>;
|
|
66
4
|
|
|
67
5
|
declare const unsealData: <T = Record<string, unknown>>(seal: string, { password, ttl, }: {
|
|
68
6
|
password: string | {
|
|
@@ -76,6 +14,6 @@ declare const sealData: (data: unknown, { password, ttl, }: {
|
|
|
76
14
|
};
|
|
77
15
|
ttl?: number | undefined;
|
|
78
16
|
}) => Promise<string>;
|
|
79
|
-
declare const getIronSession: (req: http.IncomingMessage | Request, res: http.ServerResponse | Response, userSessionOptions: IronSessionOptions) => Promise<IronSession>;
|
|
17
|
+
declare const getIronSession: (req: http.IncomingMessage | Request, res: http.ServerResponse | Response, userSessionOptions: iron_session.IronSessionOptions) => Promise<iron_session.IronSession>;
|
|
80
18
|
|
|
81
|
-
export {
|
|
19
|
+
export { getIronSession, sealData, unsealData };
|
package/edge/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -16,15 +17,15 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
}
|
|
17
18
|
return to;
|
|
18
19
|
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
20
24
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
21
25
|
|
|
22
26
|
// edge/index.ts
|
|
23
27
|
var edge_exports = {};
|
|
24
28
|
__export(edge_exports, {
|
|
25
|
-
createGetIronSession: () => createGetIronSession,
|
|
26
|
-
createSealData: () => createSealData,
|
|
27
|
-
createUnsealData: () => createUnsealData,
|
|
28
29
|
getIronSession: () => getIronSession,
|
|
29
30
|
sealData: () => sealData,
|
|
30
31
|
unsealData: () => unsealData
|
|
@@ -50,12 +51,20 @@ var defaultOptions = {
|
|
|
50
51
|
function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
51
52
|
return async (req, res, userSessionOptions) => {
|
|
52
53
|
if (!req || !res || !userSessionOptions || !userSessionOptions.cookieName || !userSessionOptions.password) {
|
|
53
|
-
throw new Error(
|
|
54
|
+
throw new Error(
|
|
55
|
+
`iron-session: Bad usage. Minimum usage is const session = await getIronSession(req, res, { cookieName: "...", password: "...". Check the usage here: https://github.com/vvo/iron-session`
|
|
56
|
+
);
|
|
54
57
|
}
|
|
55
|
-
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
56
|
-
|
|
58
|
+
const passwordsAsMap = normalizeStringPasswordToMap(
|
|
59
|
+
userSessionOptions.password
|
|
60
|
+
);
|
|
61
|
+
Object.values(
|
|
62
|
+
normalizeStringPasswordToMap(userSessionOptions.password)
|
|
63
|
+
).forEach((password) => {
|
|
57
64
|
if (password.length < 32) {
|
|
58
|
-
throw new Error(
|
|
65
|
+
throw new Error(
|
|
66
|
+
`iron-session: Bad usage. Password must be at least 32 characters long.`
|
|
67
|
+
);
|
|
59
68
|
}
|
|
60
69
|
});
|
|
61
70
|
const options = {
|
|
@@ -73,12 +82,16 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
73
82
|
if (userSessionOptions.cookieOptions.maxAge === void 0) {
|
|
74
83
|
options.ttl = 0;
|
|
75
84
|
} else {
|
|
76
|
-
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
85
|
+
options.cookieOptions.maxAge = computeCookieMaxAge(
|
|
86
|
+
userSessionOptions.cookieOptions.maxAge
|
|
87
|
+
);
|
|
77
88
|
}
|
|
78
89
|
} else {
|
|
79
90
|
options.cookieOptions.maxAge = computeCookieMaxAge(options.ttl);
|
|
80
91
|
}
|
|
81
|
-
const sealFromCookies = import_cookie.default.parse(
|
|
92
|
+
const sealFromCookies = import_cookie.default.parse(
|
|
93
|
+
"credentials" in req ? req.headers.get("cookie") || "" : req.headers.cookie || ""
|
|
94
|
+
)[options.cookieName];
|
|
82
95
|
const session = sealFromCookies === void 0 ? {} : await unsealData2(sealFromCookies, {
|
|
83
96
|
password: passwordsAsMap,
|
|
84
97
|
ttl: options.ttl
|
|
@@ -87,15 +100,23 @@ function createGetIronSession(_crypto2, unsealData2, sealData2) {
|
|
|
87
100
|
save: {
|
|
88
101
|
value: async function save() {
|
|
89
102
|
if ("headersSent" in res && res.headersSent === true) {
|
|
90
|
-
throw new Error(
|
|
103
|
+
throw new Error(
|
|
104
|
+
`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()`
|
|
105
|
+
);
|
|
91
106
|
}
|
|
92
107
|
const seal2 = await sealData2(session, {
|
|
93
108
|
password: passwordsAsMap,
|
|
94
109
|
ttl: options.ttl
|
|
95
110
|
});
|
|
96
|
-
const cookieValue = import_cookie.default.serialize(
|
|
111
|
+
const cookieValue = import_cookie.default.serialize(
|
|
112
|
+
options.cookieName,
|
|
113
|
+
seal2,
|
|
114
|
+
options.cookieOptions
|
|
115
|
+
);
|
|
97
116
|
if (cookieValue.length > 4096) {
|
|
98
|
-
throw new Error(
|
|
117
|
+
throw new Error(
|
|
118
|
+
`iron-session: Cookie length is too big ${cookieValue.length}, browsers will refuse it. Try to remove some data.`
|
|
119
|
+
);
|
|
99
120
|
}
|
|
100
121
|
addToCookies(cookieValue, res);
|
|
101
122
|
}
|
|
@@ -139,7 +160,12 @@ function createUnsealData(_crypto2) {
|
|
|
139
160
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
140
161
|
const { sealWithoutVersion, tokenVersion } = parseSeal(seal2);
|
|
141
162
|
try {
|
|
142
|
-
const data = await Iron.unseal(
|
|
163
|
+
const data = await Iron.unseal(
|
|
164
|
+
_crypto2,
|
|
165
|
+
sealWithoutVersion,
|
|
166
|
+
passwordsAsMap,
|
|
167
|
+
{ ...Iron.defaults, ttl: ttl * 1e3 }
|
|
168
|
+
);
|
|
143
169
|
if (tokenVersion === 2) {
|
|
144
170
|
return data;
|
|
145
171
|
}
|
|
@@ -172,7 +198,9 @@ function createSealData(_crypto2) {
|
|
|
172
198
|
ttl = fourteenDaysInSeconds
|
|
173
199
|
}) => {
|
|
174
200
|
const passwordsAsMap = normalizeStringPasswordToMap(password);
|
|
175
|
-
const mostRecentPasswordId = Math.max(
|
|
201
|
+
const mostRecentPasswordId = Math.max(
|
|
202
|
+
...Object.keys(passwordsAsMap).map((id) => parseInt(id, 10))
|
|
203
|
+
);
|
|
176
204
|
const passwordForSeal = {
|
|
177
205
|
id: mostRecentPasswordId.toString(),
|
|
178
206
|
secret: passwordsAsMap[mostRecentPasswordId]
|
|
@@ -195,17 +223,20 @@ var getCrypto = () => {
|
|
|
195
223
|
return globalThis.crypto;
|
|
196
224
|
if (typeof ((_c = (_b = globalThis.crypto) == null ? void 0 : _b.webcrypto) == null ? void 0 : _c.subtle) === "object")
|
|
197
225
|
return globalThis.crypto.webcrypto;
|
|
198
|
-
throw new Error(
|
|
226
|
+
throw new Error(
|
|
227
|
+
"no native implementation of WebCrypto is available in current context"
|
|
228
|
+
);
|
|
199
229
|
};
|
|
200
230
|
var _crypto = getCrypto();
|
|
201
231
|
var unsealData = createUnsealData(_crypto);
|
|
202
232
|
var sealData = createSealData(_crypto);
|
|
203
|
-
var getIronSession = createGetIronSession(
|
|
233
|
+
var getIronSession = createGetIronSession(
|
|
234
|
+
_crypto,
|
|
235
|
+
unsealData,
|
|
236
|
+
sealData
|
|
237
|
+
);
|
|
204
238
|
// Annotate the CommonJS export names for ESM import in node:
|
|
205
239
|
0 && (module.exports = {
|
|
206
|
-
createGetIronSession,
|
|
207
|
-
createSealData,
|
|
208
|
-
createUnsealData,
|
|
209
240
|
getIronSession,
|
|
210
241
|
sealData,
|
|
211
242
|
unsealData
|