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

38 lines
1.1 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');
function authenticatedMiddleware(userDatabaseService, forceToBeAuthenticated = false) {
2016-10-04 21:40:56 +08:00
return function middleware(req, res, next) {
req.profiler.start('sqlapi.job');
req.profiler.done('init');
// clone so don't modify req.params or req.body so oauth is not broken
const params = _.extend({}, res.locals, req.query, req.body);
const { user } = res.locals;
2016-10-04 21:40:56 +08:00
var authApi = new AuthApi(req, res, params);
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
});
};
}
module.exports = authenticatedMiddleware;