a simple api access token support count and ttl, which base on nodejs.
npm install access-token-api
Single Process
`nodejs`
var accessTokenApi = require('access-token-api');
var TokenApi = new accessTokenApi({
webTokenVarName:'encrypt_api_tokenStr',//default to encrypt_api_tokenStr
webInject:function(){
//if you want to custom you webtoken inject in hmlt , you can do in this function.
}
});
`web javascript`
//get the token
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',//default to encrypt_api_tokenStr
webInject:function(){
//if you want to custom you webtoken inject in hmlt , you can do in this function.
}
});
TokenApi.issue(10,10,function(err,token){
//todo
});
TokenApi.verify('token',function(err,count){
//todo
});
web page can get token by window[webTokenVarName] , default to window.encrypt_api_tokenStr
issue
issue random token.
/**
* [issuse token]
* @param {[number]} [token ttl, default unit is second]
* @param {[number]} [token avalid count]
* @return {[string]} [return token]
*/
TokenApi.issue(10,5,function(err,data){
console.log(err,data);
})
pass
verify and decline token times, when the token is valid.
TokenApi.pass('token',function(err,data){
console.log(err,data);//err ,data: {code:0, passed: true, count: 2}, when code is zero and passed is true, token is valid.
})
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
npm test
//coverage
npm run cov
=============================== Coverage summary ===============================
Statements : 95.7% ( 89/93 )
Branches : 80.95% ( 34/42 )
Functions : 100% ( 17/17 )
Lines : 95.7% ( 89/93 )
================================================================================