From a4dbc1bac27498afd67841c9da303a773f6c2137 Mon Sep 17 00:00:00 2001 From: Eneko Lakasta Date: Wed, 7 Feb 2018 18:20:56 +0100 Subject: [PATCH] remove step and check existance of proper api key --- lib/cartodb/api/auth_api.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lib/cartodb/api/auth_api.js b/lib/cartodb/api/auth_api.js index e18f1d0d..e4525929 100644 --- a/lib/cartodb/api/auth_api.js +++ b/lib/cartodb/api/auth_api.js @@ -47,6 +47,13 @@ AuthApi.prototype.authorizedBySigner = function(res, callback) { }); }; +function isValidApiKey(apikey) { + return apikey.type !== null && + apikey.user !== null && + apikey.databasePassword !== null && + apikey.databaseRole !== null; +} + // Check if a request is authorized by api_key // // @param user @@ -61,23 +68,19 @@ AuthApi.prototype.authorizedByAPIKey = function(user, req, callback) { givenKey = req.body.api_key || req.body.map_key; } if ( ! givenKey ) { - return callback(null, 0); // no api key, no authorization... + return callback(null, false); // no api key, no authorization... } - var self = this; - - step( - function () { - self.metadataBackend.getUserMapKey(user, this); - }, - function checkApiKey(err, val){ - assert.ifError(err); - return val && givenKey === val; - }, - function finish(err, authorized) { - callback(err, authorized); + this.metadataBackend.getApikey(user, givenKey, (err, apikey) => { + if (err) { + return callback(err); } - ); + if ( !isValidApiKey(apikey)) { + return callback(null, false); + } + + return callback(null, true); + }); }; /**