跳到主要内容
ecdsa.com
jose (JavaScript)JWT 库

错误信息

"nbf" claim timestamp check failed

jose (JavaScript) — JWTClaimValidationFailed, code ERR_JWT_CLAIM_VALIDATION_FAILED

含义

令牌带有 `nbf`(“not before”)声明,按验证方的时钟,这个时间仍在未来,因此 jose 以“尚未生效”为由拒绝该令牌。签名和其他声明可能完全没有问题;这个错误只与时间有关。它几乎总是意味着颁发者和验证方的时钟相差超过了你允许的容差(默认为零)。

原因

修复方法

  1. 1.

    显式允许少量时钟偏差

    `clockTolerance` 接受秒数或时长字符串。30~60 秒足以吸收实际环境中的时钟漂移;它不是用来提前几分钟接受令牌的手段。

    js
    import { jwtVerify } from "jose";
    
    const { payload } = await jwtVerify(token, jwks, {
      algorithms: ["ES256"],
      clockTolerance: "30s", // applies to nbf, exp and iat
    });
  2. 2.

    修改配置之前,先打印时间线

    比较 `nbf` 与验证方的当前时间。相差几秒说明是时钟偏差;相差几小时说明颁发者存在时区或单位错误;13 位数的 `nbf` 说明用了毫秒。

    js
    import { decodeJwt } from "jose";
    
    const { iat, nbf, exp } = decodeJwt(token);
    const now = Math.floor(Date.now() / 1000);
    console.log({ iat, nbf, exp, now, nbfInSeconds: nbf - now });
  3. 3.

    修正漂移的时钟

    在每台签发或验证令牌的主机上运行 NTP(chrony 或 systemd-timesyncd)。容差可以掩盖小的时钟偏差,但不应该靠它来掩盖大的偏差。

    bash
    timedatectl status          # "System clock synchronized: yes"
    chronyc tracking            # offset from the NTP reference
    date -u +%s                 # compare with the token's nbf

相关错误

浏览完整的签名错误库