Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dsninjas/jwt

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dsninjas/jwt

Jwt Helper for node.js projects

  • 1.0.0
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

ds-util-jwt

Jwt Helpers for node.js projects

This module makes it easy to issue and verify tokens in node.js applications.

  • Installation
  • Usage

Installation

  • Make sure that Node.js installed.
  • Install Node.js library with npm:
npm install @dsninjas/jwt
  • You can also install Node.js library with yarn:
yarn add @dsninjas/jwt

Usage

constructor(secret, audience, issuer)

The constructor helps you configure the jwt helper class.

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const jwtHelper = new JwtHelper(secret, audience, issuer);

The constructor takes the following options:

  1. secret (required): this is the secret used to create the .
  2. audience (required): this is used to check the audience.
  3. issuer (required): this is the isser of the token.

issue(payload, expirytime, subject, cb)

This method helps you to generate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.

With promise:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
const result = jwtHelper.issue({ message }, expireIn);
result.then((result) => {
    console.log(result);
}.catch((err) => {
    console.log(err);
});

With callback:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const message = 'Hello';
jwtHelper.issue({ message }, expireIn,'jwt-auth-token',(err,result) => {
    if (err) {
        console.log(err);
    }
    console.log(result);
}
  1. payload (required): this is the data you want to encrypt.
  2. expirytime (required): this is time the token woould expire.
  3. subject (optional): this is the subject of the jwt.
  4. cb (optional): this is the callback the woulld be called with the response.

Sample response:

{ 
  uuid: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14',
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U' 
}

verify(token, cb)

This method helps you to validate the token. It responses with a promise if you don't secify a callback, else returns the result with in callback.

With promise:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';

const result = jwtHelper.verify(token, expireIn);
result.then((result) => {
    console.log(result);
}.catch((err) => {
    console.log(err);
});

With callback:

const secret = 'qwerty';
const audience = 'example.com';
const issuer = 'example.com';
const expireIn = '7d';
const jwtHelper = new JwtHelper(secret, audience, issuer);
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiNmMkNU0iLCJpYXQiOjE1MTYxOTU3NTQsImV4cCI6MTUxNjgwMDU1NCwiYXVkIjoiaHR0cDovL251bi52Yy9zaW13aSIsImlzcyI6Imh0dHA6Ly9udW4udmMvc2ltd2kiLCJzdWIiOiJqd3QtYXV0aC10b2tlbiIsImp0aSI6IjRjNzFhZTlkLTc1NWEtNGQxOC05ZWVmLTRhNjAwYmZiMGMxNCJ9.khIFuxlb4mF8iZnRc_qbXfzjbzd0XNY7CKauSCcKB6U';

const result = jwtHelper.verify(token, expireIn,'jwt-auth-token',(err,result) => {
    if (err) {
        console.log(err);
    }
    console.log(result);
}
  1. token (required): this is the data you want to encrypt.
  2. cb (optional): this is the callback the woulld be called with the response.

Sample response:

{ 
  message: '6c$5M',
  iat: 1516195754,
  exp: 1516800554,
  aud: 'http://nun.vc/simwi',
  iss: 'http://nun.vc/simwi',
  sub: 'jwt-auth-token',
  jti: '4c71ae9d-755a-4d18-9eef-4a600bfb0c14' 
}

Keywords

FAQs

Package last updated on 17 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc