Rename req2params by prepareContext

This commit is contained in:
Daniel García Aubert 2017-09-22 17:56:47 +02:00
parent 6dc9cc0b23
commit 3bab081438
6 changed files with 38 additions and 38 deletions

View File

@ -9,11 +9,11 @@ var BaseController = require('./base');
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
const req2paramsMiddleware = require('../middleware/req2params-middleware');
const prepareContextMiddleware = require('../middleware/prepare-context');
function AnalysesController(authApi, pgConnection) {
BaseController.call(this, authApi, pgConnection);
this.req2paramsMiddleware = req2paramsMiddleware(authApi, pgConnection);
this.prepareContext = prepareContextMiddleware(authApi, pgConnection);
}
util.inherits(AnalysesController, BaseController);
@ -28,7 +28,7 @@ AnalysesController.prototype.register = function(router) {
router.get(
'/analyses/catalog',
this.req2paramsMiddleware,
this.prepareContext,
this.catalog.bind(this)
);
};

View File

@ -15,7 +15,7 @@ var MapStoreMapConfigProvider = require('../models/mapconfig/provider/map-store-
var QueryTables = require('cartodb-query-tables');
const req2paramsMiddleware = require('../middleware/req2params-middleware');
const prepareContextMiddleware = require('../middleware/prepare-context');
/**
* @param {AuthApi} authApi
@ -46,7 +46,7 @@ function LayergroupController(authApi, pgConnection, mapStore, tileBackend, prev
this.dataviewBackend = new DataviewBackend(analysisBackend);
this.analysisStatusBackend = new AnalysisStatusBackend();
this.req2paramsMiddleware = req2paramsMiddleware(authApi, pgConnection);
this.prepareContext = prepareContextMiddleware(authApi, pgConnection);
}
util.inherits(LayergroupController, BaseController);
@ -61,39 +61,39 @@ LayergroupController.prototype.register = function(router) {
router.get(
'/:token/:z/:x/:y@:scale_factor?x.:format',
this.req2paramsMiddleware,
this.prepareContext,
this.tile.bind(this)
);
router.get(
'/:token/:z/:x/:y.:format',
this.req2paramsMiddleware,
this.prepareContext,
this.tile.bind(this)
);
router.get(
'/:token/:layer/:z/:x/:y.(:format)',
this.req2paramsMiddleware,
this.prepareContext,
this.layer.bind(this)
);
router.get(
'/:token/:layer/attributes/:fid',
this.req2paramsMiddleware,
this.prepareContext,
this.attributes.bind(this)
);
router.get(
'/static/center/:token/:z/:lat/:lng/:width/:height.:format',
allowQueryParams(['layer']),
this.req2paramsMiddleware,
this.prepareContext,
this.center.bind(this)
);
router.get(
'/static/bbox/:token/:west,:south,:east,:north/:width/:height.:format',
allowQueryParams(['layer']),
this.req2paramsMiddleware,
this.prepareContext,
this.bbox.bind(this)
);
@ -116,33 +116,33 @@ LayergroupController.prototype.register = function(router) {
router.get(
'/:token/dataview/:dataviewName',
allowQueryParams(allowedDataviewQueryParams),
this.req2paramsMiddleware,
this.prepareContext,
this.dataview.bind(this)
);
router.get(
'/:token/:layer/widget/:dataviewName',
allowQueryParams(allowedDataviewQueryParams),
this.req2paramsMiddleware,
this.prepareContext,
this.dataview.bind(this)
);
router.get(
'/:token/dataview/:dataviewName/search',
allowQueryParams(allowedDataviewQueryParams),
this.req2paramsMiddleware,
this.prepareContext,
this.dataviewSearch.bind(this)
);
router.get(
'/:token/:layer/widget/:dataviewName/search',
allowQueryParams(allowedDataviewQueryParams),
this.req2paramsMiddleware,
this.prepareContext,
this.dataviewSearch.bind(this)
);
router.get('/:token/analysis/node/:nodeId',
this.req2paramsMiddleware,
this.prepareContext,
this.analysisNodeStatus.bind(this)
);
};

View File

@ -20,7 +20,7 @@ var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
var NamedMapMapConfigProvider = require('../models/mapconfig/provider/named-map-provider');
var CreateLayergroupMapConfigProvider = require('../models/mapconfig/provider/create-layergroup-provider');
const req2paramsMiddleware = require('../middleware/req2params-middleware');
const prepareContextMiddleware = require('../middleware/prepare-context');
/**
* @param {AuthApi} authApi
@ -53,7 +53,7 @@ function MapController(authApi, pgConnection, templateMaps, mapBackend, metadata
this.resourceLocator = new ResourceLocator(global.environment);
this.statsBackend = statsBackend;
this.req2paramsMiddleware = req2paramsMiddleware(authApi, pgConnection);
this.prepareContext = prepareContextMiddleware(authApi, pgConnection);
}
util.inherits(MapController, BaseController);
@ -66,28 +66,28 @@ MapController.prototype.register = function(app) {
app.base_url_mapconfig,
cors(),
userMiddleware,
this.req2paramsMiddleware,
this.prepareContext,
this.createGet.bind(this)
);
app.post(
app.base_url_mapconfig,
cors(),
userMiddleware,
this.req2paramsMiddleware,
this.prepareContext,
this.createPost.bind(this)
);
app.get(
app.base_url_templated + '/:template_id/jsonp',
cors(),
userMiddleware,
this.req2paramsMiddleware,
this.prepareContext,
this.jsonp.bind(this)
);
app.post(
app.base_url_templated + '/:template_id',
cors(),
userMiddleware,
this.req2paramsMiddleware,
this.prepareContext,
this.instantiate.bind(this)
);
app.options(app.base_url_mapconfig, cors('Content-Type'));

View File

@ -9,7 +9,7 @@ var BaseController = require('./base');
var cors = require('../middleware/cors');
var userMiddleware = require('../middleware/user');
var allowQueryParams = require('../middleware/allow-query-params');
const req2paramsMiddleware = require('../middleware/req2params-middleware');
const prepareContextMiddleware = require('../middleware/prepare-context');
function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileBackend, previewBackend,
surrogateKeysCache, tablesExtentApi, metadataBackend) {
@ -21,7 +21,7 @@ function NamedMapsController(authApi, pgConnection, namedMapProviderCache, tileB
this.surrogateKeysCache = surrogateKeysCache;
this.tablesExtentApi = tablesExtentApi;
this.metadataBackend = metadataBackend;
this.req2paramsMiddleware = req2paramsMiddleware(authApi, pgConnection);
this.prepareContext = prepareContextMiddleware(authApi, pgConnection);
}
util.inherits(NamedMapsController, BaseController);
@ -33,7 +33,7 @@ NamedMapsController.prototype.register = function(app) {
app.base_url_templated + '/:template_id/:layer/:z/:x/:y.(:format)',
cors(),
userMiddleware,
this.req2paramsMiddleware,
this.prepareContext,
this.tile.bind(this)
);
@ -42,7 +42,7 @@ NamedMapsController.prototype.register = function(app) {
cors(),
userMiddleware,
allowQueryParams(['layer']),
this.req2paramsMiddleware,
this.prepareContext,
this.staticMap.bind(this)
);
};

View File

@ -24,8 +24,8 @@ var REQUEST_QUERY_PARAMS_WHITELIST = [
* @param req - standard express request obj. Should have host & table
* @param callback
*/
module.exports = function req2paramsMiddleware (authApi, pgConnection) {
return function req2params (req, res, next) {
module.exports = function prepareContextMiddleware (authApi, pgConnection) {
return function prepareContext (req, res, next) {
var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST;
if (Array.isArray(req.context.allowedQueryParams)) {
allowedQueryParams = allowedQueryParams.concat(req.context.allowedQueryParams);

View File

@ -7,17 +7,17 @@ var PgConnection = require('../../../lib/cartodb/backends/pg_connection');
var AuthApi = require('../../../lib/cartodb/api/auth_api');
var TemplateMaps = require('../../../lib/cartodb/backends/template_maps');
var req2paramsMiddleware = require('../../../lib/cartodb/middleware/req2params-middleware');
var prepareContextMiddleware = require('../../../lib/cartodb/middleware/prepare-context');
var windshaft = require('windshaft');
describe('req2params', function() {
describe('prepare-context', function() {
var test_user = _.template(global.environment.postgres_auth_user, {user_id:1});
var test_pubuser = global.environment.postgres.user;
var test_database = test_user + '_db';
var req2params;
var prepareContext;
before(function() {
var redisPool = new RedisPool(global.environment.redis);
var mapStore = new windshaft.storage.MapStore();
@ -26,12 +26,12 @@ describe('req2params', function() {
var templateMaps = new TemplateMaps(redisPool);
var authApi = new AuthApi(pgConnection, metadataBackend, mapStore, templateMaps);
req2params = req2paramsMiddleware(authApi, pgConnection);
prepareContext = prepareContextMiddleware(authApi, pgConnection);
});
it('can be found in server_options', function(){
assert.ok(_.isFunction(req2params));
assert.ok(_.isFunction(prepareContext));
});
function prepareRequest(req) {
@ -45,7 +45,7 @@ describe('req2params', function() {
it('cleans up request', function(done){
var req = {headers: { host:'localhost' }, query: {dbuser:'hacker',dbname:'secret'}};
var res = {};
req2params(prepareRequest(req), res, function(err, req) {
prepareContext(prepareRequest(req), res, function(err, req) {
if ( err ) { done(err); return; }
assert.ok(_.isObject(req.query), 'request has query');
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
@ -60,7 +60,7 @@ describe('req2params', function() {
it('sets dbname from redis metadata', function(done){
var req = {headers: { host:'localhost' }, query: {} };
var res = {};
req2params(prepareRequest(req), res, function(err, req) {
prepareContext(prepareRequest(req), res, function(err, req) {
if ( err ) { done(err); return; }
assert.ok(_.isObject(req.query), 'request has query');
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
@ -75,7 +75,7 @@ describe('req2params', function() {
it('sets also dbuser for authenticated requests', function(done){
var req = {headers: { host:'localhost' }, query: {map_key: '1234'} };
var res = {};
req2params(prepareRequest(req), res, function(err, req) {
prepareContext(prepareRequest(req), res, function(err, req) {
if ( err ) { done(err); return; }
assert.ok(_.isObject(req.query), 'request has query');
assert.ok(!req.query.hasOwnProperty('dbuser'), 'dbuser was removed from query');
@ -92,7 +92,7 @@ describe('req2params', function() {
map_key: '1235'
}
};
req2params(prepareRequest(req), res, function(err, req) {
prepareContext(prepareRequest(req), res, function(err, req) {
// wrong key resets params to no user
assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')');
done();
@ -117,7 +117,7 @@ describe('req2params', function() {
};
var res = {};
req2params(prepareRequest(req), res, function(err, req) {
prepareContext(prepareRequest(req), res, function(err, req) {
if ( err ) {
return done(err);
}