JWT验证的公钥还是私钥?

时间:2018-07-02 17:22:45

标签: jwt

我将JWT与Auth0一起使用。我正在使用公钥(pemCert)验证令牌。

但是,我看到了以其他方式(例如,使用私钥)对令牌进行解码的示例。使用公钥是否和其他方法一样安全?

const jwt = require('jsonwebtoken');

function authoriseToken(req, res, next) {
  const token = req.headers.authorization;
  const pemCert = process.env.JWT_PEM;

  // If there is no token the user is not logged in
  if (!token || token.length === 0) {
    next();
    return;
  }

  const tokenCrop = token.replace('Bearer ', '');

  jwt.verify(
    tokenCrop,
    pemCert,
    { algorithm: 'RS256' },
    (err, decodedToken) => {
    if (err) {
      // If the token isn't verified eg expired then forward as if no token
      next();
    } else if (decodedToken) {
      // If the token is verified then add fields to the res
      req.authId = decodedToken.sub
      next();
    }
  },
);
}

0 个答案:

没有答案
相关问题