Skip to content
ecdsa.com
jsonwebtoken (Rust crate)Rust

Error message

Error(InvalidAlgorithm)

Also printed as

  • called `Result::unwrap()` on an `Err` value: Error(InvalidAlgorithm)
  • InvalidAlgorithm

`{:?}` prints `Error(InvalidAlgorithm)`, `{}` prints `InvalidAlgorithm`.

jsonwebtoken (Rust crate) — ErrorKind::InvalidAlgorithm from decode()

What it means

The token's `alg` is not in `validation.algorithms`, or it does not fit the family of the `DecodingKey` (an EC key cannot verify RS256). The check happens before any signature verification. The most common trigger is `Validation::default()`, which allows HS256 only.

Why it happens

How to fix it

  1. 1.

    Build the Validation for the issuer's algorithm

    Use `Validation::new` with the issuer's algorithm and set the expected issuer and audience at the same time — the defaults do not check them.

    rust
    let mut validation = Validation::new(Algorithm::ES256); // the issuer's algorithm
    validation.set_issuer(&["https://issuer.example.com/"]);
    validation.set_audience(&["api://orders"]);
    let data = decode::<Claims>(&token, &key, &validation)?;

Related errors

Browse the full signature error database