When an API rejects a JWT, the cause may be an expired claim, a mismatched issuer, a rotated signing key, or a validation rule that differs between services. This workflow shows how to inspect a token safely, separate decoding from verification, trace the request across the identity and API layers, and fix common 401 and 403 failures without weakening authentication controls.
Overview
A JSON Web Token (JWT) is usually a compact string with three dot-separated parts: a header, a payload, and a signature. The header describes the token type and signing algorithm. The payload contains claims such as the subject, issuer, audience, and expiration time. The signature allows a verifier to check that the signed content was not altered and that it was produced by a trusted key.
These parts are encoded with base64url. Encoding is not encryption, so anyone who possesses a JWT can generally decode its header and payload. Do not place passwords, private keys, session secrets, or other sensitive data in claims. Treat access tokens as credentials even when the payload appears harmless.
A JWT decoder is useful for inspecting structure and claims, but decoding alone does not prove that a token is authentic. A token can be syntactically valid and still have an invalid signature, an untrusted issuer, the wrong audience, or an expired exp claim. Verification must occur through the API’s configured JWT library, identity platform, or a controlled local test environment with the correct public key.
The most useful first distinction is between token parsing and token validation:
- Parsing: Can the service split the token, decode its segments, and read valid JSON?
- Cryptographic verification: Does the signature match the token content and a trusted key?
- Claim validation: Are issuer, audience, time, subject, and other required claims acceptable for this API?
- Authorization: Does the authenticated identity have permission to perform the requested operation?
Step-by-step workflow
1. Capture the failure without exposing the credential
Record the request path, HTTP method, response status, correlation ID, service name, and approximate timestamp. In logs, redact the token itself. If a production token was copied into a ticket, chat message, browser extension, or online tool, treat it as exposed and revoke or replace it according to your identity provider’s process.
Check whether the client sent the expected authorization format, usually an HTTP header containing a bearer token. Look for accidental quotes, line breaks, duplicated prefixes, URL encoding, or a token from a different environment. A valid token sent to the wrong API can fail audience or issuer checks.
2. Inspect the token structure
Use a trusted local decoder or a carefully selected JWT decoder to inspect the header and payload. For production credentials, prefer local tooling or a development token. Confirm that the value has three non-empty segments separated by periods and that the decoded header and payload are valid JSON.
Review the header for the declared algorithm and key identifier, often represented by alg and kid. The header is attacker-controlled input until the signature has been verified. The server should use a configured algorithm allowlist rather than blindly trusting the token’s alg value.
3. Check time-based claims
Compare the current UTC time with exp, the expiration time. Also inspect nbf, which prevents use before a specified time, and iat, which indicates when the token was issued. These values are commonly represented as Unix timestamps, not formatted date strings.
An expiration failure may come from a genuinely old token, a client reusing a cached token, or clock drift between the identity provider, API gateway, and application host. Confirm that systems synchronize time consistently. If the library supports a clock-tolerance setting, keep it narrow and document why it exists; broad tolerance can extend the usable lifetime of stolen credentials.
4. Validate issuer and audience
The iss claim identifies the token issuer. The API should compare it with the exact issuer configured for that environment. Similar-looking URLs, trailing slashes, tenant identifiers, and regional identity endpoints can matter.
The aud claim identifies the intended recipient. An access token issued for one API, resource server, or environment may be correctly signed but unsuitable for another. Confirm what the API expects and compare it with the token’s audience, including whether the claim is a string or an array where supported by the validation library.
5. Verify the signature with the right key
A signature failure means the verifier could not establish that the token was signed by a trusted key over the exact header and payload received. Common causes include a wrong public key, a malformed token, a changed payload, an unsupported algorithm, or a key rotation that the verifier has not yet discovered.
Check the identity provider’s published key set or configured verification key, then confirm that the token’s kid maps to the expected key. Do not solve a signature problem by disabling verification or accepting every algorithm. For symmetric algorithms, confirm that both sides use the same intended secret without logging it. For asymmetric algorithms, confirm that the API uses the public key while the identity provider retains the private signing key.
6. Investigate key rotation and caching
When signing keys change, a verifier may temporarily have stale key material. Review key-fetch logs, cache duration, refresh behavior, network access, and the handling of an unfamiliar kid. The correct fix is to make key discovery and refresh reliable, not to trust an unknown key or skip signature checks.
Also check for environment mix-ups: a staging API may be configured with production issuer settings, or a gateway may use a different identity provider configuration from the application behind it.
7. Separate authentication from authorization
A 401 response commonly indicates that the request could not be authenticated, although exact behavior depends on the API. A 403 commonly indicates that authentication succeeded but the identity lacks permission. These statuses are clues rather than proof.
After signature and claim validation pass, inspect scopes, roles, groups, tenant identifiers, and resource ownership rules. Confirm that the API expects the same claim names and value formats produced by the identity provider. A token can contain a role while still failing if the application expects a scope, or if a gateway strips claims before forwarding the request.
Tools and handoffs
Use tools according to the sensitivity of the token and the stage of the investigation:
- JWT decoder: Inspect header and payload structure. Use test tokens or local tooling for sensitive environments. Remember that decoding is not verification.
- API client: Reproduce the request with a newly issued development token and compare headers, method, URL, and environment variables.
- Identity-provider console or logs: Confirm token issuance time, client, scopes, issuer, audience, and signing-key status.
- API and gateway logs: Search by correlation ID. Log validation failure categories, not raw tokens, secrets, or full authorization headers.
- JWKS or key configuration: Check key identifiers, retrieval errors, cache behavior, and rotation handling without exposing private key material.
When the JWT is part of a broader API contract, document expected claims and validation rules alongside the endpoint definition. The OpenAPI contract testing workflow can help teams detect changes that affect consumers before deployment. For payload and schema problems after authentication succeeds, use the JSON Schema validation guide to separate authorization failures from request-data failures.
Quality checks
Before declaring the issue fixed, test the complete validation path with controlled tokens:
- A valid token from the correct issuer, audience, environment, and key.
- An expired token and a token that is not yet valid.
- A token signed with an untrusted key or altered after signing.
- A token with the wrong issuer or audience.
- A token with missing, malformed, or insufficient scopes and roles.
- A token using an algorithm outside the service’s approved allowlist.
- A request with no token, a malformed authorization header, and a token from another service.
Confirm that failures are observable without leaking credentials. Error responses should provide clients with a useful category, while detailed diagnostics should remain in protected server logs. Verify that the service does not accept unsigned tokens, silently ignore failed claim checks, or use a fallback key when the expected key is unavailable.
Review automated tests after changing identity-provider configuration, JWT libraries, gateway settings, or key-discovery code. Include integration tests for issuer, audience, expiration, clock tolerance, algorithm restrictions, and key rotation. Apply these checks in a controlled pipeline; the CI/CD pipeline checklist provides related guidance for safer validation and deployment workflows.
When to revisit
Revisit this debugging workflow whenever an identity provider changes, signing keys rotate, a JWT or API library is upgraded, a gateway is introduced, or an endpoint moves between environments. Also review it after recurring 401 or 403 incidents, changes to scopes or roles, updates to token lifetimes, or reports that different services validate the same token differently.
For a practical incident routine, save a redacted checklist with these questions: Which service issued the token? Which API received it? What are the exact issuer and audience values? Is the token within its valid time window? Does the key identifier resolve to a trusted current key? Did signature verification pass? Which authorization rule rejected the request? Was the failure introduced at the client, gateway, or application layer?
Work through those questions in order, issue a fresh test token after configuration changes, and record the final validation settings in the service’s security documentation. That approach fixes the immediate integration problem while preserving the controls that make JWT authentication useful.