DEV Community

TheOmniKit
TheOmniKit

Posted on Originally published at theomnikit.com AI-assisted

decoding a JWT is not the same as checking it

okay so i keep seeing people paste a JWT into a decoder, see exp in the future and think the token is fine. it is not, decoding just reads it.

a JWT is three parts joined by dots, header, payload, signature. the first two are Base64URL so anyone can read them, no key needed. that is also why you never put anything secret in the payload, make sure of this okay.

what actually makes it valid is the signature check with the issuer's secret or public key. only after that you look at the claims. exp must still be ahead of now, nbf already passed, iss and aud must be what your service expects. skip aud and a token meant for another service can be replayed on yours.

three mistakes i see a lot:

1. trusting alg: none (unsigned token, library must refuse it)
2. letting the token pick the algorithm (RS256 -> HS256 trick with the public key)
3. never checking aud
Enter fullscreen mode Exit fullscreen mode

also exp, nbf and iat are seconds since 1970, not milliseconds. if you see a 13 digit exp the issuer has a bug.

i built a decoder that runs fully in your browser (WebCrypto, nothing is sent anywhere) and it also verifies HS256/RS256/ES256 signatures if you paste the secret or key. i built it so ya i'm biased, just try it with a dummy token: https://theomnikit.com/guides/decode-and-check-a-jwt?utm_source=devto&utm_medium=post&utm_campaign=decode-and-check-a-jwt

the RFC 8725 is the one to read if you want the full list of pitfalls.

drafted this with some AI help btw, then i cleaned it up.

originally on theomnikit: https://theomnikit.com/guides/decode-and-check-a-jwt?utm_source=devto&utm_medium=post&utm_campaign=decode-and-check-a-jwt

Top comments (0)