jwt-token-encrypt
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
;
Above is a breaking change as before import was done with defaultExport !
Version < "1.0.3"
Creating JWT
// Data that will be publicly availableconst publicData = role: "user"; // Data that will only be available to users who know encryption details.const privateData = email: "user" bank: "HSBC" pin: "1234"; // Encryption settingsconst encryption = key: 'AAAAAAAAAAAAAA' algorithm: 'aes-256-cbc' ; // JWT Settingsconst 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;
Reading JWT
// Encryption settingsconst encryption =key: 'AAAAAAAAAAAAAA'algorithm: 'aes-256-cbc';const decrypted = jwtEncrypt;
Token Content
E.g.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJOS0luVldoQjFyVkxDd3hsdE1OdWlVQzZoOVV1ZEFiaSIsImRhdGEiOnsicHVibGljIjp7ImRhdGExIjoxLCJkYXRhMiI6MiwiZGF0YTMiOjN9LCJlbmNEYXRhIjoiYjliM2QyNDdkNTk4ZTlkODczOTM2NTI4MWVmN2ExZTkifSwiaWF0IjoxNTExMTk5MDg0LCJleHAiOjE1MTEyNDIyODR9.KzfcIY95RR7aPYKn5EcXZYvETDCGZIJ91p7IfXCiClw
Once decoded will hold below content jwt.io
iss: 'NKInVWhB1rVLCwxltMNuiUC6h9UudAbi'data:public:data1: 1data2: 2data3: 3encData: '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;
will result in having:
iss: 'NKInVWhB1rVLCwxltMNuiUC6h9UudAbi'data:public:data1: 1data2: 2data3: 3session: '5fb8ed70a3864cbd97b25cc8ca2c0bc7'
also to read you will need to pass new filed name
e.g.
// Encryption settingsconst encryption =key: 'AAAAAAAAAAAAAA'algorithm: 'aes-256-cbc';const decrypted = jwtEncrypt;