@webgap/token
Version:
WebGAP Tokenizer module. Handles JWT encoding / decoding features.
87 lines (64 loc) • 2.96 kB
Markdown
[](https://travis-ci.org/webgap/token)
[](https://codeclimate.com/github/webgap/token/coverage)
[](https://codeclimate.com/github/webgap/token)
[](https://gemnasium.com/webgap/token)
[](https://www.npmjs.com/package/@webgap/token)
[](https://www.npmjs.com/package/@webgap/token)
WebGAP Tokenizer module. Handles JWT encoding / decoding features. Handles authentication tokens creation.
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).
```bash
npm install @webgap/token --save
```
```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
```
```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
```
Apache License, Version 2.0