From 21c829adc21f43e3fbd4344ac14653b61b89d4d3 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 29 Mar 2018 18:59:22 +0300 Subject: [PATCH] feature(config) auth --- client/modules/config.js | 5 +---- server/cloudcmd.js | 14 +++++++------- server/config.js | 27 ++++++++++++++------------- test/server/cloudcmd.js | 16 ++++++++-------- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/client/modules/config.js b/client/modules/config.js index 8f2c54da..78e4dad4 100644 --- a/client/modules/config.js +++ b/client/modules/config.js @@ -79,7 +79,7 @@ function getHost() { return href; } -function initSocket(error) { +function initSocket() { const href = getHost(); const prefix = CloudCmd.PREFIX; const FIVE_SECONDS = 5000; @@ -95,9 +95,6 @@ function initSocket(error) { socket.send(data); }; - if (error) - return; - authCheck(socket); socket.on('connect', () => { diff --git a/server/cloudcmd.js b/server/cloudcmd.js index df335a77..bf909b04 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -5,7 +5,7 @@ const DIR_ROOT = DIR + '../'; const DIR_COMMON = DIR + '../common/'; const cloudfunc = require(DIR_COMMON + 'cloudfunc'); -const auth = require(DIR + 'auth'); +const authentication = require(DIR + 'auth'); const config = require(DIR + 'config'); const modulas = require(DIR + 'modulas'); const rest = require(DIR + 'rest'); @@ -27,7 +27,7 @@ const deepword = require('deepword'); const nomine = require('nomine'); const fileop = require('@cloudcmd/fileop'); -const authCheckNew = currify(_authCheckNew); +const auth = currify(_auth); const authenticate = currify(_authenticate); const setUrl = currify(_setUrl); @@ -94,8 +94,8 @@ function authCheck(socket, success) { socket.on('auth', authenticate(socket, success)); } -module.exports._authCheckNew = _authCheckNew; -function _authCheckNew(accept, reject, username, password) { +module.exports._auth = _auth; +function _auth(accept, reject, username, password) { if (!config('auth')) return accept(); @@ -123,7 +123,7 @@ function _authenticate(socket, success, name, pass) { function listen(prefix, socket) { prefix = getPrefix(prefix); - config.listen(socket, authCheck); + config.listen(socket, auth); edward.listen(socket, { root, @@ -145,7 +145,7 @@ function listen(prefix, socket) { fileop.listen(socket, { root, - authCheck: authCheckNew, + auth, prefix: prefix + '/fileop', }); @@ -210,7 +210,7 @@ function cloudcmd(prefix, plugins, modules) { setUrl(prefix), logout, - auth(), + authentication(), config.middle, modules && modulas(modules), diff --git a/server/config.js b/server/config.js index 8938ce47..044698a7 100644 --- a/server/config.js +++ b/server/config.js @@ -12,6 +12,7 @@ const CloudFunc = require(DIR_COMMON + 'cloudfunc'); const fullstore = require('fullstore/legacy'); const currify = require('currify/legacy'); +const wraptile = require('wraptile/legacy'); const squad = require('squad/legacy'); const promisify = require('es6-promisify').promisify; const pullout = promisify(require('pullout/legacy')); @@ -20,7 +21,6 @@ const jonny = require('jonny/legacy'); const jju = require('jju'); const writejson = require('writejson'); const tryCatch = require('try-catch'); -const exec = require('execon'); const criton = require('criton'); const HOME = require('os').homedir(); @@ -56,17 +56,18 @@ if (error && error.code !== 'ENOENT') exit(`cloudcmd --config ${ConfigHome}: ${error.message}`); const config = Object.assign({}, rootConfig, configHome); +const connectionWraped = wraptile(connection); module.exports = manage; module.exports.save = _save; module.exports.middle = middle; -module.exports.listen = (socket, authCheck) => { - check(socket, authCheck); +module.exports.listen = (socket, auth) => { + check(socket, auth); if (!manage('configDialog')) return middle; - listen(socket, authCheck); + listen(socket, auth); return middle; }; @@ -88,16 +89,16 @@ function _save(callback) { writejson(ConfigHome, config, callback); } -function listen(sock, authCheck) { +function listen(sock, auth) { const prefix = manage('prefix'); sock.of(prefix + '/config') .on('connection', (socket) => { - const connect = exec.with(connection, socket); - - exec.if(!manage('auth'), connect, (fn) => { - authCheck(socket, fn); - }); + if (!manage('auth')) + return connection(socket); + + const reject = () => socket.emit('reject'); + socket.on('auth', auth(connectionWraped(socket), reject)); }); } @@ -206,11 +207,11 @@ function cryptoPass(json) { }); } -function check(socket, authCheck) { +function check(socket, auth) { if (!socket) throw Error('socket could not be empty!'); - if (authCheck && typeof authCheck !== 'function') - throw Error('authCheck should be function!'); + if (auth && typeof auth !== 'function') + throw Error('auth should be function!'); } diff --git a/test/server/cloudcmd.js b/test/server/cloudcmd.js index ee27f6eb..dbeb57a1 100644 --- a/test/server/cloudcmd.js +++ b/test/server/cloudcmd.js @@ -13,7 +13,7 @@ const { _authenticate, _getPrefix, _authCheck, - _authCheckNew, + _auth, _replacePrefix, _replaceDist, } = cloudcmd; @@ -119,7 +119,7 @@ test('cloudcmd: replaceDist: !isDev', (t) => { t.end(); }); -test('cloudcmd: authCheckNew: reject', (t) => { +test('cloudcmd: auth: reject', (t) => { const auth = config('auth'); const accept = sinon.stub(); const reject = sinon.stub(); @@ -130,7 +130,7 @@ test('cloudcmd: authCheckNew: reject', (t) => { const reset = set('hello', 'world'); config('auth', true); - _authCheckNew(accept, reject, username, password); + _auth(accept, reject, username, password); config('auth', auth); reset(); @@ -139,7 +139,7 @@ test('cloudcmd: authCheckNew: reject', (t) => { t.end(); }); -test('cloudcmd: authCheckNew: accept', (t) => { +test('cloudcmd: auth: accept', (t) => { const auth = config('auth'); const accept = sinon.stub(); const reject = sinon.stub(); @@ -150,7 +150,7 @@ test('cloudcmd: authCheckNew: accept', (t) => { const reset = set(username, password); config('auth', true); - _authCheckNew(accept, reject, username, password); + _auth(accept, reject, username, password); config('auth', auth); reset(); @@ -159,7 +159,7 @@ test('cloudcmd: authCheckNew: accept', (t) => { t.end(); }); -test('cloudcmd: authCheckNew: accept: no auth', (t) => { +test('cloudcmd: auth: accept: no auth', (t) => { const auth = config('auth'); const accept = sinon.stub(); const reject = sinon.stub(); @@ -167,14 +167,14 @@ test('cloudcmd: authCheckNew: accept: no auth', (t) => { const password = 'toor'; config('auth', false); - _authCheckNew(accept, reject, username, password); + _auth(accept, reject, username, password); config('auth', auth); t.ok(accept.called, 'should accept'); t.end(); }); -test('cloudcmd: authCheckNew: reject', (t) => { +test('cloudcmd: auth: reject', (t) => { const auth = config('auth'); const success = sinon.stub(); const on = sinon.stub;