I'm using REST API in a PHP project to send the data to Firestore DB through curl (PHP curl).
I'm struggling in understanding how to setup an authentication rule for a "service" and the suggested approach through Google authorization looks overkilling to me.
My data must be readable by ALL but writable just by my PHP server.
So far I could ensure that a writing operation could happen only if in the dataset sent a specific code is provided (auth=123):
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.resource.data.auth==123
//request.auth.uid != null;
}
}
}
Problem is that, as everybody can read the data, it's very easy to find this field (auth) and to know that 123 is the code.
Would be possible any of the two:
Send headers (or other not recorded into DB data) and perform a rule check against that instead of the data fields?
Otherwise (less preferred) would be possible to hide ONLY the field "auth" to the users?
I know option 2 is not possible in principle but it might be worked around somehow even if I don't know how.
While I understand this is not the best approach, it would be more than enough to secure my application.
EDIT
Probably the simplest cleanest solution would be to create an user in Firestore db with a pair email/password and use that in the PHP curl request
I think I need to issue a curl request following this:
https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password
And then issuing the upload of the data through another PHP curl using as auth the token received in the first request
I feel like it's the right path but.... I get stuck at the first request with
404. That’s an error.
The requested URL /identitytoolkit/v3/relyingparty/verifyPassword?key=myapiket was not found on this server. That’s all we know.
I feel like I badly shaped my curl request... any suggestion on how to handle properly the payload?
SOLUTION
See my answer below with steps and PHP code