【发布时间】:2013-07-10 12:50:23
【问题描述】:
有没有一种方法可以让我以 Firebase 的管理员身份向 Firebase 进行身份验证,以获得对它的完全读/写访问权限(已经有安全规则保护它的一部分),或者我是否必须编写一个安全规则以某种方式允许我访问完整的 firebase,例如通过提供某个密码/密钥。
是否有标准或建议的方法?
【问题讨论】:
标签: firebase firebase-security
有没有一种方法可以让我以 Firebase 的管理员身份向 Firebase 进行身份验证,以获得对它的完全读/写访问权限(已经有安全规则保护它的一部分),或者我是否必须编写一个安全规则以某种方式允许我访问完整的 firebase,例如通过提供某个密码/密钥。
是否有标准或建议的方法?
【问题讨论】:
标签: firebase firebase-security
只有在您在客户端代码之外进行身份验证时,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"
}
}
}
【讨论】:
child(auth.uid) 吗?只是好奇..就像我说的,还在学习:)
administrators 列表,而不是有一个 isAdmin 属性。这样我们就可以拥有只有管理员才能在administrators 列表中添加或删除用户的安全性。
是的,有。您只需使用 Firebase Secret 而不是身份验证令牌进行身份验证。即。
firebaseRef.auth(MY_SECRET);
您可以在 Forge 的身份验证部分找到 Secret。
【讨论】:
auth() 现已弃用。参考马特的回答stackoverflow.com/a/27413337/978501
Object {auth: null, expires: null, token: "", uid: null, provider: "custom"},但它违反规则".read": "auth.admin == true"
As per the documentation,你需要使用firebaseRef.authWithCustomToken。
示例;
firebaseRef.authWithCustomToken(FIREBASE_SECRET, function(error, authData) {
if(!error) {
// You are authenticated
}
});
firebaseRef.auth 现已弃用,请改用 firebaseRef.authWithCustomToken。
【讨论】:
Object {auth: null, expires: null, token: "", uid: null, provider: "custom"},但它违反规则".read": "auth.admin == true"
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);
}
});
【讨论】: