簡體   English   中英

PHP Slim 4 - 使用 Firebase JWT 令牌授權 api 請求

[英]PHP Slim 4 - Authorize api request using firebase JWT token

我正在嘗試使用用於 slim 4 的 Tuupola Jwt 中間件驗證從 firebase javascript sdk 提供的 idToken,但我總是收到 401 錯誤。 這是我用來獲取令牌的客戶端代碼:

const provider = new firebase.auth.GoogleAuthProvider();
provider.addScope("profile");
provider.addScope("email");
firebase.auth().signInWithPopup(provider).then( (result) => {
 console.log(result);   
});

身份驗證流程將按預期正常工作,並且我能夠將令牌傳遞到 Authorization 標頭中,但我無法在使用 slim 4 作為 Restful api 的服務器上對其進行驗證。

我讀過關於這個問題的不同問題,但這些都沒有幫助我解決這個問題。

這是我的中間件實現:

use Tuupola\Middleware\CorsMiddleware;
use Tuupola\Middleware\JwtAuthentication;
use Slim\App as App;

return function(App $app) {
    $app->add(new Tuupola\Middleware\CorsMiddleware([
        "origin" => ["chrome-extension://oegddbimpfdpbojkmfibkebnagidflfc"],
        "methods" => ["GET", "POST", "OPTIONS"],
        "headers.allow" => ["Authorization"],
        "headers.expose" => [],
        "credentials" => true,
        "cache" => 86400
    ]));

//    $rawPublicKeys = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com');
//    $keys = json_decode($rawPublicKeys, true);
    $keys = file_get_contents('https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com');

    $app->add(new Tuupola\Middleware\JwtAuthentication([
        "algorithm" => ["RS256"],
        "header" => "X-Authorization",
        "regexp" => "/Bearer\s+(.*)$/i",
        "secret" => $keys,
        "secure" => false,
        "after" => function ($response, $arguments) {
            return $response->withHeader("X-Brawndo", "plants crave"); //this is only for test
        }
    ]));

};

這就是我的 index.php 文件中的內容,其中運行了 slim 應用程序

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Routing\RouteCollectorProxy;
use Slim\Routing\RouteContext;
use Slim\Factory\AppFactory;
use Tuupola\Middleware\CorsMiddleware;

require_once __DIR__.'/vendor/autoload.php';

$app = AppFactory::create();

$authMiddleware = require_once __DIR__.'/middleware.php';
$authMiddleware($app);

$app->get('/keygen', function(Request $request, Response $response, $args){  
    $password = bin2hex(random_bytes(3));
    $response->getBody()->write( json_encode(['generated_password' => $password]) );
    return $response->withHeader('Content-Type','application/json');
});

$app->add(new Tuupola\Middleware\CorsMiddleware([
    "origin" => ["*"],
    "methods" => ["GET", "POST", "OPTIONS"],
    "headers.allow" => ["Authorization"],
    "headers.expose" => [],
    "credentials" => true,
    "cache" => 86400
]));

$app->run();

我想要實現的是使用客戶端登錄后提供的 firebase idToken 對從客戶端向 api 發出的每個請求進行身份驗證。 當發出請求時,中間件將驗證令牌,然后授權用戶或不使用端點。

有可能解決這個問題嗎?

經過大量調試后,我發現並解決了問題。 在我的客戶端代碼中,我使用了錯誤的idToken作為Authorization: Bearer並且發送到服務器的標頭與中間件配置不匹配,在我的 axios 請求中,我發送的是X-Authorization標頭而不是Authorization 為了使用正確的令牌,我調用了firebase.auth().onAuthStateChanged( (user) =>{...})方法,當用戶對象可用時,我調用了getIdToken()方法。 此操作返回正確的 JWT 令牌以與中間件一起使用以對請求進行身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM