JWT: understand token claims and validate them safely
Learn what JWT does and how to check its signature, issuer, audience, time limits, and intended token type before trusting it.
JWT: understand token claims and validate them safely
JSON Web Token (JWT) is a compact format for carrying JSON claims between parties. A common signed form has three Base64url-encoded parts separated by periods: header, claims, and signature or message authentication code. Base64url is encoding, not encryption: assume anyone holding a signed JWT can read its claims. A JWS signature can reveal tampering; an encrypted JWE can protect confidentiality, but signing and encryption serve different purposes.
Registered claim names include iss (issuer), sub (subject), aud (intended recipient), exp (expiry), nbf (not-before), iat (issued-at), and jti (token identifier). Their presence does not make them mandatory: the token profile and application must define required claims and reject missing or invalid values.
Parsing a token only reveals its contents. It does not prove who issued it. Even a valid signature is not enough to authorize a request. Use keys configured for a trusted issuer, restrict allowed algorithms, verify the signature or authenticated decryption, then check the expected issuer, this service's audience, time claims, token type, and application-specific requirements. Do not let an untrusted header choose an algorithm or trigger arbitrary key downloads.
Validation checklist
Pin the expected issuer, purpose, token type, required claims, and allowed algorithms in server configuration; never trust the token's `alg` as policy.
Select keys only from the trusted issuer's configured key set. Treat `kid` only as a bounded key selector, not as permission to fetch a URL supplied by the token.
Reject the whole token if signature/MAC verification or JWE decryption and authentication fails. Reject algorithm/key combinations outside policy, including `none` when not explicitly allowed.
Check exact issuer and audience, `exp`, and `nbf` when present. If clock leeway is allowed, set a small maximum and synchronize server clocks.
Prevent token substitution: give ID tokens, API access tokens, and internal tokens distinct types and mutually exclusive validation rules.
Make authorization from verified claims plus the request, resource, tenant, user state, and server policy. Do not automatically turn a role string into broad access.
On failure, stop before authorization and log a safe reason without storing the raw token or sensitive claims. To diagnose a legitimate rejection, compare issuer, key rotation/cache, algorithm policy, clock, audience, and the token's actual profile; do not weaken validation wholesale.
JWT is a format, not a login session or permission. OAuth access tokens may be JWTs or opaque strings; OpenID Connect ID tokens are JWTs but are not API access tokens. A self-contained JWT may remain usable until expiry after an account or permission change unless a separate revocation or status check exists. Choose short lifetimes or another revocation design based on risk and operational cost. Minimize personal data in tokens and handle them as secrets in transit and storage.
Sources
[RFC 7519: JSON Web Token](https://www.rfc-editor.org/rfc/rfc7519.html)
[RFC 8725: JSON Web Token Best Current Practices](https://www.rfc-editor.org/rfc/rfc8725.html)
[RFC 9068: JWT Profile for OAuth 2.0 Access Tokens](https://www.rfc-editor.org/rfc/rfc9068.html)