Files
This commit is contained in:
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
namespace Incoviba\UI\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Slim\Views\Blade as View;
|
||||
use Incoviba\UI\Common\Service\Auth as Service;
|
||||
|
||||
class Auth {
|
||||
public function login(Request $request, Response $response, View $view): Response {
|
||||
$route = $_SESSION['route'] ?? '/';
|
||||
return $view->render($response, 'auth.login', compact('route'));
|
||||
}
|
||||
public function doLogin(Request $request, Response $response, Service $service): Response {
|
||||
$post = $request->getParsedBody();
|
||||
$user = $post['name'];
|
||||
$password = $post['password'];
|
||||
$output = [
|
||||
'login' => $service->login($user, $password)
|
||||
];
|
||||
if ($output['login']) {
|
||||
unset($_SESSION['route']);
|
||||
}
|
||||
$response->getBody()->write(json_encode($output, JSON_UNESCAPED_SLASHES));
|
||||
return $response
|
||||
->withStatus(200)
|
||||
->withHeader('content-type', 'application/json');
|
||||
}
|
||||
public function logout(Request $request, Response $response, Service $service): Response {
|
||||
$service->logout();
|
||||
return $response
|
||||
->withStatus(302)
|
||||
->withHeader('Location', '/');
|
||||
}
|
||||
}
|
||||
|
16
common/Controller/Base.php
Normal file
16
common/Controller/Base.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace Incoviba\UI\Common\Controller;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Slim\Views\Blade as View;
|
||||
|
||||
class Base {
|
||||
public function __invoke(Request $request, Response $response, View $view) {
|
||||
$hoy = 0;
|
||||
$pendientes = 0;
|
||||
$dias = [];
|
||||
$cierres = [];
|
||||
return $view->render($response, 'home', compact('hoy', 'pendientes', 'dias', 'cierres'));
|
||||
}
|
||||
}
|
30
common/Middleware/Auth.php
Normal file
30
common/Middleware/Auth.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace Incoviba\UI\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ResponseFactoryInterface as Factory;
|
||||
use Incoviba\UI\Common\Service\Auth as Service;
|
||||
|
||||
class Auth {
|
||||
protected $service;
|
||||
protected $factory;
|
||||
protected $exceptions;
|
||||
public function __construct(Service $service, Factory $factory, array $exception_routes) {
|
||||
$this->service = $service;
|
||||
$this->factory = $factory;
|
||||
$this->exceptions = $exception_routes;
|
||||
}
|
||||
public function __invoke(Request $request, Handler $handler): Response {
|
||||
$path = $request->getUri()->getPath();
|
||||
if (in_array($path, $this->exceptions) or str_contains($path, '.') or $this->service->isIn()) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
$route = $request->getUri();
|
||||
$_SESSION['route'] = '' . $route;
|
||||
$response = $this->factory->createResponse(302);
|
||||
return $response
|
||||
->withHeader('Location', '/auth/login');
|
||||
}
|
||||
}
|
27
common/Middleware/NotFound.php
Normal file
27
common/Middleware/NotFound.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
namespace Incoviba\UI\Common\Middleware;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ResponseFactoryInterface as Factory;
|
||||
use Slim\Exception\HttpNotFoundException;
|
||||
use Slim\Views\Blade as View;
|
||||
|
||||
class NotFound {
|
||||
protected Factory $factory;
|
||||
protected View $view;
|
||||
public function __construct(Factory $factory, View $view) {
|
||||
$this->factory = $factory;
|
||||
$this->view = $view;
|
||||
}
|
||||
public function __invoke(Request $request, Handler $handler): Response {
|
||||
try {
|
||||
return $handler->handle($request);
|
||||
} catch (HttpNotFoundException $e) {
|
||||
$page = $request->getUri()->getPath();
|
||||
$response = $this->factory->createResponse(404);
|
||||
return $this->view->render($response, '404', compact('page'));
|
||||
}
|
||||
}
|
||||
}
|
126
common/Service/Auth.php
Normal file
126
common/Service/Auth.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
namespace Incoviba\UI\Common\Service;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
class Auth {
|
||||
public function __construct(Client $client, string $cookie_name) {
|
||||
$this->setClient($client);
|
||||
$this->setCookieName($cookie_name);
|
||||
}
|
||||
protected $client;
|
||||
public function setClient(Client $client) {
|
||||
$this->client = $client;
|
||||
return $this;
|
||||
}
|
||||
protected $cookie_name;
|
||||
public function setCookieName(string $cookie_name) {
|
||||
$this->cookie_name = $cookie_name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected $cookie;
|
||||
public function getCookie() {
|
||||
if ($this->cookie === null) {
|
||||
if (isset($_COOKIE[$this->cookie_name])) {
|
||||
$this->cookie = $_COOKIE[$this->cookie_name];
|
||||
}
|
||||
}
|
||||
return $this->cookie;
|
||||
}
|
||||
public function setCookie($token, $expires) {
|
||||
return setcookie($this->cookie_name, $token, ['expires' => (int) $expires, 'SameSite' => 'Lax', 'path' => '/']);
|
||||
}
|
||||
|
||||
protected $is_in;
|
||||
public function isIn() {
|
||||
if ($this->is_in === null) {
|
||||
$token = $this->getCookie();
|
||||
if ($token === null) {
|
||||
$this->is_in = false;
|
||||
return false;
|
||||
}
|
||||
if ($this->validate($token)) {
|
||||
$this->is_in = true;
|
||||
return true;
|
||||
}
|
||||
$this->is_in = false;
|
||||
}
|
||||
return $this->is_in;
|
||||
}
|
||||
protected $authorized;
|
||||
public function validate(string $token): bool {
|
||||
if ($this->authorized === null) {
|
||||
try {
|
||||
$response = $this->client->request('POST', 'auth/validate', ['json' => ['token' => $token]]);
|
||||
} catch (RequestException $e) {
|
||||
error_log('Validate: ' . var_export($e->getResponse()->getBody()->getContents(), true));
|
||||
error_log($e);
|
||||
$this->authorized = false;
|
||||
return false;
|
||||
}
|
||||
if ($response->getStatusCode() != 200) {
|
||||
$this->authorized = false;
|
||||
return false;
|
||||
}
|
||||
$body = json_decode($response->getBody());
|
||||
if ($body->status !== 'Authorized') {
|
||||
$this->authorized = false;
|
||||
return false;
|
||||
}
|
||||
$this->authorized = true;
|
||||
}
|
||||
return $this->authorized;
|
||||
}
|
||||
public function checkAccess($route) {
|
||||
return true;
|
||||
}
|
||||
public function login($user, $password) {
|
||||
try {
|
||||
$response = $this->client->request('POST', 'auth/login', ['json' => ['name' => $user, 'password' => $password]]);
|
||||
} catch (RequestException $e) {
|
||||
error_log(var_export($e->getResponse()->getBody()->getContents(), true));
|
||||
error_log($e);
|
||||
return false;
|
||||
}
|
||||
if ($response->getStatusCode() != 200) {
|
||||
return false;
|
||||
}
|
||||
$data = json_decode($response->getBody());
|
||||
if (!$data->login) {
|
||||
return false;
|
||||
}
|
||||
$this->setCookie($data->token, $data->expires);
|
||||
return true;
|
||||
}
|
||||
public function logout() {
|
||||
try {
|
||||
$response = $this->client->request('POST', 'auth/logout', ['json' => ['token' => $this->getCookie()]]);
|
||||
} catch (\Exception $e) {
|
||||
error_log(var_export($e->getResponse()->getBody()->getContents(), true));
|
||||
error_log($e);
|
||||
return false;
|
||||
}
|
||||
$this->setCookie($this->getCookie(), 0);
|
||||
return true;
|
||||
}
|
||||
protected $user_name;
|
||||
public function user() {
|
||||
if ($this->user_name === null) {
|
||||
try {
|
||||
$response = $this->client->request('POST', 'auth/user', ['json' => ['token' => $this->getCookie()]]);
|
||||
} catch (\Exception $e) {
|
||||
error_log(var_export($e->getResponse()->getBody()->getContents(), true));
|
||||
error_log($e);
|
||||
return false;
|
||||
}
|
||||
if ($response->getStatusCode() != 200) {
|
||||
return false;
|
||||
}
|
||||
$data = json_decode($response->getBody());
|
||||
$this->user_name = $data->user;
|
||||
}
|
||||
return $this->user_name;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user