Code Monkey home page Code Monkey logo

token-generator's Introduction

token-generator

Test Status Dependency Status

an offline token generator and validator

What is it and what does it do?

It works on top of one dynamic variable, an expired timestamp, we simply create two things from it: an hash part and an obfuscated timestamp.

So, whenever you hit .generate, it will render a different token that could be always validated (even on different processes, or different nodes) as since you configure it with same salt and timestampMap.

Seems complex? ( If not, you really should dig on this and help us to make it even better )

Why should i use it?

  • It's Open-Source and free to use;
  • You could contribute to it;
  • It's extendable, because it's built on top of findhit-class;
  • You don't have to maintain a data source such as a database table to store tokens, each token carries its own check;
  • It's secure, and you could increase security by providing your own hash method;
  • We are also using it, if we trust on it, you should do it also!

Installation

    npm i --save token-generator

Usage

Token Generator is designed to have a simple impementation. You just need to create an TG with your own options just like the example above:

var TokenGenerator = require( 'token-generator' )({
        salt: 'your secret ingredient for this magic recipe',
        timestampMap: 'abcdefghij', // 10 chars array for obfuscation proposes
    });

Then you could generate and validate tokens simply as:

var token = TokenGenerator.generate();

And check with:

if ( TokenGenerator.isValid( token ) ) {
    // ...
} else {
    // ...
}

// You could also use
// .isInvalid
// .isntValid
// .isntInvalid

If you catch an invalid token, you could check why it is invalid by validating manually with .validate

try{
    TokenGenerator.validate( token )
} catch ( error ) {

    // For security proposes, error will always be 'Invalid or expired token'
    console.error( error )

    // But you can dig it up by checking `.parent` Error
    console.error( error.parent );

}

Have you liked this?

Please, star this repo and if you find any issue don't hesitate to fill it or to provide a Pull Request. ๐Ÿ‘

token-generator's People

Watchers

James Cloos avatar Tommaso Allevi avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.