【问题标题】:Firebase authenticate as adminFirebase 以管理员身份进行身份验证
【发布时间】:2013-07-10 12:50:23
【问题描述】:

有没有一种方法可以让我以 Firebase 的管理员身份向 Firebase 进行身份验证,以获得对它的完全读/写访问权限(已经有安全规则保护它的一部分),或者我是否必须编写一个安全规则以某种方式允许我访问完整的 firebase,例如通过提供某个密码/密钥。

是否有标准或建议的方法?

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    只有在您在客户端代码之外进行身份验证时,Andrew 的答案才有效(否则您显然不应该使用您的 MY_SECRET)。由于很多人,比如我自己,使用 Firebase 来避免服务器代码,这里有一个替代答案。

    在大多数 firebase 应用程序中,除了简单的登录“auth”对象(仅存储电子邮件和密码)之外,您可能还有一个“Users”对象。您可以为“Users”对象中的每个 $user 添加一个“isAdmin”或“isModerator”(或任何您想要的)。

    然后您的规则将如下所示(假设您的 auth.id 与您的 $user 密钥匹配):

    {
      "rules": {
        "someObject": {
          ".write": "root.child('Users').child(auth.uid).child('isAdmin').val() == true"
        }
      }
    }
    

    【讨论】:

    • 我刚开始使用 Firebase,我真的很喜欢你的建议,但不应该是 child(auth.uid) 吗?只是好奇..就像我说的,还在学习:)
    • 是的 @BenjaminSolum 你是对的,auth.uid 是新的 auth.id。 Firebase 完全删除了仅对提供者唯一的 auth.id,并将其替换为全局唯一 ID。我在答案中更新了它,希望它能作为一个整体起作用。
    • 补充一点,我认为最好有一个单独的administrators 列表,而不是有一个 isAdmin 属性。这样我们就可以拥有只有管理员才能在administrators 列表中添加或删除用户的安全性。
    • 您如何相信只有管理员才能将 isAdmin 设置为 true?
    • @davidtaubmann 我知道这已经有一年多了,但这不是客户端,这是 Firebase 用来确定谁可以访问什么的 JSON 对象的规则。
    【解决方案2】:

    是的,有。您只需使用 Firebase Secret 而不是身份验证令牌进行身份验证。即。

    firebaseRef.auth(MY_SECRET);
    

    您可以在 Forge 的身份验证部分找到 Secret。

    【讨论】:

    • auth() 现已弃用。参考马特的回答stackoverflow.com/a/27413337/978501
    • 我使用了 ref.authWithCustomToken(),但是返回的有效负载具有空属性:auth、expires、token、uid。除了设置为“自定义”的“提供者”属性。这是什么原因??
    • @nodebase 响应(authData)是Object {auth: null, expires: null, token: "", uid: null, provider: "custom"},但它违反规则".read": "auth.admin == true"
    【解决方案3】:

    As per the documentation,你需要使用firebaseRef.authWithCustomToken

    示例;

    firebaseRef.authWithCustomToken(FIREBASE_SECRET, function(error, authData) {
      if(!error) {
        // You are authenticated
      }
    });
    

    firebaseRef.auth 现已弃用,请改用 firebaseRef.authWithCustomToken

    【讨论】:

    • 响应是(authData)Object {auth: null, expires: null, token: "", uid: null, provider: "custom"},但它违反规则".read": "auth.admin == true"
    • 这样安全吗?这是否意味着您将在客户端应用程序中硬编码您的秘密?
    【解决方案4】:

    See this for the 'latest' documentation.

    authWithCustomToken 现在是 signInWithCustomToken(firebase 版本 3.x)

    文档中的示例:

    firebase.auth().signInWithCustomToken(token).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        if (errorCode === 'auth/invalid-custom-token') {
            alert('The token you provided is not valid.');
        } else {
            console.error(error);
        }
    });
    

    【讨论】:

    • 嗨,我看到这是连接到 firebase(java、ios js 等)的常规身份验证。如何在 Python 中执行此操作(我的意思是如何以管理员身份登录)。如何确保使用我的管理程序的人确实是管理员?
    猜你喜欢
    • 2011-09-18
    • 2018-12-05
    • 1970-01-01
    • 2021-09-19
    • 2019-06-02
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    相关资源
    最近更新 更多