2022-12-20

This commit is contained in:
2022-12-20 14:13:05 -03:00
parent 85fef16b27
commit 0f3febc00d
87 changed files with 2525 additions and 419 deletions

View File

@ -1,36 +1,74 @@
<?php
namespace Common\Service;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use DI\NotFoundException;
class Auth {
protected string $key;
public function __construct(string $api_key) {
public function __construct(string $api_key)
{
$this->key = $api_key;
}
public function isValid(Request $request): bool {
if ($request->hasHeader('Authorization')) {
$sent_key = $this->getAuthKey($request->getHeader('Authorization'));
return $this->key == $sent_key;
public function isValid(Request $request): bool
{
return $this->key == $this->getKey($request);
}
public function getKey(Request $request): string
{
$errors = [];
try {
return $this->getHeaderKey($request);
} catch (NotFoundExceptionInterface $e) {
$errors []= $e;
}
try {
return $this->getBodyKey($request);
} catch (NotFoundExceptionInterface $e) {
$errors []= $e;
}
try {
return $this->getQueryKey($request);
} catch (NotFoundExceptionInterface $e) {
$errors []= $e;
}
throw new NotFoundException('API Key not found.');
}
protected function getHeaderKey(Request $request): string
{
if ($request->hasHeader('Authorization')) {
return $this->getAuthKey($request->getHeader('Authorization'));
}
throw new NotFoundException('API Key not found on header');
}
protected function getBodyKey(Request $request): string
{
if (isset($request->getParsedBody()['api_key'])) {
$sent_key = $request->getParsedBody()['api_key'];
return $this->key == $sent_key;
return $request->getParsedBody()['api_key'];
}
$post = $request->getParsedBody() ?? json_decode($request->getBody());
$sent_key = $this->getArrayKey($post);
if ($sent_key !== null) {
return $this->key == $sent_key;
try {
return $this->getArrayKey($post);
} catch (\Exception $e) {
throw new NotFoundException('API Key not found in body.');
}
$sent_key = $this->getArrayKey($request->getQueryParams());
return $this->key == $sent_key;
}
protected function getAuthKey($auth) {
protected function getQueryKey(Request $request): string
{
try {
return $this->getArrayKey($request->getQueryParams());
} catch (\Exception $e) {
throw new NotFoundException('API Key not found in query.');
}
}
protected function getAuthKey($auth)
{
if (is_array($auth)) {
$auth = $auth[0];
}
if (str_contains($auth, 'Bearer')) {
$auth = explode(' ', $auth)[1];
$auth = trim(str_replace('Bearer', '', $auth), ' ,');
}
return $auth;
}

View File

@ -0,0 +1,60 @@
<?php
namespace Common\Service;
use function Safe\{json_encode, json_decode, file_get_contents, file_put_contents};
use ProVM\Concept\API\Route\Route;
use ProVM\Implement\API\Route\Route as RouteObj;
class Documenter
{
public function __construct(string $filename)
{
$this->setFilename($filename);
}
protected string $filename;
public function setFilename(string $filename): Documenter
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
protected array $routes;
public function setRoutes(array $routes): Documenter
{
foreach ($routes as $path => $route) {
$this->addRoute($path, $route);
}
return $this;
}
public function addRoute(string $path, Route $route): Documenter
{
$this->routes[$path] = $route;
return $this;
}
public function getRoutes(): array
{
return $this->routes;
}
protected function load(): mixed
{
$json = json_decode(trim(file_get_contents($this->getFilename())), JSON_OBJECT_AS_ARRAY);
foreach ($json['paths'] as $path => $data) {
$route = RouteObj::add($data);
$this->addRoute($path, $route);
}
return $json;
}
public function save(): void
{
$json = $this->load();
$json['paths'] = $this->getRoutes();
ksort($json['paths']);
file_put_contents($this->getFilename(), json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
}

View File

@ -0,0 +1,179 @@
<?php
namespace Common\Service;
use Slim\App;
use ProVM\Concept\API\Controller;
use ProVM\Implement\API\Route\Route;
class Router
{
public function __construct(App &$app, Documenter $documenter)
{
$this->setApp($app);
$this->setDocumenter($documenter);
}
protected App $app;
public function setApp(App &$app): Router
{
$this->app = $app;
return $this;
}
public function getApp(): App
{
return $this->app;
}
protected Documenter $documenter;
public function setDocumenter(Documenter $documenter): Router
{
$this->documenter = $documenter;
return $this;
}
public function getDocumenter(): Documenter
{
return $this->documenter;
}
protected Controller $controller;
public function setController(Controller $controller): Router
{
$this->controller = $controller;
return $this;
}
public function getController(): Controller
{
return $this->controller;
}
protected function document(): void
{
$docs = [
"/{$this->getController()->getPlural()}" => [
'get' => [
'description' => "Entrega una lista de {$this->getController()->getPlural()}",
'responses' => [
'200' => [
'description' => "Lista de {$this->getController()->getPlural()}"
]
]
]
],
"/{$this->getController()->getPlural()}/add" => [
'post' => [
'description' => "Agregar {$this->getController()->getPlural()} con una lista",
'parameters' => [
[
'name' => $this->getController()->getPlural(),
'in' => 'query',
'description' => 'Lista de datos para agregar',
'required' => true
]
],
'responses' => [
'200' => [
'description' => "Entrega un listado de {$this->getController()->getPlural()} y si fueron agregadas"
]
]
]
],
"/{$this->getController()->getPlural()}/edit" => [
'put' => [
'description' => "Editar multiples {$this->getController()->getPlural()} con una lista",
'parameters' => [
[
'name' => $this->getController()->getPlural(),
'in' => 'query',
'description' => 'Lista de datos para editar',
'required' => true
]
],
'responses' => [
'200' => [
'description' => "Entrega un listado de {$this->getController()->getPlural()} identificando si fueron editados"
]
]
]
],
"/{$this->getController()->getSingular()}/{model_id}" => [
'get' => [
'description' => "Entrega {$this->getController()->getSingular()}",
'parameters' => [
[
'name' => 'id',
'in' => 'path',
'description' => "{$this->getController()->getSingular()} id",
'required' => true
]
],
'responses' => [
'200' => [
'description' => "{$this->getController()->getSingular()} found or null"
]
]
]
],
"/{$this->getController()->getSingular()}/{model_id}/edit" => [
'put' => [
'description' => '',
'parameters' => [
[
'name' => 'model_id',
'in' => 'path',
'description' => "{$this->getController()->getSingular()} id",
'required' => true
],
[
'name' => "{$this->getController()->getSingular()}",
'in' => 'query',
'description' => '',
'required' => true
]
],
'responses' => [
'200' => [
'description' => ''
]
]
]
],
"/{$this->getController()->getSingular()}/{model_id}/remove" => [
'delete' => [
'description' => '',
'parameters' => [
[
'name' => 'model_id',
'in' => 'path',
'description' => "{$this->getController()->getSingular()} id",
'required' => true
]
],
'responses' => [
'200' => [
'description' => ''
]
]
]
]
];
foreach ($docs as $path => $r) {
$route = Route::add($r);
$this->getDocumenter()->addRoute($path, $route);
}
$this->getDocumenter()->save();
}
public function build(): void
{
$this->getApp()->group('/' . $this->getController()->getPlural(), function($app) {
$app->post('/add[/]', [get_class($this->getController()), 'add']);
$app->put('/edit[/]', [get_class($this->getController()), 'edit']);
$app->get('[/]', get_class($this->getController()));
});
$this->getApp()->group('/' . $this->getController()->getSingular() . '/{model_id}', function($app) {
$app->put('/edit[/]', [get_class($this->getController()), 'editOne']);
$app->delete('/remove[/]', [get_class($this->getController()), 'remove']);
$app->get('[/]', [get_class($this->getController()), 'get']);
});
$this->document();
}
}