feature(config) add authCheck

This commit is contained in:
coderaiser 2015-10-02 08:04:17 -04:00
parent 732d4fa658
commit c7a5f943ad
3 changed files with 64 additions and 34 deletions

View file

@ -14,6 +14,8 @@ var CloudCmd, Util, DOM, io;
Dialog = DOM.Dialog,
Images = DOM.Images,
Events = DOM.Events,
Files = DOM.Files,
showLoad = function() {
Images.show.load('top');
},
@ -62,6 +64,8 @@ var CloudCmd, Util, DOM, io;
'reconnection limit' : FIVE_SECONDS
});
auth(socket);
socket.on('connect', function() {
Config.save = save;
});
@ -88,13 +92,28 @@ var CloudCmd, Util, DOM, io;
}
}
function auth(socket) {
Files.get('config', function(error, config) {
if (error)
return Dialog.alert(TITLE, error);
if (config.auth) {
socket.emit('auth', config.username, config.password);
socket.on('auth', function() {
Dialog.alert(TITLE, 'Wrong credentials!');
});
}
});
}
Config.save = saveHttp;
this.show = function() {
var prefix = CloudCmd.PREFIX,
exec = Util.exec,
funcs = [
exec.with(DOM.Files.get, 'config-tmpl'),
exec.with(Files.get, 'config-tmpl'),
exec.with(DOM.load.parallel, [
prefix + '/css/config.css',
prefix + '/lib/client/input.js'
@ -111,7 +130,7 @@ var CloudCmd, Util, DOM, io;
if (!Template)
Template = template;
DOM.Files.get('config', function(error, config) {
Files.get('config', function(error, config) {
var data, inputs, inputFirst,
focus, obj;

View file

@ -110,6 +110,8 @@
var size = cloudfunc.MAX_SIZE,
pref = getPrefix(prefix);
config.listen(socket, authCheck);
webconsole({
socket: socket,
prefix: pref + '/console',
@ -163,9 +165,7 @@
logout,
setUrl(prefix),
auth(),
config.middle({
socket: socket,
}),
config.middle,
restafary({
prefix : cloudfunc.apiURL + '/fs',
root : root

View file

@ -19,6 +19,7 @@
jonny = require('jonny'),
readjson = require('readjson'),
tryCatch = require('try-catch'),
exec = require('execon'),
HOME = require('os-homedir')(),
apiURL = CloudFunc.apiURL,
@ -47,11 +48,15 @@
module.exports = manage;
module.exports.save = save;
module.exports.middle = function(options) {
var o = options || {};
module.exports.middle = middle;
module.exports.listen = function(socket, authCheck) {
if (!socket)
throw Error('socket could not be empty!');
if (o.socket)
socket(o.socket);
if (authCheck && typeof authCheck !== 'function')
throw Error('authCheck should be function!');
listen(socket, authCheck);
return middle;
};
@ -79,38 +84,44 @@
callback(Error('Config is empty!'));
}
function socket(sock) {
check([sock], ['socket']);
function listen(sock, authCheck) {
sock.of('/config')
.on('connection', function(socket) {
socket.emit('config', config);
var connect = exec.with(connection, socket);
socket.on('message', function(json) {
var data,
is = Util.type.object(json);
if (!is) {
socket.emit('err', 'Error: Wrong data type!');
} else {
cryptoPass(json);
data = traverse(json);
save(function(error) {
if (error) {
socket.emit('err', error.message);
} else {
socket.broadcast.send(json);
socket.send(json);
socket.emit('log', data);
}
});
}
exec.if(!manage('auth'), connect, function(fn) {
authCheck(socket, fn);
});
});
}
function connection(socket) {
socket.emit('config', config);
socket.on('message', function(json) {
var data,
is = Util.type.object(json);
if (!is) {
socket.emit('err', 'Error: Wrong data type!');
} else {
cryptoPass(json);
data = traverse(json);
save(function(error) {
if (error) {
socket.emit('err', error.message);
} else {
socket.broadcast.send(json);
socket.send(json);
socket.emit('log', data);
}
});
}
});
}
function middle(req, res, next) {
if (req.url !== apiURL + '/config') {
next();