mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 02:35:49 +00:00
feature(config) auth
This commit is contained in:
parent
bc25f1babb
commit
21c829adc2
4 changed files with 30 additions and 32 deletions
|
|
@ -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', () => {
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -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!');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue