跳到主要内容
ecdsa.com
coreos/go-oidc (Go)Go

错误信息

oidc: malformed jwt: unexpected signature algorithm "ES256"; expected ["RS256"]

也可能输出为

  • oidc: id token signed with unsupported algorithm, expected ["RS256"] got "ES256"

go-oidc v3.0~v3.5 在解析之后才检查算法,措辞是“oidc: id token signed with unsupported algorithm, expected [\"RS256\"] got \"ES256\"”;之后的版本(经 v3.10、v3.14、v3.17 和 v3.21 确认)在解析时就拒绝它,因此报“malformed jwt”。

coreos/go-oidc (Go) — IDTokenVerifier.Verify with the default SupportedSigningAlgs

含义

ID 令牌的签名算法不在验证器的 `SupportedSigningAlgs` 中。如果该列表为空,而验证器又不是用通过发现文档得到的提供商创建的,go-oidc 会退回到只接受 RS256——这是 OpenID Connect 规范规定的默认值——因此任何 ES256、PS256 或 EdDSA 令牌都会在检查签名之前被拒绝。go-oidc 把它报告为“malformed jwt”,因为这项检查发生在解析 JWS 的过程中。

原因

修复方法

  1. 1.

    显式列出提供商使用的算法

    把 `SupportedSigningAlgs` 设置为提供商使用的一个或多个算法。保持列表简短;它是一份允许列表。

    go
    verifier := oidc.NewVerifier(issuer, keySet, &oidc.Config{
    	ClientID:             clientID,
    	SupportedSigningAlgs: []string{oidc.ES256}, // what the provider actually signs with
    })
  2. 2.

    让发现文档提供算法

    `provider.Verifier` 从提供商的元数据中读取支持的算法,因此不需要手动同步任何内容。

    go
    // provider.Verifier takes the algorithms from id_token_signing_alg_values_supported.
    provider, err := oidc.NewProvider(ctx, issuer)
    if err != nil {
    	return err
    }
    verifier := provider.Verifier(&oidc.Config{ClientID: clientID})

相关错误

浏览完整的签名错误库