我正在尝试将Firebase Auth与PHP Slim (JWT)整合,但没有任何进展。我使用我的Firebase用户登录,并正确保存我的token。然后我把midleware.php设置成这样。
$app->add(new Tuupola\Middleware\JwtAuthentication([
"ignore" => ["/countries","/faqs"],
"secret" => $secrets,
"secure" => false
]));
其中 $secrets 是来自 [email protected] 的孩子. 然而,我一直得到一个错误401未授权。
当我用自定义的$secret和自定义的jwt来尝试时,同样的代码也能工作。Firebase是否需要在JwtAuthentication中添加一些额外的东西?
所以问题在于Tuupola的JwtAuthentication是如何处理kid的(实际上是kid - key id,它来自googles 公钥).
在vanilla php中,我必须从google中获取秘密,然后在使用前将其分割成数组。然而这一切都由Tuupola内部完成,这导致了我的问题。
正确的Slim 4与Firebase Auth配合使用的中间件如下。
return function (App $app) {
$app->add(CorsMiddleware::class);
$container = $app->getContainer();
$rawPublicKeys = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/[email protected]');
$keys = json_decode($rawPublicKeys, true);
$app->add(new Tuupola\Middleware\JwtAuthentication([
"ignore" => ["/api/records/countries","/faqs","/","/answer"],
"algorithm" => ["RS256"],
"header" => "X-Authorization",
"regexp" => "/Bearer\s+(.*)$/i",
"secret" => $keys,
"secure" => false
}
]));
};