可以在 Firebase 3 中实现自定义身份验证属性并将其与安全规则一起使用吗?
Can custom auth properties be implemented and used with security security rules in Firebase 3?
legacy Firebase documentation 表示您可以将属性插入数据库安全规则中使用的 auth 对象。事实上,在我升级到版本 3 之前,这种方法在我的应用程序中运行得非常好。
"Any values passed into createToken() are appended onto the auth variable for use in your Security and Firebase Rules."
新的 Firebase 身份验证服务仍然允许使用自定义身份验证令牌,new rules documentation 声明:
"Developers creating their own custom authentication tokens can optionally add additional claims to these tokens. These additional claims will become present on the auth variable in your rules."
...但这在安全规则中不起作用。要调试,在版本 3 中似乎没有办法通过 firebase API 访问 auth 对象。例如。 getAuth() 方法似乎丢失了,并且 auth.getCurrentUser() 方法没有 return 我添加给用户的任何自定义声明,所以我不清楚它们是否存在于 auth 对象中.文档 here 指出:
"You can also optionally specify additional claims to be included in the custom token. These claims will be available in the auth / request.auth objects in your Security Rules."
当 运行 应用程序时,对于路径“/domains/mydomaincom”上的任何读/写尝试,我都会收到当前经过身份验证的用户的访问被拒绝错误。
但是,我肯定会生成一个自定义签名的 JWT 令牌,并且它肯定会被 Firebase 自定义身份验证方法接受。当我将 custom-auth-server-returned JWT 令牌有效负载粘贴到 Firebase 规则模拟器中时,我肯定也得到了访问正确路径的规则(例如“/domains/mydomaincom”下的所有数据) .
当我通过 getCurrentUser() 查看用户时,我看不到 firebase auth 对象,因为它可能会出现在规则引擎中(例如,我的自定义声明的 none 似乎包含在currentUser 对象)所以我真的不明白如何进一步调试。
我正在寻找有关自定义身份验证的 Firebase 规则上还有哪些其他调试方法可用的提示,and/or 其他人是否可以确认他们已成功获得自定义身份验证属性以供使用Firebase 3 中的规则。
这是自定义 JWT 的(安全隐蔽的)内容,作为从服务器端生成的令牌解码客户端。
{
"iss": "<client_email>",
"sub": "<client_email>",
"aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"exp": 1463955043,
"iat": 1463951442,
"provider": "gas",
"domain": "mydomaincom",
"email": "myemail@mydomain.com",
"uid": "<FB UID HERE>",
"userGoogToken": "<LONG TOKEN HERE>",
"adminGoogToken": "<LONG TOKEN HERE>"
}
规则文件如下:
{
"rules": {
"domains": {
"$domain": {
".read": "$domain == auth.domain",
".write": "$domain == auth.domain"
}
}
}
}
根据您指出的文档,您可以在身份验证规则中使用的附加声明需要在 claims
属性中指定为附加声明的映射。试试这个:
{
"iss": "<client_email>",
"sub": "<client_email>",
"aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"exp": 1463955043,
"iat": 1463951442,
"provider": "gas",
"email": "myemail@mydomain.com",
"uid": "<FB UID HERE>",
"userGoogToken": "<LONG TOKEN HERE>",
"adminGoogToken": "<LONG TOKEN HERE>",
"claims": {
"domain": "mydomaincom"
}
}
legacy Firebase documentation 表示您可以将属性插入数据库安全规则中使用的 auth 对象。事实上,在我升级到版本 3 之前,这种方法在我的应用程序中运行得非常好。
"Any values passed into createToken() are appended onto the auth variable for use in your Security and Firebase Rules."
新的 Firebase 身份验证服务仍然允许使用自定义身份验证令牌,new rules documentation 声明:
"Developers creating their own custom authentication tokens can optionally add additional claims to these tokens. These additional claims will become present on the auth variable in your rules."
...但这在安全规则中不起作用。要调试,在版本 3 中似乎没有办法通过 firebase API 访问 auth 对象。例如。 getAuth() 方法似乎丢失了,并且 auth.getCurrentUser() 方法没有 return 我添加给用户的任何自定义声明,所以我不清楚它们是否存在于 auth 对象中.文档 here 指出:
"You can also optionally specify additional claims to be included in the custom token. These claims will be available in the auth / request.auth objects in your Security Rules."
当 运行 应用程序时,对于路径“/domains/mydomaincom”上的任何读/写尝试,我都会收到当前经过身份验证的用户的访问被拒绝错误。
但是,我肯定会生成一个自定义签名的 JWT 令牌,并且它肯定会被 Firebase 自定义身份验证方法接受。当我将 custom-auth-server-returned JWT 令牌有效负载粘贴到 Firebase 规则模拟器中时,我肯定也得到了访问正确路径的规则(例如“/domains/mydomaincom”下的所有数据) .
当我通过 getCurrentUser() 查看用户时,我看不到 firebase auth 对象,因为它可能会出现在规则引擎中(例如,我的自定义声明的 none 似乎包含在currentUser 对象)所以我真的不明白如何进一步调试。
我正在寻找有关自定义身份验证的 Firebase 规则上还有哪些其他调试方法可用的提示,and/or 其他人是否可以确认他们已成功获得自定义身份验证属性以供使用Firebase 3 中的规则。
这是自定义 JWT 的(安全隐蔽的)内容,作为从服务器端生成的令牌解码客户端。
{
"iss": "<client_email>",
"sub": "<client_email>",
"aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"exp": 1463955043,
"iat": 1463951442,
"provider": "gas",
"domain": "mydomaincom",
"email": "myemail@mydomain.com",
"uid": "<FB UID HERE>",
"userGoogToken": "<LONG TOKEN HERE>",
"adminGoogToken": "<LONG TOKEN HERE>"
}
规则文件如下:
{
"rules": {
"domains": {
"$domain": {
".read": "$domain == auth.domain",
".write": "$domain == auth.domain"
}
}
}
}
根据您指出的文档,您可以在身份验证规则中使用的附加声明需要在 claims
属性中指定为附加声明的映射。试试这个:
{
"iss": "<client_email>",
"sub": "<client_email>",
"aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
"exp": 1463955043,
"iat": 1463951442,
"provider": "gas",
"email": "myemail@mydomain.com",
"uid": "<FB UID HERE>",
"userGoogToken": "<LONG TOKEN HERE>",
"adminGoogToken": "<LONG TOKEN HERE>",
"claims": {
"domain": "mydomaincom"
}
}