npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

token-manager

v0.2.0

Published

Module to manage, expire and validate access tokens

Downloads

13

Readme

Token Manager

Token manager is a module aimed to create, manage and expire access tokens. The main use case is to validate authentication tokens.

Installation

npm install token-manager

QuickStart

var tm = require('token-manager');

var tokenManager = new tm.TokenManager();

var myToken = new tm.Token({
    clientId: 'some_client',                //set client id
    tokenString: 'dG9rZW5tYW5hZ2VyCgo=',    //set token content
    expiration: 10 * 60 * 1000,             //set the expiration time, in milliseconds
    roles: ['admin']
});

tokenManager.put(myToken);                  //register the token

/* ... */

tokenManager.get('dG9rZW5tYW5hZ2VyCgo=');   //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.

API

Token

  • constructor
new Token({
    clientId: 'id',
    tokenString: 'abcd',
    expiration: 1000,
    roles: ['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.

  • getClientId()

Returns the given client id.

  • getTokenString()

Returns the given token string

  • getRoles()

Returns the given roles. An empty array is returned if no role was given.

  • expire()
token.expire();

Immediately stops the token's lifecycle and expires it.

  • visit()
token.visit();

Refreshes the lifecycle of the token, meaning it stops the current expiration cycle, and start another one.

  • is(role)
token.is('admin')

Returns true if the token contains a given role.

TokenManager

  • constructor
new TokenManager();
  • put(token);
tokenManager.put( aToken );

Saves the token in the registry. Returns nothing. Blocking.

  • get(tokenString);
tokenManager.get( tokenString );

Checks for the token in the registry. It also refreshes the token lifecycle. Blocking. Returns a token object

Integration with token-manager-server

You can access a token-manager-server instance by using by using the client API provided out of the box:

    var tm = require('token-manager')

    var client = new tm.TokenManagerClient({
        endpoint: 'http://yourserver/token',
        timeout: 30000                          // defaults to 10000
    });

    /* example of sending a token */
    client.put( new tm.Token({
        clientId: 'jeff',
        tokenString: 'abcd',
        expiration: 30000
    }), function(error, data){
        console.log('posted the token')
    });

    /* example of getting a token */
    client.get( 'abc', function(error, data){
        console.log('clientId is: ' + data.getClientId());
    });

TokenManagerClient

  • constructor
new TokenManagerClient(config);

Accepts a config object with the following fields:

endpoint: a string with the complete tokenManagerServer endpoint timeout: in milliseconds. Defaults to 10000.

  • put(token, callback);
tokenManagerClient.put( aToken, function(error, data){
    if(error) throw error;
    console.log(data);
});

Saves the token in the server. Returns a data object containing the same tokenString and clientId of the token passed.

  • get(tokenString, callback);
tokenManagerClient.get( tokenString, function(error, data){
    if(error) throw error;
    console.log(data);
});

Recover a token from the server. The data object returned contains tokenString and clientId.

The recovered token has no info about expiration time.