跳到主要内容
ecdsa.com
jose (JavaScript)JWKS 与密钥发现

错误信息

Expected 200 OK from the JSON Web Key Set HTTP response

jose (JavaScript) — JOSEError from createRemoteJWKSet, code ERR_JOSE_GENERIC

含义

`createRemoteJWKSet` 请求了密钥集 URL,得到的 HTTP 状态码不是 200。jose 获取 JWKS 时禁用了重定向(`redirect: "manual"`),因此 301/302 和 404、401 一样都算失败。没有加载任何密钥,也没有检查任何令牌签名;你配置的 URL 返回的不是密钥集。

原因

修复方法

  1. 1.

    从发现文档中读取 jwks_uri

    OpenID Connect 提供商会在 `/.well-known/openid-configuration` 中以 `jwks_uri` 发布确切的密钥集 URL。在启动时解析一次,再把这个 URL 传给 `createRemoteJWKSet`。

    js
    import { createRemoteJWKSet } from "jose";
    
    // Resolve jwks_uri from the provider's discovery document instead of guessing it.
    const discovery = await fetch("https://login.example.com/.well-known/openid-configuration").then((r) => r.json());
    const jwks = createRemoteJWKSet(new URL(discovery.jwks_uri));
  2. 2.

    在不跟随重定向的情况下检查状态码

    不带 `-L` 的 curl 与 jose 的行为相同。你需要的是带有 `{"keys":[...]}` 的 200;如果是 30x,它会告诉你应该改为配置哪个 URL。

    bash
    # Status line and redirect target, without following redirects (jose does not follow them either)
    curl -sS -o /dev/null -w '%{http_code} %{redirect_url}\n' https://login.example.com/jwks
    curl -s https://login.example.com/.well-known/openid-configuration | jq -r .jwks_uri

相关错误

浏览完整的签名错误库