DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Secure Your Frontend: Practical Tips for Developers
  • A Practical Guide to Securing NodeJS APIs With JWT
  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App

Trending

  • Agentic AI for Automated Application Security and Vulnerability Management
  • A Deep Dive Into Firmware Over the Air for IoT Devices
  • Start Coding With Google Cloud Workstations
  • MCP Servers: The Technical Debt That Is Coming
  1. DZone
  2. Software Design and Architecture
  3. Security
  4. Understanding JWT

Understanding JWT

The de facto standard to transmit claims in a secure and/or integrated manner.

By 
John Doe user avatar
John Doe
·
Jul. 10, 22 · Analysis
Likes (4)
Comment
Save
Tweet
Share
9.2K Views

Join the DZone community and get the full member experience.

Join For Free

Don't get fooled by too many articles out there that make JWT sound more complex and complicated than it actually is. In simple words, JWT is a simple format that aims to transfer claims between two parties in a  compact and secure manner. A claim is a key-value pair that asserts something about a subject. 

The diagram below will give you a very simple idea of how a JWT is layered. It is a set of claims as a JSON object, that is secured by a JWS (JSON Web Signature) or a JWE (JSON Web Encryption) layer.
how a JWT is layered

The diagram below will give you a very simple idea of how a JWT is structured. It is a sequence of URL-safe base64 encoded values separated by '.' characters. 

how a JWT is structured

Example of a JWT

Let's go over a simple example to understand how all of this adds up. This JWT in the example is secured using a JWS signature (Signatures help with integrity checks. For example, it can confirm that the sender of a message is the one who they say they are. Read this for more details). 

Header

The JSON object below is the header of the JWT. All it says is that the JWT is integrity protected using a JWS and is MACed as well using an HMAC SHA-256 algorithm ( Don't worry too much about it for now).

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

To make it eligible to be a part of the resultant JWT token we need to convert it to a base64 encoded string. 

Plain Text
 
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9

Payload

The JSON object below is the payload of the JWT. It contains the info that you actually intend to transmit from the sender to the recipient. They are also called claims, as discussed above. 

JSON
 
{"iss":"joe", "exp":1300819380, "http://example.com/is_root":true}

These claims need to be base64 encoded as well. 

Plain Text
 
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ

Signature

Now we need to generate the JSON Web Signature for the JWT which is generated with the simple formula:

JavaScript
 
base64UrlEncode(HMACSHA256(base64UrlEncode(header) + "." + base64UrlEncode(payload), secret))
Take the encoded header and the encoded payload
  1. Compute the MAC of them combined with the HMAC SHA-256 algorithm and the secret
  2. base64-encode the computed result from #2

This formula gives us the resultant value as below:

Plain Text
 
dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

Resultant JWT

This is just the '.' separated sequence of the header, payload, and signature! 

Plain Text
 
eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9
.
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ
.     
dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

So, as you can see, you know JWT's now. 

JWT Claims 

A few things to remember about JWT claims:

  • Claim names within the JWT must be unique ( no collision )
  • The usage of claims in JWT is context-dependent. There are no rules that dictate the type of claims to use etc. 

There are 3 kinds of claims used in JWT and they must be used as appropriate, depending on the use case. 

Registered Claim Names

There are a bunch of registered claim names that are defined in the "JSON Web Token Claims" registry.  There is no rule that enforces the usage of these claim names. Instead, these are helpful standards that can be used as necessary by the developers. 

For example :

  • iss Claim - Identifies the issuer of the jWT
  • sub Claim - Identifies the subject of the JWT
  • exp Claim - Identifies the expiration time of the JWT

Public Claim Names

These can be defined as appropriate by the developers. Collision prevention should be kept in mind while defining these. So, they should ideally be defined in the registry. 

Private Claim Names

These care claim names that are agreed upon by the sender and the recipient of the JWT. These are prone to collisions and hence, uniqueness should be kept in mind while defining these. 

JWT Header

The JWT header generally defines the cryptographic operations applied on the JWT. If you look at the example from above, we used 2 parameters:

  • type - Defines the media type of the JWT. 
  • alg - Defines the algorithm used to encrypt/secure/sign the JWT. 

If you are wondering whether claims names can also be used as JWT header parameters, the answer is a yes. At the end of the day, this is something you can control while designing your JWT. 

Unsecured JWTs

Yes, they exist! JWTs may be created without any signature or encryption. For such JWTs set the alg header parameter value as "none". Its signature value is also an empty string, something like this : 

Plain Text
 
eyJhbGciOiJub25lIn0
.
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
cGxlLmNvbS9pc19yb290Ijp0cnVlfQ
.

Uses 

JWTs are used everywhere these days for various purposes. A few of those are explained below

  • Authorization of clients in a Client-Server application. It helps determine the identity of the client every time a request is sent to the server, without the server having to store the client's details. The signature in the token suffices. 
  • Authorization of a client across multiple servers where the servers agree to use the same JWT to identify the client. For example. if you are the client of Server1 and, Server1 redirects you to Server2 (and Server1 and Server2 agree to use the same JWT), you don't have to worry about authorization with Server2. The same JWT can be sent in your subsequent requests to Server2, and they should be able to identify you.
JWT (JSON Web Token)

Opinions expressed by DZone contributors are their own.

Related

  • Issue and Present Verifiable Credentials With Spring Boot and Android
  • Secure Your Frontend: Practical Tips for Developers
  • A Practical Guide to Securing NodeJS APIs With JWT
  • How to Set up OAuth JWT Flow and mTLS in the Salesforce Connector for a MuleSoft App

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!