使用 php 设置 firebase v3 自定义身份验证

我正在尝试按照以下指南使用来自 google 的新 firebase sdk 设置自定义身份验证:https://firebase.google.com/docs/auth/server#use_a_jwt_library
在 samble 代码中它说:

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 :

从 JSON 密钥文件中获取您的服务帐号的电子邮件地址和私钥

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

不幸的是,我不知道从哪里获得这个 json 文件.如果我去我的 firebase 控制台 (https://console.firebase.google.com/) 我设法下载了一个 json文件,但它不包含任何电子邮件地址和私钥.

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.

我设法在我的 google 云平台控制台 (http://console.conf) 中找到了一个包含电子邮件地址和私钥的 json 文件.cloud.google.com) 进入API Manager > Credentials"菜单.令人惊讶的是,我的 firebase 应用程序显示在那里.我将电子邮件和密钥复制并粘贴到示例代码中,然后出现此错误:

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 :

警告:openssl_sign():无法将提供的密钥参数强制转换为第 183 行/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php 中的私钥致命错误:未捕获的异常DomainException"' 在/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php:185 中带有消息 'OpenSSL 无法签署数据' 堆栈跟踪:#0/volume1/web/yeti/vendor/firebase/php-jwt/src/JWT.php(154): FirebaseJWTJWT::sign('eyJ0eXAiOiJKV1Q...', NULL, 'RS256') #1/volume1/web/yeti/jwt.php(21):FirebaseJWTJWT::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 第 185 行

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): FirebaseJWTJWT::sign('eyJ0eXAiOiJKV1Q...', NULL, 'RS256') #1 /volume1/web/yeti/jwt.php(21): FirebaseJWTJWT::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 ?

谢谢

推荐答案

您找到解决方案了吗?仍然遇到同样的问题!适用于 HS256,不适用于 RS256.这是谷歌云的限制吗?

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 ?

非常感谢!@dbburgess

Thank you so much ! @dbburgess

问题:使用了错误的密钥和电子邮件.这些应在与 Firebase 项目对应的 GCP 凭据部分中生成.

Problem: Was using the wrong key and email. These should be generated in the Google Cloud credentials section that corresponds to the Firebase project.

解决方案:

  • 转到console.cloud.google.com".
  • 选择相关的 Firebase 项目.
  • 然后是API 管理器"->凭据".
  • 创建凭据"->服务帐户密钥"-> 选择 JSON.
  • 创建的文件将包含所需的private_key"&'client_email'.

填写值:

$service_account_email = "autogeneratedemail@developer.gserviceaccount.com";$private_key = "-----开始私钥----- SoneVeryVeryLongKey= -----结束私钥----- ";$uid = 'UserToUseInFirebaseRules';$is_premium_account = $uid;

$service_account_email = "autogeneratedemail@developer.gserviceaccount.com"; $private_key = "-----BEGIN PRIVATE KEY----- SoneVeryVeryLongKey= -----END PRIVATE KEY----- "; $uid = 'UserToUseInFirebaseRules'; $is_premium_account = $uid;

您不需要更改create_custom_token"函数中的任何内容,也许可以根据您的需要更改到期日期/时间.

You shouldn't need to change anything in the "create_custom_token" function, maybe the expiration date/time according to your needs.

然后调用函数:

create_custom_token($uid, $is_premium_account);

相关文章