Skip to content
ecdsa.com
ruby-jwt (Ruby)Ruby

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

How to fix it

  1. 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)

Related errors

Browse the full signature error database