node.jwt
A lightweight library for encode and decode JSON Web Tokens (JWT).
Install
$ npm install node.jwt --save-dev $ yarn add node.jwt
Usage
Symmetric encryption
const jwt = const payload = "sub": "aidandai@126.com" "iss": "https://aidandai.github.io" "exp": Date / 1000 + 60 * 60 * 24 * 2 "nbf": Date / 1000 - 60 * 5 "iat": Date / 1000 "data": "uid": "1sf72323rjsdfsdkl223434k" "name": "Aidan" "authority": "1001111010101110" // get secret, you can reload this method to create your secretconst secret = jwt // encodevar token = jwt; // decodevar result = jwtconsole // {// code: '000', // error code of verify, you can learn more from lib/jwt.js// message: 'successful', // error message of verify, , you can learn more from lib/jwt.js// payload: {// "sub": "aidandai@126.com",// "iss": "https://aidandai.github.io",// "exp": Date.now() / 1000 + (60 * 60 * 24 * 2),// "nbf": Date.now() / 1000 - (60 * 5),// "iat": Date.now() / 1000,// "data": {// "uid": "1sf72323rjsdfsdkl223434k",// "name": "Aidan",// "authority": "1001111010101110",// }// }// }
Asymmetric encryption
const jwt = const payload = "sub": "aidandai@126.com" "iss": "https://aidandai.github.io" "exp": Date / 1000 + 60 * 60 * 24 * 2 "nbf": Date / 1000 - 60 * 5 "iat": Date / 1000 "data": "uid": "1sf72323rjsdfsdkl223434k" "name": "Aidan" "authority": "1001111010101110" const privateSecret = const publicSceret = getYourPublicSecret // encodevar token = jwt // decodevar result = jwtconsole // {// code: '000', // error code of verify, you can learn more from lib/jwt.js// message: 'successful', // error message of verify, , you can learn more from lib/jwt.js// payload: {// "sub": "aidandai@126.com",// "iss": "https://aidandai.github.io",// "exp": Date.now() / 1000 + (60 * 60 * 24 * 2),// "nbf": Date.now() / 1000 - (60 * 5),// "iat": Date.now() / 1000,// "data": {// "uid": "1sf72323rjsdfsdkl223434k",// "name": "Aidan",// "authority": "1001111010101110"// }// }// }
Interface
/** * jwt encode * @param * @param * @param * @param * @return */ /** * jwt decode * @param * @param * @param * @return */
Algorithms
By default the algorithm to encode is HS256
.
The supported algorithms for encoding and decoding are HS256
, HS384
, HS512
and RS256
.
// encode using HS512jwt
About JWT
Contributors
- stephenbeauchamp: https://github.com/stephenbeauchamp