refactor: move middleware out

This commit is contained in:
Ankush Menat 2023-06-29 18:26:21 +05:30
parent 164840c32e
commit 7320d2d020
3 changed files with 66 additions and 52 deletions

View file

@ -1,4 +1,3 @@
const cookie = require("cookie");
const request = require("superagent");
const { Server } = require("socket.io");
@ -7,6 +6,8 @@ const conf = get_conf();
const log = console.log; // eslint-disable-line
const subscriber = get_redis_subscriber();
const { get_hostname, get_url } = require("./utils");
const io = new Server(conf.socketio_port, {
cors: {
// Should be fine since we are ensuring whether hostname and origin are same before adding setting listeners for s socket
@ -15,43 +16,11 @@ const io = new Server(conf.socketio_port, {
},
});
io.use((socket, next) => {
if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) {
next(new Error("Invalid origin"));
return;
}
// load and register middlewares
const authenticate = require("./middlewares/authenticate");
io.use(authenticate);
if (!socket.request.headers.cookie) {
next(new Error("No cookie transmitted."));
return;
}
let cookies = cookie.parse(socket.request.headers.cookie);
if (!cookies.sid) {
next(new Error("No sid transmitted."));
return;
}
request
.get(get_url(socket, "/api/method/frappe.realtime.get_user_info"))
.type("form")
.query({
sid: cookies.sid,
})
.then((res) => {
socket.user = res.body.message.user;
socket.user_type = res.body.message.user_type;
socket.sid = cookies.sid;
socket.subscribed_documents = [];
next();
})
.catch((e) => {
next(new Error(`Unauthorized: ${e}`));
});
});
// on socket connection
// load and register handler
io.on("connection", function (socket) {
socket.join(get_user_room(socket, socket.user));
socket.join(get_website_room(socket));
@ -206,21 +175,6 @@ function get_site_name(socket) {
return socket.site_name;
}
function get_hostname(url) {
if (!url) return undefined;
if (url.indexOf("://") > -1) {
url = url.split("/")[2];
}
return url.match(/:/g) ? url.slice(0, url.indexOf(":")) : url;
}
function get_url(socket, path) {
if (!path) {
path = "";
}
return socket.request.headers.origin + path;
}
function can_subscribe_doc(args) {
if (!args) return;
if (!args.doctype || !args.docname) return;

View file

@ -0,0 +1,41 @@
const cookie = require("cookie");
const request = require("superagent");
const { get_hostname, get_url } = require("../utils");
function authenticate_with_frappe(socket, next) {
if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) {
next(new Error("Invalid origin"));
return;
}
if (!socket.request.headers.cookie) {
next(new Error("No cookie transmitted."));
return;
}
let cookies = cookie.parse(socket.request.headers.cookie);
if (!cookies.sid) {
next(new Error("No sid transmitted."));
return;
}
request
.get(get_url(socket, "/api/method/frappe.realtime.get_user_info"))
.type("form")
.query({
sid: cookies.sid,
})
.then((res) => {
socket.user = res.body.message.user;
socket.user_type = res.body.message.user_type;
socket.sid = cookies.sid;
socket.subscribed_documents = [];
next();
})
.catch((e) => {
next(new Error(`Unauthorized: ${e}`));
});
}
module.exports = authenticate_with_frappe;

19
realtime/utils.js Normal file
View file

@ -0,0 +1,19 @@
function get_hostname(url) {
if (!url) return undefined;
if (url.indexOf("://") > -1) {
url = url.split("/")[2];
}
return url.match(/:/g) ? url.slice(0, url.indexOf(":")) : url;
}
function get_url(socket, path) {
if (!path) {
path = "";
}
return socket.request.headers.origin + path;
}
module.exports = {
get_url,
get_hostname,
};