我可以使用Firebase云端功能隐藏JavaScript代码吗?

问题描述:

我有一个JavaScript网页,可以将MQTT数据发送到运行Mosquitto的服务器。我想隐藏用户的MQTT服务器名称,用户名和密码。我知道这是不可能的,因为源代码在控制台中是可见的。我可以使用Firebase云端功能隐藏JavaScript代码吗?

是否可以使用Firebase云功能执行此操作?如果是的话,怎么样?

这其实是在Cloud Functions for Firebase documentation提到的关键功能之一:

保持您的逻辑私密性和安全

在许多情况下,开发商更希望控制服务器上的应用程序逻辑避免在客户端篡改。另外,有时不允许该代码被反向设计。云功能从客户端完全绝缘,所以你可以肯定它是私有的,总是正好你想要做什么

有在火力地堡的functions-samples repo颇有些样品。

例如,向用户发送电子邮件确认的样本需要用于发送电子邮件的gmail凭据。您永远不希望将这些凭据嵌入应用程序代码中,恶意用户可能会发现它们并滥用它们。所以这段代码是在云端函数中运行的好选择。该code that reads these values从这个样本:

// Configure the email transport using the default SMTP transport and a GMail account. 
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/ 
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables. 
const gmailEmail = encodeURIComponent(functions.config().gmail.email); 
const gmailPassword = encodeURIComponent(functions.config().gmail.password); 
const mailTransport = nodemailer.createTransport(
    `smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`); 

您可以硬编码的电子邮件地址和密码,因为只有你的项目开发人员将能够看到它。但在这种情况下,示例会更好,并保留凭借functions.config()读取的服务器端配置的凭据。要了解如何设置这些,读documentation for the sample

设置gmail.emailgmail.password谷歌云环境变量,如果你的账户有2匹配用于发送电子邮件的Gmail帐户的电子邮件地址和密码(或应用程序的密码一步验证启用)。对于这一点:

firebase functions:config:set gmail.email="[email protected]" gmail.password="secretpassword" 

我想即使API密钥可以安全地在火力功能存储。我已经在an articleGithub repo中写下了我的想法,详细了解如何保存Firebase配置变量并访问它们。看一看。