PHP Slim with Firebase JWT

╄→尐↘猪︶ㄣ 提交于 2021-02-08 06:53:23

问题


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
]));

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

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


回答1:


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

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.

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
        }
    ]));
};


来源:https://stackoverflow.com/questions/60817662/php-slim-with-firebase-jwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!