security - 使用 Firebase 应用 key 对服务器进行身份验证

标签 security authentication firebase

I want to implement the server authentication method described here in paragraph 1 .

  1. Using a Firebase app secret: All authentication methods can accept a Firebase app secret instead of a JWT token. This will grant the server complete read and write access to the entire Firebase database. This access will never expire unless it is revoked via the App Dashboard.

我需要指导如何做到这一点。我不想使用任何 auth 变量,因为我不会对用户进行身份验证,而是通过某个单一的静态 key 对我的服务器进行身份验证。

那么我的安全规则会是什么样子?

这是我到目前为止所拥有的。

安全规则.json
{
  "rules": {
    ".read": true,
    ".write": "secret == 'mykey'"
  }
}

我如何在服务器端 HTTP 请求中实现这一点?我会创建一个名为 secret 的 header ,其值 mykey 如下所示:

服务器.js
{"secret": "mykey"}

最佳答案

要在 HTTP 请求中使用 key ,请将其传递到 URL 的 auth 参数中。例如

curl 'https://yours.firebaseio.com/.json?auth=<your_secret>'

当您使用您的 key 通过 Firebase 进行身份验证时,生成的 session 将以管理员身份运行。它对整个 Firebase 数据库具有完全读/写访问权限,就像访问 Firebase 仪表板时一样。因此您不需要在安全规则中授予任何权限。

如果您希望能够检测服务器,则应使用自定义 token 而不是您的 key 。创建自定义 token 时,您可以准确确定 auth 变量中包含的内容。例如

{
  "uid": "myserver"
}

现在您可以在安全规则中检查该特定 uid:

{
  "rules": {
    ".read": true,
    ".write": "auth.uid = 'myserver'"
  }
}

您可以使用此 jsfiddle 创建自定义 token :http://jsfiddle.net/firebase/XDXu5/

关于security - 使用 Firebase 应用 key 对服务器进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34876641/

相关文章:

rest - 如何禁用 Firebase 数据库的 REST API 调用?

ios - Firebase FCM 与 google plus cordova 插件冲突 - IOS

javascript - 登录时从用户集合中获取用户数据

Magento 客户自动登录

php - 使用 eval 解析表单输入的方程式的最安全方法

ruby - 如何使用 omniauth-openid gem 使 OpenID 登录安全?

javascript - 通过 HTTP header 限制 Javascript 执行

authentication - Google 登录后立即保存信息到 Azure 移动服务(.NET 后端)

如果计算机不在域中,则 c# Active Directory 身份验证用户

security - OAuth2 中刷新 token 有什么好处