Enabled Caching of Access Tokens

- Updated AuthTokenCredential to store access token in file storage
- Updated samples to include the configuration to disable/enable caching
This commit is contained in:
japatel
2014-12-02 14:53:14 -06:00
parent 6cf15ab4bf
commit ddd1a2ddf6
8 changed files with 170 additions and 20 deletions

View File

@@ -2,6 +2,8 @@
namespace PayPal\Auth;
use PayPal\Cache\AuthorizationCache;
use PayPal\Common\PPModel;
use PayPal\Common\PPUserAgent;
use PayPal\Common\ResourceModel;
use PayPal\Core\PPConstants;
@@ -10,12 +12,15 @@ use PayPal\Core\PPHttpConnection;
use PayPal\Core\PPLoggingManager;
use PayPal\Exception\PPConfigurationException;
use PayPal\Rest\RestHandler;
use PayPal\Validation\JsonValidator;
/**
* Class OAuthTokenCredential
*/
class OAuthTokenCredential extends ResourceModel
{
public static $CACHE_PATH = '/../../../var/auth.cache';
/**
* Private Variable
*
@@ -107,6 +112,15 @@ class OAuthTokenCredential extends ResourceModel
*/
public function getAccessToken($config)
{
// Check for persisted data first
$token = AuthorizationCache::pull($config, $this->clientId);
if ($token) {
// We found it
$this->accessToken = $token['accessToken'];
$this->tokenCreateTime = $token['tokenCreateTime'];
$this->tokenExpiresIn = $token['tokenExpiresIn'];
}
// Check if Access Token is not null and has not expired.
// The API returns expiry time as a relative time unit
// We use a buffer time when checking for token expiry to account
@@ -121,12 +135,15 @@ class OAuthTokenCredential extends ResourceModel
// If accessToken is Null, obtain a new token
if ($this->accessToken == null) {
// Get a new one by making calls to API
$this->updateAccessToken($config);
AuthorizationCache::push($config, $this->clientId, $this->accessToken, $this->tokenCreateTime, $this->tokenExpiresIn);
}
return $this->accessToken;
}
/**
* Get a Refresh Token from Authorization Code
*

View File

@@ -0,0 +1,94 @@
<?php
namespace PayPal\Cache;
use PayPal\Core\PPConfigManager;
use PayPal\Validation\JsonValidator;
abstract class AuthorizationCache
{
public static $CACHE_PATH = '/../../../var/auth.cache';
/**
* A pull method which would read the persisted data based on clientId.
* If clientId is not provided, an array with all the tokens would be passed.
*
* @param array|null $config
* @param string $clientId
* @return mixed|null
*/
public static function pull($config = null, $clientId = null)
{
// Return if not enabled
if (!self::isEnabled($config)) { return null; }
$tokens = null;
if (file_exists(__DIR__ . self::$CACHE_PATH)) {
// Read from the file
$cachedToken = file_get_contents(__DIR__ . self::$CACHE_PATH);
if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
$tokens = json_decode($cachedToken, true);
if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
// If client Id is found, just send in that data only
return $tokens[$clientId];
} else if ($clientId) {
// If client Id is provided, but no key in persisted data found matching it.
return null;
}
}
}
return $tokens;
}
/**
* Persists the data into a cache file provided in $CACHE_PATH
*
* @param array|null $config
* @param $clientId
* @param $accessToken
* @param $tokenCreateTime
* @param $tokenExpiresIn
*/
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
{
// Return if not enabled
if (!self::isEnabled($config)) { return; }
if (!is_dir(dirname(__DIR__ . self::$CACHE_PATH))) {
if (mkdir(dirname(__DIR__ . self::$CACHE_PATH), 0755, true) == false) {
return;
}
}
// Reads all the existing persisted data
$tokens = self::pull();
$tokens = $tokens ? $tokens : array();
if (is_array($tokens)) {
$tokens[$clientId] = array(
'clientId' => $clientId,
'accessToken' => $accessToken,
'tokenCreateTime' => $tokenCreateTime,
'tokenExpiresIn' => $tokenExpiresIn
);
}
file_put_contents(__DIR__ . self::$CACHE_PATH, json_encode($tokens));
}
/**
* Determines from the Configuration if caching is currently enabled/disabled
*
* @param $config
* @return bool
*/
public static function isEnabled($config)
{
$config = ($config && is_array($config)) ? $config : PPConfigManager::getInstance()->getConfigHashmap();
if (array_key_exists("cache.enabled", $config)) {
$value = $config['cache.enabled'];
return (trim($value) == 'true') ? true : false;
}
return false;
}
}

View File

@@ -12,7 +12,7 @@ class PPConstants
{
const SDK_NAME = 'PayPal-PHP-SDK';
const SDK_VERSION = '0.15.1';
const SDK_VERSION = '0.16.0';
const REST_SANDBOX_ENDPOINT = "https://api.sandbox.paypal.com/";
const OPENID_REDIRECT_SANDBOX_URL = "https://www.sandbox.paypal.com/webapps/auth/protocol/openidconnect";