JWT

JSON Web Token (JWT) are a popular choice for securing APIs and web applications. However, they’re often misunderstood or misused. Draw the three-segment structure and the use/don’t-use line before adopting it.
What is a JWT?
A JWT (most commonly serialized as a signed JWS) is a Base64URL-encoded three-part string header.payload.signature. The header declares the token type and signing algorithm, the payload carries claims, and the signature guarantees integrity and issuer authenticity.
Key boundary: a standard signed JWS does not encrypt the payload—anyone holding the token can Base64URL-decode and read its claims. Never put passwords, secrets, or sensitive PII in a JWS payload; confidentiality comes from HTTPS/TLS in transit (or JWE when encrypted tokens are required).
JWT Structure
Decoded structure of a signed JWT:
{
"header": {
"typ": "JWT",
"alg": "HS256"
},
"payload": {
"sub": "1234567890",
"iss": "https://auth.example.com",
"aud": "https://api.example.com",
"exp": 1795200000
},
"signature": "<HMAC-SHA256 signature>"
}Key distinctions:
typ: "JWT"identifies the token media type;Beareris the HTTP transport scheme (Authorization: Bearer <token>), not a JWT headertyp.HS256uses a symmetric shared secret (HMAC-SHA256); when verifiers should not hold signing keys, use an asymmetric algorithm such asRS256,ES256, orEdDSA.
How JWTs Work and Verification Order
- Issuance: After authentication, the issuer signs a JWT with
iss,aud,sub, andexpusing a pinned algorithm. - Transport: The client sends the token over HTTPS in
Authorization: Bearer <token>. - Verification (following RFC 8725):
- Enforce a server-side allowlist of expected signing algorithms (never trust
alg: noneor attacker-chosen algorithm switches), then verify the signature. - Validate standard claims:
exp,nbf,iss(issuer), andaud(audience). - A valid signature proves authenticity, not authorization—application logic must still check resource-level permissions for
sub.
- Enforce a server-side allowlist of expected signing algorithms (never trust
Advantages of JWTs
- Stateless verification: Resource servers can verify token authenticity and expiry without hitting a session store on every request.
- Compact: Fits cleanly in HTTP headers, mobile clients, and service-to-service calls.
- Tamper-evident: Signatures/MACs detect any modification to header or payload (confidentiality still relies on TLS or JWE).
- Extensible claims: Carries standard claims plus small non-sensitive metadata.
Disadvantages of JWTs
- Key compromise: If the signing secret or private key leaks, attackers can forge arbitrary valid tokens—rotate keys and guard signing material.
- Readable payload: Signed JWS payloads are Base64URL-encoded, not encrypted.
- Revocation overhead: Issued tokens remain valid until
exp; immediate logout or revocation requires short TTLs plus refresh-token rotation or a revocation list.
When to Use JWTs
JWTs fit stateless identity assertions across services (OAuth 2.0 / OIDC access tokens, microservice auth, mobile APIs). When you need instant server-side revocation or rich mutable session state, server-side sessions are usually simpler and safer.
Conclusion
JWTs are stateless, compact, and tamper-evident, fitting API, web, and mobile auth. Remember that signing ensures integrity rather than confidentiality, and signature verification is separate from authorization.