CartoDB-SQL-API/app/middlewares/authenticated-request.js

34 lines
1.0 KiB
JavaScript
Raw Normal View History

2016-10-04 21:40:56 +08:00
'use strict';
var _ = require('underscore');
var AuthApi = require('../auth/auth_api');
2018-02-19 21:20:09 +08:00
module.exports = function authenticatedRequest (userDatabaseService, forceToBeAuthenticated = false) {
return function authenticatedRequestMiddleware (req, res, next) {
2016-10-04 21:40:56 +08:00
req.profiler.start('sqlapi.job');
req.profiler.done('init');
const params = _.extend({}, res.locals, req.query, req.body);
const { user } = res.locals;
2018-02-19 21:20:09 +08:00
const authApi = new AuthApi(req, res, params);
2016-10-04 21:40:56 +08:00
userDatabaseService.getConnectionParams(authApi, user, function (err, dbParams, authDbParams, userLimits) {
2016-10-04 21:40:56 +08:00
req.profiler.done('setDBAuth');
if (err) {
return next(err);
2016-10-04 21:40:56 +08:00
}
if (forceToBeAuthenticated && !dbParams.authenticated) {
return next(new Error('permission denied'));
2016-10-04 21:40:56 +08:00
}
res.locals.userDbParams = dbParams;
res.locals.authDbParams = authDbParams;
res.locals.userLimits = userLimits;
2016-10-04 21:40:56 +08:00
next();
2016-10-04 21:40:56 +08:00
});
};
2018-02-19 21:20:09 +08:00
};