FIX: Remove login for API

This commit is contained in:
2023-11-25 00:55:31 -03:00
parent 3cadaca746
commit ec7d8e69ab
34 changed files with 140 additions and 71 deletions

View File

@ -47,20 +47,20 @@ class Money
protected function getValue(Service\Redis $redisService, string $redisKey, Service\Money $moneyService,
DateTimeInterface $date, string $provider): float
{
if (isset($this->data[$date->format('Y-m-d')])) {
return $this->data[$date->format('Y-m-d')];
if (isset($this->data[$provider][$date->format('Y-m-d')])) {
return $this->data[$provider][$date->format('Y-m-d')];
}
try {
$this->data = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$date->format('Y-m-d')])) {
$this->data[$provider] = (array) $this->fetchRedis($redisService, $redisKey);
if (!isset($this->data[$provider][$date->format('Y-m-d')])) {
throw new EmptyRedis($redisKey);
}
} catch (EmptyRedis) {
$result = $moneyService->get($provider, $date);
$this->data[$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data, $this->time);
$this->data[$provider][$date->format('Y-m-d')] = $result;
$this->saveRedis($redisService, $redisKey, $this->data[$provider], $this->time);
}
return $this->data[$date->format('Y-m-d')];
return $this->data[$provider][$date->format('Y-m-d')];
}
/*public function uf(ServerRequestInterface $request, ResponseInterface $response, Service\Redis $redisService, Service\Money $moneyService): ResponseInterface
{

View File

@ -0,0 +1,13 @@
<?php
namespace Incoviba\Exception;
use Throwable;
use Exception;
class MissingAuthorizationHeader extends Exception
{
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

View File

@ -0,0 +1,40 @@
<?php
namespace Incoviba\Middleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Incoviba\Exception\MissingAuthorizationHeader;
class API
{
public function __construct(protected ResponseFactoryInterface $responseFactory, protected string $key) {}
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$key = $this->getKey($request);
} catch (MissingAuthorizationHeader $exception) {
return $this->responseFactory->createResponse(401);
}
if ($this->validate($key)) {
return $handler->handle($request);
}
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($incoming_key): bool
{
return $incoming_key === md5($this->key);
}
}