Skip to content

Commit

Permalink
Merged feature/sprint-4 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
cerealkill committed Oct 6, 2015
2 parents ba14b22 + 634fa7c commit 352440d
Show file tree
Hide file tree
Showing 22 changed files with 979 additions and 575 deletions.
14 changes: 10 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pids

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
unit-test-coverage

# Coverage directory used by tools like istanbul
coverage
Expand All @@ -29,8 +30,13 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# IDE
.settings
#docs
docs

#idea
.idea

# Mac
.DS_STORE
#Visual Code
.settings
launch.json
.DS_Store
28 changes: 28 additions & 0 deletions app/auth/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

var passport = require('passport');
var BearerStrategy = require('passport-http-bearer').Strategy;
var helper = require('./lib/auth-server-wrapper');
var etcd = require('carbono-service-manager');

module.exports = function (app) {
app.use(passport.initialize());

passport.use(new BearerStrategy(
function (token, done) {
var promise = helper.findUser(token, etcd.getServiceUrl('auth'));

if (promise) {
promise.then(
function (user) {
return done(null, user);
},
function (err) {
if (err) { return done(err); }
return done(null, false);
}
);
}
}
));
};
110 changes: 110 additions & 0 deletions app/auth/lib/auth-server-wrapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
'use strict';

/**
* Helper methods to communicate with Authentication Server
*
* @module Authentication Server Client
*/

var pjson = require('../../../package.json');
var CJM = require('carbono-json-messages');
var request = require('request');
var uuid = require('node-uuid');
var q = require('q');

/**
* Try to extract user informations.
*
* @param {string} message - body attribute extracted from response object.
* @return {Object=} Object representing an User.
*
* @function
*/
function extractUser(message) {
var validStructure = message &&
message.hasOwnProperty('data') &&
message.data.hasOwnProperty('items') &&
message.data.items.length > 0 &&
message.data.items[0].hasOwnProperty('userInfo');

if (validStructure) {
return message.data.items[0].userInfo;
}

return null;
}

/**
* Builds the options used by request module.
*
* @param {string} token - Bearer Token
* @param {string} authUrl - Base url for access carbono-auth
* @return {Object} Object representing options for request module.
*
* @function
*/
function buildRequestOptions(token, authUrl) {
var cjm = new CJM({apiVersion: pjson.version});
cjm.setData({
id: uuid.v4(),
items: [
{
token: token,
},
],
});

return {
uri: 'http://' + authUrl + '/bearer/validate',
method: 'POST',
json: cjm.toObject(),
};
}

/**
* Find a user for the given token.
*
* @param {string} token - Bearer Token
* @param {string} authUrl - Base url for access carbono-auth
* @return {Object} Object representing a promise.
*
* @function
*/
module.exports.findUser = function (token, authUrl) {
if (token) {
var deffered = q.defer();

request(
buildRequestOptions(token, authUrl),
function (err, res) {
if (err) {
// Request error
deffered.reject('[VALIDATE TOKEN] Request error: ' + err);

} else if (res.statusCode === 200) {
// User retrieved
var user = extractUser(res.body);

if (user) {
deffered.resolve(user);
} else {
deffered.reject(
'[VALIDATE TOKEN] Can\'t read user informations.');
}

} else if (res.statusCode === 404) {
// User not found
deffered.reject(null);

} else {
// Unknown status code
deffered.reject(
'[VALIDATE TOKEN] Error: ' + res.statusCode);
}
});

return deffered.promise;
}

return null;
};
5 changes: 5 additions & 0 deletions app/authenticate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

var passport = require('passport');

module.exports.auth = passport.authenticate('bearer', { session: false });
67 changes: 20 additions & 47 deletions app/controllers/file.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,41 @@
'use strict';

/**
* @todo Get cm from user context
*/
module.exports = function (app) {

this.create = function (socket) {
/**
* On connection, register listeners to commands that the client (IDE) may
* emit to the code machine, and re-emits them to the CM connection.
*/
this.registerCommands = function (socket) {
if (!app.cm) {
console.log('No instance of Code Machine found!');
return;
}
socket.on('create', function () {
var file = app.cm.create();
socket.emit('created', file);
});
};

this.list = function (socket) {
if (!app.cm) {
return;
}
socket.on('list', function () {
var list = app.cm.list();
socket.emit('listed', list);
app.cm.commands.forEach(function (cmd) {
socket.on(cmd, app.cm.emit.bind(app.cm, cmd));
});
};

this.delete = function (socket) {
/**
* On connection, register listeners to events that the code machine may
* emit, and re-emits them to the socket with client (IDE).
*/
this.registerEvents = function (socket) {
if (!app.cm) {
console.log('No instance of Code Machine found!');
return;
}
socket.on('delete', function () {
var res = app.cm.del();
socket.emit('deleted', res);
});
};

this.apNode = function (socket) {
if (!app.cm) {
return;
}
socket.on('apNode', function () {
var node = app.cm.apNode();
socket.emit('added', node);
});
};
app.cm.events.forEach(function (ev) {
var listener = socket.emit.bind(socket, ev);

this.rmNode = function (socket) {
if (!app.cm) {
return;
}
socket.on('rmNode', function () {
var res = app.cm.rmNode();
socket.emit('removed', res);
app.cm.on(ev, listener);
socket.on('disconnect', function () {
app.cm.removeListener(ev, listener);
});
});
};

this.edNodeAtt = function (socket) {
if (!app.cm) {
return;
}
socket.on('edNodeAtt', function () {
var res = app.cm.edNodeAtt();
socket.emit('changed', res);
});
};
return this;
};
67 changes: 32 additions & 35 deletions app/controllers/infra.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
'use strict';
var CJM = require('carbono-json-messages');
var uuid = require('node-uuid');
var bo = require('../lib/mission-control-bo');

var CJM = require('carbono-json-messages');
var uuid = require('node-uuid');
var bo = require('../lib/mission-control-bo');
var pjson = require('../../package.json');
var etcd = require('carbono-service-manager');

module.exports = function (app, etcdManager) {
module.exports = function () {

this.createContainer = function (req, res) {

var projectId = req.body.projectId;
var machineAlias = req.body.machineAlias;
var projectId = req.body.data.items[0].projectId;
var machineAlias = req.body.data.items[0].machineAlias;
var cjm = null;
var error = null;

// Check data consistency
// @todo Create an definitive helper for handling errors.
// There are too much repeated code in the repositories.
if (!projectId || !machineAlias) {
cjm = new CJM({apiVersion: pjson.version});
cjm = createContainerValidation(cjm, error, projectId,
machineAlias);
if (cjm !== null) {
res.status(400);
error = {
code: 400,
message: 'Missing mandatory parameters',
errors:
'projectId and machineAlias should have an valid value',
};
cjm.setError(error);
res.json(cjm);
res.end();
return;
}

// Get the IPE module URL
var ipeURL = etcdManager.getIpeUrl();
if (!ipeURL) {
cjm = new CJM({apiVersion: pjson.version});
error = {
code: 500,
message: 'Internal data is missing',
errors:
'ipeURL is not set in etcdManager, I can\'t send the request!',
};
res.status(500);
cjm.setError(error);
res.json(cjm);
res.end();
return;
}
var ipeURL = etcd.getServiceUrl('ipe');

// Now we can make the call to the business object.
bo.createUserContainer(ipeURL, projectId, machineAlias,
Expand Down Expand Up @@ -90,5 +67,25 @@ module.exports = function (app, etcdManager) {
);
};

function createContainerValidation(cjm, error, projectId,
machineAlias) {
// Check data consistency
// @todo Create an definitive helper for handling errors.
// There are too much repeated code in the repositories.
if (!projectId || !machineAlias) {
cjm = new CJM({apiVersion: pjson.version});
error = {
code: 400,
message: 'Missing mandatory parameters',
errors:
'projectId and machineAlias should have an valid value',
};
cjm.setError(error);
return cjm;
} else {
return null;
}
}

return this;
};
Loading

0 comments on commit 352440d

Please sign in to comment.