diff --git a/lib/cartodb/api/auth_api.js b/lib/cartodb/api/auth_api.js index 8782f907..e9f10262 100644 --- a/lib/cartodb/api/auth_api.js +++ b/lib/cartodb/api/auth_api.js @@ -62,7 +62,7 @@ function isValidApiKey(apikey) { // AuthApi.prototype.authorizedByAPIKey = function(user, res, callback) { const apikeyToken = res.locals.api_key; - const apikeyUsername = res.locals.apikeyUsername; + const basicAuthUsername = res.locals.basicAuthUsername; if ( ! apikeyToken ) { return callback(null, false); // no api key, no authorization... @@ -91,7 +91,7 @@ AuthApi.prototype.authorizedByAPIKey = function(user, res, callback) { return callback(error); } - if (!usernameMatches(apikeyUsername, res.locals.user)) { + if (!usernameMatches(basicAuthUsername, res.locals.user)) { const error = new Error('Forbidden'); error.type = 'auth'; error.subtype = 'api-key-username-mismatch'; @@ -149,8 +149,8 @@ function isNameNotFoundError (err) { return err.message && -1 !== err.message.indexOf('name not found'); } -function usernameMatches (apikeyUsername, requestUsername) { - return !(apikeyUsername && (apikeyUsername !== requestUsername)); +function usernameMatches (basicAuthUsername, requestUsername) { + return !(basicAuthUsername && (basicAuthUsername !== requestUsername)); } /** diff --git a/lib/cartodb/middleware/context/apikey-credentials.js b/lib/cartodb/middleware/context/apikey-credentials.js index 225c8ab6..b70718ab 100644 --- a/lib/cartodb/middleware/context/apikey-credentials.js +++ b/lib/cartodb/middleware/context/apikey-credentials.js @@ -3,8 +3,10 @@ module.exports = function apikeyToken () { return function apikeyTokenMiddleware(req, res, next) { const apikeyCredentials = getApikeyCredentialsFromRequest(req); + res.locals.api_key = apikeyCredentials.token; - res.locals.apikeyUsername = apikeyCredentials.username; + res.locals.basicAuthUsername = apikeyCredentials.username; + return next(); }; };