gpt4 book ai didi

node.js - 验证 Node PEM_read_bio_PUBKEY 上的 RS256 jwt 失败

转载 作者:搜寻专家 更新时间:2023-11-01 00:11:02 24 4
gpt4 key购买 nike

我正在努力我正在尝试验证使用 RS256 算法的 jwt。当使用 hs256 算法时一切正常

let opts = {
audience: 'y',
issuer: `https://x.auth0.com/`,
algorithms: ["RS256"]
}

jwt.verify(payload.token, 'secret', opts, (err, decoded) => {
if (err) {
console.log("invalid token in iamonline service " + err.message);
return;
}

我一直收到错误:PEM_read_bio_PUBKEY failed

虽然 auth0documentation为此,它假定您正在使用我没有使用的 express。我正在通过 websocket 执行此操作,因此没有中间件。

烦人的一点是 HS256 对我来说没问题,但 auth0 自定义登录表单似乎需要 RS256

最佳答案

RS256 需要一个公钥来验证,但是你提供的是一个字符串

jwt.verify(payload.token, 'secret', opts, (err, decoded) => {

查看auth0的文档

jwt.verify(token, secretOrPublicKey, [options, callback])

token is the JsonWebToken string

secretOrPublicKey is a string or buffer containing either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA.

您需要提供 PEM 公钥而不是 secret。 PEM 文件内容将以 -----BEGIN PUBLIC KEY-----

开头
var publicKey = fs.readFileSync('public.pem');

关于node.js - 验证 Node PEM_read_bio_PUBKEY 上的 RS256 jwt 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42912770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com