使用PHP设置firebase v3自定义身份验证 [英] Setting up firebase v3 custom auth with php
问题描述
我试图根据以下指南,使用来自Google的新的firebase sdk设置自定义身份验证: https:/ /firebase.google.com/docs/auth/server#use_a_jwt_library
在samble代码中,它表示:
lockquote
从JSON密钥文件中获取您的服务帐户的电子邮件地址和私钥
不幸的是我不知道在哪里得到这个json文件。如果我转到我的Firebase控制台( https://console.firebase.google.com/ ),我设法阻止了一个json文件,但它不包含任何电子邮件地址和私钥。
我设法在我的谷歌云平台控制台中找到包含电子邮件地址和私钥的json文件( http://console.cloud.google.com ),然后转到API管理器>凭据菜单。令人惊讶的是,我的firebase应用程序显示在那里。我复制并粘贴了电子邮件和密钥到示例代码,然后我得到这个错误:
$ b
警告:openssl_sign():提供的关键参数无法强制转换为/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php中的私钥,致183线路致命错误:未捕获的异常DomainException带有消息OpenSSL无法签署数据in / volume1 / web / yeti / vendor / firebase / php-jwt / src / JWT.php:185堆栈跟踪:#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}抛入/ volume1 / web / yeti / vendor / firebase /php-jwt/src/JWT.php on line 185
有人知道我做错了什么吗? / p>
谢谢
您是否找到解决方案?仍然遇到同样的问题!适用于HS256,不适用于RS256。它是谷歌云类的限制吗?
非常感谢! @dbburgess
问题:使用错误的密钥和电子邮件。这些应该在与Firebase项目相对应的Google Cloud凭据部分中生成。
解决方案: $ service_account_email =
$ b
$ private_key =----- BEGIN私钥----- \\\\\\\\\\\\\\\\\\\\
$ uid ='UserToUseInFirebaseRules';
$ is_premium_account = $ uid;
您不需要在create_custom_token函数中更改任何内容,也可以根据您的需要更改过期日期/时间。
然后调用函数:
$ $ $ codereate_custom_token($ uid,$ is_premium_account);
I'm trying to set up custom auth with the new firebase sdk from google following those guidelines : https://firebase.google.com/docs/auth/server#use_a_jwt_library
In the samble code it says :
Get your service account's email address and private key from the JSON key file
Unfortunately I have not idea where to get this json file. If I go to my firebase console (https://console.firebase.google.com/) I manage to donwload a json file but it does not contain any email adress and private key.
I managed to find a json file that contains an email adress and a private key in my google cloud platform console (http://console.cloud.google.com) by goind into the "API Manager > Credentials" menu. Surprisingly my firebase app was showed there. I copy and pasted the email and key into the sample code, then I got this error :
Warning: openssl_sign(): supplied key param cannot be coerced into a private key in /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php on line 183 Fatal error: Uncaught exception 'DomainException' with message 'OpenSSL unable to sign data' in /volume1/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} thrown in /volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php on line 185
Does someone has an idea of what I'm doing wrong ?
Thanks
Did you find the solution ? Still experiencing the same issue ! Works with HS256 and doesn't with RS256. Is it google cloud kind of limitation ?
Thank you so much ! @dbburgess
Problem: Was using the wrong key and email. These should be generated in the Google Cloud credentials section that corresponds to the Firebase project.
Solution:
- Go to 'console.cloud.google.com'.
- Select the related Firebase project.
- Then 'API Manager' -> 'Credentials'.
- 'Create Credentials' -> 'Service Account Key' -> Choose JSON.
- The created file will contain the needed 'private_key' & 'client_email'.
Fill the values:
$service_account_email = "autogeneratedemail@developer.gserviceaccount.com"; $private_key = "-----BEGIN PRIVATE KEY-----\nSoneVeryVeryLongKey=\n-----END PRIVATE KEY-----\n"; $uid = 'UserToUseInFirebaseRules'; $is_premium_account = $uid;
You shouldn't need to change anything in the "create_custom_token" function, maybe the expiration date/time according to your needs.
Then call the function:
create_custom_token($uid, $is_premium_account);
这篇关于使用PHP设置firebase v3自定义身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!