1. The Architecture of a JSON Web Token
A JSON Web Token (JWT) is an open industry standard (RFC 7519) designed to securely transmit verifiable claims between two distributed computing parties. Compact, URL-safe, and digitally signed, JWTs are fundamental to modern OAuth 2.0 and OpenID Connect specifications.
A typical serialized token consists of three distinct components separated by dots: the Header, the Payload, and the Signature.
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkFsZXggRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
2. Critical Security Pitfalls
Developers routinely introduce vulnerabilities through misconfiguration:
- The 'none' Algorithm Vulnerability: Never accept unverified header algorithms where the signature verification routine is bypassed. Always enforce strict verification on the server side.
- Weak Symmetric Secrets: Using short or predictable HMAC secrets exposes the token to offline dictionary brute-force attacks. Use cryptographically secure keys of at least 256 bits (HMAC-SHA256).
- Storing Sensitive Secrets in Claims: A JWT payload is merely Base64Url-encoded, not encrypted. Anyone who intercepts the token can decode and inspect its contents immediately.
3. Where to Safely Store JWTs
Storing access tokens in localStorage or sessionStorage exposes applications to Cross-Site Scripting (XSS). An attacker injecting malicious JavaScript can exfiltrate stored tokens effortlessly.
The industry gold standard is to store access and refresh tokens inside HttpOnly, Secure, SameSite=Strict cookies. This ensures the browser automatically attaches credentials while completely isolating the token string from client-side script execution.