【问题标题】:PHP Slim with Firebase JWT带有 Firebase JWT 的 PHP Slim
【发布时间】:2020-07-04 03:48:04
【问题描述】:

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

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

其中 $secrets 是来自 securetoken@system.gserviceaccount.com 的孩子。但是我不断收到错误 401 not authorized。

当我尝试使用自定义 $secret 和自定义 jwt 时,相同的代码可以工作。 Firebase 在 JwtAuthentication 中是否需要额外的东西?

【问题讨论】:

  • 您没有提供足够的信息。 $secrets is the child come from... 是什么意思我相信你的意思是关键?

标签: php firebase jwt middleware slim


【解决方案1】:

所以问题是来自 Tuupola 的 JwtAuthentication 如何处理孩子(它实际上是孩子 - 来自谷歌 public keys 的密钥 id)。

在原版 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/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 javascript sdk 登录用户,然后在服务器端代码中检查令牌?
  • 这由使用 Slim 4 作为后端的移动应用程序使用。用户使用他们的 firebase 凭据登录应用,然后在后端使用他们的令牌以允许访问 Rest API。
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 2018-08-14
  • 2018-07-03
  • 2015-06-24
  • 2017-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多