Skip to content

Commit

Permalink
Merge pull request #543 from joola/feature/#542
Browse files Browse the repository at this point in the history
#542 added routing short term cache.
  • Loading branch information
itayw committed Jun 15, 2014
2 parents 3fb351b + d73fff5 commit fb54167
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/common/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ auth.validateToken = function (req, token, APIToken, callback) {
});

if (APIToken) {
var cachedUser = joola.memory.get('APIToken:' + APIToken);
if (joola.memory.get('APIToken:' + APIToken))
return callback(null, cachedUser);

joola.logger.trace({category: 'security'}, 'Verifying API Token [' + APIToken + ']');
joola.dispatch.users.verifyAPIToken({user: joola.SYSTEM_USER}, APIToken, function (err, user) {
if (err)
Expand All @@ -199,14 +203,15 @@ auth.validateToken = function (req, token, APIToken, callback) {
});

//return auth.generateToken(user, callback);
joola.memory.set('APIToken:' + APIToken, user, 1000);
return callback(null, user);
});
}
else {
if (typeof token === 'object')
token = token._;

var cached = joola.memory.get(token);
var cached = joola.memory.get('token:' + token);
if (cached)
return process.nextTick(function () {
return callback(null, cached);
Expand All @@ -227,7 +232,7 @@ auth.validateToken = function (req, token, APIToken, callback) {
//all is good
_token.user = JSON.parse(_token.user);
//extend the token
joola.memory.set(_token._, _token);
joola.memory.set('token:' + _token._, _token);
return auth.extendToken(req, _token, callback);
}
else
Expand Down Expand Up @@ -255,6 +260,11 @@ auth.validateRoute = function (req, modulename, action, callback) {
modulename = req;
}


var cached = joola.memory.get('validateRoute:' + modulename + ':' + action);
if (cached)
return callback(null, cached);

var module;

if (!modulename)
Expand Down Expand Up @@ -284,6 +294,7 @@ auth.validateRoute = function (req, modulename, action, callback) {
return callback({code: 404, message: 'Not Found'});
});

joola.memory.set('validateRoute:' + modulename + ':' + action, _action);
return process.nextTick(function () {
return callback(null, _action);
});
Expand Down Expand Up @@ -335,6 +346,11 @@ auth.validateAction = function (action, req, res, callback) {

if (typeof req.user === 'string')
req.user = JSON.parse(req.user);

var cached = joola.memory.get('validateAction:' + action.name + ':' + req.user.username);
if (cached)
return callback(null, true);

var userPermissions = [];
if (req.user.roles) {
joola.workspaces.get({user: req.user}, req.user.workspace, function (err, workspace) {
Expand Down Expand Up @@ -373,6 +389,7 @@ auth.validateAction = function (action, req, res, callback) {
req.guestAllowedAction = true;

req.user.permissions = userPermissions;
joola.memory.set('validateAction:' + action.name + ':' + req.user.username, true);
return process.nextTick(function () {
return callback(null, true);
});
Expand Down

0 comments on commit fb54167

Please sign in to comment.