mirror of
https://github.com/ENSL/ensl_gathers.git
synced 2024-11-22 12:41:11 +00:00
83 lines
2.2 KiB
JavaScript
83 lines
2.2 KiB
JavaScript
"use strict";
|
|
|
|
var winston = require("winston");
|
|
var User = require("../lib/user/user");
|
|
var config = require("./config");
|
|
var EnslClient = require("../lib/ensl/client");
|
|
var chatController = require("../lib/chat/controller");
|
|
var gatherController = require("../lib/gather/controller");
|
|
var userController = require("../lib/user/controller");
|
|
var eventController = require("../lib/event/controller");
|
|
var usersHelper = require("../lib/user/helper");
|
|
var env = process.env.NODE_ENV || "development";
|
|
var parseCookies = EnslClient.parseCookies;
|
|
|
|
var assignRandomUser = (socket, next) => {
|
|
usersHelper.getRandomUser(function (error, user) {
|
|
if (error) {
|
|
winston.error(error);
|
|
return next(new Error("Authentication Failed"))
|
|
}
|
|
socket._user = user;
|
|
return next();
|
|
});
|
|
};
|
|
|
|
var assignFixedUser = (socket, next, userId) => {
|
|
usersHelper.getFixedUser(userId, function (error, user) {
|
|
if (error) {
|
|
winston.error(error);
|
|
return next(new Error("Authentication Failed"))
|
|
}
|
|
socket._user = user;
|
|
return next();
|
|
});
|
|
};
|
|
|
|
var handleFailedAuth = (socket, next) => {
|
|
if (process.env.RANDOM_USER) {
|
|
return assignRandomUser(socket, next);
|
|
} else if (process.env.FIXED_USER) {
|
|
return assignFixedUser(socket, next, process.env.FIXED_USER);
|
|
} else {
|
|
return next(new Error("Authentication Failed"));
|
|
}
|
|
};
|
|
|
|
module.exports = io => {
|
|
var rootNamespace = io.of('/')
|
|
|
|
// Authentication
|
|
io.use((socket, next) => {
|
|
let cookies = parseCookies(socket);
|
|
|
|
if (!cookies) {
|
|
return handleFailedAuth(socket, next);
|
|
}
|
|
|
|
let session = cookies[config.session_store_name];
|
|
|
|
if (!session) {
|
|
return handleFailedAuth(socket, next);
|
|
}
|
|
|
|
EnslClient.decodeSession(session, function (error, userId) {
|
|
if (error) return handleFailedAuth(socket, next);
|
|
User.find(userId, (error, user) => {
|
|
if (error) {
|
|
winston.error(error);
|
|
return next(new Error("Authentication failed"));
|
|
}
|
|
socket._user = user;
|
|
if (socket._user.bans.gather) return next(new Error("Gather Banned"));
|
|
winston.info("Logged in:", user.username, user.id);
|
|
return next();
|
|
});
|
|
});
|
|
});
|
|
|
|
userController(rootNamespace);
|
|
chatController(rootNamespace);
|
|
gatherController(rootNamespace);
|
|
eventController(rootNamespace);
|
|
};
|