JSON Web Tokens (JWTs, standardised in RFC 7519) carry signed identity and permission claims between an authentication server and the APIs that trust it. Because a JWT looks like an opaque string of random characters, many developers assume its contents are encrypted — they are not. Anyone holding a standard signed JWT (JWS) can decode its header and payload instantly. This guide dissects a JWT’s three segments, explains every standard claim, compares HS256 vs RS256/ES256 verification, lists the real-world security vulnerabilities that break JWT auth, and provides a step-by-step debugging checklist for 401/403 errors.
Anatomy of a JWT: three dot-separated segments
A signed JWT (JWS) consists of three Base64URL-encoded parts joined by periods (.):
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9 ← 1. Header (JSON metadata)
.eyJzdWIiOiIxMjM0IiwibmFtZSI6IkFzaGEiLCJleHAiOjE5OTk5OTk5OTl9 ← 2. Payload (JSON claims)
.qQh4rMFEqk8Hc7Z_8b2kP1x… ← 3. Signature (cryptographic MAC or signature)
- Header: Declares the signing algorithm (
alg, such asHS256,RS256, orES256), token type (typ: "JWT"), and often a key identifier (kid) so the verifying API knows which key to pick during key rotation. - Payload: A JSON object containing claims — statements about the authenticated subject (user ID, tenant, roles, scopes) and token lifetime.
- Signature: Computed over the exact ASCII string
base64url(header) + "." + base64url(payload)using a secret or private key. Changing a single byte of the header or payload invalidates the signature immediately.
SnipFerry’s JWT Decoder splits any token (even with a leading Bearer prefix), formats the header and payload, converts Unix timestamps to UTC and local dates, checks active/expired status, and verifies HS256, HS384, and HS512 signatures locally using your browser’s Web Crypto API when you enter a shared secret.
Registered (standard) JWT claims
| Claim | Full name | Purpose & validation rule |
|---|---|---|
iss | Issuer | Identifies the auth server that issued the token (e.g. https://auth.example.com); must match expected issuer exactly |
sub | Subject | Unique, stable identifier for the user or service principal (prefer immutable IDs over emails that can change) |
aud | Audience | Intended recipient API(s); an API must reject tokens minted for a different service |
exp | Expiration Time | Unix timestamp (seconds) after which the token must be rejected |
nbf | Not Before | Unix timestamp (seconds) before which the token is not yet valid |
iat | Issued At | Unix timestamp (seconds) when the token was minted; useful for forced logout checks |
jti | JWT ID | Unique token identifier (often a UUID) used for one-time tokens or revocation denylists |
All time claims are Unix timestamps in seconds (not milliseconds!). See Unix Timestamps and Cron Explained for converting epoch values and avoiding timezone bugs.
HS256 vs RS256 vs ES256: choosing a signing algorithm
| Algorithm | Cryptography | Key model | Best for |
|---|---|---|---|
HS256 / HS384 / HS512 | Symmetric HMAC-SHA | One shared secret signs and verifies | Single backend or tightly coupled internal services |
RS256 | Asymmetric RSA-SHA256 | Private key signs; public key (JWKS) verifies | Microservices, OAuth2/OIDC providers (Auth0, Google, Okta) |
ES256 | Asymmetric ECDSA P-256 | Private key signs; smaller/faster than RSA | Modern APIs, edge workers, mobile & IoT tokens |
With asymmetric keys (RS256/ES256), your identity provider keeps the private key locked down and publishes public keys at /.well-known/jwks.json. Downstream microservices verify signatures using the public key without ever gaining the ability to forge new tokens.
JWS (signed) vs JWE (encrypted)
Most JWTs you see in APIs are JWS (3 segments, readable payload, tamper-proof signature). When the payload itself contains confidential fields that even the client must not read, RFC 7516 defines JWE (JSON Web Encryption), which has 5 dot-separated segments and cannot be inspected without the decryption key.
Critical JWT security pitfalls to avoid
- Accepting
alg: "none"or trusting the token’s header. Never let the incoming JWT header choose how your server verifies it. Hard-code an explicit allowlist of algorithms (for examplealgorithms: ["RS256"]) in your JWT verification library. - RS256 → HS256 algorithm confusion. If a server expects RS256 but accepts HS256, an attacker can grab the public RSA key and use it as an HMAC shared secret to forge arbitrary admin tokens. Pinning the expected algorithm blocks this completely.
- Putting secrets or PII in the payload. Treat JWT claims as public to the user and any proxy logging headers. Never embed passwords, API secrets, or sensitive personal records in a JWS.
- Weak HS256 secrets. Short secrets like
"secret"or"dev123"can be cracked offline with GPU tools in seconds. Use at least 256 bits (32+ random bytes) generated with a CSPRNG — see Hashes, UUIDs and Secure Passwords. - Long-lived access tokens with no revocation plan. Stateless JWTs cannot be easily revoked before
exp. Keep access tokens short-lived (5–15 minutes) and pair them with rotating, revocable refresh tokens stored server-side. - Storing tokens in
localStorage. Any XSS vulnerability on your origin can exfiltratelocalStoragetokens instantly. For browser apps, preferHttpOnly; Secure; SameSite=Laxcookies (or a Backend-for-Frontend pattern). - Missing
audorisschecks. Skipping audience validation allows a valid token from your staging environment or a sibling app to authenticate against your production API.
Step-by-step 401 / 403 debugging checklist
- Strip the prefix and count dots: A valid JWS has exactly two dots (3 segments). Missing dots or extra whitespace/newlines break parsing immediately.
- Inspect
expandnbf: Most “sudden 401” failures are expired tokens or server clock drift (allow 30–60 seconds of clock skew on verifiers). - Verify
issandaudverbatim: Trailing slashes matter —https://auth.example.com/does not equalhttps://auth.example.com. - Match header
algandkid: After a key rotation, stale JWKS caches on the API server reject tokens signed by the newkid. - Check authorisation claims: Confirm the expected
scope,roles, orpermissionskey name and type (array vs space-separated string) match what your middleware reads. - Verify the signature locally: For HS256 tokens during local dev, paste the dev secret into SnipFerry’s JWT Decoder to confirm whether the secret or payload encoding differs. Never paste live production tokens into third-party servers — decode locally and prefer expired or test tokens.
Want to see how Base64URL encoding works inside each JWT segment? Read Base64 Explained.