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
15 changes: 13 additions & 2 deletions apikeys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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 @@ -35,6 +37,10 @@ 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
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;
//set grace period
var gracePeriod = config.hasOwnProperty("gracePeriod") ? config.gracePeriod : 0;
acceptField.gracePeriod = gracePeriod;
Expand Down Expand Up @@ -198,8 +204,13 @@ module.exports.init = function(config, logger, stats) {
// 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
25 changes: 20 additions & 5 deletions oauth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ map.setup({
purgeInterval: 10000
});

var tokenCacheSize = 100;
var tokenCacheSize = 100; //default cache size for access tokens
var tokenCacheTTL = 60000; //set default token cache TTL to 1 minute
var cacheKeyTTL = 60000; //set default apikey cache TTL to 1 minute
var cacheSize = 100; //default cache size for api keys

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

Expand All @@ -43,6 +46,10 @@ module.exports.init = function(config, logger, stats) {
var apiKeyHeaderName = config.hasOwnProperty('api-key-header') ? config['api-key-header'] : 'x-api-key';
var keepAuthHeader = config.hasOwnProperty('keep-authorization-header') ? config['keep-authorization-header'] : false;
cacheKey = config.hasOwnProperty('cacheKey') ? config.cacheKey : false;
//cache ttl
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 35 - avoid magic numbers. same for other cache vars

//cache size
cacheSize = config.hasOwnProperty("cacheSize") ? config.cacheSize : 100;
//set grace period
var gracePeriod = config.hasOwnProperty('gracePeriod') ? config.gracePeriod : 0;
acceptField.gracePeriod = gracePeriod;
Expand All @@ -59,6 +66,8 @@ module.exports.init = function(config, logger, stats) {
}
//token cache settings
tokenCache = config.hasOwnProperty('tokenCache') ? config.tokenCache : false;
//token cache ttl
tokenCacheTTL = config.hasOwnProperty("tokenCacheTTL") ? config.cacheKeyTTL : 60000;
//max number of tokens in the cache
tokenCacheSize = config.hasOwnProperty('tokenCacheSize') ? config.tokenCacheSize : 100;
//
Expand Down Expand Up @@ -198,6 +207,7 @@ module.exports.init = function(config, logger, stats) {
var isValid = false;
var oauthtoken = token && token.token ? token.token : token;
var decodedToken = JWS.parse(oauthtoken);

if (tokenCache == true) {
debug('token caching enabled')
map.read(oauthtoken, function(err, tokenvalue) {
Expand Down Expand Up @@ -230,8 +240,8 @@ module.exports.init = function(config, logger, stats) {
} else {
if (tokenvalue == null || tokenvalue == undefined) {
map.size(function(err, sizevalue) {
if (!err && sizevalue != null && sizevalue < 100) {
map.store(oauthtoken, oauthtoken);
if (!err && sizevalue != null && sizevalue < tokenCacheSize) {
map.store(oauthtoken, oauthtoken, tokenCacheTTL);
} else {
debug('too many tokens in cache; ignore storing token');
}
Expand Down Expand Up @@ -288,8 +298,13 @@ module.exports.init = function(config, logger, stats) {
// 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
9 changes: 6 additions & 3 deletions oauthv2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ map.setup({
purgeInterval: 10000
});

var tokenCacheSize = 100;
var tokenCacheSize = 100; //default cache size for access tokens
var tokenCacheTTL = 60000; //set default token cache TTL to 1 minute

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

Expand All @@ -50,6 +51,8 @@ module.exports.init = function(config, logger, stats) {
}
//token cache settings
tokenCache = config.hasOwnProperty('tokenCache') ? config.tokenCache : false;
//token cache ttl
tokenCacheTTL = config.hasOwnProperty("tokenCacheTTL") ? config.cacheKeyTTL : 60000;
Copy link
Contributor

@relloller relloller Feb 4, 2019

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 tokenCacheTTL value prev set above avoid magic numbers. same for other cache vars

//max number of tokens in the cache
tokenCacheSize = config.hasOwnProperty('tokenCacheSize') ? config.tokenCacheSize : 100;
//
Expand Down Expand Up @@ -129,8 +132,8 @@ module.exports.init = function(config, logger, stats) {
} else {
if (tokenvalue == null || tokenvalue == undefined) {
map.size(function(err, sizevalue) {
if (!err && sizevalue != null && sizevalue < 100) {
map.store(oauthtoken, oauthtoken);
if (!err && sizevalue != null && sizevalue < tokenCacheSize) {
map.store(oauthtoken, oauthtoken, tokenCacheTTL);
} else {
debug('too many tokens in cache; ignore storing token');
}
Expand Down