【发布时间】:2021-02-06 03:42:06
【问题描述】:
我在我的 Flutter 应用中使用 Firebase 身份验证来管理用户(Facebook、Google、电子邮件...)。当用户登录我的应用程序时,我将 Firebase 令牌发送到我的 PHP 服务器,在该服务器上使用 JWT 验证该令牌。
问题是,虽然通过电子邮件登录生成的令牌已正确验证,但 Facebook 或 Google 登录生成的令牌失败并显示“SignatureInvalidException:签名验证失败”。
Flutter 应用中的 Facebook 登录代码:
FirebaseAuth auth = FirebaseAuth.instance;
final LoginResult result = await FacebookAuth.instance.login();
final FacebookAuthCredential facebookAuthCredential = FacebookAuthProvider.credential(result.accessToken.token);
UserCredential user = await auth.signInWithCredential(facebookAuthCredential);
String token = await auth.currentUser.getIdToken();
print(token.toString());
在https://jwt.io/调试器中正确验证了token的信息,所以header和payload都没问题。
在 PHP 服务器中(两种情况下的代码相同):
-
验证电子邮件和密码令牌(OK):
stdClass 对象([iss] => https://securetoken.google.com/project-123546 [aud] => project-123546 [auth_time] => 1603424825 [user_id] => S2gpfdsa156dsacdsfQ2z1 [sub] => S2gpfdsa156dsacdsfQ2z1 [iat] => [56034248 exp] => 1603428425 [email] => user@example.com [email_verified] => [firebase] => stdClass Object ( [identities] => stdClass Object ( [email] => Array ( [0] => user@ example.com ) ) [sign_in_provider] => 密码 ) )
-
验证 Facebook 令牌 (KO):
致命错误:未捕获的 Firebase\JWT\SignatureInvalidException:签名验证在 server\vendor\firebase\php-jwt\src\JWT.php:122 中失败 堆栈跟踪:#0 server\token.php(18) : Firebase\JWT\JWT::decode('eygswbdsasdJSUzI...', '-----BEGIN CERT...', Array) #1 {main} 在 server\vendor\firebase\php-jwt\src 中抛出\JWT.php 在第 122 行
PHP 代码:
$token = "...TOKEN-FROM-APP...";
$pkeys_raw = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com');
$pkeys = json_decode($pkeys_raw, true);
$decoded = JWT::decode($token, $pkeys, ["RS256"]);
print_r($decoded);
KeyID 与 https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com 中的密钥匹配,但我认为应用程序(或 Firebase)未使用 Facebook 和 Google 登录中的私钥正确签署令牌。不过我在邮箱和密码登录也是用auth.currentUser.getIdToken(),所以没有区别。
知道如何解决这个问题吗?
【问题讨论】:
标签: php firebase flutter firebase-authentication jwt