Get Started with JSON Web Tokens (2024)

What is JSON Web Token?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with HMAC algorithm) or a public/private key pair using RSA.

Let’s explain some concepts of this definition further.

  • Compact: Because of its size, it can be sent through an URL, POST parameter, or inside an HTTP header. Additionally, due to its size its transmission is fast.
  • Self-contained: The payload contains all the required information about the user, to avoid querying the database more than once.

Interested in getting up-to-speed with JWTs as soon as possible? DOWNLOAD THE FREE EBOOK

Get Started with JSON Web Tokens (1)

When should you use JSON Web Tokens?

These are some scenarios where JSON Web Tokens are useful:

  • Authentication: This is the typical scenario for using JWT, once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used among systems of different domains.
  • Information Exchange: JWTs are a good way of securely transmitting information between parties, because as they can be signed, for example using a public/private key pair, you can be sure that the sender is who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn’t changed.

JWTs consist of three parts separated by dots (.), which are:

  • Header
  • Payload
  • Signature

Therefore, a JWT typically looks like the following.

xxxxx.yyyyy.zzzzz

Let’s break down the different parts.

Header

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

For example:

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

Then, this JSON is Base64Url encoded to form the first part of the JWT.

Payload

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

    • Reserved claims: These are a set of predefined claims, which are not mandatory but recommended, thought to provide a set of useful, interoperable claims. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), among others.

Notice that the claim names are only three characters long as JWT is meant to be compact.

  • Public claims: These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.
  • Private claims: These are the custom claims created to share information between parties that agree on using them.

An example of payload could be:

{ 'sub': '1234567890', 'name': 'John Doe', 'admin': true}

The payload is then Base64Url encoded to form the second part of the JWT.

Signature

To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way.

HMACSHA256( base64UrlEncode(header) + '.' + base64UrlEncode(payload), secret)

The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message was’t changed in the way.

Putting all together

The output is three Base64 strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact compared to XML-based standards such as SAML.

The following shows a JWT that has the previous header and payload encoded and it is signed with a secret.

Get Started with JSON Web Tokens (2)

You can browse to jwt.io where you can play with a JWT and put these concepts in practice. jwt.io allows you to decode, verify and generate JWT.

How JSON Web Tokens work?

In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned. Since tokens are credentials, great care must be taken to prevent security issues. In general, you should not keep tokens longer than required.

You also should not store sensitive session data in browser storage due to lack of security.

Whenever the user wants to access a protected route, it should send the JWT, typically in the Authorization header using the Bearer schema. Therefore the content of the header should look like the following.

Authorization: Bearer <token>

This is a stateless authentication mechanism as the user state is never saved in the server memory. The server’s protected routes will check for a valid JWT in the Authorization header, and if there is, the user will be allowed. As JWTs are self-contained, all the necessary information is there, reducing the need of going back and forward to the database.

This allows to fully rely on data APIs that are stateless and even make requests to downstream services. It doesn’t matter which domains are serving your APIs, as Cross-Origin Resource Sharing (CORS) won’t be an issue as it doesn’t use cookies.
Get Started with JSON Web Tokens (3)

Why should you use JSON Web Tokens?

Let’s talk about the benefits of JSON Web Tokens (JWT) comparing it to Simple Web Tokens (SWT) and Security Assertion Markup Language Tokens (SAML).

As JSON is less verbose than XML, when it is encoded its size is also smaller; making JWT more compact than SAML. This makes JWT a good choice to be passed in HTML and HTTP environments.

Security-wise, SWT can only be symmetric signed by a shared secret using the HMAC algorithm. While JWT and SAML tokens can also use a public/private key pair in the form of a X.509 certificate to sign them. However, signing XML with XML Digital Signature without introducing obscure security holes is very difficult compared to the simplicity of signing JSON.

JSON parsers are common in most programming languages, because they map directly to objects, conversely XML doesn’t have a natural document-to-object mapping. This makes it easier to work with JWT than SAML assertions.

Regarding usage, JWT is used at an Internet scale. This highlights the ease of client side processing of JWTs on multiple platforms, especially, mobile.
Get Started with JSON Web Tokens (4)

How we use JSON Web Tokens in Auth0?

In Auth0, we issue JWTs as a result of the authentication process. When the user logs in using Auth0, a JWT is created, signed, and sent to the user. Auth0 supports signing JWT with both HMAC and RSA algorithms. This token will be then used to authenticate and authorize with APIs which will grant access to their protected routes and resources.

We also use JWTs to perform authentication and authorization in Auth0’s API v2, replacing the traditional usage of regular opaque API keys. Regarding authorization, JSON Web Tokens allow granular security, that is the ability to specify a particular set of permissions in the token, which improves debuggability.

Get Started with JSON Web Tokens (2024)
Top Articles
How to Build a House for under $250K | Steiner Homes | NWI
How to Find and Delete Miner Virus from PC - Monetory
It’s Time to Answer Your Questions About Super Bowl LVII (Published 2023)
Kevin Cox Picks
Brendon Tyler Wharton Height
THE 10 BEST Women's Retreats in Germany for September 2024
Volstate Portal
Prices Way Too High Crossword Clue
[PDF] INFORMATION BROCHURE - Free Download PDF
How To Delete Bravodate Account
Hmr Properties
2016 Hyundai Sonata Price, Value, Depreciation & Reviews | Kelley Blue Book
Seattle Rpz
Mary Kay Lipstick Conversion Chart PDF Form - FormsPal
Christina Khalil Forum
Po Box 35691 Canton Oh
Ess.compass Associate Login
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
Shasta County Most Wanted 2022
Persona 4 Golden Taotie Fusion Calculator
Satisfactory: How to Make Efficient Factories (Tips, Tricks, & Strategies)
Phoebus uses last-second touchdown to stun Salem for Class 4 football title
Phantom Fireworks Of Delaware Watergap Photos
Mals Crazy Crab
Keyn Car Shows
Culver's.comsummerofsmiles
Goodwill Of Central Iowa Outlet Des Moines Photos
Pulitzer And Tony Winning Play About A Mathematical Genius Crossword
Hrconnect Kp Login
Motorcycle Blue Book Value Honda
manhattan cars & trucks - by owner - craigslist
UAE 2023 F&B Data Insights: Restaurant Population and Traffic Data
Frequently Asked Questions - Hy-Vee PERKS
De beste uitvaartdiensten die goede rituele diensten aanbieden voor de laatste rituelen
Steven Batash Md Pc Photos
Selfservice Bright Lending
Nacho Libre Baptized Gif
Keeper Of The Lost Cities Series - Shannon Messenger
Midsouthshooters Supply
KM to M (Kilometer to Meter) Converter, 1 km is 1000 m
“Los nuevos desafíos socioculturales” Identidad, Educación, Mujeres Científicas, Política y Sustentabilidad
Mathews Vertix Mod Chart
Woody Folsom Overflow Inventory
Autozone Battery Hold Down
FedEx Authorized ShipCenter - Edouard Pack And Ship at Cape Coral, FL - 2301 Del Prado Blvd Ste 690 33990
About Us
Vagicaine Walgreens
Random Animal Hybrid Generator Wheel
Aaca Not Mine
Tyrone Dave Chappelle Show Gif
Costco Gas Price Fort Lauderdale
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6216

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.