[node. js] Creating jwts (JSON Web Tokens) in Node

Source: Internet
Author: User
Tags hmac sha256 algorithm

In this lesson we'll look at all of the pieces, combine together to create a JWT (J AWT) or JSON Web Token. You'll use node to create a JWT, and then verify it in the JWT debugger.

What is the JSON WEB Token structure?

JSON Web Tokens consist of three parts separated by dots ( . ), which is:

    • Header
    • Payload
    • Signature

Therefore, a JWT typically looks like the following.

xxxxx.yyyyy.zzzzz

Let's break down the different parts.

Create a header:

The header typically consists of the parts:the type of the token, which is JWT, and the hashing algorithm being Used, such as HMAC SHA256 or RSA.

Let Header = {  ' JWT ',  ' HS256 'new Buffer (json.stringify (header)). ToString (' base64 '); Console.log (header);

Create a paylaod:

The second part of the tokens is the payload, which contains the claims. Claims is statements about an entity (typically, the user) and additional metadata. There is three types of claims: reserved, public, and privateclaims.

Let payload = {  iat:Date.now (),  ' nodebotanist ',  ' nodebotanist 'new Buffer (json.stringify (payload)). ToString (' base64 '); Console.log ("payload", payload);

Create a signature:

To create the signature part of the encoded header, the encoded payload, a secret, the algorithm specified I n the header, and sign that.

For example if your want to use the HMAC SHA256 algorithm, the signature would be created in the following-to-do:

HMACSHA256 (  + "." +  base64urlencode (payload),  secret)

Let key = header + '. ' += Crypto.createhmac (' sha256 ', ' Zhentian '= signature.digest (' base64 '= Header + '. ' +payload + '. ' + Keyconsole.log ("token", token)

----------------

Let Header ={typ:' JWT ', ALG:' HS256 '};header=NewBuffer (json.stringify (header)). toString (' base64 '); Console.log (header); let payload={Iat:Date.now (), ISS:' Nodebotanist ', Username:' Nodebotanist '};p ayload=NewBuffer (json.stringify (payload)). ToString (' base64 '); Console.log ("Payload", payload); let key= header + '. ' +Payload;let Signature= Crypto.createhmac (' sha256 ', ' Zhentian '); signature.update (key); key= Signature.digest (' base64 '); let token= header + '. ' +payload + '. ' +Keyconsole.log ("token", token)

Debugger

[node. js] Creating jwts (JSON Web Tokens) in Node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.