From 1bbee1121bd45e4c02c4a423a3f8cfed8d1150f7 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Fri, 27 Jun 2025 18:00:45 -0400 Subject: [PATCH] FIX: Authorization en FastCGI --- cli/setup/setups/client.php | 10 +++++++--- cli/src/Service/FastCGI.php | 10 +++++++++- cli/src/Service/Login.php | 7 +++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cli/setup/setups/client.php b/cli/setup/setups/client.php index 2d84b25..35b8003 100644 --- a/cli/setup/setups/client.php +++ b/cli/setup/setups/client.php @@ -16,7 +16,8 @@ return [ $container->get(Psr\Log\LoggerInterface::class), implode(DIRECTORY_SEPARATOR, [$container->get('folders')->cache, 'token']), $container->get('API_USERNAME'), - $container->get('API_PASSWORD') + $container->get('API_PASSWORD'), + $container->get('API_KEY') ); }, GuzzleHttp\HandlerStack::class => function(ContainerInterface $container) { @@ -24,7 +25,7 @@ return [ $stack->setHandler($container->get(GuzzleHttp\Handler\CurlHandler::class)); $stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) { $login = $container->get(Incoviba\Service\Login::class); - return $request->withHeader('Authorization', "Bearer {$login->getKey($container->get('API_KEY'))}"); + return $request->withHeader('Authorization', "Bearer {$login->getKey()}"); })); $stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) { if (!$request->hasHeader('Authorization')) { @@ -41,10 +42,13 @@ return [ ]); }, Incoviba\Service\FastCGI::class => function(ContainerInterface $container) { - return new Incoviba\Service\FastCGI( + $fcgi = new Incoviba\Service\FastCGI( + $container->get(Incoviba\Service\Login::class), $container->has('SOCKET_HOST') ? $container->get('SOCKET_HOST') : 'web', $container->has('SOCKET_PORT') ? $container->get('SOCKET_PORT') : 9000, $container->has('SOCKET_ROOT') ? $container->get('SOCKET_ROOT') : '/code/public/index.php' ); + $fcgi->setLogger($container->get(Psr\Log\LoggerInterface::class)); + return $fcgi; }, ]; diff --git a/cli/src/Service/FastCGI.php b/cli/src/Service/FastCGI.php index bba45e9..b097bee 100644 --- a/cli/src/Service/FastCGI.php +++ b/cli/src/Service/FastCGI.php @@ -8,7 +8,7 @@ use Incoviba\Exception\Client\FastCGI as FastCGIException; class FastCGI implements LoggerAwareInterface { - public function __construct(protected string $hostname, protected int $port, + public function __construct(protected Login $loginService, protected string $hostname, protected int $port, protected string $documentRoot, protected int $connectionTimeout = 5000, protected int $readTimeout = 5000) { @@ -43,6 +43,7 @@ class FastCGI implements LoggerAwareInterface if (!isset($this->socket)) { $this->connect(); } + $request = $this->setHeaders($request); try { $this->socketIds []= $this->client->sendAsyncRequest($this->socket, $request); } catch (FCGI\Exceptions\FastCGIClientException $exception) { @@ -97,4 +98,11 @@ class FastCGI implements LoggerAwareInterface $request->setRequestUri($uri); return $this->sendRequest($request); } + + protected function setHeaders(FCGI\Interfaces\ProvidesRequestData $request): FCGI\Interfaces\ProvidesRequestData + { + $apiKey = $this->loginService->getKey(); + $request->setCustomVar('HTTP_AUTHORIZATION', "Bearer {$apiKey}"); + return $request; + } } diff --git a/cli/src/Service/Login.php b/cli/src/Service/Login.php index da2d8db..a1e6340 100644 --- a/cli/src/Service/Login.php +++ b/cli/src/Service/Login.php @@ -10,7 +10,7 @@ class Login { public function __construct(protected ClientInterface $client, protected LoggerInterface $logger, protected string $tokenFilename, - protected string $username, protected string $password) {} + protected string $username, protected string $password, protected string $apiKey) {} public function login(): string { @@ -84,8 +84,11 @@ class Login } return $response->getStatusCode() === 200; } - public function getKey(string $apiKey, string $separator = 'g'): string + public function getKey(?string $apiKey = null, string $separator = 'g'): string { + if ($apiKey === null) { + $apiKey = $this->apiKey; + } try { $savedToken = $this->retrieveToken(); $token = implode('', [md5($apiKey), $separator, $savedToken]);