跳到主要内容
ecdsa.com
PyJWT (Python)JWT 库

错误信息

The specified alg value is not allowed

也可能输出为

  • jwt.exceptions.InvalidAlgorithmError: The specified alg value is not allowed

PyJWT (Python) — jwt.exceptions.InvalidAlgorithmError

含义

令牌头部中的 `alg` 不在你传给 `jwt.decode()` 的 `algorithms` 列表中。PyJWT 在验证签名之前就检查这一点,因为头部是由生成令牌的一方选定的,不得由它来决定如何验证。令牌可能签名正确——只是用了你没有允许的算法。

原因

修复方法

  1. 1.

    先读取头部

    `jwt.get_unverified_header()` 可以在不信任任何内容的前提下显示 `alg` 和 `kid`。把结果与你的允许列表以及颁发者发布的密钥进行对比。

    python
    print(jwt.get_unverified_header(token))  # {'alg': 'ES256', 'kid': '...', 'typ': 'JWT'}
  2. 2.

    只允许颁发者使用的那种算法

    把颁发者使用的算法放进 `algorithms`,并从它的 JWKS 中查找密钥。迁移期间同时列出两种算法,并通过 `kid` 让每种算法与各自的密钥对应。

    python
    from jwt import PyJWKClient
    
    jwks = PyJWKClient("https://issuer.example.com/.well-known/jwks.json")
    signing_key = jwks.get_signing_key_from_jwt(token)
    claims = jwt.decode(
        token,
        signing_key.key,
        algorithms=["ES256"],  # the algorithm(s) your issuer actually uses, never the token's own header
        audience="https://api.example.com",
    )

相关错误

浏览完整的签名错误库