简体   繁体   English

在 Chrome 扩展中安全地存储 Django REST API 令牌

[英]Securely storing Django REST API token in Chrome extension

I'm making a Chrome extension which should allow users to enter their username and password into a pop-up interface, post those to the server via AJAX, and receive a Django REST API token which the Chrome extension can use to interact with the Django app (get user info, create new records, etc. - so it should be secure).我正在制作一个 Chrome 扩展,它应该允许用户在弹出界面中输入他们的用户名和密码,通过 AJAX 将它们发布到服务器,并接收一个 Django REST API 令牌,Chrome 扩展可以使用它与 Django 交互应用程序(获取用户信息、创建新记录等 - 所以它应该是安全的)。

Right now I've got the Chrome extension making the request and receiving the API token, but I'm struggling with how to store the API token securely.现在我已经让 Chrome 扩展程序发出请求并接收 API 令牌,但我正在努力解决如何安全地存储 API 令牌。

I know that chrome.storage is out because the docs say that it's not secure.我知道 chrome.storage 已经过时了,因为文档说它不安全。 Would it be possible to use cookies here?可以在这里使用cookies吗? The extension needs to run on multiple domains, so I'm not sure if I can use cookies securely.该扩展程序需要在多个域上运行,所以我不确定我是否可以安全地使用 cookie。

I'd like to not require the user to log in every time they use Chrome, but I'm not sure how to go about storing the API data.我不希望用户每次使用 Chrome 时都登录,但我不确定如何存储 API 数据。

You should use chrome.identity :你应该使用chrome.identity

Developers can leverage chrome.identity API to store user credentials.开发人员可以利用 chrome.identity API 来存储用户凭据。 A Chrome extension usually makes API requests to external resources. Chrome 扩展程序通常会向外部资源发出 API 请求。 A best practice is to store an OAuth API token.最佳实践是存储 OAuth API 令牌。 Such tokens can perform authorized requests on behalf of users without user names / passwords, and can be revoked by the user anytime.此类令牌可以在没有用户名/密码的情况下代表用户执行授权请求,并且可以随时被用户撤销。

Chrome API provides a chrome.identity service, which provides a secure way for an extension to authenticate, fetch and refresh tokens. Chrome API 提供了一个 chrome.identity 服务,它为扩展程序提供了一种安全的方式来验证、获取和刷新令牌。 This API enables a user to perform authentication against a third-party service.此 API 使用户能够针对第三方服务执行身份验证。 Chrome can interactively display a popup UI, which: Chrome 可以交互地显示一个弹出式 UI,其中:

Here's the official docs .这是官方文档

Chrome does not allow extensions to modify/deal with cookies. Chrome 不允许扩展程序修改/处理 cookie。

In the documentation it says: "Use the Chrome Identity API to authenticate users: the getAuthToken for users logged into their Google Account and the launchWebAuthFlow for users logged into a non-Google account. If your app uses its own server to authenticate users, you will need to use the latter "文档,它说:“使用Chrome身份API来验证用户:在getAuthToken用户登录到自己的谷歌账户和launchWebAuthFlow用户登录到一个非谷歌帐户,如果您的应用程序使用它自己的服务器进行身份验证的用户,你将需要使用后者

You can find the documentation about non-Google accounts here .您可以在此处找到有关非 Google 帐户的文档。

You can store your token in local storage which is only accessible by your extension but be aware that, it is not encrypted and can be seen through Chrome's development tools.您可以将您的令牌存储在只能由您的扩展程序访问的本地存储中,但请注意,它未加密,可以通过 Chrome 的开发工具查看。

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

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