2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-04-07 00:20:33 +08:00
|
|
|
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;
|
2018-03-23 21:13:27 +08:00
|
|
|
const layergroupToken = LayergroupToken.parse(req.params.token);
|
2017-09-22 20:05:40 +08:00
|
|
|
|
2018-03-02 01:09:49 +08:00
|
|
|
res.locals.token = layergroupToken.token;
|
|
|
|
res.locals.cache_buster = layergroupToken.cacheBuster;
|
2017-09-22 20:05:40 +08:00
|
|
|
|
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);
|
2017-09-22 20:05:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 01:09:49 +08:00
|
|
|
return next();
|
|
|
|
};
|
2017-09-22 20:05:40 +08:00
|
|
|
};
|