JWT (JSON Web Token)
A specification that allows us to use JWT to deliver secure and reliable information between users and servers.
The JWT consists of three parts: header (head), Payload (load), signature (signature).
First, head header
The header is used to describe the most basic information about the JWT, such as its type and the algorithm used in the signature, to generate the JWT header head with Base64url encoding.
var base64url = require ("Base64url"); var header = { ' typ ': ' JWT ', ' alg ': ' hs256 '}; var base64urlheader = Base64url (json.stringify (header));
Header head:
- Typ: Type
- ALG: The algorithm used for signing
Second, load payload
var payload = { "ISS": "Momobutong", "IAT": IAT, "exp": Exp, "AUD": "www.example.com", "sub": "[email protected]", "From_user": "B" , " Target_user ":" A "}var base64urlpayload = Base64url (json.stringify (payload));
- ISS: The issuer of the JWT
- Sub: The user to which the JWT is intended
- AUD: The party receiving the JWT
- EXP (expires): Expiration Time (Unix timestamp)
- IAT (issued at): Issue time
Third, Signature signature
The signature signature is composed of header, load payload and key.
var crypto = require ("crypto"); var signature = Crypto.createhmac (' sha256 ', ' Secretkey '). Update (base64urlheader+ '. ') +base64urlpayload). Digest (' Hex ');
Generate JWT
JWT JSON Web Token