Configure firebase v3 custom auth with php

I am trying to configure custom auth using the new firebase sdk from Google, following these guidelines: https://firebase.google.com/docs/auth/server#use_a_jwt_library
In the samble code, it says:

Get your service account email address and private key from the JSON key file

Unfortunately, I do not know where to get this json file. If I go to the firebase console ( https://console.firebase.google.com/ ), I can download the json file, but it does not contain any email addresses and private key.

I managed to find the json file containing the email address and private key on the Google Cloud Cloud console ( http://console.cloud.google.com ) by looking at "API Manager> Credentials". Surprisingly, my firebase application was shown there. I copy and paste the email address and key into the sample code, then I got this error:

Warning: openssl_sign (): the specified key parameter cannot be forced into the private key in / volume 1 / web / yeti / vendor / firebase / php-jwt / src / JWT.php on line 183 Fatal error: exception thrown 'ExceptionException' with the message "OpenSSL cannot sign data" in / volume 1 / web / yeti / vendor / firebase / php-jwt / src / JWT.php: 185 Stack trace: # 0 / volume1 / web / yeti / vendor / firebase / php -jwt / src / JWT.php (154): Firebase \ JWT \ JWT :: sign ('eyJ0eXAiOiJKV1Q ...', NULL, 'RS256') # 1 / volume1 / web / yeti / jwt.php (21): Firebase \ JWT \ JWT :: encode (Array, NULL, 'RS256') # 2 / volume1 / web / yeti / jwt.php (24): create_custom_token ('1234', false) # 3 {main} throw in / volume1 /web/yeti/vendor/firebase/php-jwt/src/JWT.php on line 185

Does anyone have an idea of ​​what I'm doing wrong?

thanks

+7
source share
4 answers

Did you find a solution? Still experiencing the same problem! Works with HS256 and not with RS256. Is this a cloud limitation?


Thank you very much! @dbburgess

Problem: used the wrong key and email. They must be generated in the Google Cloud credentials section of the Firebase project.

Decision:

  • Go to console.cloud.google.com
  • Select the appropriate Firebase project.
  • Then "API Manager" → "Credentials".
  • “Create Credentials” → “Service Account Key” → Select JSON.
  • The created file will contain the necessary "private_key" and "client_email".

Fill in the values:

$ service_account_email = " autogeneratedemail@developer.gserviceaccount.com "; $ private_key = "----- START A PRIVATE KEY ----- \ nSoneVeryVeryLongKey = \ n ----- END PRIVATE KEY ----- \ n"; $ uid = 'UserToUseInFirebaseRules'; $ is_premium_account = $ uid;

You do not need to change anything in the function "create_custom_token", perhaps the expiration date / time according to your needs.

Then call the function:

create_custom_token($uid, $is_premium_account); 
+7
source

Found myself what was wrong! The sample php code from the documentation does not work. Instead

 return JWT::encode($payload, $private_key, "RS256"); 

using

 return JWT::encode($payload, $private_key, "HS256"); 

Edit:
Actually, it was just an example php code from google firebase document, which was completely faulty. it passed a blank key to php-jwt. It looks like they updated it today and it works great :)

+3
source

This is what I do, and everything works fine. What you provided in the claims array is shown in auth in the security rules. The email and key come from the json file that you receive when you create the service account (see the "Before You Begin" section).

 $userId = '1234'; $email = ' sample@email.com '; $key = 'giant_key_goes_here'; $payload = [ 'iss' => $email, 'sub' => $email, 'aud' => 'https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit', 'iat' => time(), 'exp' => time() + 60 * 60, 'uid' => $userId, 'claims' => [ 'uid' => $userId, ], ]; $token = JWT::encode($payload, $key, 'RS256'); 

It should be noted that the format on the keys is a bit complicated ... Your key will look something like this (only an approximate key):

 -----BEGIN PRIVATE KEY----- MIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp wmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5 1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh 3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2 pIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX GukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il AkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF L0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k X6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl U9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ 37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0= -----END PRIVATE KEY----- 

You may need to do a little fancy formatting, this is basically what I did:

 $key = "-----BEGIN PRIVATE KEY-----\nMIICXAIBAAKBgQCqGKukO1De7zhZj6+H0qtjTkVxwTCpvKe4eCZ0FPqri0cb2JZfXJ/DgYSF6vUp\nwmJG8wVQZKjeGcjDOL5UlsuusFncCzWBQ7RKNUSesmQRMSGkVb1/3j+skZ6UtW+5u09lHNsj6tQ5\n1s1SPrCBkedbNf0Tp0GbMJDyR4e9T04ZZwIDAQABAoGAFijko56+qGyN8M0RVyaRAXz++xTqHBLh\n3tx4VgMtrQ+WEgCjhoTwo23KMBAuJGSYnRmoBZM3lMfTKevIkAidPExvYCdm5dYq3XToLkkLv5L2\npIIVOFMDG+KESnAFV7l2c+cnzRMW0+b6f8mR1CJzZuxVLL6Q02fvLi55/mbSYxECQQDeAw6fiIQX\nGukBI4eMZZt4nscy2o12KyYner3VpoeE+Np2q+Z3pvAMd/aNzQ/W9WaI+NRfcxUJrmfPwIGm63il\nAkEAxCL5HQb2bQr4ByorcMWm/hEP2MZzROV73yF41hPsRC9m66KrheO9HPTJuo3/9s5p+sqGxOlF\nL0NDt4SkosjgGwJAFklyR1uZ/wPJjj611cdBcztlPdqoxssQGnh85BzCj/u3WqBpE2vjvyyvyI5k\nX6zk7S0ljKtt2jny2+00VsBerQJBAJGC1Mg5Oydo5NwD6BiROrPxGo2bpTbu/fhrT8ebHkTz2epl\nU9VQQSQzY1oZMVX8i1m5WUTLPz2yLJIBQVdXqhMCQBGoiuSoSjafUhV7i1cEGpb88h5NBYZzWXGZ\n37sJ5QsW+sJyoNde3xH8vdXhzU7eT82D6X/scw9RZz+/6rCJ4p0=\n-----END PRIVATE KEY-----\n"; 

Note that line breaks turn into \n , and all of them are reset to one line. There are various ways to do it, but ... Based on the error you received, a problem may arise with this.

+3
source

instead

 $key = 'giant_key_goes_here'; token = JWT::encode($payload, $key, 'RS256'); 

use

 define("FIREBASE_PRIVATE_KEY","giant_key_goes_here"); token = JWT::encode($payload, FIREBASE_PRIVATE_KEY, 'RS256'); 
-1
source

All Articles