Attempt to re-authenticate when access token expires
This commit is contained in:
parent
5ef0ccf9a7
commit
8bfc9fcc6e
@ -26,6 +26,8 @@ class Kitsu {
|
||||
const AUTH_URL = 'https://kitsu.io/api/oauth/token';
|
||||
const AUTH_USER_ID_KEY = 'kitsu-auth-userid';
|
||||
const AUTH_TOKEN_CACHE_KEY = 'kitsu-auth-token';
|
||||
const AUTH_TOKEN_EXP_CACHE_KEY = 'kitsu-auth-token-expires';
|
||||
const AUTH_TOKEN_REFRESH_CACHE_KEY = 'kitsu-auth-token-refresh';
|
||||
|
||||
/**
|
||||
* Determine whether an anime is airing, finished airing, or has not yet aired
|
||||
|
@ -90,13 +90,74 @@ class Auth {
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make the call to re-authenticate with the existing refresh token
|
||||
*
|
||||
* @param string $token
|
||||
* @return boolean
|
||||
*/
|
||||
public function reAuthenticate(string $token)
|
||||
{
|
||||
try
|
||||
{
|
||||
$auth = $this->model->reAuthenticate($token);
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check whether the current user is authenticated
|
||||
*
|
||||
@ -124,7 +185,18 @@ class Auth {
|
||||
*/
|
||||
public function get_auth_token()
|
||||
{
|
||||
return $this->segment->get('auth_token', FALSE);
|
||||
$token = $this->segment->get('auth_token', FALSE);
|
||||
$refresh_token = $this->segment->get('refresh_token', FALSE);
|
||||
$isExpired = time() > $this->segment->get('auth_token_expires', 0);
|
||||
|
||||
// Attempt to re-authenticate with refresh token
|
||||
if ($isExpired && $refresh_token)
|
||||
{
|
||||
$reauthenticated = $this->reAuthenticate($refresh_token);
|
||||
return $this->segment->get('auth_token', FALSE);
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
// End of KitsuAuth.php
|
@ -97,7 +97,7 @@ class Model {
|
||||
*
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return bool|string
|
||||
* @return bool|array
|
||||
*/
|
||||
public function authenticate(string $username, string $password)
|
||||
{
|
||||
@ -120,6 +120,32 @@ class Model {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the current session with a refresh token
|
||||
*
|
||||
* @param string $token
|
||||
* @return bool|array
|
||||
*/
|
||||
public function reAuthenticate(string $token)
|
||||
{
|
||||
$response = $this->getResponse('POST', K::AUTH_URL, [
|
||||
'headers' => [],
|
||||
'form_params' => [
|
||||
'grant_type' => 'refresh_token',
|
||||
'refresh_token' => $token
|
||||
]
|
||||
]);
|
||||
|
||||
$data = Json::decode((string)$response->getBody());
|
||||
|
||||
if (array_key_exists('access_token', $data))
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the userid for a username from Kitsu
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user