Files
This commit is contained in:
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