JWT

What is JWT? Complete Guide to JSON Web Tokens

What is a JSON Web Token (JWT)?

JWT (JSON Web Token) is an open standard (RFC 7519) for securely transmitting information between parties as a JSON object. It's compact, URL-safe, and self-contained, making it perfect for authentication and information exchange.

JWT Structure: Three Parts

A JWT consists of three parts separated by dots (.):

xxxxx.yyyyy.zzzzz
HEADER.PAYLOAD.SIGNATURE

1. Header

Contains metadata about the token, including type and signing algorithm:

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

2. Payload

Contains the claims (statements about the user and additional data):

{
      "sub": "1234567890",
      "name": "John Doe",
      "admin": true,
      "iat": 1516239022,
      "exp": 1516242622
  }

3. Signature

Used to verify the token hasn't been tampered with:

HMACSHA256(
  base64UrlEncode(header) + "." + base64UrlEncode(payload),
  secret
  )

Standard JWT Claims

ClaimNameDescription
issIssuerWho created the token
subSubjectUser ID/identifier
audAudienceWho the token is intended for
expExpirationWhen the token expires (timestamp)
nbfNot BeforeToken not valid before this time
iatIssued AtWhen the token was created
jtiJWT IDUnique token identifier

How JWT Authentication Works

  1. Login: User sends credentials to server
  2. Token Generation: Server validates credentials and creates JWT
  3. Token Storage: Client stores JWT (usually in localStorage or httpOnly cookie)
  4. API Requests: Client sends JWT in Authorization header
  5. Verification: Server verifies signature and extracts user info
  6. Response: Server processes request and returns data

Signing Algorithms

HMAC (Symmetric)

  • HS256: HMAC with SHA-256 (most common)
  • HS384: HMAC with SHA-384
  • HS512: HMAC with SHA-512

RSA (Asymmetric)

  • RS256: RSA with SHA-256
  • RS384: RSA with SHA-384
  • RS512: RSA with SHA-512

JWT Best Practices

Security Tips:

  • Always use HTTPS in production
  • Set short expiration times (15-30 minutes)
  • Use strong secret keys (min 256 bits for HS256)
  • Validate all claims (iss, aud, exp)
  • Never store sensitive data in JWT payload
  • Don't use "none" algorithm in production

JWT vs Session-Based Auth

FeatureJWTSession
StorageClient-sideServer-side
ScalabilityExcellent (stateless)Harder (needs shared storage)
SizeLargerSmaller (just session ID)
RevocationDifficultEasy

Common Use Cases

  • Single Sign-On (SSO): Share authentication across multiple domains
  • Mobile Apps: Stateless authentication for mobile clients
  • Microservices: Pass user context between services
  • API Authentication: Secure REST API endpoints

Try It Yourself

Decode and verify JWT tokens with our free online tools:

Conclusion

JWT is a powerful standard for stateless authentication. Its self-contained nature makes it perfect for modern distributed systems, but proper security practices are essential. Always validate tokens, use strong secrets, and set appropriate expiration times.