DEV Community

GoToolsOnline
GoToolsOnline

Posted on

Decoding a JWT is not verifying it

A JSON Web Token looks encrypted, but most JWTs are only signed. The header and payload are just Base64URL-encoded JSON, which anyone can read. That leads to a common, dangerous mix-up: decoding a token and trusting a token are different things.

What decoding gives you

Split the token on the dots: header.payload.signature. Base64URL-decode the first two parts and you have JSON, for example:

{ "alg": "RS256", "typ": "JWT", "kid": "abc123" }
{ "sub": "42", "exp": 1767225600, "iat": 1767222000, "aud": "my-api" }
Enter fullscreen mode Exit fullscreen mode

That is handy for debugging ("why is this request 401?"), but it proves nothing. Anyone can create a token with "sub": "1" and "role": "admin" in it.

What verifying means

Your server must:

  1. Check the signature with the right key: a shared secret for HS256, or the issuer's public key for RS256/PS256/ES256/EdDSA (often fetched from a JWKS endpoint and matched by kid).
  2. Pin the allowed algorithms on the server side. Never let the token's own "alg" header decide, and never accept "alg": "none".
  3. Check exp (expiry) and nbf (not before), allowing a small clock skew of a few seconds to a minute.
  4. Check iss (issuer) and aud (audience), so a token issued for another app can't be replayed against yours.

Use your platform's maintained JWT library for this rather than hand-rolled code.

Don't put secrets in the payload

Because the payload is readable by anyone who has the token, never put passwords, API keys or personal data you wouldn't show the user in it. If you truly need confidentiality, that is JWE (encrypted tokens), a different format.

Debugging tips

  • "Token expired" right after login usually means clock skew between servers, or exp set in milliseconds instead of seconds.
  • exp and iat are Unix timestamps in seconds; convert them to readable dates before guessing.
  • Be careful pasting production tokens into random websites; a live token is a credential until it expires.

Disclosure: I run GoToolsOnline. Its free JWT decoder shows the header and claims with readable dates, flags expiry, nbf and alg none, and can verify the signature against a secret, PEM key, JWK or JWKS: https://gotoolsonline.com/tools/jwt-decoder

Top comments (0)