JWT Magic: How it avoids Middle Man Attacks (MMT) With Out Using Server Storage

DotNet Full Stack Dev
5 min readNov 7, 2024

--

Imagine logging into your favorite app, whether it’s a social media platform or an e-commerce site. You log in, get verified, and are granted access to everything you need — all without the server keeping tabs on every single user session. This magic is made possible by JSON Web Tokens (JWT), which let the server recognize you without needing to store a single token. Intrigued? Let’s take a deep dive into the fascinating world of JWT and see how this stateless authentication powerhouse keeps you securely connected.

Setting the Scene: JWT in Real Life

Think of an event where only registered attendees are allowed in. At the entrance, security gives you a unique wristband with your name, role (guest or VIP), and the event’s time frame — but they don’t write down your name in a log. Instead, your wristband has all the necessary information, including an embedded seal proving it came from the event’s organizer. As long as that wristband is intact, you’re verified and can enjoy the event.

📌Explore more at: DotNet Full Stack Dev
🌟 Clapping would be appreciated! 🚀

A JWT works the same way. Once you’ve been authenticated (think “registered for the event”), the server hands you a “wristband” — a token that says you’re legit. And just like security can check your wristband without storing attendee names, the server validates your JWT without storing a token. But how does it work? Let’s take a look at the core of JWT and how this self-validating system keeps us secure.

What’s Inside a JWT?

A JWT looks like a mysterious string of characters. Here’s a simplified example:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ
.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

This odd-looking sequence is divided into three parts:

  1. Header: Includes metadata, like the algorithm used to sign the token (e.g., HS256).
  2. Payload: Contains the actual data, such as user ID, roles, or expiration time.
  3. Signature: Proves the token’s authenticity using a “secret” key shared by the server.

Step-by-Step: How JWTs Validate Without Storage

Let’s break down exactly how JWTs let the server recognize valid tokens without ever saving a single one.

Step 1: Client Logs In and Gets a JWT

When you log in, your credentials are validated. The server then generates a JWT specifically for you, signing it with a secret key that only the server knows. This token is sent back to you, the client, to be stored in your browser, usually in a secure cookie or a browser’s storage.

Step 2: You Make a Request with Your JWT

Now that you have a JWT, every time you send a request (say, to view your profile), your browser attaches this token in the Authorization header as a “proof of identity.”

Step 3: The Server Verifies Your JWT Without Storage

When the server receives your request, it takes the following steps to confirm your token is valid:

  1. Extracts Header and Payload: The server decodes the token’s header and payload to see your details and claims, like your user ID and the expiration time.
  2. Recreates the Signature: Using the same header and payload, the server applies the same secret key and hashing algorithm to generate a new signature.
  3. Checks the Signature Match: If the signature generated on the server matches the token’s original signature, then bingo! The token is verified as authentic, meaning it was created by the server and hasn’t been tampered with.

And that’s it — the token is verified, without any need for the server to save it!

Why JWT Is Perfect for Scalable Systems

In stateless authentication, the server is free from the responsibility of remembering every user’s session. This has huge advantages, especially for scalable systems where thousands or millions of users are constantly making requests. Here are some of the perks:

  • Scalability: No token storage on the server means you can easily add more servers. Each server can independently verify tokens without worrying about syncing session data.
  • Flexibility in Microservices: JWTs shine in microservice architectures, where different services need to authenticate users independently. Each service can validate the token without checking in with a centralized authentication server.
  • Reduced Complexity: The server doesn’t need to worry about session management or token storage — JWTs are self-contained and validate independently.

Real-World JWT Validation Checks and Security Measures

In real-world applications, there are a few extra checks for secure token validation:

  • Expiration (exp): Most JWTs include an expiration claim (exp) that specifies how long the token is valid. This reduces security risks if tokens get exposed.
  • Issuer and Audience (iss and aud): Servers often validate that the JWT was issued by a trusted source (iss) and is intended for the correct audience (aud), adding a layer of security.
  • Revocation Challenges: Since JWTs are stateless, revoking a token is tricky — the server can’t “un-give” a JWT once issued. One solution is to use short expiration times with refresh tokens.

Example: How an E-Commerce App Can Use JWTs

Imagine an e-commerce platform where users log in to browse products, add to cart, and make purchases. Here’s how JWTs enable secure, efficient authentication:

  1. User Logs In: After entering credentials, the server creates a JWT and sends it to the client. The token includes a user ID, role (buyer/seller), and expiration time.
  2. User Browses and Adds Items to Cart: Each action (view, add, checkout) is accompanied by the JWT. The server quickly verifies the token, checks the user’s role, and proceeds without needing to store session info.
  3. Purchases and Order History: The client makes requests with the JWT, which grants access to the order history or checkout process if the token is valid.

For each request, the server knows the client is authenticated and authorized for certain actions, without managing any session or token data on the server.

Wrapping Up: JWT as the Secret to Stateless Magic

JWTs offer an elegant solution to stateless authentication in distributed systems, allowing servers to validate tokens without the burden of storing them. By harnessing a secure signature with a secret key, JWTs deliver a reliable and scalable way to manage user authentication. The next time you log in to an app without waiting for session synchronization, you’re likely witnessing JWTs in action, quietly and efficiently handling your authentication behind the scenes.

JWTs are a game-changer for modern applications, providing a seamless way to balance security and scalability in the world of stateless authentication. So, the next time you see one of those funky-looking tokens, you’ll know that there’s more to them than meets the eye — it’s the secret to keeping you logged in, verified, and secure without a single server-side storage requirement.

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet