General Intermediate

JWT

JSON Web Token. An open standard for securely transmitting information between parties as a compact, self-contained JSON object that is digitally signed.

Pronunciation

/dʒɒt/
"jot"

What is it

JWT stands for JSON Web Token (pronounced “jot”). It’s the most widely used standard for securely transmitting identity and permission information between systems.

When an API says you need a “token” to authenticate, in most modern cases that token is a JWT.

JWT Structure

A JWT has 3 parts separated by dots:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.
eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFuYSBHYXJjw61hIiwicm9sZSI6ImFkbWluIiwiaWF0IjoxNjQwMDAwMDAwLCJleHAiOjE2NDAwMDcyMDB9.
SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

1. Header

Indicates the signing algorithm used:

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

2. Payload

The data we want to transmit:

{
  "sub": "1234567890",
  "name": "Ana García",
  "role": "admin",
  "iat": 1640000000,
  "exp": 1640007200
}

3. Signature

Guarantees the token wasn’t tampered with. Calculated with header + payload + secret key.

How JWT Authentication Works

1. User logs in (email + password)

2. Server verifies credentials

3. Server generates JWT signed with secret key

4. Server sends JWT to client

5. Client stores JWT (localStorage or secure cookie)

6. On each request, client sends: Authorization: Bearer <JWT>

7. Server verifies signature → if valid, processes the request

JWT Advantages

AdvantageDescription
StatelessThe server doesn’t need to store sessions in the database
PortableThe same token works across multiple services
Self-containedThe token carries user information — no need to look it up
VerifiableAny service with the public key can verify it

Standard Payload Fields

FieldNameDescription
subSubjectUser identifier
issIssuerWho issued the token
audAudienceWho the token is for
expExpirationWhen it expires
iatIssued AtWhen it was issued
nbfNot BeforeWhen it becomes valid

Security Best Practices

✅ Do:

  • Use short expiration times (exp) — 15 minutes to 24 hours
  • Always transmit over HTTPS
  • Store tokens in HttpOnly cookies for web apps (not localStorage)
  • Use secure algorithms (RS256 for production, HS256 for development)

❌ Don’t:

  • Store sensitive data in the payload (it’s encoded, not encrypted)
  • Create tokens that never expire
  • Transmit over unencrypted HTTP

JWT vs Traditional Sessions

AspectTraditional SessionJWT
StorageOn server (DB or Redis)On client (token)
ScalabilityRequires shared sessionsAny server can verify
RevocationImmediate (delete the session)Complex (must wait for expiry or use blacklist)
SizeSmall IDLarger token
Best forTraditional web appsAPIs, microservices, mobile apps
  • [[API]] - APIs use JWT to authenticate who makes each request
  • [[REST]] - REST APIs use JWT in the Authorization header
  • [[Shift-Left Security]] - Token security must be thought through from design time
  • [[Microservices]] - JWT allows multiple services to validate identity without central coordination

Additional Resources: