JWT (JSON Web Token) – Complete Notes

 


JWT (JSON Web Token) – Complete Notes

What is JWT?

JWT (JSON Web Token) is a stateless authentication mechanism used to securely transmit information between parties as a JSON object.

It is digitally signed using:

  • HMAC (symmetric key) or
  • RSA (public/private key pair)

So the data can be verified and trusted.


Key Advantages of JWT

1. Compact

JWT is small in size and can be easily sent in:

Authorization: Bearer <token>

Which makes it fast and network efficient.

2. Stateless (Self-contained)

JWT contains all required user information.
Server does not need to query database on every request.

3. Supports Expiry

JWT has built-in expiry using:

exp (expiration time)

4. Supports Custom Data

You can add:

  • userId
  • roles
  • permissions
    inside JWT claims.

Where is JWT Used?

JWT is commonly used for:

  1. Authentication – verifying user identity
  2. Authorization – checking permissions
  3. SSO (Single Sign-On) – login once, access multiple apps

JWT Authentication Flow

From Page 1 diagram:

  1. Client logs in with username/password
  2. Authentication server generates JWT
  3. Client stores token
  4. Client sends token in every request
  5. Resource server validates JWT
  6. Response returned to client

Old Method: Session-Based Authentication (JSESSIONID)

From Page 2:

Flow:

  1. User logs in
  2. Server creates session
  3. Session ID stored in DB
  4. Cookie sent to client
  5. Every request → DB lookup

Disadvantages:

  • Stateful
  • DB dependency
  • Not scalable
  • Problem in distributed systems

JWT Structure

JWT has 3 parts:

xxxxx.yyyyy.zzzzz
Part Description
Header Token type & algorithm
Payload User data (claims)
Signature Digital signature

 


JWT Header Example

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

Contains:

  • Token type
  • Algorithm used (RSA / HMAC)

JWT Payload (Claims)

Payload stores user data.

Registered Claims (Standard)

Claim Meaning
iss Issuer
sub Subject (user)
aud Audience
exp Expiry time
iat Issued at
jti Unique token id

Public Claims

Custom but shared across systems.

Private Claims

Custom and internal to your system.


JWT Signature Creation

Steps:

  1. Base64 encode Header
  2. Base64 encode Payload
  3. Join with dot
  4. Sign using RSA or HMAC
  5. Append signature

This ensures:

  • Token integrity
  • Token authenticity

Sample API Request

From Page 3:

curl --location --request GET "https://exampleHost.com/api/resource" \
--header "Authorization: Bearer eyJhbGciOiJIUzI1NiIs..."

 


Challenges with JWT

1. Token Invalidation Problem

If user is blocked:

  • Token still valid until expiry

Solutions:

  • Maintain blacklist in DB/Redis
  • Use short-lived tokens
  • Use token once (OTP style)

2. JWT is Encoded, Not Encrypted

Anyone can decode payload.

Solution:

  • Use JWE (JSON Web Encryption)
  • Encrypt payload data

3. alg = none Attack

Some attackers send:

"alg": "none"

This disables signature.

Such tokens must be rejected.


4. JWK Exploit

Never trust public key sent inside token.

Always fetch keys from:

https://auth-server/.well-known/jwks.json

Use kid to select correct key.


Interview Questions & Answers

Q1. Is JWT encrypted?

No. JWT is only Base64 encoded, not encrypted.


Q2. Why JWT is called stateless?

Because server does not store any session.


Q3. Where is user data stored in JWT?

Inside payload (claims).


Q4. What happens when JWT expires?

Client must re-authenticate.


Q5. Can JWT be revoked?

Not directly. You need:

  • blacklist
  • short expiry
  • refresh tokens

Q6. Difference between Session and JWT?

Session JWT
Stateful Stateless
DB lookup No DB
Cookie Authorization header
Not scalable Highly scalable

Q7. Why JWT is preferred in microservices?

Because:

  • No shared session
  • No central DB
  • Works across services

Final Summary

JWT is:

✔ Stateless
✔ Scalable
✔ Perfect for APIs
✔ Ideal for microservices

But has challenges:

❌ No easy logout
❌ Token revocation hard
❌ Payload visible

So best practice:

Use short expiry + refresh tokens + HTTPS + JWE (if needed)

JWT is not just an authentication method –
it is the foundation of modern API security.

Leave a Reply