带有Firebase JWT的PHP Slim [英] PHP Slim with Firebase JWT

查看:95
本文介绍了带有Firebase JWT的PHP Slim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Firebase Auth与PHP Slim(JWT)集成在一起,但没有任何运气.我使用Firebase用户登录并正确保存了令牌.然后我将我的midleware.php设置如下:

I am trying to integrate Firebase Auth with PHP Slim (JWT) without any luck. I login using my firebase user and save my token correctly. Then I set my midleware.php like this:

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "ignore" => ["/countries","/faqs"],
    "secret" => $secrets,
    "secure" => false
]));

其中$ secrets是来自securetoken@system.gserviceaccount.com的孩子.但是,我不断收到未经授权的错误401.

where $secrets is the kid coming from securetoken@system.gserviceaccount.com. However I keep getting an error 401 not authorized.

当我使用自定义的$ secret和自定义的jwt尝试相同的代码时,它们会起作用. Firebase是否需要JwtAuthentication中的其他功能?

Same code works when I try it with a custom $secret and custom jwt. Does Firebase need something extra in the JwtAuthentication?

推荐答案

所以问题是Tuupola的JwtAuthentication如何处理孩子(实际上是孩子-来自Google的密钥ID

So the issue was how JwtAuthentication from Tuupola handles the kid (it is actually kid - key id which comes from googles public keys).

在香草PHP中,我必须从Google那里获取秘密,然后在使用它之前将其拆分为多个数组.但是,这一切都是由Tuupola内部完成的,这导致了我的问题.

In vanilla PHP, I had to get the secrets from google, then split it into arrays before using it. However, this is all done by Tuupola internally, which caused my issue.

Slim 4与Firebase Auth配合使用的正确中间件如下:

The correct Middleware for Slim 4 to work with Firebase Auth is the following:

return function (App $app) {
    $app->add(CorsMiddleware::class);

    $container = $app->getContainer();
    
    $rawPublicKeys = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com');
    $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
        }
    ]));
};

这篇关于带有Firebase JWT的PHP Slim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆