insite-cookie 1.0.37
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 +21 -0
- package/README.md +13 -0
- package/lib/Middleware.js +60 -0
- package/lib/index.js +2 -0
- package/lib/parse.js +13 -0
- package/lib/tokenMap.js +1 -0
- package/lib/ws/client/SetterClient.js +52 -0
- package/lib/ws/client/index.js +1 -0
- package/lib/ws/common.js +3 -0
- package/lib/ws/server/SetterServer.js +80 -0
- package/lib/ws/server/index.js +1 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Eugene Nesvetaev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { tokenMap } from "./tokenMap";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class CookieMiddleware {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
const {
|
|
7
|
+
requestRegExp = /^\/cookie\/?\?/
|
|
8
|
+
} = options;
|
|
9
|
+
|
|
10
|
+
this.listeners = { GET: [
|
|
11
|
+
[ requestRegExp, this.handler ]
|
|
12
|
+
] };
|
|
13
|
+
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
handler = (request, response) => {
|
|
17
|
+
const token = request.url.split("?")[1];
|
|
18
|
+
|
|
19
|
+
if (token && tokenMap.has(token))
|
|
20
|
+
response.writeHead(200, {
|
|
21
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
22
|
+
"Set-Cookie": CookieMiddleware.cookify(...tokenMap.getWithTimeout(token))
|
|
23
|
+
}).end();
|
|
24
|
+
else
|
|
25
|
+
return false;
|
|
26
|
+
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
static make({ name, value, maxAge, expiresAt, domain, path = "/", secure = true, httpOnly = true, sameSite = "Strict" }) {
|
|
31
|
+
const cookie = [ `${name}=${value}` ];
|
|
32
|
+
|
|
33
|
+
if (maxAge !== undefined)
|
|
34
|
+
cookie.push(`Max-Age=${maxAge}`);
|
|
35
|
+
else if (expiresAt)
|
|
36
|
+
cookie.push(`Max-Age=${Math.round((expiresAt.getTime() - Date.now()) / 1000)}`);
|
|
37
|
+
|
|
38
|
+
if (domain)
|
|
39
|
+
cookie.push(`Domain=${domain}`);
|
|
40
|
+
|
|
41
|
+
if (path)
|
|
42
|
+
cookie.push(`Path=${path}`);
|
|
43
|
+
|
|
44
|
+
if (secure)
|
|
45
|
+
cookie.push("Secure");
|
|
46
|
+
|
|
47
|
+
if (httpOnly)
|
|
48
|
+
cookie.push("HttpOnly");
|
|
49
|
+
|
|
50
|
+
if (sameSite)
|
|
51
|
+
cookie.push(`SameSite=${sameSite}`);
|
|
52
|
+
|
|
53
|
+
return cookie.join("; ");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static cookify(object, options) {
|
|
57
|
+
return Object.entries(object).map(([ name, value ]) => this.make({ name, value, ...options }));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
}
|
package/lib/index.js
ADDED
package/lib/parse.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function parseCookie(cookie) {
|
|
2
|
+
return cookie ? cookie.split(/;\s*/).reduce((object, keyEqValue) => {
|
|
3
|
+
const [ key, value ] = keyEqValue.split(/=/);
|
|
4
|
+
|
|
5
|
+
try {
|
|
6
|
+
object[key] = JSON.parse(value);
|
|
7
|
+
} catch {
|
|
8
|
+
object[key] = value;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return object;
|
|
12
|
+
}, {}) : {};
|
|
13
|
+
}
|
package/lib/tokenMap.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const tokenMap = new Map();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { headers } from "../common";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class CookieSetterClient {
|
|
5
|
+
constructor(ws, options = {}) {
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
method = "GET",
|
|
9
|
+
url = "/cookie",
|
|
10
|
+
onload,
|
|
11
|
+
onerror
|
|
12
|
+
} = options;
|
|
13
|
+
|
|
14
|
+
this.method = method;
|
|
15
|
+
this.url = url;
|
|
16
|
+
|
|
17
|
+
if (onload)
|
|
18
|
+
this.onload = onload;
|
|
19
|
+
|
|
20
|
+
if (onerror)
|
|
21
|
+
this.onerror = onerror;
|
|
22
|
+
|
|
23
|
+
ws.on(`message:${headers.set}`, this.#handleSet);
|
|
24
|
+
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
#handleSet = token => {
|
|
28
|
+
const xhr = new XMLHttpRequest();
|
|
29
|
+
|
|
30
|
+
xhr.onload = this.onload;
|
|
31
|
+
xhr.onerror = this.onerror;
|
|
32
|
+
|
|
33
|
+
xhr.open(this.method, `${this.url}?${token}`, true);
|
|
34
|
+
|
|
35
|
+
xhr.send(null);
|
|
36
|
+
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
onload() {
|
|
40
|
+
|
|
41
|
+
if (this.readyState === 4 && this.status !== 200)
|
|
42
|
+
console.error("Set cookie:", this.statusText);
|
|
43
|
+
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
onerror() {
|
|
47
|
+
|
|
48
|
+
console.error("Set cookie:", this.statusText);
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./SetterClient";
|
package/lib/ws/common.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { parseCookie } from "../../parse";
|
|
3
|
+
import { tokenMap } from "../../tokenMap";
|
|
4
|
+
import { headers } from "../common";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const cookieTokenTimeout = 2000;
|
|
8
|
+
const cookieSessionIdSymbol = Symbol("cookieSessionId");
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export class CookieSetterServer {
|
|
12
|
+
constructor(wss, { users, domain, maxAge }) {
|
|
13
|
+
wss.cookieSetter = this;
|
|
14
|
+
|
|
15
|
+
this.domain = domain ?? process.env.HOST;
|
|
16
|
+
this.maxAge = maxAge ?? users?.sessions.collection.expireAfterSeconds;
|
|
17
|
+
|
|
18
|
+
wss.options.WebSocket.prototype.setCookie = CookieSetterServer.set;
|
|
19
|
+
wss.options.WebSocket.prototype.unsetCookie = CookieSetterServer.unset;
|
|
20
|
+
|
|
21
|
+
if (users) {
|
|
22
|
+
this.users = users;
|
|
23
|
+
|
|
24
|
+
wss.on("client-connect", CookieSetterServer.handleClientConnect);
|
|
25
|
+
wss.on("client-session", CookieSetterServer.handleClientSession);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
static set(cookie, options) {
|
|
32
|
+
|
|
33
|
+
const token = `${Math.round(performance.now() * Number.MAX_SAFE_INTEGER).toString(36)}$${crypto.randomBytes(Math.randomInt(4, 8)).toString("hex")}`;
|
|
34
|
+
|
|
35
|
+
tokenMap.setWithTimeout(token, [ cookie, options ], cookieTokenTimeout);
|
|
36
|
+
|
|
37
|
+
this.sendMessage(headers.set, token);
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static unset(names) {
|
|
42
|
+
this.setCookie(names.reduce((cookie, name) => {
|
|
43
|
+
cookie[name] = "";
|
|
44
|
+
|
|
45
|
+
return cookie;
|
|
46
|
+
}, {}), { maxAge: 0 });
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static parse = parseCookie;
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
static handleClientConnect(ws, request) {
|
|
54
|
+
ws[cookieSessionIdSymbol] = null;
|
|
55
|
+
|
|
56
|
+
if (request.headers.cookie) {
|
|
57
|
+
const { sessionId = null } = parseCookie(request.headers.cookie);
|
|
58
|
+
|
|
59
|
+
ws[cookieSessionIdSymbol] = sessionId;
|
|
60
|
+
|
|
61
|
+
if (sessionId)
|
|
62
|
+
ws.setSession(sessionId, true);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static handleClientSession(ws) {
|
|
68
|
+
if (ws[cookieSessionIdSymbol] !== ws.session?._id) {
|
|
69
|
+
ws[cookieSessionIdSymbol] = ws.session?._id;
|
|
70
|
+
|
|
71
|
+
if (ws.isOpen && ws.session) {
|
|
72
|
+
const { domain, maxAge } = this.cookieSetter;
|
|
73
|
+
ws.setCookie({ sessionId: ws.session._id }, { domain, maxAge });
|
|
74
|
+
} else
|
|
75
|
+
ws.unsetCookie([ "sessionId" ]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./SetterServer";
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "insite-cookie",
|
|
3
|
+
"version": "1.0.37",
|
|
4
|
+
"description": "Cookie support for inSite",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/index.js",
|
|
7
|
+
"module": "./lib/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./lib/index.js",
|
|
10
|
+
"./ws/server": "./lib/ws/server/index.js",
|
|
11
|
+
"./ws/client": "./lib/ws/client/index.js",
|
|
12
|
+
"./parse": "./lib/parse.js"
|
|
13
|
+
},
|
|
14
|
+
"packageManager": "yarn@1.22.22",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/nesvet/insite-cookie.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"insite",
|
|
24
|
+
"cookie",
|
|
25
|
+
"client",
|
|
26
|
+
"server"
|
|
27
|
+
],
|
|
28
|
+
"author": "Eugene Nesvetaev <dev@nesvet.dev> (https://nesvet.dev/)",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"insite-ws": "^1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@nesvet/eslint-config": "^1"
|
|
35
|
+
}
|
|
36
|
+
}
|