If you've worked with a modern login system or API, you've handled a JSON Web Token, the long string that starts with eyJ and travels in an Authorization: Bearer header. JWTs are popular because a server can check one without a database lookup: the token carries its own claims and a signature that proves who issued them. That convenience comes with sharp edges. Most JWT security incidents come from servers that trust what a token says without checking enough of it. This guide takes a real token apart and explains each piece, the signing algorithms, and the mistakes that keep turning up.

Key Takeaways
  • A signed JWT is three Base64url strings joined by dots: header.payload.signature. The header and payload are readable by anyone.
  • Decoding is not verifying. Only a valid signature, checked with a key you already trust, proves a token is genuine.
  • After the signature, a server must still check exp, nbf, iss and aud.
  • HS256 uses one shared secret. RS256, ES256 and EdDSA sign with a private key and verify with a public one.
  • Most real attacks exploit verifiers that trust the token's own alg header, accept none, or skip claim checks.
Advertisement

The Three Parts of a JWT

RFC 7519 defines a JWT as a set of claims carried inside a JSON Web Signature (JWS, RFC 7515) or, less often, a JSON Web Encryption object (JWE, RFC 7516). The everyday form is the JWS compact serialization:

BASE64URL(header) . BASE64URL(payload) . BASE64URL(signature)

Base64url is the URL-safe Base64 alphabet: - and _ instead of + and /, with no = padding. It is an encoding, not encryption; our Base64 guide explains the difference. Anyone who gets hold of a token can read every claim in it, so never put passwords, API keys or other secrets in a payload.

Worked Example: Decoding a Token by Hand

Here is the HS256 demo token from our JWT decoder, split at the dots. The decoder's HS256 example button loads exactly this token.

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 .eyJpc3MiOiJodHRwczovL2F1dGguZXhhbXBsZS5jb20iLCJzdWIiOiJ1c2VyLTEwMjQiLCJhdWQi... .Q1TruB2UG-IdW4dAWzGR6miRNXEtp61ON7XTXjDxi0Y

The first segment decodes to the header:

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

The second decodes to the payload, the claims set:

{ "iss": "https://auth.example.com", "sub": "user-1024", "aud": "api.example.com", "name": "Ada Example", "role": "editor", "iat": 1793404800, "nbf": 1793404800, "exp": 1793408400, "jti": "4f1c9a2e-7b3d-4e8f-9a61-2c5d8e0b7f13" }

The third segment is 43 Base64url characters, which decode to 32 bytes beginning 43 54 eb b8. That is the length of an HMAC-SHA-256 output. You can reproduce the decoding with nothing but a browser console:

const [h, p] = token.split('.'); const b64 = s => s.replace(/-/g, '+').replace(/_/g, '/'); JSON.parse(atob(b64(h))); // { alg: 'HS256', typ: 'JWT' } JSON.parse(atob(b64(p))); // the claims (fine for ASCII; use TextDecoder for UTF-8)

The times are NumericDate values, seconds since 1970-01-01 UTC, the same count our Unix time guide explains. 1793404800 is 2026-10-31 00:00:00 UTC, and exp is 3,600 seconds later, so this is a one-hour token. Verifying it means recomputing HMAC-SHA-256 over the exact ASCII text header.payload with the shared secret and comparing the result with the signature. In the decoder, the demo secret verifies. Change "editor" to "admin" and re-encode the payload, and the same check fails.

Registered Claims and What a Server Must Check

RFC 7519 registers seven short claim names. None is mandatory in the format itself, but most APIs require several of them:

ClaimMeaningServer check
issIssuerMust be an issuer you trust
subSubject (user or client ID)Unique within the issuer; use it as the identity
audAudience (string or array)Must include your service
expExpiration timeReject at or after this time
nbfNot beforeReject before this time
iatIssued atOptional age limit
jtiUnique token IDOptional replay or revocation lists

RFC 7519 lets implementers allow "some small leeway, usually no more than a few minutes" for clock skew on exp and nbf. Anything else in the payload, like name and role above, is a public or private claim the application defines. Collision-resistant public names are registered with IANA; OpenID Connect adds email, nonce and others.

Signing Algorithms: HS, RS, PS, ES and EdDSA

The alg header names one of the algorithms registered in RFC 7518, or EdDSA from RFC 8037.

HMAC: HS256, HS384, HS512

The issuer and verifier share one secret key. HMAC is fast and simple, but every party that can verify a token can also forge one, so HS256 suits a single service that issues and checks its own tokens. RFC 7518 requires a key at least as long as the hash output, 256 bits for HS256. A short human-chosen secret can be brute-forced offline from any token that leaks, because the attacker can test guesses against the signature without contacting your server. Generate the key randomly, for example 32 bytes from a cryptographic random generator.

Asymmetric: RS256, PS256, ES256, EdDSA

The issuer signs with a private key and publishes the public key, usually as a JSON Web Key Set (JWKS) at a well-known URL. Verifiers can check tokens but can't create them, which is why identity providers and OpenID Connect use these algorithms.

The Vulnerabilities That Keep Coming Back

1. Accepting alg: none

RFC 7519 defines "unsecured" JWTs with "alg": "none" and an empty signature. Some early libraries read the algorithm from the token and accepted these, so an attacker could strip the signature and edit the claims at will. RFC 8725, the JWT Best Current Practices, says libraries must let the application specify which algorithms are acceptable, and must not use any others.

2. Algorithm confusion (RS256 to HS256)

If a server verifies RS256 tokens with a generic verify(token, key) call that picks the algorithm from the header, an attacker can send a token marked HS256 and signed with HMAC, using the server's public RSA key as the secret. The library then "verifies" it with the same public key. The fix is the same as for none: pin the expected algorithm, and keep symmetric and asymmetric keys in separate code paths.

3. Not validating claims

A correctly signed token can still be the wrong token:

The OWASP JWT cheat sheet lists these checks.

4. Trusting keys the token points to

The jku and x5u headers point to a URL to fetch the key from, and jwk and x5c embed a key directly. A verifier that follows them is letting the attacker pick the key. Use only keys from your own configuration or your identity provider's JWKS, matched by kid. Treat kid itself as untrusted input: never put it into a file path or SQL query unescaped.

5. Weak secrets and long lifetimes

Because JWTs are usually stateless, a valid token can't easily be revoked before it expires. Keep access-token lifetimes short, often 5 to 15 minutes. Pair them with refresh tokens that the server can revoke, and rotate signing keys with a published kid so old keys can be retired.

Where to Store Tokens in a Browser

There is no perfect answer, only trade-offs:

Whichever you choose, preventing XSS matters more than where the token sits.

Signed vs. Encrypted Tokens

A JWS protects integrity: anyone can read it, nobody can change it undetected. A JWE (RFC 7516) protects confidentiality and has five parts:

Its header names a key-management algorithm such as RSA-OAEP-256 and a content cipher such as A256GCM. Use JWE only when the claims must stay secret from the client or intermediaries. Often the simpler design is to keep sensitive data on the server and put only an opaque ID in the token. The decoder recognizes JWEs and shows their header without attempting decryption.

Frequently Asked Questions

Is a JWT encrypted?

A normal signed JWT is not. Its header and payload are only Base64url-encoded and can be read by anyone. Only a JWE, the five-part form, is encrypted.

Can I verify a JWT without the secret or key?

No. You can decode it, but verifying an HS256 token needs the shared secret, and verifying an RS256, ES256 or EdDSA token needs the issuer's public key, usually published in its JWKS.

How long should a JWT be valid?

Access tokens are usually kept short, minutes rather than days, because they're hard to revoke. Use a revocable refresh token to get new access tokens.

Why does my JWT signature fail to verify?

The usual causes are using the wrong key or algorithm, a secret encoded differently from the issuer's (text vs. Base64 bytes), a token modified or re-serialized in transit, and ES256 signatures in DER format instead of raw r‖s. The JWT decoder reports which of these it can detect.

What is the difference between a JWT and a session cookie?

A traditional session cookie holds a random ID that the server looks up in its own store, so revoking it is instant. A JWT carries the claims itself and is checked by signature, which scales without shared state but makes revocation harder. JWTs can also be sent inside cookies, so the two aren't mutually exclusive.

Sources: RFC 7519 (JWT); RFC 7515 (JWS); RFC 7516 (JWE); RFC 7518 (JWA); RFC 8725 (JWT BCP); OWASP JWT Cheat Sheet; OWASP HTML5 Security Cheat Sheet.