【问题标题】:signature verification failed for JWT - node/express/identityServer/asp.net.JWT 的签名验证失败 - node/express/identityServer/asp.net。
【发布时间】:2018-11-03 04:22:47
【问题描述】:

我在节点/express 应用程序的服务器端验证 JWT 时遇到问题。令牌是在 asp.net 核心应用程序的 Identity Server 中生成的。生成的令牌是 RS256 令牌类型,这意味着在 Identity Server 中创建时需要生成私钥和公钥。我需要检索具有有效签名的有效证书。

在客户端(Angular)上,一旦登录,我将在所有请求中传递 Bearer 令牌。我需要以某种方式验证该令牌。使用 RS256 令牌类型的方法是确保公钥匹配。我正在使用

const jwt2 = require('jwt-simple');

用于我的 JWT 验证。

问题是秘密,这里是 jwt-simple 文档jwt-simple link。如果我在 decode false 中设置第三个值,它会起作用,因为它忽略了所需的秘密/证书。

我正在中间件中进行此验证,因此所有端点都会命中它。 我看到了这个问题 - SO Similar Issue 并运行了相同的命令。我仍然收到错误,因为令牌与证书没有任何关系,因为我是从 Identity Server 项目中获取的。所以我需要从该项目中检索证书公钥。

我如何能够在令牌中发送该证书或以某种方式检索该有效证书?

v1 -(使用自签名 server.crt 作为证书并收到此错误)

错误:签名验证失败

App.js

//This is for a self-signed certificate locally with no correlation to the token itself.
const options = {
    key: fs.readFileSync('./key.pem', 'utf8'),
    cert: fs.readFileSync('./server.crt', 'utf8')
 };

app.use((req, res, next) => {
    if(!req.headers.authorization){
        return res.status(403).json({ error: 'No credentials sent!'});
    } else {
        let token = req.headers.authorization.split(' ')[1]

        var decoded = jwt.decode(token, options.cert);

        if(decoded){
            let currentTime = new Date().getTime()/1000
            if(decoded.exp <= currentTime){
                return res.status(403).json({
                    error: 'Token has expired'
                }); 
            }
        }
        else if(!decoded){
            return res.status(403).json({
                error: 'invalid token'
            }); 
        }
    }
    next();
})

JWT.io 解析令牌结构 -

标题

{
  "alg": "RS256",
  "kid": "1231231231231231231",
  "typ": "JWT",
  "x5t": "si7bdXd6......HnxhO4Wi_s"
}

我可以用 x5t 做什么吗?为长篇道歉。谢谢。

【问题讨论】:

    标签: node.js express asp.net-core asp.net-identity jwt


    【解决方案1】:

    如果签名公钥是随令牌一起提供的,而你盲目地信任它,它基本上就违背了拥有签名令牌的目的。

    您需要一些其他机制来共享身份验证服务的公钥。根据您对密钥轮换的要求以及您的应用程序的一般工作方式,您可以在应用程序启动时从静态路径/url 获取它,或者您可能希望构建一种机制来定期检查更新的有效公钥( s)。

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 2018-04-30
      • 2018-10-02
      • 2018-09-10
      • 2016-12-27
      • 2019-06-14
      • 2016-12-16
      • 2020-05-15
      • 2021-09-10
      相关资源
      最近更新 更多