access-token-api

A simple api access token support count and ttl,which base on nodejs. It can protect your api,prevent CSRF attacks, api called count with ttl.
install
npm install access-token-api
usage
Single Process
`nodejs`
var accessTokenApi = require('access-token-api');
var TokenApi = new accessTokenApi({
webTokenVarName:'encrypt_api_tokenStr',
webInject:function(html,token,callback){
var htmlEndIndex = html.indexOf('</html>');
var tokenScript = '<script>window.' + this.config.webTokenVarName + '=' + token + '</script>';
var prevHtml = html.substring(0, htmlEndIndex);
var nextHtml = html.substr(htmlEndIndex);
prevHtml += tokenScript;
prevHtml += nextHtml;
callback(null, prevHtml);
}
});
`web javascript`
window[webTokenVarName]
Multi Process
`nodejs`
var redis = require("redis"),
client = redis.createClient(6379,'localhost');
var accessTokenApi = require('access-token-api');
var TokenApi = new accessTokenApi({
storeConfig:{
get:function(key,callback){
client.GET(key,function(err,reply){
callback(err,reply);
});
},
set:function(key,data,ttl,callback){
client.PSETEX(key,ttl,data,function(err,reply){
callback(err,reply);
});
},
remove:function(key,callback){
client.DEL(key,function(err,data){
callback(err);
});
}
},
webTokenVarName:'encrypt_api_tokenStr',
webInject:function(){
}
});
TokenApi.issue(10,10,function(err,token){
});
TokenApi.verify('token',function(err,count){
});
storeConfig more params's config please to see store-ttl
web page can get token by window[webTokenVarName] , default to window.encrypt_api_tokenStr
API
issue
issue random token.
TokenApi.issue(10,5,function(err,data){
console.log(err,data);
})
TokenApi.issue(10,5,'givenToken',function(err,data){
console.log(err,data);
})
limit
limit function call times with ttl.
TokenApi.limit('apiname', 10, 5,function(err){
if(!err){
}
})
pass
verify and decline token times, when the token is valid.
TokenApi.pass('token',function(err,data){
console.log(err,data);
})
passPromise
verify and decline token times, when the token is valid.
TokenApi.passPromise('token').then(function(data) {
}).catch(function (err) {
})
verify
verify the token
TokenApi.verify('token',function(err,data){
console.log(err,data);
})
remove
remove the token
TokenApi.remove('token',function(err,data){
console.log(err,data);
})
decline
decline the token times
TokenApi.decline('token',function(err,data){
console.log(err);
})
webInject
custom web frontend way to inject token into page
TokenApi.webInject('html','token',function(err,html){
console.log(err);
})
test
1. redis-server
2. npm test
npm run cov
publish log
-
0.2.1
add api passPromise , other api support promise.
-
0.2.0
add api limit , which one key can call some times with ttl.
-
0.1.0
issuse api support issue given token.