Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support explicit ttl #93

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
27 changes: 24 additions & 3 deletions apikeys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var url = require("url");
var rs = require("jsrsasign");
var fs = require("fs");
var path = require("path");
<<<<<<< HEAD
const memoredpath = path.resolve(__dirname,'../../..')+'/third_party/memored/memored';
=======
const memoredpath = '../third_party/memored/index';
>>>>>>> 7d0c3c0
var cache = require(memoredpath);
var JWS = rs.jws.JWS;
var requestLib = require("request");
Expand All @@ -23,6 +27,8 @@ acceptField.alg = acceptAlg;

var productOnly;
var cacheKey = false;
var cacheKeyTTL = 60000; //set default cache TTL to 1 minute
var cacheSize = 100; //default cache size

module.exports.init = function(config, logger, stats) {

Expand All @@ -36,6 +42,16 @@ module.exports.init = function(config, logger, stats) {
var keepApiKey = config.hasOwnProperty('keep-api-key') ? config['keep-api-key'] : false;
//cache api keys
cacheKey = config.hasOwnProperty("cacheKey") ? config.cacheKey : false;
//cache ttl
<<<<<<< HEAD
cacheKeyTTL = config.hasOwnProperty("cacheKeyTTL") ? config.cacheKeyTTL : cacheKeyTTL;
//cache size
cacheSize = config.hasOwnProperty("cacheSize") ? config.cacheSize : cacheSize;
=======
cacheKeyTTL = config.hasOwnProperty("cacheKeyTTL") ? config.cacheKeyTTL : 60000;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't respect the default cacheKeyTTL value prev set on line 25 - avoid magic numbers. same for cacheSize

//cache size
cacheSize = config.hasOwnProperty("cacheSize") ? config.cacheSize : 100;
>>>>>>> 7d0c3c0
//set grace period
var gracePeriod = config.hasOwnProperty("gracePeriod") ? config.gracePeriod : 0;
acceptField.gracePeriod = gracePeriod;
Expand Down Expand Up @@ -192,16 +208,21 @@ module.exports.init = function(config, logger, stats) {
req.token = decodedToken;

var authClaims = _.omit(decodedToken, PRIVATE_JWT_VALUES);
req.headers["x-authorization-claims"] = new Buffer(JSON.stringify(authClaims)).toString("base64");
req.headers["x-authorization-claims"] = Buffer.from(JSON.stringify(authClaims)).toString("base64");

if (apiKey) {
var cacheControl = req.headers["cache-control"] || "no-cache";
if (cacheKey || (cacheControl && cacheControl.indexOf("no-cache") < 0)) { // caching is toFixed
// default to now (in seconds) + 30m if not set
decodedToken.exp = decodedToken.exp || +(((Date.now() / 1000) + 1800).toFixed(0));
//apiKeyCache[apiKey] = decodedToken;
cache.store(apiKey, decodedToken);
debug("api key cache store", apiKey);
cache.size(function(err, sizevalue) {
if (!err && sizevalue != null && sizevalue < cacheSize) {
cache.store(apiKey, decodedToken, cacheKeyTTL);
} else {
debug('too many keys in cache; ignore storing token');
}
});
} else {
debug("api key cache skip", apiKey);
}
Expand Down
2 changes: 1 addition & 1 deletion monitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ module.exports.init = function(config /*, logger, stats */) {
next();
}
};
}
}
Loading