2015-07-20 09:41:58 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-07-22 15:35:40 +00:00
|
|
|
var winston = require("winston");
|
2015-07-22 23:30:14 +00:00
|
|
|
var User = require("../lib/user/user");
|
2015-08-18 09:56:35 +00:00
|
|
|
var config = require("./config");
|
|
|
|
var EnslClient = require("../lib/ensl/client");
|
2015-07-22 23:30:14 +00:00
|
|
|
var chatController = require("../lib/chat/controller");
|
|
|
|
var gatherController = require("../lib/gather/controller");
|
|
|
|
var userController = require("../lib/user/controller");
|
2015-08-18 11:40:48 +00:00
|
|
|
var usersHelper = require("../lib/user/helper");
|
|
|
|
var env = process.env.NODE_ENV || "development";
|
2015-08-18 11:09:33 +00:00
|
|
|
var parseCookies = EnslClient.parseCookies;
|
2015-08-18 09:56:35 +00:00
|
|
|
|
2015-08-18 11:40:48 +00:00
|
|
|
var assignRandomUser = (socket, next) => {
|
2015-08-28 20:37:55 +00:00
|
|
|
usersHelper.getRandomUser(function (error, user) {
|
2015-08-18 11:40:48 +00:00
|
|
|
if (error) {
|
|
|
|
winston.error(error);
|
|
|
|
return next(new Error("Authentication Failed"))
|
|
|
|
}
|
2015-08-28 20:37:55 +00:00
|
|
|
socket._user = user;
|
2015-08-18 11:40:48 +00:00
|
|
|
return next();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-10 23:56:53 +00:00
|
|
|
module.exports = io => {
|
2015-07-22 16:38:58 +00:00
|
|
|
var rootNamespace = io.of('/')
|
|
|
|
|
2015-08-18 09:56:35 +00:00
|
|
|
// Authentication
|
2015-08-10 23:56:53 +00:00
|
|
|
io.use((socket, next) => {
|
2015-08-19 13:59:28 +00:00
|
|
|
let cookies = parseCookies(socket);
|
|
|
|
|
|
|
|
let session;
|
|
|
|
if (cookies) {
|
|
|
|
session = EnslClient.decodeSession(cookies[config.session_store_name]);
|
|
|
|
}
|
2015-08-18 09:56:35 +00:00
|
|
|
|
2015-08-18 11:40:48 +00:00
|
|
|
if (!session || typeof session.user !== 'number') {
|
2015-08-31 15:01:57 +00:00
|
|
|
if (process.env.RANDOM_USER) {
|
2015-08-18 11:40:48 +00:00
|
|
|
return assignRandomUser(socket, next);
|
|
|
|
} else {
|
|
|
|
return next(new Error("Authentication Failed"));
|
|
|
|
}
|
|
|
|
}
|
2015-08-18 09:56:35 +00:00
|
|
|
|
2015-08-28 14:00:14 +00:00
|
|
|
User.find(session.user, (error, user) => {
|
|
|
|
if (error) {
|
2015-07-22 15:35:40 +00:00
|
|
|
winston.error(error);
|
2015-08-28 14:00:14 +00:00
|
|
|
return next(new Error("Authentication failed"));
|
2015-08-27 12:02:45 +00:00
|
|
|
}
|
2015-08-28 14:00:14 +00:00
|
|
|
socket._user = user;
|
|
|
|
if (socket._user.bans.gather) return next(new Error("Gather Banned"));
|
|
|
|
winston.info("Logged in:", user.username, user.id);
|
2015-08-18 09:56:35 +00:00
|
|
|
return next();
|
|
|
|
});
|
2015-07-21 00:24:14 +00:00
|
|
|
});
|
2015-07-20 22:47:18 +00:00
|
|
|
|
2015-07-22 16:38:58 +00:00
|
|
|
userController(rootNamespace);
|
2015-07-22 16:28:15 +00:00
|
|
|
chatController(rootNamespace);
|
|
|
|
gatherController(rootNamespace);
|
2015-07-20 22:47:18 +00:00
|
|
|
};
|