Error message
Expected a different algorithm (JWT::IncorrectAlgorithm)
Also printed as
- JWT::IncorrectAlgorithm: Expected a different algorithm
ruby-jwt (Ruby) — JWT::IncorrectAlgorithm from JWT.decode
What it means
The token's `alg` header is not among the algorithms you allowed — and if you passed no `algorithm:`/`algorithms:` option at all, ruby-jwt allows none. Since ruby-jwt 2.x the verifier must name the algorithm explicitly, because trusting the token's own header is the root of the well-known algorithm-confusion attacks. The signature was not checked.
Why it happens
No algorithm passed to JWT.decode
commonCode from ruby-jwt 1.x tutorials calls `JWT.decode(token, key, true)`. Current versions reject every token in that case with this error.
The issuer uses a different algorithm than configured
commonA migration from RS256 to ES256 (or from HS256 to an asymmetric algorithm) on the issuer side breaks verifiers that still pin the old value.
A token from another issuer, or a probe
occasionalTokens with `alg: none` or `HS256` sent to an API that verifies RS256/ES256 are rejected here — as they should be.
How to fix it
- 1.
Name the issuer's algorithm explicitly
Pass `algorithm:` (or `algorithms:` for a short list). With several algorithms, use a JWKS loader so each token is verified with a key of the matching type.
ruby payload, header = JWT.decode(token, public_key, true, algorithm: "ES256") # several allowed algorithms (e.g. during a migration), each matched to the right key type: payload, header = JWT.decode(token, nil, true, algorithms: ["ES256", "RS256"], jwks: jwks_loader)