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

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

Firebase云function可以做到吗? 如果是的话,怎么样?

这实际上是Firebase文档云function中提到的关键function之一:

保持您的逻辑私密和安全

在很多情况下,开发人员更愿意控制服务器上的应用程序逻辑,以避免在客户端篡改。 另外,有时候不允许代码被反向devise。 云function与客户端完全隔离,因此您可以确定它是私密的,并且始终按照您的要求进行

Firebase的function样本库中有相当多的样本 。

例如,向用户发送电子邮件确认的示例需要用于发送电子邮件的gmail凭据。 您永远不会想要在应用程序代码中embedded这些凭据,恶意用户可能会发现这些凭据并滥用这些凭据。 所以这个代码是在云端函数中运行的好select。 从此示例中读取这些值的代码 :

// 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()读取服务器端configuration中的凭据。 要了解如何设置这些,请阅读示例的文档 :

设置gmail.emailgmail.password Google Cloud环境variables以匹配用于发送电子邮件的Gmail帐户的电子邮件和密码(或者如果您的帐户启用了两步validation,则为应用密码)。 为了这个用途:

 firebase functions:config:set gmail.email="myusername@gmail.com" gmail.password="secretpassword" 

我认为即使API密钥可以安全地存储在firebasefunction。 我已经在一篇文章和一个Github仓库中记下了我的想法,详细了解如何保存firebaseconfigurationvariables并访问它们。 看一看。

Interesting Posts