JWT - JSON Web Tokens for Authentication
About 2 min read
A JWT (JSON Web Token) is a token that encodes JSON-formatted data, signed or encrypted, into a compact string. Standardized as RFC 7519 in 2015, it is used as an OAuth 2.0 access token and as an assertion for single sign-on (SSO). It is characterized by enabling stateless authentication that does not retain session information on the server side.
3-Part Structure - Header.Payload.Signature
A JWT consists of three parts separated by dots (.). Each part is Base64URL-encoded and cannot be read directly by humans, but once decoded the contents can be inspected as JSON.
The Difference Between JWS and JWE
The term JWT is sometimes used to mean a "signed token," but in the specification there are two types: JWS (JSON Web Signature) and JWE (JSON Web Encryption).
The payload is merely Base64URL-encoded, so anyone can read its contents by decoding it. The signature makes it possible to detect tampering, but it cannot keep the contents confidential.
The payload itself is encrypted, so only those who hold the decryption key can read its contents. It takes a 5-part structure (Header, Encrypted Key, IV, Ciphertext, Authentication Tag). It is used when confidential information must be included in the token.
With JWS, the premise is that no personal or confidential data is included in the payload.
Benefits and Risks of Stateless Authentication
With stateless authentication using JWTs, the server does not retain session information. Because authentication is completed simply by verifying the token signature, there is no need to manage session tokens in Redis or a database, making horizontal scaling easy.
| Aspect | JWT (stateless) | Cookie session (stateful) |
|---|---|---|
| Server-side state | None (the token itself carries the information) | Present (a session store is required) |
| Scalability | High (verifiable on any server) | A session-sharing mechanism is required |
| Immediate revocation | Difficult (a blacklist scheme is required) | Easy (simply delete the session) |
| Token size | Large (hundreds of bytes to several KB) | Small (only the session ID) |
| CSRF resistance | Resistant when using the Authorization header | Countermeasures such as the SameSite attribute are required |
One risk is that "a JWT, once issued, cannot be revoked immediately." Even if a user changes their password, a leaked JWT can continue to be used until it expires. To mitigate this problem, one approach is to set a short expiration for the access token and reissue it with a refresh token. For requirements where immediate revocation is mandatory, options include a server-side blacklist and cookie-based session management.
Representative Attacks That Target JWTs
An attack that rewrites the alg field in the Header to "none" to bypass signature verification. Some libraries had implementations that accepted alg:none. As a countermeasure, explicitly whitelist the algorithms permitted on the server side.
An attack that rewrites the alg of a token signed with RS256 (asymmetric key) to HS256 (symmetric key) and uses the public key as the HMAC secret key. Because the public key is available to anyone, an attacker can generate a valid signature.
If the HS256 secret key is a short string or a guessable value, the key can be identified by brute force. Use a random key of at least 256 bits.
The techniques of session hijacking apply to JWTs as well. The fundamentals of encryption are explained in a separate term. Please also refer to countermeasures against session hijacking, defenses against session token theft, and best practices for API key management.
Was this article helpful?
Related Terms
- Pretexting - Fabricated Scenarios for Data TheftPretexting is a social-engineering technique that fabricates a scenario to extract information. Impersonating IT support or HR is typical, and it often serves as the opening move in a BEC attack.
- HSTS - HTTP Strict Transport SecurityHSTS is a security header that forces browsers to use HTTPS for all connections to a domain. It prevents SSL-stripping attacks and can be preloaded into browsers for first-visit protection.
- Cyber Kill Chain - Seven Phases of an AttackThe Cyber Kill Chain is a framework that breaks cyberattacks into seven phases. Proposed by Lockheed Martin in 2011, it helps defenders systematically plan countermeasures at each stage.
- OSINT - Open Source IntelligenceOSINT gathers intelligence from publicly available sources such as social media, WHOIS, and DNS records. Both attackers in reconnaissance and defenders in threat analysis use it.