2015-03-16 07:21:55 +08:00
|
|
|
var step = require('step');
|
2015-04-23 17:29:55 +08:00
|
|
|
var assert = require('assert');
|
2015-01-20 23:56:06 +08:00
|
|
|
var _ = require('underscore');
|
2015-06-29 22:38:13 +08:00
|
|
|
var templateName = require('../backends/template_maps').templateName;
|
2015-03-24 00:35:09 +08:00
|
|
|
var CdbRequest = require('../models/cdb_request');
|
2015-04-27 20:52:36 +08:00
|
|
|
var NamedMapsCacheEntry = require('../cache/model/named_maps_entry');
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
function NamedMapsController(app, serverOptions, templateMaps, metadataBackend, templateBaseUrl, surrogateKeysCache) {
|
2015-01-20 23:56:06 +08:00
|
|
|
this.app = app;
|
2015-01-21 00:07:55 +08:00
|
|
|
this.serverOptions = serverOptions;
|
2015-01-20 23:56:06 +08:00
|
|
|
this.templateMaps = templateMaps;
|
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.templateBaseUrl = templateBaseUrl;
|
2015-01-23 23:37:38 +08:00
|
|
|
this.surrogateKeysCache = surrogateKeysCache;
|
2015-01-20 23:56:06 +08:00
|
|
|
}
|
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
module.exports = NamedMapsController;
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbRequest = new CdbRequest();
|
2015-01-20 23:56:06 +08:00
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.register = function(app) {
|
2015-01-20 23:56:06 +08:00
|
|
|
app.get(this.templateBaseUrl + '/:template_id/jsonp', this.jsonp.bind(this));
|
2015-01-20 23:58:12 +08:00
|
|
|
app.post(this.templateBaseUrl, this.create.bind(this));
|
2015-01-21 00:07:55 +08:00
|
|
|
app.put(this.templateBaseUrl + '/:template_id', this.update.bind(this));
|
2015-01-21 00:17:06 +08:00
|
|
|
app.get(this.templateBaseUrl + '/:template_id', this.retrieve.bind(this));
|
2015-01-21 00:34:23 +08:00
|
|
|
app.del(this.templateBaseUrl + '/:template_id', this.destroy.bind(this));
|
2015-01-21 00:39:33 +08:00
|
|
|
app.get(this.templateBaseUrl, this.list.bind(this));
|
2015-01-21 00:45:47 +08:00
|
|
|
app.options(this.templateBaseUrl + '/:template_id', this.options.bind(this));
|
2015-01-21 00:57:53 +08:00
|
|
|
app.post(this.templateBaseUrl + '/:template_id', this.instantiate.bind(this));
|
2015-01-20 23:58:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add a template
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.create = function(req, res) {
|
2015-01-20 23:58:12 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-20 23:58:12 +08:00
|
|
|
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-20 23:58:12 +08:00
|
|
|
function checkPerms(){
|
2015-01-21 00:07:55 +08:00
|
|
|
self.serverOptions.authorizedByAPIKey(req, this);
|
2015-01-20 23:58:12 +08:00
|
|
|
},
|
|
|
|
function addTemplate(err, authenticated) {
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.ifError(err);
|
2015-04-23 18:01:53 +08:00
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can get template maps');
|
2015-04-23 18:05:52 +08:00
|
|
|
ifInvalidContentType(req, 'template POST data must be of type application/json');
|
2015-01-20 23:58:12 +08:00
|
|
|
var cfg = req.body;
|
|
|
|
self.templateMaps.addTemplate(cdbuser, cfg, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err, tpl_id){
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
return { template_id: tpl_id };
|
2015-01-20 23:58:12 +08:00
|
|
|
},
|
2015-04-23 17:47:01 +08:00
|
|
|
finishFn(self.app, res, 'POST TEMPLATE')
|
2015-01-20 23:58:12 +08:00
|
|
|
);
|
2015-01-20 23:56:06 +08:00
|
|
|
};
|
|
|
|
|
2015-01-21 00:07:55 +08:00
|
|
|
// Update a template
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.update = function(req, res) {
|
2015-01-21 00:07:55 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-21 00:07:55 +08:00
|
|
|
var template;
|
|
|
|
var tpl_id;
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-21 00:07:55 +08:00
|
|
|
function checkPerms(){
|
|
|
|
self.serverOptions.authorizedByAPIKey(req, this);
|
|
|
|
},
|
|
|
|
function updateTemplate(err, authenticated) {
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.ifError(err);
|
2015-04-23 18:01:53 +08:00
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated user can update templated maps');
|
2015-04-23 18:05:52 +08:00
|
|
|
ifInvalidContentType(req, 'template PUT data must be of type application/json');
|
|
|
|
|
2015-01-21 00:07:55 +08:00
|
|
|
template = req.body;
|
2015-04-23 17:29:55 +08:00
|
|
|
tpl_id = templateName(req.params.template_id);
|
2015-01-21 00:07:55 +08:00
|
|
|
self.templateMaps.updTemplate(cdbuser, tpl_id, template, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err){
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
|
|
|
|
return { template_id: tpl_id };
|
2015-01-21 00:07:55 +08:00
|
|
|
},
|
2015-04-23 17:47:01 +08:00
|
|
|
finishFn(self.app, res, 'PUT TEMPLATE')
|
2015-01-21 00:07:55 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-01-21 00:17:06 +08:00
|
|
|
// Get a specific template
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.retrieve = function(req, res) {
|
2015-01-21 00:17:06 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-14 22:40:15 +08:00
|
|
|
if (req.profiler) {
|
2015-01-21 00:17:06 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.get_template');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-21 00:17:06 +08:00
|
|
|
var tpl_id;
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-21 00:17:06 +08:00
|
|
|
function checkPerms(){
|
|
|
|
self.serverOptions.authorizedByAPIKey(req, this);
|
|
|
|
},
|
|
|
|
function updateTemplate(err, authenticated) {
|
2015-04-23 18:01:53 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can get template maps');
|
2015-04-23 18:05:52 +08:00
|
|
|
|
2015-04-23 17:29:55 +08:00
|
|
|
tpl_id = templateName(req.params.template_id);
|
2015-01-21 00:17:06 +08:00
|
|
|
self.templateMaps.getTemplate(cdbuser, tpl_id, this);
|
|
|
|
},
|
2015-04-23 17:29:55 +08:00
|
|
|
function prepareResponse(err, tpl_val) {
|
2015-01-21 00:17:06 +08:00
|
|
|
if ( err ) throw err;
|
|
|
|
if ( ! tpl_val ) {
|
|
|
|
err = new Error("Cannot find template '" + tpl_id + "' of user '" + cdbuser + "'");
|
|
|
|
err.http_status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
// auth_id was added by ourselves,
|
|
|
|
// so we remove it before returning to the user
|
|
|
|
delete tpl_val.auth_id;
|
|
|
|
return { template: tpl_val };
|
|
|
|
},
|
2015-04-23 17:47:01 +08:00
|
|
|
finishFn(self.app, res, 'GET TEMPLATE')
|
2015-01-21 00:17:06 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-01-21 00:34:23 +08:00
|
|
|
// Delete a specific template
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.destroy = function(req, res) {
|
2015-01-21 00:34:23 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-14 22:40:15 +08:00
|
|
|
if (req.profiler) {
|
2015-01-21 00:34:23 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.delete_template');
|
|
|
|
}
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-21 00:34:23 +08:00
|
|
|
var tpl_id;
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-21 00:34:23 +08:00
|
|
|
function checkPerms(){
|
2015-01-21 00:39:33 +08:00
|
|
|
self.serverOptions.authorizedByAPIKey(req, this);
|
2015-01-21 00:34:23 +08:00
|
|
|
},
|
|
|
|
function updateTemplate(err, authenticated) {
|
2015-04-23 18:01:53 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated users can delete template maps');
|
2015-04-23 18:05:52 +08:00
|
|
|
|
2015-04-23 17:29:55 +08:00
|
|
|
tpl_id = templateName(req.params.template_id);
|
2015-01-21 00:34:23 +08:00
|
|
|
self.templateMaps.delTemplate(cdbuser, tpl_id, this);
|
|
|
|
},
|
2015-03-16 07:16:36 +08:00
|
|
|
function prepareResponse(err/*, tpl_val*/){
|
2015-01-21 00:34:23 +08:00
|
|
|
if ( err ) throw err;
|
|
|
|
return { status: 'ok' };
|
|
|
|
},
|
2015-04-23 17:47:01 +08:00
|
|
|
finishFn(self.app, res, 'DELETE TEMPLATE', ['', 204])
|
2015-01-21 00:34:23 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-01-21 00:39:33 +08:00
|
|
|
// Get a list of owned templates
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.list = function(req, res) {
|
2015-01-21 00:39:33 +08:00
|
|
|
var self = this;
|
2015-04-14 22:40:15 +08:00
|
|
|
if ( req.profiler ) {
|
2015-01-21 00:39:33 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.get_template_list');
|
|
|
|
}
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-21 00:39:33 +08:00
|
|
|
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-21 00:39:33 +08:00
|
|
|
function checkPerms(){
|
|
|
|
self.serverOptions.authorizedByAPIKey(req, this);
|
|
|
|
},
|
|
|
|
function listTemplates(err, authenticated) {
|
2015-04-23 18:01:53 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
ifUnauthenticated(authenticated, 'Only authenticated user can list templated maps');
|
2015-04-23 18:05:52 +08:00
|
|
|
|
2015-01-21 00:39:33 +08:00
|
|
|
self.templateMaps.listTemplates(cdbuser, this);
|
|
|
|
},
|
|
|
|
function prepareResponse(err, tpl_ids){
|
2015-04-23 17:29:55 +08:00
|
|
|
assert.ifError(err);
|
|
|
|
return { template_ids: tpl_ids };
|
2015-01-21 00:39:33 +08:00
|
|
|
},
|
2015-04-23 17:47:01 +08:00
|
|
|
finishFn(self.app, res, 'GET TEMPLATE LIST')
|
2015-01-21 00:39:33 +08:00
|
|
|
);
|
|
|
|
};
|
2015-01-21 00:34:23 +08:00
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.instantiate = function(req, res) {
|
2015-01-21 00:57:53 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-14 22:40:15 +08:00
|
|
|
if (req.profiler) {
|
2015-01-21 00:57:53 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.instance_template_post');
|
|
|
|
}
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-21 00:57:53 +08:00
|
|
|
function() {
|
2015-04-23 18:05:52 +08:00
|
|
|
ifInvalidContentType(req, 'template POST data must be of type application/json');
|
|
|
|
|
2015-01-21 00:57:53 +08:00
|
|
|
self.instantiateTemplate(req, res, req.body, this);
|
|
|
|
}, function(err, response) {
|
2015-04-14 22:41:04 +08:00
|
|
|
self.finish_instantiation(err, response, res);
|
2015-01-21 00:57:53 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.options = function(req, res, next) {
|
2015-01-21 00:45:47 +08:00
|
|
|
this.app.doCORS(res, "Content-Type");
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
|
2015-01-20 23:56:06 +08:00
|
|
|
/**
|
|
|
|
* jsonp endpoint, allows to instantiate a template with a json call.
|
|
|
|
* callback query argument is mandatory
|
|
|
|
*/
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.jsonp = function(req, res) {
|
2015-01-20 23:56:06 +08:00
|
|
|
var self = this;
|
|
|
|
|
2015-04-14 22:40:15 +08:00
|
|
|
if (req.profiler) {
|
2015-01-20 23:56:06 +08:00
|
|
|
req.profiler.start('windshaft-cartodb.instance_template_get');
|
|
|
|
}
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-20 23:56:06 +08:00
|
|
|
function() {
|
|
|
|
if ( req.query.callback === undefined || req.query.callback.length === 0) {
|
|
|
|
throw new Error('callback parameter should be present and be a function name');
|
|
|
|
}
|
|
|
|
var config = {};
|
|
|
|
if(req.query.config) {
|
|
|
|
try {
|
|
|
|
config = JSON.parse(req.query.config);
|
|
|
|
} catch(e) {
|
|
|
|
throw new Error('badformed config parameter, should be a valid JSON');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.instantiateTemplate(req, res, config, this);
|
|
|
|
}, function(err, response) {
|
2015-04-14 22:41:04 +08:00
|
|
|
self.finish_instantiation(err, response, res);
|
2015-01-20 23:56:06 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Instantiate a template
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.instantiateTemplate = function(req, res, template_params, callback) {
|
2015-01-20 23:56:06 +08:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.app.doCORS(res);
|
|
|
|
|
|
|
|
var template;
|
|
|
|
var layergroup;
|
|
|
|
var fakereq; // used for call to createLayergroup
|
2015-03-24 00:35:09 +08:00
|
|
|
var cdbuser = cdbRequest.userByReq(req);
|
2015-01-20 23:56:06 +08:00
|
|
|
// Format of template_id: [<template_owner>]@<template_id>
|
2015-04-23 17:29:55 +08:00
|
|
|
var tpl_id = templateName(req.params.template_id);
|
2015-01-20 23:56:06 +08:00
|
|
|
var auth_token = req.query.auth_token;
|
2015-03-16 07:21:55 +08:00
|
|
|
step(
|
2015-01-20 23:56:06 +08:00
|
|
|
function getTemplate(){
|
|
|
|
self.templateMaps.getTemplate(cdbuser, tpl_id, this);
|
|
|
|
},
|
2015-01-22 22:40:40 +08:00
|
|
|
function checkAuthorized(err, templateValue) {
|
2015-01-20 23:56:06 +08:00
|
|
|
if ( req.profiler ) req.profiler.done('getTemplate');
|
|
|
|
if ( err ) throw err;
|
2015-01-22 22:40:40 +08:00
|
|
|
if ( ! templateValue ) {
|
2015-01-20 23:56:06 +08:00
|
|
|
err = new Error("Template '" + tpl_id + "' of user '" + cdbuser + "' not found");
|
|
|
|
err.http_status = 404;
|
|
|
|
throw err;
|
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
|
|
|
|
template = templateValue;
|
|
|
|
|
2015-01-20 23:56:06 +08:00
|
|
|
var authorized = false;
|
|
|
|
try {
|
2015-01-22 22:40:40 +08:00
|
|
|
authorized = self.templateMaps.isAuthorized(template, auth_token);
|
2015-01-20 23:56:06 +08:00
|
|
|
} catch (err) {
|
|
|
|
// we catch to add http_status
|
|
|
|
err.http_status = 403;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
if ( ! authorized ) {
|
|
|
|
err = new Error('Unauthorized template instanciation');
|
|
|
|
err.http_status = 403;
|
|
|
|
throw err;
|
|
|
|
}
|
2015-01-22 22:40:40 +08:00
|
|
|
|
|
|
|
if (req.profiler) {
|
|
|
|
req.profiler.done('authorizedByCert');
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:56:06 +08:00
|
|
|
return self.templateMaps.instance(template, template_params);
|
|
|
|
},
|
|
|
|
function prepareParams(err, instance){
|
|
|
|
if ( req.profiler ) req.profiler.done('TemplateMaps_instance');
|
|
|
|
if ( err ) throw err;
|
|
|
|
layergroup = instance;
|
2015-04-08 17:11:48 +08:00
|
|
|
fakereq = {
|
|
|
|
query: {},
|
|
|
|
params: {
|
|
|
|
user: req.params.user
|
|
|
|
},
|
|
|
|
headers: _.clone(req.headers),
|
2015-04-01 21:11:58 +08:00
|
|
|
context: _.clone(req.context),
|
2015-01-20 23:56:06 +08:00
|
|
|
method: req.method,
|
|
|
|
res: res,
|
|
|
|
profiler: req.profiler
|
|
|
|
};
|
2015-04-27 20:47:58 +08:00
|
|
|
self.serverOptions.setDBParams(cdbuser, fakereq.params, this);
|
2015-01-20 23:56:06 +08:00
|
|
|
},
|
|
|
|
function setApiKey(err){
|
|
|
|
if ( req.profiler ) req.profiler.done('setDBParams');
|
|
|
|
if ( err ) throw err;
|
|
|
|
self.metadataBackend.getUserMapKey(cdbuser, this);
|
|
|
|
},
|
|
|
|
function createLayergroup(err, val) {
|
|
|
|
if ( req.profiler ) req.profiler.done('getUserMapKey');
|
|
|
|
if ( err ) throw err;
|
|
|
|
fakereq.params.api_key = val;
|
|
|
|
self.app.createLayergroup(layergroup, fakereq, this);
|
|
|
|
},
|
2015-01-22 22:40:40 +08:00
|
|
|
function prepareResponse(err, layergroup) {
|
|
|
|
if ( err ) {
|
2015-06-06 01:38:38 +08:00
|
|
|
return callback(err, { errors: [''+err] });
|
2015-01-20 23:56:06 +08:00
|
|
|
}
|
|
|
|
var tplhash = self.templateMaps.fingerPrint(template).substring(0,8);
|
2015-01-22 22:40:40 +08:00
|
|
|
layergroup.layergroupid = cdbuser + '@' + tplhash + '@' + layergroup.layergroupid;
|
2015-06-18 07:13:33 +08:00
|
|
|
res.header('X-Layergroup-Id', layergroup.layergroupid);
|
2015-01-23 23:37:38 +08:00
|
|
|
|
2015-04-27 20:52:36 +08:00
|
|
|
self.surrogateKeysCache.tag(res, new NamedMapsCacheEntry(cdbuser, template.name));
|
2015-01-23 23:37:38 +08:00
|
|
|
|
2015-06-06 01:38:38 +08:00
|
|
|
callback(null, layergroup);
|
|
|
|
}
|
2015-01-20 23:56:06 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2015-04-27 21:01:49 +08:00
|
|
|
NamedMapsController.prototype.finish_instantiation = function(err, response, res) {
|
2015-01-20 23:56:06 +08:00
|
|
|
if (err) {
|
|
|
|
var statusCode = 400;
|
2015-06-06 01:38:38 +08:00
|
|
|
response = { errors: [''+err] };
|
2015-01-20 23:56:06 +08:00
|
|
|
if ( ! _.isUndefined(err.http_status) ) {
|
|
|
|
statusCode = err.http_status;
|
|
|
|
}
|
|
|
|
this.app.sendError(res, response, statusCode, 'POST INSTANCE TEMPLATE', err);
|
|
|
|
} else {
|
|
|
|
this.app.sendResponse(res, [response, 200]);
|
|
|
|
}
|
|
|
|
};
|
2015-01-21 01:14:10 +08:00
|
|
|
|
2015-04-23 17:47:01 +08:00
|
|
|
function finishFn(app, res, description, okResponse) {
|
|
|
|
return function finish(err, response){
|
|
|
|
var statusCode = 200;
|
|
|
|
if (err) {
|
|
|
|
statusCode = 400;
|
2015-06-06 01:38:38 +08:00
|
|
|
response = { errors: ['' + err] };
|
2015-04-23 17:47:01 +08:00
|
|
|
if ( ! _.isUndefined(err.http_status) ) {
|
|
|
|
statusCode = err.http_status;
|
|
|
|
}
|
|
|
|
app.sendError(res, response, statusCode, description, err);
|
|
|
|
} else {
|
|
|
|
app.sendResponse(res, okResponse || [response, statusCode]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:01:53 +08:00
|
|
|
function ifUnauthenticated(authenticated, description) {
|
|
|
|
if (authenticated !== 1) {
|
|
|
|
var err = new Error(description);
|
|
|
|
err.http_status = 403;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:05:52 +08:00
|
|
|
function ifInvalidContentType(req, description) {
|
|
|
|
if ( ! req.headers['content-type'] || req.headers['content-type'].split(';')[0] != 'application/json' ) {
|
|
|
|
throw new Error(description);
|
|
|
|
}
|
|
|
|
}
|