Windshaft-cartodb/lib/api/middlewares/layergroup-token.js

35 lines
1.1 KiB
JavaScript
Raw Normal View History

'use strict';
const LayergroupToken = require('../../models/layergroup-token');
2018-03-02 20:29:30 +08:00
const authErrorMessageTemplate = function (signer, user) {
return `Cannot use map signature of user "${signer}" on db of user "${user}"`;
};
2018-03-02 01:09:49 +08:00
module.exports = function layergroupToken () {
2018-03-02 20:08:57 +08:00
return function layergroupTokenMiddleware (req, res, next) {
2018-03-02 20:29:30 +08:00
const user = res.locals.user;
const layergroupToken = LayergroupToken.parse(req.params.token);
2018-03-02 01:09:49 +08:00
res.locals.token = layergroupToken.token;
res.locals.cache_buster = layergroupToken.cacheBuster;
if (layergroupToken.templateHash) {
res.locals.templateHash = layergroupToken.templateHash;
}
2018-03-02 01:09:49 +08:00
if (layergroupToken.signer) {
res.locals.signer = layergroupToken.signer;
2018-03-02 20:29:30 +08:00
2018-03-02 20:14:02 +08:00
if (res.locals.signer !== user) {
2018-03-02 20:29:30 +08:00
const err = new Error(authErrorMessageTemplate(res.locals.signer, user));
2018-03-02 01:09:49 +08:00
err.type = 'auth';
2019-10-22 01:07:24 +08:00
err.http_status = (req.query && req.query.callback) ? 200 : 403;
2018-03-02 01:09:49 +08:00
return next(err);
}
}
2018-03-02 01:09:49 +08:00
return next();
};
};