From fda7661dad88e946ee5bd26fadf8445aed7c9423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 29 Dec 2017 12:23:52 +0100 Subject: [PATCH] Create authorizedByAPIKey middleware --- lib/cartodb/controllers/named_maps_admin.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/cartodb/controllers/named_maps_admin.js b/lib/cartodb/controllers/named_maps_admin.js index 0f54ddf3..8fcaaac0 100644 --- a/lib/cartodb/controllers/named_maps_admin.js +++ b/lib/cartodb/controllers/named_maps_admin.js @@ -198,6 +198,26 @@ NamedMapsAdminController.prototype.list = function(req, res, next) { ); }; +NamedMapsAdminController.prototype.authorizedByAPIKey = function (action) { + return function authorizedByAPIKeyMiddleware (req, res, next) { + const { user } = res.locals; + + this.authApi.authorizedByAPIKey(user, req, (err, authenticated) => { + if (err) { + return next(err); + } + + if (!authenticated) { + const error = new Error(`Only authenticated user can ${action} templated maps`); + error.http_status = 403; + return next(error); + } + + next(); + }); + }.bind(this); +}; + function finishFn(controller, req, res, description, status, next) { return function finish(err, body){ if (err) {