Skip to content
Advertisement

PHP Slim with Firebase JWT

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 TuupolaMiddlewareJwtAuthentication([
    "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?

Advertisement

Answer

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 TuupolaMiddlewareJwtAuthentication([
        "ignore" => ["/api/records/countries","/faqs","/","/answer"],
        "algorithm" => ["RS256"],
        "header" => "X-Authorization",
        "regexp" => "/Bearers+(.*)$/i",
        "secret" => $keys,
        "secure" => false
        }
    ]));
};
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement