jsonwebtoken_1
v4.0.1
Published
JSON Web Token implementation (symmetric and asymmetric)
Downloads
18
Readme
Installation
$ npm install jsonwebtoken_1
Usage
jwt.sign(payload, secretOrPrivateKey, [options, callback])
Import in node.js file
const jwt = require('jsonwebtoken_1')
//or
import jwt from ('jsonwebtoken_1')
---must specify the algorithm as 'HS256' for Symmetric and 'RS256' or 'ES256' for asymmetric in options.
jwt.sign(payload, secretOrPrivateKey, {options}) secretOrPrivateKey is a string (utf-8 encoded), buffer, object, or KeyObject containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA. In case of a private key with passphrase an object { key, passphrase } can be used (based on crypto documentation), in this case be sure you pass the algorithm. When signing with RSA algorithms the minimum modulus length is 2048 except when the allowInsecureKeySizes option is set to true. Private keys below this size will be rejected with an error.
Sign with (HMAC SHA256)
const jwt = require('jsonwebtoken_1');
var token = jwt.sign({name: 'suman' }, 'secret',{ expiresIn : '15m',algorithm:'HS256'});
sign with RSA SHA256
const jwt = require('jsonwebtoken_1');
var privateKey = fs.readFileSync('private.key');// get private key should be pem file
var token = jwt.sign({ name: 'suman' }, privateKey, { expiresIn : '15m',algorithm:'RS256'});
sign with ES256
const jwt = require('jsonwebtoken_1');
var privateKey = fs.readFileSync('private.key');// get private key should be pem file
var token = jwt.sign({ name: 'suman' }, privateKey, { expiresIn : '15m',algorithm:'ES256'});
verify a token symmetric
jwt.verify(token, 'secret',function(err, decoded) {
console.log(decoded.name) // suman
});
verify a token asymmetric
var publickey = fs.readFileSync('public.key'); // get public key should be pem file
jwt.verify(token, publickey,function(err, decoded) {
console.log(decoded.name) // suman
});