encrypt-jwt-payload
v1.0.1
Published
This package can be used for encrypting the JWT payload.
Downloads
1
Readme
Encrypt JWT Payload
Simple package for encrypting the JWT payload to hide sensitive data
You can define 2 types of data inside the payload: public and private. The private data will be encrypted while the public data will be present normally.
uses jsonwebtoken and object-path
INSTALL
## > With NPM
## npm i encrypt-jwt-payload
USAGE
First, define the data in the format to be used
// Data that will be publicly available
const publicData = <object with public data>
// Data that will only be available to users who know encryption details.
const privateData = <object with private data>
const iv = crypto.randomBytes(16); // generating a random buffer of 16 numbers
// Encryption settings
const encryption = {
key: <the key to be used for encryption of the payload>,
algorithm: <algorithm to be used for encryption>,
iv: iv
}
// JWT Settings
const jwtDetails = {
secret: <insert the key to be used for signing the token>,
expiresIn: <set the time of expiration of the token in seconds>
}
After defining the JWT details
1. generateJWT
const token = await generateJWT(
jwtDetails,
publicData,
encryption,
privateData
)
2. readJWT
await readJWT(token, encryption) //encryption should be the same as what we defined earlier
Response
If the keys match
const token = await generateJWT(
jwtDetails,
publicData,
encryption,
privateData
)
Token Generated: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7InB1YmxpYyI6eyJyb2xlIjoidXNlciJ9LCJlbmNEYXRhIjoiNWRkNTEwYTY2Y2ZhMjM0MTIzNWZkYTRhMTM0ODY2NDYyZGU3MjM3OWEzZmJhMWM1NWM5MmJkMWVkMmYzNTU0NzQ0OWE5YzYwNDVjODgxYWQ5ZDE5MDBiY2M0ZGZhYzFkZTg4NjI2YmU1YjEzN2NiNzcwOGEwZTMxYzYyZDk2YTUifSwiaWF0IjoxNjY3Mzg1NjgxLCJleHAiOjE2NjczODc0ODF9.6jvU-3Y5woTYTr_rAd9yZmv4aWfxgqZAwqDMnzIzZmw
await readJWT(token, encryption) //encryption should be the same as what we defined earlier
/* format of the decrypted and decoded jwt payload
{
data: <object containing both public and private data>,
iat: <time at which the JWT was generated>,
exp: <expiry time of the JWT>
}
*/