DEV Community

Cover image for How to decode a JWT
Brian Morrison II
Brian Morrison II

Posted on

1

How to decode a JWT

JWTs are used a lot in authentication, but have you ever wondered how libraries extract information about the user from this massive blob of text?

Here is what a raw JWT looks like:

A JWT

Oftentimes these are stored in a cookie or in the browser’s local storage to be sent with HTTP requests.

JWTs have three parts

JWTs are made up of three separate parts, each separated by a period.

The header contains info about the JWT and the encryption algorithm used to sign it. The payload (or claims) section contains encoded information such as who the JWT was created for, who created it, when it expires, etc. And the signature is a cryptographically signed version of the header and claims to prevent tampering.

Here is what that same JWT looks like, split on the periods:

A JWT split

Decoding the payload

While every part of the JWT is important, the data that gets returned from extraction and verification libraries comes from the payload.

The payload is simply a JSON object that’s been base64 encoded. Anything encoded with base64 can be decoded as well since it’s NOT encryption. Reversing the encoding returns the same data that was passed in!

The claims of the JWT decoded into a JSON object

🤗 If you are interested in more content like this and want to support me, consider joining my newsletter!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay