授权使用Google API返回的数据

时间:2015-04-07 07:33:49

标签: google-api firebase

我对Firebase来说很新,所以原谅任何基本缺乏知识。我正在为一个项目评估它。

Firebase可以很好地处理Google身份验证。但是,根据从Google API返回的信息授权用户呢?当Firebase如此庞大的客户端时,我该如何做到这一点呢?

例如,假设我想根据Google Books API是否确认用户拥有图书来限制对firebase数据的访问。我知道我可以在对用户进行身份验证后将数据传递给令牌(这是半伪代码):

ref.child("users").child(authData.uid).set({
  provider: authData.provider,
  ownsBook: getBookInfo.ownsBook()
});

我想使用" ownsBook"用于决定用户是否可以访问firebase数据的参数,但由于这一切都必须在客户端进行,有什么阻止某人通过JS控制台手动覆盖它?

你通常会如何在Firebase中使用这样的东西?

1 个答案:

答案 0 :(得分:2)

我认为Firebase不会公开来自Google Books API的信息(但我可能错了)。

除此之外:您可以使用Firebase的安全规则限制对数据的访问:https://www.firebase.com/docs/security/quickstart.html

因此,如果我们假设您拥有这样的数据结构中的图书和所有权:

root
  users
    google:3246734
      booksOwned
        book:743267
  books
    book:743267
      title....

您的规则可能类似于

{
  "rules": {
    "book": {
      $book_id: { 
        ".read":
          "root.child(auth.uid).booksOwned.child($book_id).exists()" 
        }
      }
    }
  }
}

为了获得对特定图书的访问权限(由$book_id表示),我们会检查用户booksOwned节点中是否存在该图书ID。