Adamant Solutions

JWT "invalid signature": why verification fails

The signature check failed: the library computed the signature with the key it has and it does not match the signature in the token. That means the key is wrong, the token was changed, or the algorithm or key format is not what you expect. Answer the questions to find which. (Never "fix" this by turning verification off.)

Find your cause

All the causes and fixes

Look at the header first

  • Decode the token with the JWT inspector (it only decodes; it does not verify). The alg and kid tell you which of the other paths applies.

The shared secret differs

  • With HS256 the verifier needs the same secret as the signer. Compare the values from the real runtime config, not from the repository.
  • A different environment (test vs production) or an old secret after rotation is the most common cause.
  • Compare a hash of both secrets, not the secrets themselves, if you must check across systems.

The secret is interpreted differently

  • Some libraries expect the secret as raw text, others as base64-decoded bytes. Using one on the signing side and the other on the verifying side gives a perfectly valid-looking but different key.
  • Trailing newlines, surrounding quotes and whitespace from env files and secret managers are a classic trap: trim the value.

The signing key was rotated or the JWKS is cached

  • The token was signed with a new key, but your verifier still uses the old one. Look up the key by the token's kid.
  • Refresh the JWKS when you see an unknown kid (with a rate limit so attackers cannot make you fetch endlessly), and respect cache headers.
  • Keep old keys in the issuer's JWKS for as long as old tokens are valid.

The key belongs to a different issuer or environment

  • Check the iss claim and the issuer's JWKS URL (from its discovery document). Multi-tenant providers have one key set per tenant.
  • Production tokens will not verify with test keys and vice versa.

The token was changed on the way

  • Copy and paste can add spaces, line breaks or quotes; URL encoding can alter characters; a "Bearer " prefix must not be part of the token.
  • Header size limits in proxies or servers can truncate long tokens: check for a token that ends abruptly.
  • A signature check fails if even one character of the header or payload changed.

Key format or algorithm mismatch

  • The library may expect a PEM public key but receive a certificate, a JWK or the wrong key in a bundle. Convert to the format it expects.
  • Pin the accepted algorithms in the verifier instead of trusting the alg in the token header.
  • ES256 signatures use a raw (r||s) format in JWT, not DER: converting wrongly produces failures.
  • RS256 vs PS256 are different algorithms even with the same key.

Frequently asked questions

Does invalid signature mean the token was forged?

Usually not: it is far more often a wrong or outdated key, a key-format problem or a token damaged on the way. But treat an unexplained failure on a token you did not expect as suspicious.

Can I just disable signature verification?

No. Without verification anyone can create a token with any claims. Fix the key instead.

Why does it fail only sometimes?

Often key rotation with a cached JWKS, or several issuers or environments behind one load balancer.

About this page

Nothing you select here is sent anywhere: the questions run in your browser. If you paste anything into the related tools, read their privacy notes and use test data. See the privacy notice.

Related: JWT inspector · OAuth2 error doctor · JWT expired · All error guides

Still stuck, or designing the flow for your API?

We configure and review OAuth2/OIDC and API gateways, including WSO2 Identity Server and API Manager. See the WSO2 services or the security review.

Message us on LinkedIn