What is a JWT token
Tags: jwt, web, security
Published: 2020-10-01 12:37:04

---
title: What is a JWT token
published: true
description: A quick intro on what a JWT token is
tags: ["jwt", "web","security"]
canonical_url: "https://loige.co/whats-in-a-jwt"
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/rfu9m7fbc03t30x6bn1i.png
---

If you on this page, chances are that you have been seeing JWT tokens for a while and you are curious to find out what they really are and how they actually work! Why did they get so mainstream anyway?! 🤔

## It's just a string with a well-defined format

JWT stands for JSON Web Token and a JWT token is a string composed by 3 parts: a *header*, a *body* (sometimes also referred to as *payload*) and a *signature*. Just to look at a more concrete example, a JWT token looks like this:

```
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6ImZyb20gSldUIn0.XoByFQCJvii_iOTO4xlz23zXmb4yuzC3gqrWNt3EHrg
```

Parts are separated by a `.` (dot) character, so we can identify the three parts as follows:

  - header: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9`
  - body: `eyJoZWxsbyI6ImZyb20gSldUIn0`
  - signature: `XoByFQCJvii_iOTO4xlz23zXmb4yuzC3gqrWNt3EHrg`

Header and Body are JSON strings encoded using the [Base64Url](https://tools.ietf.org/html/rfc4648#section-5) algorithm (a URL-safe variation of the standard Base64 encoding algorithm).

If we decode them this is what we get:

  - header: `{ "alg": "HS256", "typ": "JWT" }`
  - body: `{ "hello": "from JWT" }`

The signature part contains bytes which represent a cryptographic signature of header and body (also encoded using Base64Url) and can be used to verify the authenticity of a token.


## When JWT tokens are used

JWT tokens are mostly used as a mechanism for "stateless" authentication and authorization. Let's try to discuss what this means with a simple example:

![a4a86605-d236-458f-b1db-8b082bfdcd7c](https://dev-to-uploads.s3.amazonaws.com/i/p4esldyt1esupb9na34u.png)

In this picture, John is authenticating against an auth server. The auth server recognizes his credentials and gives him back a token. John can now use the token to connect to specific services.

Every time John makes a request to a service, he will need to attach his token to the request. The service can look at the token to understand if the request can be authorized. The service can read the information embedded within the token to understand that the request is coming from John and can verify that the signature was applied by the Auth server.

This process is "stateless" because this validation can be done without having to make an explicit request to the Auth server. This is a great property for distributed systems or, in general, systems that deal with a high load of requests.
 

## Now you can be nosy

Now that you know how a JWT token is made you can start to be nosy and look into the JWT tokens you bump into.

You can copy-paste tokens into [jwt.io](https://jwt.io) or use a command-line tool like [`jwtinfo`](https://github.com/lmammino/jwtinfo) to read the content of the header and the body in plain text.

Trust me, if you do that you will be surprised by how many interesting information you can find there!


## Conclusion

JWT tokens are an interesting approach to authentication and they are particularly convenient in distributed systems when we want to minimise the communication and the amount of load to the authentication provider. Once a token is created, it can be validated without the need to call the authentication provider again.

If you are curious to understand more about how JWT works (and even how you could crack tokens using Node.js 😼) check out this talk I gave a couple of years ago at [BuzzJS 2018](https://buzzjs.com/) in the beautiful New York City:

{% youtube uBYdxOQ57nQ %}

While you watch that, you can have [the slides](https://loige.link/jwt-crack-ny) too! 🤗

If you found this article interesting please follow me here, on [Twitter](https://twitter.com/loige) and check out my [personal website/blog](https://loige.co) for other web related articles.

Also, if you like Node.js please have a look at my book, [Node.js Design Patterns third edition](https://www.nodejsdesignpatterns.com/), which can help you to learn how to design and implement production-grade Node.js applications using proven patterns and techniques.

[![Node.js Design Patterns book hold by man](https://dev-to-uploads.s3.amazonaws.com/i/gm3sfzusx8s8bhpbpid0.png)](https://www.nodejsdesignpatterns.com/)

Thank you! 👋