<commit_before>var assert = require('assert');
var _ = require('underscore');
var step = require('step');


// Whitelist query parameters and attach format
var REQUEST_QUERY_PARAMS_WHITELIST = [
    'config',
    'map_key',
    'api_key',
    'auth_token',
    'callback',
    'zoom',
    'lon',
    'lat',
    // analysis
    'filters' // json
];

// jshint maxcomplexity:8
/**
 * Whitelist input and get database name & default geometry type from
 * subdomain/user metadata held in CartoDB Redis
 * @param req - standard express request obj. Should have host & table
 * @param callback
 */
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);
        }

        req.query = _.pick(req.query, allowedQueryParams);

        var user = req.context.user;

        if ( req.params.token ) {
            // Token might match the following patterns:
            // - {user}@{tpl_id}@{token}:{cache_buster}
            var tksplit = req.params.token.split(':');
            req.params.token = tksplit[0];
            if ( tksplit.length > 1 ) {
                req.params.cache_buster= tksplit[1];
            }
            tksplit = req.params.token.split('@');
            if ( tksplit.length > 1 ) {
                req.params.signer = tksplit.shift();
                if ( ! req.params.signer ) {
                    req.params.signer = user;
                }
                else if ( req.params.signer !== user ) {
                    var err = new Error(
                            'Cannot use map signature of user "' + req.params.signer + '" on db of user "' + user + '"'
                    );
                    err.http_status = 403;
                    req.profiler.done('req2params');
                    next(err);
                    return;
                }
                if ( tksplit.length > 1 ) {
                    /*var template_hash = */tksplit.shift(); // unused
                }
                req.params.token = tksplit.shift();
            }
        }

        // bring all query values onto req.params object
        _.extend(req.params, req.query);

        // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to
        // parse url params to an object and it's performed after matching path and controller.
        req.locals = {};
        _.extend(req.locals, req.params);

        req.profiler.done('req2params.setup');

        step(
            function getPrivacy(){
                authApi.authorize(req, this);
            },
            function validateAuthorization(err, authorized) {
                req.profiler.done('authorize');
                assert.ifError(err);
                if(!authorized) {
                    err = new Error("Sorry, you are unauthorized (permission denied)");
                    err.http_status = 403;
                    throw err;
                }
                return null;
            },
            function getDatabase(err){
                assert.ifError(err);
                pgConnection.setDBConn(user, req.params, this);
            },
            function finishSetup(err) {
                if ( err ) {
                    if (err.message && -1 !== err.message.indexOf('name not found')) {
                        err.http_status = 404;
                    }
                    req.profiler.done('req2params');
                    return next(err, req);
                }

                // Add default database connection parameters
                // if none given
                _.defaults(req.params, {
                    dbuser: global.environment.postgres.user,
                    dbpassword: global.environment.postgres.password,
                    dbhost: global.environment.postgres.host,
                    dbport: global.environment.postgres.port
                });

                // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to
                // parse url params to an object and it's performed after matching path and controller.
                _.defaults(req.locals, req.params);

                req.profiler.done('req2params');
                next(null, req);
            }
        );
    };
};
<commit_msg>Return an array of middlewares instead of big one in prepare context<commit_after>var assert = require('assert');
var _ = require('underscore');
var step = require('step');

// Whitelist query parameters and attach format
var REQUEST_QUERY_PARAMS_WHITELIST = [
    'config',
    'map_key',
    'api_key',
    'auth_token',
    'callback',
    'zoom',
    'lon',
    'lat',
    // analysis
    'filters' // json
];

// jshint maxcomplexity:8
/**
 * Whitelist input and get database name & default geometry type from
 * subdomain/user metadata held in CartoDB Redis
 * @param req - standard express request obj. Should have host & table
 * @param callback
 */
module.exports = function prepareContextMiddleware (authApi, pgConnection) {
    return [
        function cleanUpQueryParams (req, res, next) {
            var allowedQueryParams = REQUEST_QUERY_PARAMS_WHITELIST;

            if (Array.isArray(req.context.allowedQueryParams)) {
                allowedQueryParams = allowedQueryParams.concat(req.context.allowedQueryParams);
            }

            req.query = _.pick(req.query, allowedQueryParams);

            next();
        },
        function prepareContext (req, res, next) {
            var user = req.context.user;

            if ( req.params.token ) {
                // Token might match the following patterns:
                // - {user}@{tpl_id}@{token}:{cache_buster}
                var tksplit = req.params.token.split(':');
                req.params.token = tksplit[0];
                if ( tksplit.length > 1 ) {
                    req.params.cache_buster= tksplit[1];
                }
                tksplit = req.params.token.split('@');
                if ( tksplit.length > 1 ) {
                    req.params.signer = tksplit.shift();
                    if ( ! req.params.signer ) {
                        req.params.signer = user;
                    }
                    else if ( req.params.signer !== user ) {
                        var err = new Error(
                                'Cannot use map signature of user "' + req.params.signer + '" on db of user "' + user + '"'
                        );
                        err.http_status = 403;
                        req.profiler.done('req2params');
                        next(err);
                        return;
                    }
                    if ( tksplit.length > 1 ) {
                        /*var template_hash = */tksplit.shift(); // unused
                    }
                    req.params.token = tksplit.shift();
                }
            }

            // bring all query values onto req.params object
            _.extend(req.params, req.query);

            // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to
            // parse url params to an object and it's performed after matching path and controller.
            req.locals = {};
            _.extend(req.locals, req.params);

            req.profiler.done('req2params.setup');

            step(
                function getPrivacy(){
                    authApi.authorize(req, this);
                },
                function validateAuthorization(err, authorized) {
                    req.profiler.done('authorize');
                    assert.ifError(err);
                    if(!authorized) {
                        err = new Error("Sorry, you are unauthorized (permission denied)");
                        err.http_status = 403;
                        throw err;
                    }
                    return null;
                },
                function getDatabase(err){
                    assert.ifError(err);
                    pgConnection.setDBConn(user, req.params, this);
                },
                function finishSetup(err) {
                    if ( err ) {
                        if (err.message && -1 !== err.message.indexOf('name not found')) {
                            err.http_status = 404;
                        }
                        req.profiler.done('req2params');
                        return next(err, req);
                    }

                    // Add default database connection parameters
                    // if none given
                    _.defaults(req.params, {
                        dbuser: global.environment.postgres.user,
                        dbpassword: global.environment.postgres.password,
                        dbhost: global.environment.postgres.host,
                        dbport: global.environment.postgres.port
                    });

                    // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to
                    // parse url params to an object and it's performed after matching path and controller.
                    _.defaults(req.locals, req.params);

                    req.profiler.done('req2params');
                    next(null, req);
                }
            );
        }
    ];
};

{'fraction_non_alphanumeric': 0.10098884914790658, 'fraction_numerical': 0.004207868714496108, 'mean_word_length': 1.3608145021107525, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4428533', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2304, 'n_words': 577}