FIX: Authorization en FastCGI
This commit is contained in:
@ -16,7 +16,8 @@ return [
|
|||||||
$container->get(Psr\Log\LoggerInterface::class),
|
$container->get(Psr\Log\LoggerInterface::class),
|
||||||
implode(DIRECTORY_SEPARATOR, [$container->get('folders')->cache, 'token']),
|
implode(DIRECTORY_SEPARATOR, [$container->get('folders')->cache, 'token']),
|
||||||
$container->get('API_USERNAME'),
|
$container->get('API_USERNAME'),
|
||||||
$container->get('API_PASSWORD')
|
$container->get('API_PASSWORD'),
|
||||||
|
$container->get('API_KEY')
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
GuzzleHttp\HandlerStack::class => function(ContainerInterface $container) {
|
GuzzleHttp\HandlerStack::class => function(ContainerInterface $container) {
|
||||||
@ -24,7 +25,7 @@ return [
|
|||||||
$stack->setHandler($container->get(GuzzleHttp\Handler\CurlHandler::class));
|
$stack->setHandler($container->get(GuzzleHttp\Handler\CurlHandler::class));
|
||||||
$stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) {
|
$stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) {
|
||||||
$login = $container->get(Incoviba\Service\Login::class);
|
$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) {
|
$stack->push(GuzzleHttp\Middleware::mapRequest(function(Psr\Http\Message\RequestInterface $request) use ($container) {
|
||||||
if (!$request->hasHeader('Authorization')) {
|
if (!$request->hasHeader('Authorization')) {
|
||||||
@ -41,10 +42,13 @@ return [
|
|||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
Incoviba\Service\FastCGI::class => function(ContainerInterface $container) {
|
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_HOST') ? $container->get('SOCKET_HOST') : 'web',
|
||||||
$container->has('SOCKET_PORT') ? $container->get('SOCKET_PORT') : 9000,
|
$container->has('SOCKET_PORT') ? $container->get('SOCKET_PORT') : 9000,
|
||||||
$container->has('SOCKET_ROOT') ? $container->get('SOCKET_ROOT') : '/code/public/index.php'
|
$container->has('SOCKET_ROOT') ? $container->get('SOCKET_ROOT') : '/code/public/index.php'
|
||||||
);
|
);
|
||||||
|
$fcgi->setLogger($container->get(Psr\Log\LoggerInterface::class));
|
||||||
|
return $fcgi;
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@ -8,7 +8,7 @@ use Incoviba\Exception\Client\FastCGI as FastCGIException;
|
|||||||
|
|
||||||
class FastCGI implements LoggerAwareInterface
|
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 string $documentRoot,
|
||||||
protected int $connectionTimeout = 5000, protected int $readTimeout = 5000)
|
protected int $connectionTimeout = 5000, protected int $readTimeout = 5000)
|
||||||
{
|
{
|
||||||
@ -43,6 +43,7 @@ class FastCGI implements LoggerAwareInterface
|
|||||||
if (!isset($this->socket)) {
|
if (!isset($this->socket)) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
|
$request = $this->setHeaders($request);
|
||||||
try {
|
try {
|
||||||
$this->socketIds []= $this->client->sendAsyncRequest($this->socket, $request);
|
$this->socketIds []= $this->client->sendAsyncRequest($this->socket, $request);
|
||||||
} catch (FCGI\Exceptions\FastCGIClientException $exception) {
|
} catch (FCGI\Exceptions\FastCGIClientException $exception) {
|
||||||
@ -97,4 +98,11 @@ class FastCGI implements LoggerAwareInterface
|
|||||||
$request->setRequestUri($uri);
|
$request->setRequestUri($uri);
|
||||||
return $this->sendRequest($request);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ class Login
|
|||||||
{
|
{
|
||||||
public function __construct(protected ClientInterface $client, protected LoggerInterface $logger,
|
public function __construct(protected ClientInterface $client, protected LoggerInterface $logger,
|
||||||
protected string $tokenFilename,
|
protected string $tokenFilename,
|
||||||
protected string $username, protected string $password) {}
|
protected string $username, protected string $password, protected string $apiKey) {}
|
||||||
|
|
||||||
public function login(): string
|
public function login(): string
|
||||||
{
|
{
|
||||||
@ -84,8 +84,11 @@ class Login
|
|||||||
}
|
}
|
||||||
return $response->getStatusCode() === 200;
|
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 {
|
try {
|
||||||
$savedToken = $this->retrieveToken();
|
$savedToken = $this->retrieveToken();
|
||||||
$token = implode('', [md5($apiKey), $separator, $savedToken]);
|
$token = implode('', [md5($apiKey), $separator, $savedToken]);
|
||||||
|
Reference in New Issue
Block a user