Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jwt_delegate startup #257

Open
wants to merge 2 commits into
base: experimental
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions server-nodejs/conf_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
"enable": true,
"path" : "./extensions/user_setup.js"
},
"jwt_delegate": {
"enable": false,
"path": "./extensions/jwt_delegate.js",
"conf": {
"server": "http://localhost:3000/api/signIn/"
}
},
"jwt": {
"enable": false,
"path": "./extensions/jwt.js",
Expand Down
2 changes: 1 addition & 1 deletion server-nodejs/extensions/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = function (app) {
user.address = req.connection.remoteAddress;
user.class = user.class || 'guest';
req.user = {_id: user._id, user: user, token: jwt.sign(payload, conf.secret + user.password, options)};
debug('Token generated for user: %s, token: %s', user.username, user.token);
debug('Token generated for user: %s, token: %s', user.username, req.user.token);
return res.status(200).json(req.user);
});
});
Expand Down
69 changes: 38 additions & 31 deletions server-nodejs/extensions/jwt_delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,65 @@
*
*
*/

module.exports = function (app) {
var db = app.locals.db;
var db = app.locals.db;
//let XHR = require('xmlhttprequest-ssl');
var XMLHttpRequest = require("xmlhttprequest-ssl").XMLHttpRequest;
var debug = require('debug')('damas:extensions:jwt_delegate');


var server = app.locals.conf.jwt_delegate.server;

app.use('/api/signIn', function (req, res, next) {
debug(server);
debug(server);
if ('undefined' != req.body.username) {
var xhr = new XMLHttpRequest();
xhr.onerror = function () {
debug(server);
console.error("Connection impossible with " + "'" + server + "'");
debug(server);
console.error("Error request");
next();
}
//xhr.onload = function (err) {
xhr.onreadystatechange = function (err) {
if (405 === this.status) {
this.onerror();
}
if (4 === this.readyState && 200 === this.status) {
var response = JSON.parse(this.responseText);
if (undefined === response.user) {
console.warn("Impossible authentication with that version of damas.core. Please, update damas-core or disabled jwt_delegate extention.");
return next();
}
db.create([response.user], function (err, nodes) {
if (err) {
console.warn("Creation user node impossible in the database.");
if (4 === this.readyState) {
if (200 === this.status) {
var response = JSON.parse(this.responseText);
if (undefined === response.user) {
console.warn("Impossible authentication with that version of damas.core. Please, update damas-core or disabled jwt_delegate extention.");
return next();
}
if (null === nodes[0]) {
db.update([response.user], function (err, doc) {
if (err) {
console.warn("Updating user node impossible.");
db.create([response.user], function (err, nodes) {
if (err) {
console.warn("Creation user node impossible in the database.");
return next();
}
if (null === nodes[0]) {
db.update([response.user], function (err, doc) {
if (err) {
console.warn("Updating user node impossible.");
return next();
}
return next();
}
});
}
});
});
} else {
return next();
}
});
} else if (this.status >= 500) {
debug(server);
console.warn("No response from the server " + "'" + server + "'" + " for your request. Please check the server URL in the conf.json.");
return next();
} else if (this.status === 401) {
debug(server);
console.warn("Invalid username or passeword");
return next();
}
}
}
xhr.open("POST", server, true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var login = "username=" + req.body.username + "&password=" + req.body.password;
debug(login);
debug(login);
xhr.send(login);
}
next();
});
}