HummingBirdAnimeClient/src/AnimeClient/API/Kitsu/Auth.php

167 lines
3.6 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
/**
2017-02-15 16:13:32 -05:00
* Hummingbird Anime List Client
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
*
2021-02-04 11:57:01 -05:00
* PHP version 8
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2020-12-10 17:06:50 -05:00
* @version 5.2
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\AnimeClient\API\Kitsu;
2019-12-06 15:46:56 -05:00
use Aura\Session\Segment;
use Aviat\AnimeClient\API\CacheTrait;
2020-08-26 15:22:14 -04:00
use Aviat\AnimeClient\Kitsu as K;
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
use Aviat\Ion\Event;
use const Aviat\AnimeClient\SESSION_SEGMENT;
2019-12-09 14:34:23 -05:00
/**
* Kitsu API Authentication
*/
final class Auth
{
2017-01-27 12:35:28 -05:00
use CacheTrait;
use ContainerAware;
/**
* Anime API Model
*/
2020-04-10 20:01:46 -04:00
private Model $model;
/**
* Session object
*/
2020-04-10 20:01:46 -04:00
private Segment $segment;
/**
* Constructor
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
2017-01-27 12:35:28 -05:00
$this->setCache($container->get('cache'));
$this->segment = $container->get('session')
->getSegment(SESSION_SEGMENT);
$this->model = $container->get('kitsu-model');
2023-05-19 10:54:08 -04:00
Event::on('::unauthorized::', $this->reAuthenticate(...));
}
/**
* Make the appropriate authentication call,
* and save the resulting auth token if successful
*/
2018-09-27 16:45:12 -04:00
public function authenticate(string $password): bool
{
$config = $this->container->get('config');
2018-10-05 14:32:05 -04:00
$username = $config->get('kitsu_username');
2018-10-05 14:32:05 -04:00
$auth = $this->model->authenticate($username, $password);
return $this->storeAuth($auth);
}
/**
* Make the call to re-authenticate with the existing refresh token
*/
public function reAuthenticate(?string $refreshToken = NULL): bool
{
2020-05-08 19:15:21 -04:00
$refreshToken ??= $this->getRefreshToken();
if (empty($refreshToken))
{
return FALSE;
}
$auth = $this->model->reAuthenticate($refreshToken);
return $this->storeAuth($auth);
}
/**
* Check whether the current user is authenticated
*/
public function isAuthenticated(): bool
{
return $this->getAuthToken() !== NULL;
}
/**
* Clear authentication values
*/
public function logout(): void
{
$this->segment->clear();
}
/**
* Retrieve the authentication token from the session
*/
2020-05-08 19:15:21 -04:00
public function getAuthToken(): ?string
{
if (PHP_SAPI === 'cli')
{
2022-01-07 12:33:01 -05:00
return $this->segment->get('auth_token')
?? $this->cache->get(K::AUTH_TOKEN_CACHE_KEY);
}
2022-01-07 12:33:01 -05:00
return $this->segment->get('auth_token');
}
2020-05-08 19:15:21 -04:00
/**
* Retrieve the refresh token
*/
private function getRefreshToken(): ?string
{
if (PHP_SAPI === 'cli')
{
return $this->segment->get('refresh_token')
2022-01-07 12:33:01 -05:00
?? $this->cache->get(K::AUTH_TOKEN_REFRESH_CACHE_KEY);
}
return $this->segment->get('refresh_token');
}
2020-05-08 19:15:21 -04:00
/**
* Save the new authentication information
*/
2023-05-09 12:49:36 -04:00
private function storeAuth(array|false $auth): bool
{
if (FALSE !== $auth)
{
$expire_time = $auth['created_at'] + $auth['expires_in'];
2020-05-08 19:15:21 -04:00
// Set the token in the cache for command line operations
// Set the token expiration in the cache
// Set the refresh token in the cache
2020-05-08 19:15:21 -04:00
$saved = $this->cache->setMultiple([
K::AUTH_TOKEN_CACHE_KEY => $auth['access_token'],
K::AUTH_TOKEN_EXP_CACHE_KEY => $expire_time,
K::AUTH_TOKEN_REFRESH_CACHE_KEY => $auth['refresh_token'],
]);
// Set the session values
2020-05-08 19:15:21 -04:00
if ($saved)
{
$this->segment->set('auth_token', $auth['access_token']);
$this->segment->set('auth_token_expires', $expire_time);
$this->segment->set('refresh_token', $auth['refresh_token']);
2020-05-08 19:15:21 -04:00
return TRUE;
}
}
return FALSE;
}
}
// End of KitsuAuth.php