简体   繁体   English

如何在 nodejs/angular 中保护 JWT 令牌和刷新令牌

[英]How to secure JWT token and refresh token in nodejs/angular

i am new in node js.我是节点 js 的新手。 I am building a simple notes taking app and wanted to use JWT tokens for authentication and to secure my API's.我正在构建一个简单的笔记应用程序,并希望使用 JWT 令牌进行身份验证并保护我的 API。 On research i came to know that i need to create two tokens:在研究中,我知道我需要创建两个令牌:

  1. access token (short expire time like 10 minutes)访问令牌(过期时间短,如 10 分钟)
  2. refresh token (longer expire time 30 days)刷新令牌(更长的过期时间 30 天)

My config file我的配置文件

"secret": "*************",
"refreshTokenSecret": "*************",
"port": 5000,
"tokenLife": 900,
"refreshTokenLife": 86400

Code for middleware中间件代码

const jwt = require('jsonwebtoken')
const config = require('./config')

module.exports = (req,res,next) => {
  const token = req.body.token || req.query.token || req.headers['x-access-token']
  // decode token
  if (token) {
    // verifies secret and checks exp
    jwt.verify(token, config.secret, function(err, decoded) {
        if (err) {
            return res.status(401).json({"error": true, "message": 'Unauthorized access.' });
        }
      req.decoded = decoded;
      next();
    });
  } else {
    // if there is no token
    // return an error
    return res.status(403).send({
        "error": true,
        "message": 'No token provided.'
    });
  }
}

Here is the response这是回复

在此处输入图像描述

access token can be saved in local storage.访问令牌可以保存在本地存储中。 but articles said save refresh token as http-only cookie.但文章说将刷新令牌保存为仅限 http 的 cookie。 i need the answer of following points (Keeping in mind that i am just a beginner):我需要以下几点的答案(请记住,我只是一个初学者):

  1. How to store refresh token as http-only cookie (any node-js code example would be a great help)?如何将刷新令牌存储为仅限 http 的 cookie(任何 node-js 代码示例都会有很大帮助)?
  2. How to secure it on client side and should I save refresh token to database?如何在客户端保护它,我应该将刷新令牌保存到数据库吗?
  3. Is there any other better solution to secure my API's?还有其他更好的解决方案来保护我的 API 吗?

You can use an http-only cookie using the following:您可以通过以下方式使用仅限 http 的 cookie:

public authenticateUser(user: User, res: Response) {
        const authJwtToken = this.generateJWT({
            email: user.email,
            uuid: user.uuid
        });

        const cookieOptions = {
            maxAge: 3600000,
            secure: true,
            httpOnly: true
        };

        res.cookie('access_token', authJwtToken, cookieOptions);
    }
// you can then res.send({...}) or wtv

Not that there is nothing from preventing you to store more than one cookie so I can't see a reason why not storing both of them in the same manner.并不是说没有什么可以阻止您存储多个 cookie,所以我看不出为什么不以相同的方式存储它们的原因。

Now whether you will store it on the database depends on what you want to achieve.现在是否将其存储在数据库中取决于您要实现的目标。 Generally it is not required but note that in that case the server cannot in any way invalidate a single JWT.通常不需要,但请注意,在这种情况下,服务器不能以任何方式使单个 JWT 无效。 (You could in theory change the signing key but this would invalidate all of them). (理论上您可以更改签名密钥,但这会使它们全部无效)。

In case you want to be able to achieve functionality such as 'log me out of all devices' you would need to store the JWTs issued for each user in a database (preferably an in-memory one such as Redis or Memcached) and do a second check with the extra information on whether they have been invalidated or not - even though such functionality is typically achieved using sessions instead of JWT如果您希望能够实现诸如“让我退出所有设备”之类的功能,则需要将为每个用户发出的 JWT 存储在数据库中(最好是内存中的 JWT,例如 Redis 或 Memcached)并执行第二次检查有关它们是否已失效的额外信息-即使通常使用会话而不是 JWT 来实现此类功能

See this example how i secured my getByRefId api in nodjs:请参阅此示例,我如何在 nodjs 中保护我的getByRefId api:

In routes file:在路线文件中:

router.get("/refId/:refId", helper.auth, groupController.getByRefId);

helper.auth is   function  :

auth: (req, res, next) => {
    var token = req.body.token || req.headers['authorization'] || req.headers['Authorization'];

    if (token.startsWith('Bearer ')) {
        // Remove Bearer from string
        token = token.slice(7, token.length).trimLeft();
    }

    if (token) {
        jwt.verify(token, 'MY_SECRET', function (err, decoded) {
            if (err) {
                console.error('JWT Verification Error', err);
                return res.status(403).send(err);
            } else {
                req.payload = decoded;
                return next();
            }
        });
    } else {
        res.status(403).send('Token not provided');
    }
}

This use jwt = require('jsonwebtoken') library you can install it in nodejs project这个使用jwt = require('jsonwebtoken')库,你可以将它安装在 nodejs 项目中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM