Jobs setup
This commit is contained in:
@ -6,19 +6,19 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Exception\Request\Auth\Unauthorized;
|
||||
use ProVM\Common\Service\Auth as Service;
|
||||
|
||||
class Auth
|
||||
{
|
||||
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, string $api_key)
|
||||
public function __construct(ResponseFactoryInterface $factory, LoggerInterface $logger, protected Service $service)
|
||||
{
|
||||
$this->setResponseFactory($factory);
|
||||
$this->setLogger($logger);
|
||||
$this->setAPIKey($api_key);
|
||||
}
|
||||
|
||||
protected ResponseFactoryInterface $factory;
|
||||
protected LoggerInterface $logger;
|
||||
protected string $api_key;
|
||||
|
||||
public function getResponseFactory(): ResponseFactoryInterface
|
||||
{
|
||||
@ -28,10 +28,6 @@ class Auth
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
public function getAPIKey(): string
|
||||
{
|
||||
return $this->api_key;
|
||||
}
|
||||
|
||||
public function setResponseFactory(ResponseFactoryInterface $factory): Auth
|
||||
{
|
||||
@ -43,30 +39,23 @@ class Auth
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
public function setAPIKey(string $key): Auth
|
||||
{
|
||||
$this->api_key = $key;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if ($request->getMethod() === 'OPTIONS') {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
$auths = $request->getHeader('Authorization');
|
||||
foreach ($auths as $auth) {
|
||||
if (str_contains($auth, 'Bearer')) {
|
||||
$key = str_replace('Bearer ', '', $auth);
|
||||
if (sha1($this->getAPIKey()) === $key) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
try {
|
||||
if ($this->service->validate($request)) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
} catch (Unauthorized $e) {
|
||||
$response = $this->getResponseFactory()->createResponse($e->getCode());
|
||||
$response->getBody()->write(json_encode(['error' => $e->getCode(), 'message' => $e->getMessage()]));
|
||||
}
|
||||
$this->getLogger()->debug(sha1($this->getAPIKey()));
|
||||
$response = $this->getResponseFactory()->createResponse(401);
|
||||
$response->getBody()->write(\Safe\json_encode(['error' => 401, 'message' => 'Incorrect token']));
|
||||
$response = $this->getResponseFactory()->createResponse(413);
|
||||
$response->getBody()->write(\Safe\json_encode(['error' => 413, 'message' => 'Incorrect token']));
|
||||
return $response
|
||||
->withHeader('Content-Type', 'application/json');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user