UNPKG

@webgap/token

Version:

WebGAP Tokenizer module. Handles JWT encoding / decoding features.

87 lines (64 loc) 2.96 kB
# WebGAP Token [![Build Status](https://travis-ci.org/webgap/token.svg)](https://travis-ci.org/webgap/token) [![Test Coverage](https://codeclimate.com/github/webgap/token/badges/coverage.svg)](https://codeclimate.com/github/webgap/token/coverage) [![Code Climate](https://codeclimate.com/github/webgap/token/badges/gpa.svg)](https://codeclimate.com/github/webgap/token) [![Dependency Status](https://gemnasium.com/webgap/token.png)](https://gemnasium.com/webgap/token) [![NPM version](http://img.shields.io/npm/v/@webgap/token.svg?style=flat)](https://www.npmjs.com/package/@webgap/token) [![NPM downloads](http://img.shields.io/npm/dm/@webgap/token.svg?style=flat)](https://www.npmjs.com/package/@webgap/token) # README WebGAP Tokenizer module. Handles JWT encoding / decoding features. Handles authentication tokens creation. # Dependencies Loads default configuration using [**@webgap/configuration**](https://github.com/webgap/configuration).<br/> Handles Dates using [**moment**](https://github.com/moment/moment).<br/> Handles JWT using [**jwt-simple**](https://github.com/hokaccha/node-jwt-simple). Handles Authentication Token creation using [**hat**](https://github.com/substack/node-hat). # API ## Installation ```bash npm install @webgap/token --save ``` ## Usage ```javascript // jwt tokens var HS512Algorithm = require('../index').JsonWebToken.HS512Algorithm; var RS256Algorithm = require('../index').JsonWebToken.RS256Algorithm; var HSAlgorithm = new HS512Algorithm({ secret: 'a_secret_key' }); var HSDataEncoded = HSAlgorithm.encode('data'); HSAlgorithm.decode(HSDataEncoded); // decodes the data HSAlgorithm.isValid(HSDataEncoded); // true or false var RSAlgorithm = new RS256Algorithm({ privateKeyPath: __dirname + '/keys/token-private.ppk', publicKeyPath: __dirname + '/keys/token-public.key' }); var RSDataEncoded = RSAlgorithm.encode({data: { field: 0}}); RSAlgorithm.decode(RSDataEncoded); // decodes the data RSAlgorithm.isValid(RSDataEncoded); // true or false // authentication tokens var HatAuthAlgorithm = require('../index').AuthToken.HatAlgorithm; var hatAlgorithm = new HatAuthAlgorithm(); hatAlgorithm.encode(); // returns a token like: 92d522476573347b7fd9ebcfc02f3a501bb6db82c745bc92de9553d5ca3b1a41 ``` ## Options ```javascript // common options var options = { host: 'localhost', validityThreshold: 100 }; // HS algorithm uses a secret key options.secret: 'a_secret_key' // RS algorithm uses pub / private keys options.privateKeyPath: '/keys/token-private.ppk', options.publicKeyPath: '/keys/token-public.key' ``` If no 'options' is provided, the module will try to load default configurations from [**@webgap/configuration**](https://github.com/webgap/configuration) module. Default configurations for this module are as follows: ```bash SERVER.HOST - the hostname GENERAL.TOKEN.VALIDITY_TIME - the jwt token validity in milliseconds ``` # License Apache License, Version 2.0