lui*_*lui 4 rest curl firebase-security firebase-authentication google-cloud-firestore
我在 PHP 项目中使用 REST API 通过curl (PHP curl) 将数据发送到 Firestore DB。
\n\n我正在努力理解如何为“服务”设置身份验证规则,而通过 Google 授权建议的方法对我来说似乎太过分了。
\n\n我的数据必须可供所有人读取,但只能由我的 PHP 服务器写入。
\n\n到目前为止,我可以确保只有在发送的数据集中提供了特定代码(auth=123)时才会发生写入操作:
\n\nservice cloud.firestore {\n match /databases/{database}/documents {\n match /{document=**} {\n allow read, write: if request.resource.data.auth==123\n //request.auth.uid != null;\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n问题是,由于每个人都可以读取数据,因此很容易找到该字段(auth)并知道 123 是代码。
\n\n两者中的任何一个都可能:
\n\n发送标头(或其他未记录到数据库数据中的标头)并针对该标头而不是数据字段执行规则检查?
否则(不太优选)可能只向用户隐藏字段“auth”?
我知道选项 2 原则上是不可能的,但即使我不知道如何解决,它也可能以某种方式解决。
\n\n虽然我知道这不是最好的方法,但这足以保护我的应用程序。
\n\n编辑
\n\n可能最简单、最干净的解决方案是在 Firestore 数据库中使用一对电子邮件/密码创建一个用户,并在 PHP curl 请求中使用它
\n\n我想我需要在此之后发出一个curl请求:
\n\nhttps://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password
\n\n然后使用第一个请求中收到的令牌作为身份验证,通过另一个 PHPcurl 发出数据上传
\n\n我觉得这是正确的道路,但是......我在第一个请求时陷入困境
\n\n 404. That\xe2\x80\x99s an error.\n\nThe requested URL /identitytoolkit/v3/relyingparty/verifyPassword?key=myapiket was not found on this server. That\xe2\x80\x99s all we know.\nRun Code Online (Sandbox Code Playgroud)\n\n我觉得我错误地塑造了我的卷曲请求......关于如何正确处理有效负载有什么建议吗?
\n\n解决方案
\n\n请参阅下面我的答案以及步骤和 PHP 代码
\n解决方案
好的,我找到了在使用通过 curl PHP 使用 Firebase Firestore REST API 时实现简单但有效的身份验证的解决方案。
1- 创建 Firebase 项目
2-就我而言,我需要每个人都能够读取数据,并且只有一个用户能够写入数据。所以在“数据库/规则”中我输入了:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
3- 在“身份验证/登录方法”中启用“电子邮件/密码”作为登录提供者
4- 在“身份验证/用户”中,添加用户并提供一对“电子邮件/密码”
5- 确定您的项目 API 密钥,可以在“项目概述/设置/常规/Web API 密钥”中找到
6-发出curl请求以获取通过用户名和密码进行验证的tokenId,并使用此令牌来验证Firestore操作。代码如下:
<?php
// 1) Retrieve Authorization TOKEN
// https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password
$firestore_key = "MY_KEY";
$url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword";
$postfields = array(
"email" => 'email@domain.com',
"password" => 'password123456',
"returnSecureToken" => true
);
$datapost = json_encode($postfields);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_URL => $url . '?key='.$firestore_key,
CURLOPT_USERAGENT => 'cURL',
CURLOPT_POSTFIELDS => $datapost
));
$response = curl_exec( $curl );
curl_close( $curl );
$responsJson=json_decode($response,true);
// 2) Add data
//https://medium.com/@aliuwahab/firestore-firebase-php-saving-data-to-a-firestore-database-using-php-curl-2921da3b0ed4
$ID=10000001;
$firestore_data = [
"status" => ["integerValue" => 1500]
];
$data = ["fields" => (object)$firestore_data];
// Possible *value options are:
// stringValue
// doubleValue
// integerValue
// booleanValue
// arrayValue
// bytesValue
// geoPointValue
// mapValue
// nullValue
// referenceValue
// timestampValue
$json = json_encode($data);
#Provide your firestore project ID Here
$project_id = "myspecific_project"; //found in "Database/Data"
#Provide your firestore documents group
$documents_group = "my_group";
// /sf/ask/3560671411/#50866783
$url = "https://firestore.googleapis.com/v1beta1/projects/$project_id/databases/(default)/documents/$documents_group/$ID/";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array('Content-Type: application/json',
'Content-Length: ' . strlen($json),
'X-HTTP-Method-Override: PATCH',
'Authorization: Bearer '.$responsJson['idToken']
),
CURLOPT_URL => $url . '?key='.$firestore_key ,
CURLOPT_USERAGENT => 'cURL',
CURLOPT_POSTFIELDS => $json
));
$response = curl_exec( $curl );
curl_close( $curl );
// Show result
echo $response . "\n";
?>
Run Code Online (Sandbox Code Playgroud)
注意:在代码中,我标记了帮助我找到可行解决方案的信息链接
| 归档时间: |
|
| 查看次数: |
1375 次 |
| 最近记录: |