Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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