Token manager is a module aimed to create, manage and expire access tokens. The main use case is to validate authentication tokens.
npm install token-manager
var tm = ;var tokenManager = ;var myToken =clientId: 'some_client' //set client idtokenString: 'dG9rZW5tYW5hZ2VyCgo=' //set token contentexpiration: 10 * 60 * 1000 //set the expiration time, in millisecondsroles: 'admin';tokenManager; //register the token/* ... */tokenManager; //restores the token and refreshes its expiration time.
Every time you create a Token object, it's lifecycle starts, set to expire after a delimited amount of time. When a given token is checked with TokenManager.get() method, it's lifecycle restarts.
If the expiration time for a given token has passed without any refresh, the token is set to expired, raising an error the next time it's requested.
clientId: 'id'tokenString: 'abcd'expiration: 1000roles: 'client' 'admin';
** clientId: A String containing the client id. Required.
** tokenString: A String containing the token data. Required.
** expiration: The expiration time for the token in milliseconds. Required.
** roles: An array containing roles associated with the clientId. Optional.
Returns the given client id.
Returns the given token string
Returns the given roles. An empty array is returned if no role was given.
Immediately stops the token's lifecycle and expires it.
Refreshes the lifecycle of the token, meaning it stops the current expiration cycle, and start another one.
Returns true if the token contains a given role.
Saves the token in the registry. Returns nothing. Blocking.
Checks for the token in the registry. It also refreshes the token lifecycle. Blocking. Returns a token object
You can access a token-manager-server instance by using by using the client API provided out of the box:
var tm =var client =endpoint: ''timeout: 30000 // defaults to 10000;/* example of sending a token */client;/* example of getting a token */client;
Accepts a config object with the following fields:
endpoint: a string with the complete tokenManagerServer endpoint
timeout: in milliseconds. Defaults to 10000.
Saves the token in the server. Returns a data object containing the same tokenString and clientId of the token passed.
Recover a token from the server. The data object returned contains tokenString and clientId.
The recovered token has no info about expiration time.