2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-07-11 01:10:55 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param metadataBackend
|
|
|
|
* @param options
|
|
|
|
* @constructor
|
2018-04-10 16:16:07 +08:00
|
|
|
* @type {UserLimitsBackend}
|
2015-07-11 01:10:55 +08:00
|
|
|
*/
|
2019-10-22 01:07:24 +08:00
|
|
|
function UserLimitsBackend (metadataBackend, options) {
|
2015-07-11 01:10:55 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.options = options || {};
|
|
|
|
this.options.limits = this.options.limits || {};
|
2018-02-28 20:11:18 +08:00
|
|
|
|
|
|
|
this.preprareRateLimit();
|
2015-07-11 01:10:55 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
module.exports = UserLimitsBackend;
|
2015-07-11 01:10:55 +08:00
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
UserLimitsBackend.prototype.getRenderLimits = function (username, apiKey, callback) {
|
2015-07-11 01:10:55 +08:00
|
|
|
var self = this;
|
2017-07-06 01:08:19 +08:00
|
|
|
|
|
|
|
var limits = {
|
|
|
|
cacheOnTimeout: self.options.limits.cacheOnTimeout || false,
|
|
|
|
render: self.options.limits.render || 0
|
|
|
|
};
|
|
|
|
|
2017-08-10 21:58:25 +08:00
|
|
|
self.getTimeoutRenderLimit(username, apiKey, function (err, timeoutRenderLimit) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2015-07-11 01:10:55 +08:00
|
|
|
}
|
2017-08-10 21:58:25 +08:00
|
|
|
|
|
|
|
if (timeoutRenderLimit && timeoutRenderLimit.render) {
|
|
|
|
if (Number.isFinite(timeoutRenderLimit.render)) {
|
|
|
|
limits.render = timeoutRenderLimit.render;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, limits);
|
|
|
|
});
|
2017-07-06 01:08:19 +08:00
|
|
|
};
|
2015-07-11 01:10:55 +08:00
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
UserLimitsBackend.prototype.getTimeoutRenderLimit = function (username, apiKey, callback) {
|
2018-07-30 23:21:19 +08:00
|
|
|
isAuthorized(this.metadataBackend, username, apiKey, (err, authorized) => {
|
2018-07-30 23:15:04 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
2018-07-30 23:09:34 +08:00
|
|
|
}
|
2017-07-18 17:56:54 +08:00
|
|
|
|
2018-07-30 23:21:19 +08:00
|
|
|
this.metadataBackend.getUserTimeoutRenderLimits(username, (err, timeoutRenderLimit) => {
|
2017-07-06 01:08:19 +08:00
|
|
|
if (err) {
|
2018-07-30 23:18:35 +08:00
|
|
|
return callback(err);
|
2017-07-06 01:08:19 +08:00
|
|
|
}
|
2017-07-18 17:56:54 +08:00
|
|
|
|
2018-07-31 21:19:41 +08:00
|
|
|
return callback(
|
2018-07-30 23:18:35 +08:00
|
|
|
null,
|
|
|
|
{ render: authorized ? timeoutRenderLimit.render : timeoutRenderLimit.renderPublic }
|
|
|
|
);
|
2018-07-30 23:09:34 +08:00
|
|
|
});
|
2018-07-30 23:16:27 +08:00
|
|
|
});
|
2015-07-11 01:10:55 +08:00
|
|
|
};
|
2018-02-28 20:11:18 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
function isAuthorized (metadataBackend, username, apiKey, callback) {
|
2018-07-30 23:15:04 +08:00
|
|
|
if (!apiKey) {
|
|
|
|
return callback(null, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
metadataBackend.getUserMapKey(username, function (err, userApiKey) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null, userApiKey === apiKey);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
UserLimitsBackend.prototype.preprareRateLimit = function () {
|
2018-02-28 20:11:18 +08:00
|
|
|
if (this.options.limits.rateLimitsEnabled) {
|
2018-03-03 21:46:39 +08:00
|
|
|
this.metadataBackend.loadRateLimitsScript();
|
2018-02-28 20:11:18 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-10 16:16:07 +08:00
|
|
|
UserLimitsBackend.prototype.getRateLimit = function (user, endpointGroup, callback) {
|
2018-03-03 21:46:39 +08:00
|
|
|
this.metadataBackend.getRateLimit(user, 'maps', endpointGroup, callback);
|
2018-02-28 20:11:18 +08:00
|
|
|
};
|