identity
Version:
Identity client
49 lines (38 loc) • 1.53 kB
JavaScript
exports.application = function(api, next){
//allow param access_token for all actions
api.params.globalSafeParams.push('access_token');
api.actions.addPreProcessor(function(connection, actionTemplate, next){
var access_token = connection.params.access_token;
if(access_token){
connection.session_tmp = true;
api.cache.load('application:' + access_token, function(err, app_user){
if(app_user && app_user.permissions){
connection.user = app_user;
connection.user.application = true;
next(connection, true);
}else{
//check the access_token and get the corresponding user
api.identity.application('identity', access_token).get('user', null, function(err, resp){
if(resp.data){
if(resp.data.properties && resp.data.properties.is_application){
if(resp.data.permissions.length > 0){
connection.user = resp.data;
return api.cache.save('application:' + access_token, resp.data, 1000*60*60*23, function(){
next(connection, true);
});
}
}else{
connection.user = resp.data;
}
}
next(connection, true);
});
}
});
}else{
//just go on...
next(connection, true);
}
}, 11);
next();
};