在哪里“隐藏"API 密钥 [英] Where to "hide" an API key

查看:174
本文介绍了在哪里“隐藏"API 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于在哪里以及如何:

My question is about where and how to:

  1. 隐藏 API 密钥
  2. 访问我的 app.js 文件中隐藏的 API 密钥

在这种情况下,我正在处理一个 Algolia 管理 API 密钥(但旨在以相当通用的方式提出这个问题).

In this case I am dealing with an Algolia Admin API Key (but aiming to ask this question in a fairly generic way).

为了让我的应用正常工作,根据某些用户操作,我需要更新我的 Algolia 索引(这需要一个管理 API 密钥才能执行).

In order for my app to work, based on certain user actions I need to update my Algolia index (which requires an Admin API Key in order to do).

我知道我不应该向前端公开我的管理 API 密钥(即不要将它放在我的 app.js 文件中).如何安全地将我的 API 管理密钥传递到我的 app.js 文件,以便我可以更新我的 Algolia 索引?

I understand that I am not supposed to expose my Admin API Key to the front-end (i.e. don’t put it in my app.js file). How can I securely pass my API Admin Key to my app.js file, so that I can make updates to my Algolia index?

我遇到的一些事情:

-我应该将它隐藏在 config.json 文件中吗?我无法弄清楚如何使用从我的 js 中的配置文件导出的信息(无论如何这可能会破坏目的?).我读了这个

-Should I hide it in a config.json file? I can't figure out how use information exported from a config file in my js (which maybe would defeat the purpose anyways?). I read this

-还在 Firestore 文档中提到,在与 Algolia 合作时,应用 ID 和 API 密钥存储在函数配置变量中".如何将我的 API 密钥存储在配置变量中?

-Also on the Firestore docs it mentions, in reference to working with Algolia, that "App ID and API Key are stored in functions config variables". How do I store my API key in config variables?

-我阅读了这是关于使用环境变量,并将它们导出到 app.js 文件.但是,如果我随后将 app-env 文件推送到服务器(我假设我需要这样做才能让 app.js 文件读取 API 密钥),这比将其放入文件中更安全吗?

-I read this about using environment variables, and exporting them to the app.js file. But if I then push the app-env file to the server (which I assume I'll need to in order for the app.js file to read the API key) how is that more secure than just putting it in the file?

推荐答案

您不能通过任何机制将密钥提供给客户并保密.

You cannot give the key to the client, through any mechanism, and keep it secret.

构建网络服务(使用您选择的服务器端语言,使用任何适当的身份验证)并编写您自己的 API 以允许执行有限的操作选择.然后,您的网络服务充当 Algolia 的客户端.

Build a web service (in your server-side language of choice, with whatever authentication is appropriate) and write your own API to allow a limited selection of actions to be performed. Your web service then acts as a client to Algolia.

这篇关于在哪里“隐藏"API 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆