-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged feature/sprint-4 into develop
- Loading branch information
Showing
22 changed files
with
979 additions
and
575 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
} | ||
} | ||
)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.