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
|
|
|
*
|
2018-10-01 11:35:51 -04:00
|
|
|
* PHP version 7.1
|
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>
|
2018-01-15 14:43:15 -05:00
|
|
|
* @copyright 2015 - 2018 Timothy J. Warren
|
2017-01-05 22:24:45 -05:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2018-10-01 11:35:51 -04:00
|
|
|
* @version 4.1
|
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
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
use const Aviat\AnimeClient\SESSION_SEGMENT;
|
|
|
|
|
2017-01-27 12:35:28 -05:00
|
|
|
use Aviat\AnimeClient\API\{
|
|
|
|
CacheTrait,
|
|
|
|
Kitsu as K
|
|
|
|
};
|
2017-01-05 22:24:45 -05:00
|
|
|
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
|
2017-01-12 15:41:20 -05:00
|
|
|
use Exception;
|
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
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
private $model;
|
2017-01-05 22:24:45 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Session object
|
|
|
|
*
|
2017-02-22 14:46:35 -05:00
|
|
|
* @var \Aura\Session\Segment
|
2017-01-05 22:24:45 -05:00
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
private $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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the appropriate authentication call,
|
|
|
|
* and save the resulting auth token if successful
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @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
|
|
|
|
2017-01-27 12:35:28 -05:00
|
|
|
if (FALSE !== $auth)
|
2017-01-05 22:24:45 -05:00
|
|
|
{
|
2017-01-27 12:35:28 -05:00
|
|
|
// Set the token in the cache for command line operations
|
2017-02-09 20:10:13 -05:00
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY);
|
2017-01-27 12:35:28 -05:00
|
|
|
$cacheItem->set($auth['access_token']);
|
|
|
|
$cacheItem->save();
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2017-06-19 15:31:24 -04:00
|
|
|
// Set the token expiration in the cache
|
|
|
|
$expire_time = $auth['created_at'] + $auth['expires_in'];
|
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_EXP_CACHE_KEY);
|
|
|
|
$cacheItem->set($expire_time);
|
|
|
|
$cacheItem->save();
|
|
|
|
|
|
|
|
// Set the refresh token in the cache
|
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_REFRESH_CACHE_KEY);
|
|
|
|
$cacheItem->set($auth['refresh_token']);
|
|
|
|
$cacheItem->save();
|
|
|
|
|
|
|
|
// Set the session values
|
2017-01-27 12:35:28 -05:00
|
|
|
$this->segment->set('auth_token', $auth['access_token']);
|
2017-06-19 15:31:24 -04:00
|
|
|
$this->segment->set('auth_token_expires', $expire_time);
|
|
|
|
$this->segment->set('refresh_token', $auth['refresh_token']);
|
2018-10-05 14:32:05 -04:00
|
|
|
|
2017-01-05 22:24:45 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:31:24 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the call to re-authenticate with the existing refresh token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-09-27 16:45:12 -04:00
|
|
|
public function reAuthenticate(string $token): bool
|
2017-06-19 15:31:24 -04:00
|
|
|
{
|
2018-10-05 14:32:05 -04:00
|
|
|
$auth = $this->model->reAuthenticate($token);
|
2017-06-19 15:31:24 -04:00
|
|
|
|
|
|
|
if (FALSE !== $auth)
|
|
|
|
{
|
|
|
|
// Set the token in the cache for command line operations
|
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_CACHE_KEY);
|
|
|
|
$cacheItem->set($auth['access_token']);
|
|
|
|
$cacheItem->save();
|
|
|
|
|
|
|
|
// Set the token expiration in the cache
|
|
|
|
$expire_time = $auth['created_at'] + $auth['expires_in'];
|
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_EXP_CACHE_KEY);
|
|
|
|
$cacheItem->set($expire_time);
|
|
|
|
$cacheItem->save();
|
|
|
|
|
|
|
|
// Set the refresh token in the cache
|
|
|
|
$cacheItem = $this->cache->getItem(K::AUTH_TOKEN_REFRESH_CACHE_KEY);
|
|
|
|
$cacheItem->set($auth['refresh_token']);
|
|
|
|
$cacheItem->save();
|
|
|
|
|
|
|
|
// Set the session values
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-05 22:24:45 -05:00
|
|
|
/**
|
|
|
|
* Check whether the current user is authenticated
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-09-27 16:45:12 -04:00
|
|
|
public function isAuthenticated(): bool
|
2017-01-05 22:24:45 -05:00
|
|
|
{
|
|
|
|
return ($this->get_auth_token() !== FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear authentication values
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-09-27 16:45:12 -04:00
|
|
|
public function logout(): void
|
2017-01-05 22:24:45 -05:00
|
|
|
{
|
|
|
|
$this->segment->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the authentication token from the session
|
|
|
|
*
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
public function get_auth_token()
|
|
|
|
{
|
2017-06-19 15:31:24 -04:00
|
|
|
$token = $this->segment->get('auth_token', FALSE);
|
2018-09-27 16:45:12 -04:00
|
|
|
$refreshToken = $this->segment->get('refresh_token', FALSE);
|
2018-10-05 14:32:05 -04:00
|
|
|
$isExpired = time() >= $this->segment->get('auth_token_expires', 0);
|
2017-06-19 15:31:24 -04:00
|
|
|
|
|
|
|
// Attempt to re-authenticate with refresh token
|
2018-09-27 16:45:12 -04:00
|
|
|
if ($isExpired && $refreshToken)
|
2017-06-19 15:31:24 -04:00
|
|
|
{
|
2018-09-27 16:45:12 -04:00
|
|
|
if ($this->reAuthenticate($refreshToken))
|
|
|
|
{
|
|
|
|
return $this->segment->get('auth_token', FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2017-06-19 15:31:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
2017-01-05 22:24:45 -05:00
|
|
|
}
|
|
|
|
}
|
2016-12-20 12:55:43 -05:00
|
|
|
// End of KitsuAuth.php
|