110 lines
3.6 KiB
PHP
110 lines
3.6 KiB
PHP
<?php
|
|
namespace Incoviba\Service;
|
|
|
|
use Exception;
|
|
use Psr\Http\Client\ClientExceptionInterface;
|
|
use Psr\Http\Client\ClientInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class Login
|
|
{
|
|
public function __construct(protected ClientInterface $client, protected LoggerInterface $logger,
|
|
protected string $tokenFilename,
|
|
protected string $username, protected string $password, protected string $apiKey) {}
|
|
|
|
public function login(): string
|
|
{
|
|
$this->logger->info('Logging in');
|
|
$url = '/api/login';
|
|
try {
|
|
$response = $this->client->request('POST', $url, [
|
|
'body' => http_build_query([
|
|
'username' => $this->username,
|
|
'password' => $this->password
|
|
]),
|
|
'headers' => ['Content-Type' => 'application/x-www-form-urlencoded']
|
|
]);
|
|
} catch (ClientExceptionInterface $exception) {
|
|
$this->logger->error($exception, [
|
|
'username' => $this->username
|
|
]);
|
|
return '';
|
|
}
|
|
|
|
if ($response->getStatusCode() !== 200) {
|
|
$this->logger->error('Login failed', [
|
|
'statusCode' => $response->getStatusCode(),
|
|
'body' => $response->getBody()->getContents(),
|
|
'username' => $this->username,
|
|
'headers' => $response->getHeaders()
|
|
]);
|
|
return '';
|
|
}
|
|
|
|
$this->logger->info('Logged in');
|
|
$body = $response->getBody()->getContents();
|
|
$data = json_decode($body, true);
|
|
if (!key_exists('token', $data)) {
|
|
$this->logger->error('Token not found', [
|
|
'body' => $body
|
|
]);
|
|
return '';
|
|
}
|
|
$result = file_put_contents($this->tokenFilename, $data['token']);
|
|
if ($result === false) {
|
|
$this->logger->error('Failed to save token');
|
|
return '';
|
|
}
|
|
return $data['token'];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @throws Exception
|
|
*/
|
|
public function retrieveToken(): string
|
|
{
|
|
if (!file_exists($this->tokenFilename)) {
|
|
throw new Exception('Token file not found');
|
|
}
|
|
return file_get_contents($this->tokenFilename);
|
|
}
|
|
public function tryToken(string $token): bool
|
|
{
|
|
$url = '/api/tokens/try';
|
|
try {
|
|
$response = $this->client->request('GET', $url, [
|
|
'headers' => ['Authorization' => "Bearer {$token}"]
|
|
]);
|
|
} catch (ClientExceptionInterface $exception) {
|
|
$this->logger->error($exception, [
|
|
'token' => $token
|
|
]);
|
|
return false;
|
|
}
|
|
return $response->getStatusCode() === 200;
|
|
}
|
|
public function getKey(?string $apiKey = null, string $separator = 'g'): string
|
|
{
|
|
if ($apiKey === null) {
|
|
$apiKey = $this->apiKey;
|
|
}
|
|
try {
|
|
$savedToken = $this->retrieveToken();
|
|
$token = implode('', [md5($apiKey), $separator, $savedToken]);
|
|
if (!$this->tryToken($token)) {
|
|
throw new Exception('Token not valid');
|
|
}
|
|
} catch (Exception $exception) {
|
|
$this->logger->notice($exception);
|
|
$savedToken = $this->login();
|
|
if ($savedToken === '' and file_exists($this->tokenFilename)) {
|
|
unlink($this->tokenFilename);
|
|
return '';
|
|
}
|
|
$token = implode('', [md5($apiKey), $separator, $savedToken]);
|
|
}
|
|
return $token;
|
|
}
|
|
}
|