跳到主要内容
ecdsa.com
ruby-jwt (Ruby)Ruby

错误信息

Expected a different algorithm (JWT::IncorrectAlgorithm)

也可能输出为

  • JWT::IncorrectAlgorithm: Expected a different algorithm

ruby-jwt (Ruby) — JWT::IncorrectAlgorithm from JWT.decode

含义

令牌的 `alg` 头部参数不在你允许的算法之中——如果你根本没有传入 `algorithm:`/`algorithms:` 选项,ruby-jwt 不允许任何算法。从 ruby-jwt 2.x 开始,验证方必须显式指定算法,因为信任令牌自身的头部正是众所周知的算法混淆攻击的根源。签名没有经过验证。

原因

修复方法

  1. 1.

    显式指定颁发者使用的算法

    传入 `algorithm:`(如果是一个简短的列表,则传入 `algorithms:`)。如果有多种算法,就使用 JWKS 加载器,让每个令牌都用类型匹配的密钥验证。

    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)

相关错误

浏览完整的签名错误库