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
*