我正在尝试使用 PHP 从服务器访问 Firebase,Google Auth library , 和一个 wrapper对于 Firebase 的 REST...这非常适合实现这一点:
use Firebase\JWT\JWT;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use GuzzleHttp\Client;
$email = 'account@email.com';
$key = 'private_key_goes_here';
$scopes = [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/firebase.database',
];
$creds = [
'client_email' => $email,
'private_key' => $key,
];
$serviceAccount = new ServiceAccountCredentials($scopes, $creds);
$handler = HttpHandlerFactory::build(new Client());
$token = $serviceAccount->fetchAuthToken($handler);
$firebase = new \Firebase\FirebaseLib($url, $token);
$value = $firebase->get('test/hello');
# $value now stores "world"
但是,这要求Firebase中的安全规则是通用读/写的,这是我不希望的。如果我将我的安全规则更新为:
{
"rules": {
"test": {
".read": "auth != null"
}
}
}
$value
中的结果变为 {"error": "Permission denied"}
。我进行了广泛的搜索,并尝试了许多排列组合和可能的解决方案,但没有得出结论性的结果。
我用过 this code向最终客户提供 JWT token ,最终客户可以成功使用它们并毫无问题地利用安全规则。我最初对服务器尝试了相同的方法,但没有成功。我选择尝试结合这两种方法:
# Snipping code that didn't change...
$serviceAccount = new ServiceAccountCredentials($scopes, $creds);
$handler = HttpHandlerFactory::build(new Client());
$payload = [
'iss' => $email,
'sub' => $email,
'aud' => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit',
'iat' => time(),
'exp' => time() + 60 * 60,
'uid' => '123',
'claims' => [
'uid' => '123',
],
];
$payload = $serviceAccount->updateMetadata($payload);
$token = JWT::encode($payload, $key, 'RS256');
$firebase = new \Firebase\FirebaseLib($url, $token);
$value = $firebase->get('test/hello');
这似乎很接近,但是 $value
现在包含 {"error": "Missing claim 'kid' in auth header."
。为了解决这个问题,我修改了编码调用:
$token = JWT::encode($payload, $key, 'RS256', 'key_id_goes_here');
这会导致一个略有不同的错误:Invalid claim 'kid' in auth header.
,表明我在正确的轨道上......但不完全正确。直接使用 JWT token 会产生完全相同的结果。任何想法我做错了什么?电子邮件、私钥和 key ID 都直接来 self 创建服务帐户时提供的 json
凭证文件。
我查看了数十页的文档和帖子,以下是最有帮助的:
- Using JWT for Server Auth (Firebase Docs)
- Using Custom Tokens to make REST requests to FB DB as an admin
- Is it still possible to do server side verification of tokens in Firebase 3?
交叉发布到 Firebase Google Group .
最佳答案
在使用将成为安全规则中的 auth
变量的服务帐户进行身份验证时,您可以指定 auth_variable_override
查询参数。它应该是一个正确转义的 JSON 对象。例如要执行 {"uid":123}
你要添加:
?auth_variable_override=%7B%22uid%22%3A%22123%22%7D
到您的请求 URL 的末尾。
关于php - 使用带有身份验证的 PHP 进行 Firebase REST 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003097/