1.0.4 • Published 6 years ago

jwt-token-encrypt v1.0.4

Weekly downloads
89
License
MIT
Repository
github
Last release
6 years ago

jwt-token-encrypt

Known Vulnerabilities

This module allows you to generate JSON Web-Tokens with some elements of the data encrypted and read it in a very simple way, without worry too much about encryption.

Install

npm install 'jwt-token-encrypt' --save

Usage

import * as jwtEncrypt from 'jwt-token-encrypt';

Above is a breaking change as before import was done with defaultExport !

Version < "1.0.3"

Creating JWT

// Data that will be publicly available
const publicData = {
    role: "user"
};

// Data that will only be available to users who know encryption details.
const privateData = {
    email: "user",
    bank: "HSBC",
    pin: "1234",
};

// Encryption settings
const encryption = {
    key: 'AAAAAAAAAAAAAA',
    algorithm: 'aes-256-cbc',
  };

// JWT Settings
const jwtDetails = {
    secret: '1234567890', // to sign the token
    // Default values that will be automatically applied unless specified.
    // algorithm: 'HS256',
    // expiresIn: '12h',
    // notBefore: '0s',
    // Other optional values
    key: 'ThisIsMyAppISS',// is used as ISS but can be named iss too
};

const token = await jwtEncrypt.generateJWT(
      jwtDetails,
      publicData,
      encryption,
      privateData
    );

Reading JWT

// Encryption settings
const encryption = {
    key: 'AAAAAAAAAAAAAA',
    algorithm: 'aes-256-cbc',
  };

const decrypted = jwtEncrypt.readJWT(token, encryption);

Token Content

E.g.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJOS0luVldoQjFyVkxDd3hsdE1OdWlVQzZoOVV1ZEFiaSIsImRhdGEiOnsicHVibGljIjp7ImRhdGExIjoxLCJkYXRhMiI6MiwiZGF0YTMiOjN9LCJlbmNEYXRhIjoiYjliM2QyNDdkNTk4ZTlkODczOTM2NTI4MWVmN2ExZTkifSwiaWF0IjoxNTExMTk5MDg0LCJleHAiOjE1MTEyNDIyODR9.KzfcIY95RR7aPYKn5EcXZYvETDCGZIJ91p7IfXCiClw

Once decoded will hold below content jwt.io

{
    iss: 'NKInVWhB1rVLCwxltMNuiUC6h9UudAbi',
    data: {
      public: {
            data1: 1,
            data2: 2,
            data3: 3
        },
      encData: '5fb8ed70a3864cbd97b25cc8ca2c0bc7',
    },
  },

As you can see private data:

privateData = {
   email: "user",
   bank: "HSBC",
   pin: "1234",
}

is got encripted and respresented with:

{
    ....
    encData: '5fb8ed70a3864cbd97b25cc8ca2c0bc7',
    ....
}

To change encData label you need to pass extra parameter to generateJWT method: e.g.

const token = await jwtEncrypt.generateJWT(
     jwtDetails,
     publicData,
     encryption,
     privateData,
     'session',
   );

will result in having:

{
    iss: 'NKInVWhB1rVLCwxltMNuiUC6h9UudAbi',
    data: {
      public: {
            data1: 1,
            data2: 2,
            data3: 3
        },
      session: '5fb8ed70a3864cbd97b25cc8ca2c0bc7',
    },
  },

also to read you will need to pass new filed name

e.g.

// Encryption settings
const encryption = {
   key: 'AAAAAAAAAAAAAA',
   algorithm: 'aes-256-cbc',
 };

const decrypted = jwtEncrypt.readJWT(token, encryption, 'session');
1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago