Skip to content

Commit

Permalink
Merge pull request #55 from jason-fox/feature/prettier
Browse files Browse the repository at this point in the history
Add prettier to standardize codebase
  • Loading branch information
Álvaro Alonso authored Aug 29, 2018
2 parents 727963f + de7183c commit 19bb175
Show file tree
Hide file tree
Showing 12 changed files with 1,542 additions and 284 deletions.
13 changes: 13 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{json,yml,md,babelrc,eslintrc,remarkrc}]
indent_style = space
indent_size = 2
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "tamia",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"valid-jsdoc": 0
}
}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
config.js
cert
cert
.eslintcache
95 changes: 45 additions & 50 deletions controllers/root.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
var config = require('./../config.js'),
proxy = require('./../lib/HTTPClient.js'),
IDM = require('./../lib/idm.js').IDM,
AZF = require('./../lib/azf.js').AZF;
const config = require('./../config.js');
const proxy = require('./../lib/HTTPClient.js');
const IDM = require('./../lib/idm.js').IDM;
const AZF = require('./../lib/azf.js').AZF;

var log = require('./../lib/logger').logger.getLogger("Root");
const log = require('./../lib/logger').logger.getLogger("Root");

var Root = (function() {
const Root = (function() {

//{token: {user_info: {}, date: Date, verb1: [res1, res2, ..], verb2: [res3, res4, ...]}}
var tokens_cache = {};
//{token: {userInfo: {}, date: Date, verb1: [res1, res2, ..], verb2: [res3, res4, ...]}}
const tokensCache = {};

var pep = function(req, res) {
const pep = function(req, res) {

var token_header = req.headers['authorization'];
var auth_token = token_header ? token_header.split('Bearer ')[1] : req.headers['x-auth-token'];
const tokenHeader = req.headers.authorization;
let authToken = tokenHeader ? tokenHeader.split('Bearer ')[1] : req.headers['x-auth-token'];

if (auth_token === undefined && req.headers['authorization'] !== undefined) {
var header_auth = req.headers['authorization'].split(' ')[1];
auth_token = new Buffer(header_auth, 'base64').toString();
if (authToken === undefined && req.headers.authorization !== undefined) {
const headerAuth = req.headers.authorization.split(' ')[1];
authToken = new Buffer(headerAuth, 'base64').toString();
}

if (auth_token === undefined) {
if (authToken === undefined) {
log.error('Auth-token not found in request header');
var auth_header = 'IDM uri = ' + config.idm_host;
res.set('WWW-Authenticate', auth_header);
const authHeader = 'IDM uri = ' + config.idm_host;
res.set('WWW-Authenticate', authHeader);
res.status(401).send('Auth-token not found in request header');
} else {

if (config.magic_key && config.magic_key === auth_token) {
var options = {
if (config.magic_key && config.magic_key === authToken) {
const options = {
host: config.app.host,
port: config.app.port,
path: req.url,
method: req.method,
headers: proxy.getClientIp(req, req.headers)
};
var protocol = config.app.ssl ? 'https' : 'http';
const protocol = config.app.ssl ? 'https' : 'http';
proxy.sendData(protocol, options, req.body, res);
return;

}

var action = undefined
var resource = undefined
var authzforce = undefined
let action
let resource
let authzforce

if (config.authorization.enabled) {
if (config.authorization.pdp === 'authzforce') {
Expand All @@ -54,22 +54,22 @@ var Root = (function() {
}
}

IDM.check_token(auth_token, action, resource, authzforce, function (user_info) {
IDM.checkToken(authToken, action, resource, authzforce, function (userInfo) {

// Set headers with user information
req.headers['X-Nick-Name'] = user_info.id;
req.headers['X-Display-Name'] = user_info.displayName;
req.headers['X-Roles'] = JSON.stringify(user_info.roles);
req.headers['X-Organizations'] = JSON.stringify(user_info.organizations);
req.headers['X-Nick-Name'] = userInfo.id;
req.headers['X-Display-Name'] = userInfo.displayName;
req.headers['X-Roles'] = JSON.stringify(userInfo.roles);
req.headers['X-Organizations'] = JSON.stringify(userInfo.organizations);

if (config.authorization.enabled) {

if (config.authorization.pdp === 'authzforce') {

// Check decision through authzforce
AZF.check_permissions(auth_token, user_info, req, function () {
AZF.checkPermissions(authToken, userInfo, req, function () {

redir_request(req, res, user_info);
redirRequest(req, res, userInfo);

}, function (status, e) {
if (status === 401) {
Expand All @@ -83,19 +83,14 @@ var Root = (function() {
res.status(503).send('Error in AZF communication');
}

}, tokens_cache);
}, tokensCache);
} else if (userInfo.authorization_decision === "Permit") {
redirRequest(req, res, userInfo);
} else {

// Check decision through idm
if (user_info.authorization_decision === "Permit") {
redir_request(req, res, user_info);
} else {
res.status(401).send('User access-token not authorized');
}

res.status(401).send('User access-token not authorized');
}
} else {
redir_request(req, res, user_info);
redirRequest(req, res, userInfo);
}

}, function (status, e) {
Expand All @@ -107,25 +102,25 @@ var Root = (function() {
log.error('Error in IDM communication ', e);
res.status(503).send('Error in IDM communication');
}
}, tokens_cache);
};
}, tokensCache);
}
};

var public = function(req, res) {
redir_request(req, res);
const publicFunc = function(req, res) {
redirRequest(req, res);
};

var redir_request = function (req, res, user_info) {
const redirRequest = function (req, res, userInfo) {

if (user_info) {
if (userInfo) {
log.info('Access-token OK. Redirecting to app...');
} else {
log.info('Public path. Redirecting to app...');
}

var protocol = config.app.ssl ? 'https' : 'http';
const protocol = config.app.ssl ? 'https' : 'http';

var options = {
const options = {
host: config.app.host,
port: config.app.port,
path: req.url,
Expand All @@ -136,8 +131,8 @@ var Root = (function() {
};

return {
pep: pep,
public: public
pep,
public: publicFunc
}
})();

Expand Down
42 changes: 21 additions & 21 deletions lib/HTTPClient.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
var express = require('express'),
app = express(),
XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

var log = require('./logger').logger.getLogger("HTTP-Client");
const log = require('./logger').logger.getLogger("HTTP-Client");

exports.getClientIp = function(req, headers) {
var ipAddress = req.connection.remoteAddress;
const ipAddress = req.connection.remoteAddress;

var forwardedIpsStr = req.header('x-forwarded-for');
let forwardedIpsStr = req.header('x-forwarded-for');

if (forwardedIpsStr) {
// 'x-forwarded-for' header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
forwardedIpsStr += "," + ipAddress;
} else {
forwardedIpsStr = "" + ipAddress;
forwardedIpsStr = String(ipAddress);
}

headers['x-forwarded-for'] = forwardedIpsStr;
Expand All @@ -25,8 +23,6 @@ exports.getClientIp = function(req, headers) {


exports.sendData = function(protocol, options, data, res, callBackOK, callbackError) {
var xhr, body, result;

options.headers = options.headers || {};

callbackError = callbackError || function(status, resp) {
Expand All @@ -36,31 +32,34 @@ exports.sendData = function(protocol, options, data, res, callBackOK, callbackEr
};
callBackOK = callBackOK || function(status, resp, headers) {
res.statusCode = status;
for (var idx in headers) {
var header = headers[idx];
for (const idx in headers) {
res.setHeader(idx, headers[idx]);
}
log.debug("Response: ", status);
log.debug(" Body: ", resp);
res.send(resp);
};

var url = protocol + "://" + options.host + ":" + options.port + options.path;
xhr = new XMLHttpRequest();
const url = protocol + "://" + options.host + ":" + options.port + options.path;
const xhr = new XMLHttpRequest();
xhr.open(options.method, url, true);
if (options.headers["content-type"]) {
xhr.setRequestHeader("Content-Type", options.headers["content-type"]);
}
for (var headerIdx in options.headers) {
for (const headerIdx in options.headers) {
switch (headerIdx) {
// Unsafe headers
case "host":
break;
case "connection":
break;
case "referer":
break;
// case "accept-encoding":
// case "accept-charset":
// case "cookie":
case "content-type":
break;
case "origin":
break;
default:
Expand All @@ -69,7 +68,8 @@ exports.sendData = function(protocol, options, data, res, callBackOK, callbackEr
}
}

xhr.onerror = function(error) {
xhr.onerror = function() {
// DO NOTHING?
}
xhr.onreadystatechange = function () {

Expand All @@ -82,9 +82,9 @@ exports.sendData = function(protocol, options, data, res, callBackOK, callbackEr
flag = true;

if (xhr.status !== 0 && xhr.status < 400) {
var allHeaders = xhr.getAllResponseHeaders().split('\r\n');
var headers = {};
for (var h in allHeaders) {
const allHeaders = xhr.getAllResponseHeaders().split('\r\n');
const headers = {};
for (const h in allHeaders) {
headers[allHeaders[h].split(': ')[0]] = allHeaders[h].split(': ')[1];
}
callBackOK(xhr.status, xhr.responseText, headers);
Expand All @@ -94,7 +94,7 @@ exports.sendData = function(protocol, options, data, res, callBackOK, callbackEr
}
};

var flag = false;
let flag = false;
log.debug("Sending ", options.method, " to: " + url);
log.debug(" Headers: ", options.headers);
log.debug(" Body: ", data);
Expand All @@ -104,14 +104,14 @@ exports.sendData = function(protocol, options, data, res, callBackOK, callbackEr
} catch (e) {

callbackError(e.message);
return;

}
} else {
try {
xhr.send();
} catch (e) {
callbackError(e.message);
return;

}
}
}
Loading

0 comments on commit 19bb175

Please sign in to comment.