A rejected JWT can result from an expired timestamp, an unexpected audience, a signature mismatch, or a token that never reached the API. This guide provides a safe workflow for decoding a JWT locally, checking its claims and headers, separating inspection from cryptographic verification, and creating recurring checkpoints for authentication configuration.
Overview
JSON Web Tokens are compact strings commonly used to carry authentication or authorization data between a client and an API. A signed JWT normally contains three Base64URL-encoded parts separated by periods: a header, a payload, and a signature. The header describes the token type and signing algorithm; the payload contains claims; and the signature allows a verifier to detect changes to the first two parts.
A JWT decoder can reveal the header and payload without proving that the token is authentic. That distinction is central to troubleshooting. Decoding helps you answer questions such as whether exp is in the past or whether aud contains the API identifier you expected. Verification uses a trusted key and validation rules to establish whether the token should be accepted.
| Operation | What it tells you | What it does not prove |
|---|---|---|
| Decode | Displays readable header and payload values | That the token was issued by a trusted party or was not altered |
| Signature verification | Checks the signature with the expected key and algorithm | That every application policy is satisfied |
| Claim validation | Checks issuer, audience, time limits, and required claims | That the request has the right business permissions unless those are checked separately |
For an initial investigation, use a redacted token or a locally run decoder whenever possible. Do not paste production access tokens, refresh tokens, private keys, or copied authorization headers into an untrusted online tool. Treat a bearer token as a credential: if it may have been exposed, follow your normal revocation or rotation procedure.
What to track
1. Capture the failure context
Start with the exact request and response. Record the endpoint, HTTP method, status code, server-side error category, environment, and approximate time. Confirm that the client actually sent the expected header, usually in the form Authorization: Bearer <token>. Browser developer tools, an API client, or request logging with secrets removed can help establish whether the problem is in token creation, transport, or validation.
Never log the complete token in ordinary application logs. If correlation is required, record a short non-reversible identifier generated for diagnostics, or log only safe metadata after carefully considering whether it could identify a credential.
2. Inspect the header
The alg value indicates the intended signing algorithm, while kid may identify a key in a rotating key set. Compare these values with the server configuration and the issuer's documented metadata. The API should choose an allowed algorithm from its own configuration rather than blindly trusting an algorithm value supplied by the token.
An unexpected algorithm, missing key identifier, or key identifier that no longer exists can point to a deployment mismatch or incomplete key rotation. Avoid fixing this by accepting arbitrary algorithms or disabling signature checks.
3. Inspect time-related claims
The exp claim is the expiration time, expressed as a numeric date. A token is normally rejected after that time. The nbf claim means not before, and iat indicates when the token was issued. Compare these values with the server's current UTC time, not only the clock shown on your development laptop.
A JWT expiration error can be caused by genuine expiry, a unit mistake such as milliseconds being treated as seconds, or clock drift between services. A small, explicitly configured clock-skew allowance may help with ordinary synchronization differences, but it should not become a way to accept substantially stale credentials.
4. Check identity and authorization claims
The iss claim identifies the issuer and should match the configured trusted issuer exactly, including relevant scheme, host, and path conventions. The aud claim identifies the intended recipient. A token issued for one API, frontend, or environment may be validly signed but still inappropriate for another.
Review required claims such as sub, scopes, roles, or permissions according to your application contract. A successful signature check does not automatically grant access. Your endpoint still needs to apply authorization rules and verify that the authenticated subject may perform the requested action.
5. Verify with the same rules used by the API
Use the validation function from the JWT library already approved for your service, configured with a fixed set of acceptable algorithms and the expected issuer, audience, and key source. Keep verification separate from decoding in both code and mental models. A useful diagnostic sequence is:
- Confirm the token is present and has the expected three-part structure.
- Decode the header and payload for inspection only.
- Retrieve the trusted public key or configured verification secret through the service's normal mechanism.
- Verify the signature and explicitly validate time, issuer, audience, and required claims.
- Apply endpoint-specific authorization checks after authentication succeeds.
Library APIs differ by language and version, so check the documentation for the version used by your application rather than copying an old snippet. Keep secrets and signing keys in the environment or secret-management system, not in source code. The environment variables and secrets management guide provides a broader checklist for separating configuration across environments.
Cadence and checkpoints
JWT troubleshooting should not happen only after an outage. Add lightweight checkpoints to the maintenance routine for each service that issues or validates tokens.
- Per deployment: Test a valid token, an expired token, a token with the wrong audience, and a token signed with an unacceptable algorithm. Confirm that failures produce useful internal diagnostics without exposing token contents.
- Monthly or quarterly: Review issuer and audience configuration, token lifetime decisions, clock synchronization, key identifiers, and the behavior of authentication middleware in each environment.
- Before key rotation: Confirm how new keys are published, how validators refresh them, how long old keys remain available, and whether both old and new tokens behave as expected during the transition.
- After dependency updates: Re-run authentication tests when changing a JWT, OAuth, API gateway, framework, or HTTP middleware package. Pay particular attention to defaults for algorithms, claim validation, and clock tolerance.
- After an incident: Preserve the safe diagnostic facts: error category, issuer, audience, key identifier, environment, and timing. Remove or rotate any credential that may have entered logs, tickets, chat, or debugging tools.
Keep these checks alongside API contract tests rather than relying on manual inspection. The OpenAPI documentation checklist can help keep authentication expectations visible to teams that consume the API.
How to interpret changes
Patterns in failures usually narrow the search:
| Observed message or behavior | Likely area to investigate |
|---|---|
| Missing authorization or token not found | Client storage, request construction, proxy behavior, or a missing header |
| Malformed token | Truncation, quoting, whitespace, wrong token type, or a value copied from the wrong response field |
| Signature invalid | Wrong key, wrong algorithm configuration, altered token, stale key set, or issuer mismatch |
| Token expired | exp, server clock, token lifetime, refresh flow, or seconds-versus-milliseconds conversion |
| Not yet valid | nbf or clock skew between the issuing and validating systems |
| Invalid issuer or audience | Environment configuration, service URL conventions, or a token intended for a different resource |
| Authenticated but forbidden | Scope, role, tenant, resource ownership, or endpoint authorization logic |
Do not assume that a 401 and a 403 have identical causes. In many systems, 401 indicates that authentication is missing or failed, while 403 indicates that authentication succeeded but authorization did not. Exact behavior depends on the framework and API contract, so use the service's documented semantics and server-side diagnostics.
When several services fail at once, prioritize shared dependencies: identity-provider configuration, key discovery, DNS or network access to the key endpoint, time synchronization, and recently changed environment variables. When only one client fails, compare its token claims and header construction with a known-good client before changing server validation.
When to revisit
Revisit this JWT debugging guide on a monthly or quarterly cadence, and immediately after changes to authentication libraries, identity-provider settings, signing algorithms, key rotation, API gateway rules, token lifetimes, or deployment environments. It is also worth returning whenever a login flow changes from one token type to another, such as introducing refresh tokens or service-to-service credentials.
Use this practical runbook when an API rejects a token:
- Redact the credential and record the endpoint, status, environment, and time.
- Confirm that the client sends the token in the expected header and that intermediaries do not remove or rewrite it.
- Decode locally to inspect
alg,kid,iss,aud,exp,nbf, and relevant authorization claims. - Compare those values with the validator's configured issuer, audience, allowed algorithms, clock tolerance, and required claims.
- Run cryptographic verification with the trusted key, then test endpoint authorization separately.
- Document the finding without storing the token, and update automated tests if the failure exposed a gap.
JWTs are useful because they make claims portable, but portability does not remove the need for strict validation and careful secret handling. Treat decoding as observation, verification as proof of integrity, and authorization as a separate policy decision. That separation makes authentication failures easier to diagnose without turning debugging into a new security risk.