不使用Firebase身份验证的实时数据库的Firebase规则

时间:2017-08-09 09:15:22

标签: ios objective-c swift firebase firebase-realtime-database

我对Firebase服务很新,但我已经为我的iOS应用程序完成了代码,该应用程序负责Firebase实时数据库交互。

现在我想保护我的应用并使用Firebase规则保护它。存在一个问题,即我正在为用户使用自己的身份验证,因此我不使用Firebase身份验证。

所以问题是我如何使用Firebase规则和没有Firebase身份验证来保护我的数据库。

2 个答案:

答案 0 :(得分:3)

注意:据我所知,您无法在Firebase中使用自定义身份验证系统直接

假设:您有一个认证服务器,其中集成了Firebase Admin SDK(可以/已经)。

您需要创建自定义令牌才能在数据库/存储中使用您的身份验证:

https://firebase.google.com/docs/auth/admin/create-custom-tokens

  

经过身份验证后,将在访问其他身份时使用此身份   Firebase服务,例如Firebase实时数据库和云   存储。此外,JWT的内容将在   您的Firebase实时数据库安全规则中的auth对象和   您的云存储安全规则中的request.auth对象。

从上方链接中省略Java和Python

在服务器中:

// Step 1: Your client has sent the credentials.
// Step 2: Fetch the client's unique id, and create a custom token with the Admin SDK.

var uid = "some-uid"; 

admin.auth().createCustomToken(uid)
  .then(function(customToken) {
    // Send token back to client
  })
  .catch(function(error) {
    console.log("Error creating custom token:", error);
  });

然后在iOS部分:

// Step 1: Login with your own authentication system.
// Step 2: Send your credentials to your server, and fetch the customToken.
// Step 3: Sign in with FIRAuth:

[[FIRAuth auth] signInWithCustomToken:customToken
                           completion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
  // ...
}];

答案 1 :(得分:3)

正如Stevenson所说,如果没有firebase身份验证,你就无法使用安全规则。

要使用firebase身份验证,请按以下步骤操作

您可以从服务器获取firebase令牌以及您自己的身份验证令牌,然后您可以使用自定义令牌身份验证API      (https://firebase.google.com/docs/auth/ios/custom-auth)使用firebase进行身份验证

  1. 要了解服务器端的自定义令牌生成,请按照此https://firebase.google.com/docs/auth/admin/create-custom-tokens
  2. 进行操作
  3. 在实时数据库中存储用户数据的一种常见模式是将所有用户存储在单个用户节点中,其子节点是每个用户的uid值。如果您想限制对此数据的访问,以便只有登录用户可以看到自己的数据,那么您的规则将如下所示:
  4. 示例:

    {
          "rules": {
                "users": {
                  "$uid": {
                    ".read":  "auth != null && auth.uid == $uid",
                    ".write": "auth != null && auth.uid == $uid"
                  }
                }
              }
     }
    

    此处 auth 是授权对象。 uid 是您在服务器端从自定义令牌生成发送的唯一令牌(上述用户ID)。有关详细信息,请查看有关用户安全性的firebase文档。