419adea234
* Create middleware and service for pubsub metrics * Use pubsub middleware for all request * Replace isEnabled with isDisabled to avoid negation * Use new headers names * Add acceptance and unit tests * Remove commented log calls * Remove only filters in tests Remove typo * Refactor service to ease integration test * Fix interaction with query controller * Use middleware at api-router and test all controllers * Rename user middleware function * Use sinon latest version * Create middleware and service for pubsub metrics * Use pubsub middleware for all request * Replace isEnabled with isDisabled to avoid negation * Use new headers names * Add acceptance and unit tests * Remove commented log calls * Remove only filters in tests Remove typo * Refactor service to ease integration test * Fix interaction with query controller * Use middleware at api-router and test all controllers * Rename user middleware function * Use sinon latest version * Fix tests * Fix typos * Checks if pubsub config exists to enable the service * Fix typo * Normalize headers values for pubsub * Trim fields when normalizing * Trim fields when normalizing
43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
'use strict';
|
|
|
|
const CdbRequest = require('../../models/cartodb-request');
|
|
|
|
module.exports = function user (metadataBackend) {
|
|
const cdbRequest = new CdbRequest();
|
|
|
|
return function userMiddleware (req, res, next) {
|
|
res.locals.user = getUserNameFromRequest(req, cdbRequest);
|
|
|
|
getUserId(metadataBackend, res.locals.user, function (err, userId) {
|
|
if (err || !userId) {
|
|
const error = new Error('Unauthorized');
|
|
error.type = 'auth';
|
|
error.subtype = 'user-not-found';
|
|
error.http_status = 404;
|
|
error.message = errorUserNotFoundMessageTemplate(res.locals.user);
|
|
next(error);
|
|
}
|
|
|
|
res.locals.userId = userId;
|
|
return next();
|
|
});
|
|
};
|
|
};
|
|
|
|
function getUserNameFromRequest (req, cdbRequest) {
|
|
return cdbRequest.userByReq(req);
|
|
}
|
|
|
|
function getUserId (metadataBackend, userName, callback) {
|
|
metadataBackend.getUserId(userName, function (err, userId) {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
return callback(null, userId);
|
|
});
|
|
}
|
|
|
|
function errorUserNotFoundMessageTemplate (user) {
|
|
return `Sorry, we can't find CARTO user '${user}'. Please check that you have entered the correct domain.`;
|
|
}
|