node.js - 如何在数据库中存储OAuth 2访问 token ?

标签 node.js database access-token

我已经看到了很多关于此的话题,但是对于如何做到这一点没有什么具体的看法。

我目前正在让用户通过OAuth2与第三者进行身份验证,并且得到访问 token 作为返回。

我想长时间存储此访问 token ,因此我正在使用数据库来存储它。

我的问题涉及如何处理此数据库条目以确保安全性。

我无法对它进行哈希处理(就像输入密码一样),因为我需要能够代表用户读取和使用原始文档来调用第三方。

因此,我将其保留下来,找到一种2向加密方法(是否有最好的/推荐的npm软件包?)或我不知道的另一种解决方案。

我对访问 token 的安全性没有经验,因此不知道采用哪种最佳途径,不胜感激。

谢谢

最佳答案

I would like to store this access token for a long time and so I am using a database to do so


一种解决方案是在将数据保存到数据库之前对其进行加密,并在每次需要访问它时对其进行解密。在您的情况下,我认为对称加密是正确的选择,因此您将需要拥有一个必须始终保持安全的私钥。此类加密最常用的算法是AESt也已知的Advanced Encryption Standard,对于您的用例,建议使用AES-256实现。
尽管Security Stack Exchange上的this question不能完全解决您的情况,但答案可能有助于您更好地了解对数据库字段中的数据进行加密/解密的流程。包含可视化表示流的This answer可能是您首先要看的。
现在,您对流程有了一个直观的了解,您可能需要阅读this article,它引导您完成用于在数据库中存储敏感数据的基本加密策略:

To safely store your data in a database, you’d start by generating a strong secret key value in a byte array. This is best generated programmatically. This single key can be used to encrypt all of the data you’d like to store.

When you perform an encryption operation you initialize your Encryptor with this key, then generate a new, unique Initialization Vector for each record you’re going to encrypt.

When your application needs to work with the data, the IV is included in the data row which can be used in conjunction with the private key to decrypt the data for use in the software.


到这个时候,您必须对如何在数据库中保护 token 有更好的了解,如果允许的话,我想在我出发之前提出一项建议和一项警报...
首先,我强烈建议您仅支持通过安全通信 channel (也称为https)进行通信。如今没有借口不使用https,SSL证书现已免费提供Lets-encrypt

I cannot hash it (as I would a password) as I need to be able to read and use the original to call the 3rd party on behalf of the user.


基于此,我将假设您的 Node 服务器是唯一一个代表用户调用第三方服务数据,然后将自身作为API公开给使用它的客户端(可能是网站和/或移动应用程序)的客户端。
的确如此,我想提醒您,您的服务器可能成为this series of articles上暴露的API滥用主体:
  • Data Scraping - Automated harvesting of proprietary data from the API.
  • Account Hijack - Reuse of stolen credentials to log into accounts on your service.
  • Fake Account Factories - Automated API manipulation to create large numbers of bot controlled accounts.
  • Aggregation - Your data is aggregated with that of others as part of a commercial enterprise without permission.
  • Cheating as as Service - Web apps that allow users to cheat gamified and rewards based platforms.

关于node.js - 如何在数据库中存储OAuth 2访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52842641/

相关文章:

ios - ios 和 mongodb 的时间戳选择,moment.js

react-native - 如何使用 Expo 获取 FB Access Token

php - 跟踪像素插入数据库时​​生成 500(内部服务器错误)

mysql - 如何确保数据库表中只有一行?

mysql - 数据透视表的外键和约束,删除级联

azure - 调用/ token 端点没有给我 access_token

Azure AD 访问 token 不包含组声明

node.js - typescript node.js 快速路由分隔文件最佳实践

javascript - Bluebird 忘记返回警告丢失

node.js - 如何在 node.js 中创建一个简单的套接字?