FIX: Authorization en FastCGI

This commit is contained in:
Juan Pablo Vial
2025-06-27 18:00:45 -04:00
parent 352e33179c
commit 1bbee1121b
3 changed files with 21 additions and 6 deletions

View File

@ -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;
},
];

View File

@ -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;
}
}

View File

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