Error message
Signature verification failed
PyJWT (Python) — jwt.exceptions.InvalidSignatureError
What it means
PyJWT decoded the token's structure and ran the signature check, which failed: the signature does not match the key and payload. InvalidSignatureError subclasses DecodeError, so an over-broad except DecodeError can mislabel this as a parsing problem — it is a key/content mismatch. For ES256 tokens there is one extra subtlety: the JWS signature must be the raw 64-byte r‖s form, so tokens minted by hand-rolled signers that emitted ASN.1 DER fail here even when the key is right.
Why it happens
Different key than the issuer used
commonA rotated key pair where the verifier kept the old public key, a copy-paste of the wrong PEM, or two services reading different secret versions from the secret manager. With JWKS-based issuers, a kid mismatch has the same effect.
A hand-rolled signer produced the wrong signature
occasionalBesides DER instead of raw r‖s, a home-made signer that hashes with SHA-384 under an ES256 header, or signs the JSON instead of the base64url header.payload text, yields a well-formed signature that never verifies. A key damaged by config plumbing does not end up here: a PEM with literal \n sequences fails to load with "Unable to load PEM file", and quotes or indentation around an intact PEM are tolerated.
Token altered after signing
occasionalMiddleware that re-serializes the payload JSON or changes a single character changes the signed bytes. The signature is over the exact base64url text — cosmetic changes are not cosmetic. (Added base64 padding is rejected earlier, as "Invalid payload padding", and a cut-off token as "Not enough segments".)
How to fix it
- 1.
Fingerprint the key on both sides
Print a SHA-256 fingerprint of the public key the verifier loaded and compare it with the issuer's. A difference ends the search; a match points at the token's bytes instead.
python import hashlib from cryptography.hazmat.primitives import serialization public_key = serialization.load_pem_public_key(pem_bytes) # the key the verifier uses spki = public_key.public_bytes(serialization.Encoding.DER, serialization.PublicFormat.SubjectPublicKeyInfo) print(hashlib.sha256(spki).hexdigest()) # compare with the issuer's - 2.
Check kid before assuming corruption
Read the unverified header and make sure you are verifying with the key the token names. With multiple issuer keys, resolve by kid via PyJWKClient.
python import jwt header = jwt.get_unverified_header(token) print(header) # {'alg': 'ES256', 'kid': '...'} from jwt import PyJWKClient key = PyJWKClient("https://issuer.example/.well-known/jwks.json") \ .get_signing_key_from_jwt(token).key payload = jwt.decode(token, key, algorithms=["ES256"], audience="api://orders")