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