No history yet

Introduction to JWTs

What is a JWT?

Think of a JSON Web Token, or JWT, as a secure, self-contained digital passport. When you travel, you show your passport to prove who you are and what permissions you have, like entering a country. A JWT does something similar for websites and apps.

It’s a compact way for two parties, like your browser and a web server, to securely exchange information. The server can issue a JWT to your browser after you log in. Then, for every subsequent request you make, your browser presents this JWT to prove you are authenticated. The server checks the JWT's validity and, if it’s trustworthy, grants you access.

This process avoids the need to send your username and password with every single request, which would be inefficient and less secure.

JWT

noun

An open standard for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed.

At first glance, a JWT just looks like a long string of random characters. But it's not random at all. It's a highly structured piece of information, encoded for safe transmission.

The Three Parts of a Token

Every JWT is made of three distinct parts, separated by dots. Each part is Base64Url encoded, which is a way of making the data safe to send across the web.

The structure is simple: Header, Payload, and Signature.

Header.Payload.Signature

1. Header The header typically consists of two parts: the type of the token, which is JWT, and the signing algorithm being used, such as HMAC SHA256 or RSA. This section tells the server how the token was constructed.

{
  "alg": "HS256",
  "typ": "JWT"
}

2. Payload The payload contains the claims. Claims are statements about an entity (typically, the user) and additional data. There are three types of claims: registered, public, and private claims.

  • Registered claims: These are a set of predefined claims which are not mandatory but recommended. Some common ones include iss (issuer), exp (expiration time), and sub (subject).
  • Public claims: These can be defined at will but should be defined in a public registry to avoid collisions.
  • Private claims: These are custom claims created to share information between parties that agree on using them.
{
  "sub": "1234567890",
  "name": "Alex Chen",
  "iat": 1516239022,
  "admin": true
}

3. Signature The final part is the signature. To create it, you take the encoded header, the encoded payload, a secret key, and run them through the algorithm specified in the header. The signature is used to verify that the message wasn't changed along the way. If someone tampers with the header or payload, the signature will no longer be valid.

How JWTs Are Used

The most common uses for JWTs are authentication and authorization.

Authentication is the process of verifying who a user is. When a user logs in with their credentials, the server creates a JWT and sends it back. From then on, the client includes this JWT in the header of its requests to access protected routes or resources. It's a stateless process, meaning the server doesn't need to keep a record of which users are logged in. It just needs to validate the JWT it receives.

Authorization comes next. Once the user is authenticated, the JWT can determine what they are allowed to do. The payload of the token can contain information about the user's role or permissions. For example, a claim like "admin": true could grant a user access to administrative functions. The server reads these claims to enforce access control.

In short: Authentication confirms you are who you say you are. Authorization determines what you're allowed to see and do.

Another use case is securely transmitting information. Because JWTs are signed, you can be sure that the sender is who they say they are and that the data wasn't tampered with. This makes them a good choice for sharing information between different parts of a distributed system.

Quiz Questions 1/4

What are the three parts of a JSON Web Token, in the correct order?

Quiz Questions 2/4

The server needs to store a record of issued JWTs in a database to validate them upon receiving a request.

JWTs provide a simple and powerful way to handle user sessions and secure communication in modern web applications.