1.0.1 • Published 3 years ago
my-first-library-jtw-test v1.0.1
jwt-lib
Install
Install with npm
npm install jwt-lib
Generate public/private
NOTE:
if issued within the same folder as the test file and only specify the filename(s), otherwise provide full path.
ssh-keygen -t rsa -b 4096 -m PEM -f RS256.key
Usage
const jwtSign = require('my-jwt-module/security/jwt/jwtSign');
const jwtVerify = require('my-jwt-module/security/jwt/jwtVerify');
const Demo = () => {
const header = {
alg: 'RS256',
typ: 'JWT',
};
const payload = {
sub: '1234567890',
id: 83028302,
iat: 1516239022,
exp: 9876543219,
iss: 'MY TENANT',
};
const privateKeyPath = 'RS256.key';
const publicKeyPath = 'RS256.key.pub';
const validAlgorithm = ['RS256'];
const trustedTenant = 'MY TENANT';
const signedJsonWebToken = jwtSign(
header,
payload,
privateKeyPath,
trustedTenant
);
if (signedJsonWebToken.error) return signedJsonWebToken.error;
if (signedJsonWebToken.e) return signedJsonWebToken.e;
console.log(`\nSuccessfully Signed JWT:\n\n${signedJsonWebToken}\n`);
const validateSignedJsonWebToken = jwtVerify(
signedJsonWebToken,
validAlgorithm,
publicKeyPath
);
if (validateSignedJsonWebToken.error) return validateSignedJsonWebToken.error;
if (validateSignedJsonWebToken.e) return validateSignedJsonWebToken.e;
return validateSignedJsonWebToken;
};
console.log(Demo());