I'm migrating to the new database and 3.0 client libs. I'm updating the part which generates a custom auth token (on our server) to do a PATCH
to update a resource in the Firebase DB.
These PATCH requests used to be made by our server to Firebase using admin
claims based on this: https://www.firebase.com/docs/rest/guide/user-auth.htm
For the new DB, I'm generating the JWT token (using ruby-jwt
) like this:
payload = {
aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
claims: custom_claims.merge({ admin: true }),
exp: now_seconds + (60 * 60), # Maximum expiration time is one hour
iat: now_seconds,
iss: service_account_email,
sub: service_account_email,
uid: uid
}
JWT.encode(payload, private_key, "RS256")
A PATCH
request with this token to the Firebase DB fails with: Missing claim 'kid' in auth header
.
Here is the equivalent of Michael Bleigh's answer using the ruby googleauth module:
You will also need to set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path of your service account JSON file. the value forauth_client.sub
comes fromclient_email
in this JSON file.Of course, as above, this is only valid in a server application you control.
Also, making the request to the firebase REST API is still an exercise for the reader.
references
In the new Firebase you need to directly use a Service Account to create administrative access credentials. Here is a Node.js snippet that shows how to make a REST call to the Database:
To do the same in Ruby, you might take a look at the googleauth gem for fetching the access token using Service Account credentials.