check user exists in user middleware
This way, we keep sending a 404 error if the user does not exist.
This commit is contained in:
parent
017dc69c02
commit
fa5a99211c
@ -10,10 +10,6 @@ function ApikeyAuth(req, metadataBackend, username, apikey) {
|
|||||||
|
|
||||||
module.exports = ApikeyAuth;
|
module.exports = ApikeyAuth;
|
||||||
|
|
||||||
function errorUserNotFoundMessageTemplate (user) {
|
|
||||||
return `Sorry, we can't find CARTO user '${user}'. Please check that you have entered the correct domain.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function usernameMatches(basicAuthUsername, requestUsername) {
|
function usernameMatches(basicAuthUsername, requestUsername) {
|
||||||
return !(basicAuthUsername && (basicAuthUsername !== requestUsername));
|
return !(basicAuthUsername && (basicAuthUsername !== requestUsername));
|
||||||
}
|
}
|
||||||
@ -21,8 +17,8 @@ function usernameMatches(basicAuthUsername, requestUsername) {
|
|||||||
ApikeyAuth.prototype.verifyCredentials = function (callback) {
|
ApikeyAuth.prototype.verifyCredentials = function (callback) {
|
||||||
this.metadataBackend.getApikey(this.username, this.apikey, (err, apikey) => {
|
this.metadataBackend.getApikey(this.username, this.apikey, (err, apikey) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
err.http_status = 404;
|
err.http_status = 500;
|
||||||
err.message = errorUserNotFoundMessageTemplate(this.username);
|
err.message = 'Unexpected error fetching from Redis';
|
||||||
|
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ function composeJobMiddlewares (metadataBackend, userDatabaseService, jobService
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
initializeProfilerMiddleware('job'),
|
initializeProfilerMiddleware('job'),
|
||||||
userMiddleware(),
|
userMiddleware(metadataBackend),
|
||||||
rateLimitsMiddleware(userLimitsService, endpointGroup),
|
rateLimitsMiddleware(userLimitsService, endpointGroup),
|
||||||
authorizationMiddleware(metadataBackend, forceToBeAuthenticated),
|
authorizationMiddleware(metadataBackend, forceToBeAuthenticated),
|
||||||
connectionParamsMiddleware(userDatabaseService),
|
connectionParamsMiddleware(userDatabaseService),
|
||||||
|
@ -36,7 +36,7 @@ QueryController.prototype.route = function (app) {
|
|||||||
const queryMiddlewares = endpointGroup => {
|
const queryMiddlewares = endpointGroup => {
|
||||||
return [
|
return [
|
||||||
initializeProfilerMiddleware('query'),
|
initializeProfilerMiddleware('query'),
|
||||||
userMiddleware(),
|
userMiddleware(this.metadataBackend),
|
||||||
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
||||||
authorizationMiddleware(this.metadataBackend),
|
authorizationMiddleware(this.metadataBackend),
|
||||||
connectionParamsMiddleware(this.userDatabaseService),
|
connectionParamsMiddleware(this.userDatabaseService),
|
||||||
|
@ -1,10 +1,36 @@
|
|||||||
const CdbRequest = require('../models/cartodb_request');
|
const CdbRequest = require('../models/cartodb_request');
|
||||||
|
|
||||||
module.exports = function user () {
|
module.exports = function user(metadataBackend) {
|
||||||
const cdbRequest = new CdbRequest();
|
const cdbRequest = new CdbRequest();
|
||||||
|
|
||||||
return function userMiddleware (req, res, next) {
|
return function userMiddleware (req, res, next) {
|
||||||
res.locals.user = cdbRequest.userByReq(req);
|
res.locals.user = getUserNameFromRequest(req, cdbRequest);
|
||||||
next();
|
|
||||||
|
checkUserExists(metadataBackend, res.locals.user, function(userExists) {
|
||||||
|
if (userExists) {
|
||||||
|
return next();
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function getUserNameFromRequest(req, cdbRequest) {
|
||||||
|
return cdbRequest.userByReq(req);
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkUserExists(metadataBackend, userName, callback) {
|
||||||
|
metadataBackend.getUserId(userName, function(err) {
|
||||||
|
callback(!err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function errorUserNotFoundMessageTemplate(user) {
|
||||||
|
return `Sorry, we can't find CARTO user '${user}'. Please check that you have entered the correct domain.`;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user