From a6b716dfa218ac2c600c4e4c286eacd8214e1bc9 Mon Sep 17 00:00:00 2001 From: "gustavo.marin" Date: Fri, 6 Feb 2015 09:53:26 +0100 Subject: [PATCH 1/2] added private host, Host headers and user's IP. --- config_sample.json | 1 + src/middlewares/prepareOptions.js | 7 +++++-- src/routes/userCreation.js | 7 +++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config_sample.json b/config_sample.json index 9cee307..cffbf7d 100644 --- a/config_sample.json +++ b/config_sample.json @@ -1,6 +1,7 @@ { "public_port" : 3000, "private_port" : 3001, + "private_server": "localhost", "accessToken" : { "cipherKey" : "unsecureKey1", "signKey" : "unsecureKey2", diff --git a/src/middlewares/prepareOptions.js b/src/middlewares/prepareOptions.js index 3e5d8f7..6a600d0 100644 --- a/src/middlewares/prepareOptions.js +++ b/src/middlewares/prepareOptions.js @@ -3,10 +3,13 @@ var fs = require('fs'); function prepareOptions (req, res, next){ var options = { - url: 'http://localhost:' + config.private_port + req.url, + url: 'http://'+ config.private_server + ':' + config.private_port + req.url, headers: { 'Content-Type': req.header('Content-Type'), - 'x-user-id': req.tokenInfo.userId + 'x-user-id': req.tokenInfo.userId, + 'Host': req.headers.host, + 'X-Real-IP': req.connection.remoteAddress, + 'X-Forwarded-For': req.header('X-Forwarded-For') || req.connection.remoteAddress }, method: req.method, followRedirect: false diff --git a/src/routes/userCreation.js b/src/routes/userCreation.js index 2de21eb..c58f267 100644 --- a/src/routes/userCreation.js +++ b/src/routes/userCreation.js @@ -11,9 +11,12 @@ var config = JSON.parse(require('fs').readFileSync('config.json','utf8')); function createUser(req, body, res, next, user) { var options = { - url: 'http://localhost:' + config.private_port + req.url, + url: 'http://' + config.private_host + ':' + config.private_port + req.url, headers: { - 'Content-Type': 'application/json; charset=utf-8' + 'Content-Type': 'application/json; charset=utf-8', + 'Host': req.headers.host, + 'X-Real-IP': req.connection.remoteAddress, + 'X-Forwarded-For': req.header('X-Forwarded-For') || req.connection.remoteAddress }, method: req.method, body: JSON.stringify(body) From 1f4da664f43d16aa9e014936edfdc90dbe103b57 Mon Sep 17 00:00:00 2001 From: "gustavo.marin" Date: Fri, 6 Feb 2015 12:10:43 +0100 Subject: [PATCH 2/2] updated localhost to config.private_host --- config_schema.json | 1 + features/step_definitions/client_pass_through_pin.js | 2 +- tests/proxy/protectedCallsPassThrough.js | 8 ++++---- tests/proxy/protectedCallsStandard-platformSF.js | 2 +- tests/proxy/protectedCallsStandard.js | 4 ++-- tests/redirect.js | 2 +- tests/verify-phone.js | 6 +++--- 7 files changed, 13 insertions(+), 12 deletions(-) diff --git a/config_schema.json b/config_schema.json index 00b0edc..94169ba 100644 --- a/config_schema.json +++ b/config_schema.json @@ -3,6 +3,7 @@ "type": "object", "properties": { "public_port": { "type": "integer", "required": true }, + "private_host": { "type": "string", "required": true }, "private_port": { "type": "integer", "required": true }, "accessToken": { "type": "object", diff --git a/features/step_definitions/client_pass_through_pin.js b/features/step_definitions/client_pass_through_pin.js index 3364e39..5cdc840 100644 --- a/features/step_definitions/client_pass_through_pin.js +++ b/features/step_definitions/client_pass_through_pin.js @@ -23,7 +23,7 @@ var myStepDefinitionsWrapper = function () { }; options.headers[config.version.header] = "test/1"; - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path) .reply(201, {id: "a1b2c3d4e5f6"}); diff --git a/tests/proxy/protectedCallsPassThrough.js b/tests/proxy/protectedCallsPassThrough.js index a0a9c5c..4d74ac3 100644 --- a/tests/proxy/protectedCallsPassThrough.js +++ b/tests/proxy/protectedCallsPassThrough.js @@ -25,7 +25,7 @@ module.exports = { var expectedPrivateResponse = clone(expectedPublicRequest); delete(expectedPrivateResponse[config.passThroughEndpoint.password]); - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path, expectedPrivateResponse) .reply(201, {id: expectedUserId}); @@ -39,7 +39,7 @@ module.exports = { redisMng.insertKeyValue(redisKey + '.attempts', config.userPIN.attempts , config.redisKeys.user_phone_verify.expireInSec, function(err){ assert.equal(err, null); - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path, expectedPrivateResponse) .reply(201, {id: expectedUserId}); @@ -106,7 +106,7 @@ module.exports = { var expectedPrivateResponse = clone(expectedPublicRequest); delete(expectedPrivateResponse[config.passThroughEndpoint.password]); - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path, expectedPrivateResponse) .reply(203, {id: expectedUserId}); @@ -181,7 +181,7 @@ module.exports = { var expectedPrivateResponse = clone(expectedPublicRequest); delete(expectedPrivateResponse[config.passThroughEndpoint.password]); - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path, expectedPrivateResponse) .reply(201, {id: expectedUserId}); diff --git a/tests/proxy/protectedCallsStandard-platformSF.js b/tests/proxy/protectedCallsStandard-platformSF.js index d6b425d..b15274a 100644 --- a/tests/proxy/protectedCallsStandard-platformSF.js +++ b/tests/proxy/protectedCallsStandard-platformSF.js @@ -128,7 +128,7 @@ module.exports = { }; function nockProtectedStandartCall(id, expectedSfData, expectedBody) { - nock('http://localhost:' + config.private_port, { + nock('http://' + config.private_host + ':' + config.private_port, { reqheaders: { 'x-user-id': id, 'x-sf-data': JSON.stringify(expectedSfData), diff --git a/tests/proxy/protectedCallsStandard.js b/tests/proxy/protectedCallsStandard.js index 4d9745a..9deab17 100644 --- a/tests/proxy/protectedCallsStandard.js +++ b/tests/proxy/protectedCallsStandard.js @@ -44,7 +44,7 @@ module.exports = { ciphertoken.createToken(accessTokenSettings, createdUser._id, null, {}, function (err, loginToken) { var expectedBody = {field1: 'value1', field2: 'value2'}; - nock('http://localhost:' + config.private_port, { + nock('http://' + config.private_host + ':' + config.private_port, { reqheaders: { 'x-user-id': createdUser._id, 'content-type': 'application/json; charset=utf-8' @@ -88,7 +88,7 @@ module.exports = { ciphertoken.createToken(accessTokenSettings, createdUser._id, null, {}, function (err, loginToken) { var expectedBody = {field1: 'value1', field2: 'value2'}; - nock('http://localhost:' + config.private_port, { + nock('http://' + config.private_host + ':' + config.private_port, { reqheaders: { 'x-user-id': createdUser._id, 'content-type': 'application/json; charset=utf-8' diff --git a/tests/redirect.js b/tests/redirect.js index 8a54f37..315d215 100644 --- a/tests/redirect.js +++ b/tests/redirect.js @@ -56,7 +56,7 @@ describe('Redirect', function(){ }; options.headers[config.version.header] = "test/1"; - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post('/whatever') .reply(302, 'Redirecting', { 'Location': redirectURL diff --git a/tests/verify-phone.js b/tests/verify-phone.js index 6a3babb..2574689 100644 --- a/tests/verify-phone.js +++ b/tests/verify-phone.js @@ -194,7 +194,7 @@ describe('/api/profile (verify phone)', function(){ var expectedUserId = 'a1b2c3d4e5f6'; - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path) .reply(201, {id: expectedUserId}); @@ -289,7 +289,7 @@ describe('/api/profile (verify phone)', function(){ var expectedUserId = 'a1b2c3d4e5f6'; - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path) .reply(201, {id: expectedUserId}); @@ -350,7 +350,7 @@ describe('/api/profile (verify phone)', function(){ var expectedUserId = 'a1b2c3d4e5f6'; - nock('http://localhost:' + config.private_port) + nock('http://' + config.private_host + ':' + config.private_port) .post(config.passThroughEndpoint.path) .reply(201, {id: expectedUserId});