FIX: Remove login for API
This commit is contained in:
@ -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
|
||||
{
|
||||
|
13
app/src/Exception/MissingAuthorizationHeader.php
Normal file
13
app/src/Exception/MissingAuthorizationHeader.php
Normal 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);
|
||||
}
|
||||
}
|
40
app/src/Middleware/API.php
Normal file
40
app/src/Middleware/API.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user