Final goal: an Angular client receives a token valid for one hour in order to query data from FireStore.
Steps to produce a prove of concept and learn how to work with Custom Tokens:
1 - I created a project in Firebase using firebase tool (https://console.firebase.google.com/project/firetestjimis/overview)
2 - I added Firestore database and created a collection. I chose production instead of test because this POC is aimed for security reasons.
3 - I added manually an user in Firebase/Authentication/Add User
4 - I copied User UID from above user added (it is used bellow)
5 - I created a very simple firebase Cloud Function applications in order to answer back a Custom Token. Basically I ran firebase init and added this code in index.tx
import * as functions from 'firebase-functions';
import * as admin from "firebase-admin";
export const getCustomToken = functions.https.onRequest((request, response) => {
if (admin.apps.length < 1) { //Checks if app already initialized
admin.initializeApp();
}
const uid = "UID mentioned above";
admin.auth().createCustomToken(uid)
.then(function (customToken) {
console.log(customToken.toString);
response.send(customToken);
})
.catch(function (error) {
console.log("Error creating custom token:", error);
});
});
I reached this by following other stackoverflow answer
6 - I succesfully can get a Custom Token from https://us-central1-firetestjimis.cloudfunctions.net/getCustomToken
7 - I can successfully post this Custom Token to https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken and get back idTken like
{
"kind": "identitytoolkit#VerifyCustomTokenResponse",
"idToken": "eyJhbGciOiJSUzI1NiI ... .... aMorw",
"refreshToken": "AE0u-Ney9OJ04Z3xA7ACsmI1S637hXeuCTEdaEU9cxhhPnlwh-9q0X7QpSxVRIYdTdrTgXUbS9Q6yUdAWVeXzhGMiLLLHtwSWSoVSWnZs3Gp1Sb050tThNPQiSsSss8GkCigft3PTBkY4nIbRy3A5dA8FHCLbMYQSfKYqvu8To76UyMVCYONJOM",
"expiresIn": "3600",
"isNewUser": false
}
8 - Now I want to post a simple docuemnt to Firestore collection throw
curl --location --request POST 'https://firestore.googleapis.com/v1/projects/firetestjimis/databases/(default)/documents/transfer' \
--header 'Authorization: Bearer /eyJhbGc ... ... iNaMorw' \
--header 'Content-Type: application/json' \
--data-raw '{
"fields": {
"id": {
"stringValue": "1"
},
"status": {
"stringValue": "fracasso"
}
}
}'
and I get this error:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
So my main question is: isn't that idToken returned from https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken a valid token to reach the related Firestore?