Se extra getKey y configuraciones the paths

This commit is contained in:
Juan Pablo Vial
2024-08-27 14:46:03 -04:00
parent f85af642d2
commit f9f133d3a9

View File

@ -5,19 +5,24 @@ use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
use Incoviba\Exception\MissingAuthorizationHeader;
use Incoviba\Service;
class API
{
public function __construct(protected ResponseFactoryInterface $responseFactory,
protected LoggerInterface $logger,
protected Service\API $apiService,
protected Service\Login $loginService,
protected array $permittedPaths,
protected array $simplePaths,
protected string $key) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$key = $this->getKey($request);
$key = $this->apiService->getKey($request);
} catch (MissingAuthorizationHeader $exception) {
return $this->responseFactory->createResponse(401);
}
@ -29,16 +34,6 @@ class API
}
return $this->responseFactory->createResponse(403);
}
protected function getKey(ServerRequestInterface $request): string
{
$auth_headers = $request->getHeader('Authorization');
foreach ($auth_headers as $header) {
if (str_contains($header, 'Bearer')) {
return substr($header, strlen('Bearer '));
}
}
throw new MissingAuthorizationHeader();
}
protected function validate(ServerRequestInterface $request, $incoming_key): bool
{
$selector = null;
@ -61,19 +56,11 @@ class API
protected function noComplexKeyNeeded(ServerRequestInterface $request): bool
{
$uri = $request->getUri();
$validPaths = [
'/api/login',
'/api/login/',
];
return in_array($uri->getPath(), $validPaths);
return in_array($uri->getPath(), $this->simplePaths);
}
protected function validPermitted(ServerRequestInterface $request): bool
{
$uri = $request->getUri();
$validPaths = [
'/api',
'/api/',
];
return in_array($uri->getPath(), $validPaths);
return in_array($uri->getPath(), $this->permittedPaths);
}
}