Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
fixed caching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Anubhav Chaturvedi committed Nov 13, 2017
1 parent 7d2bec6 commit 31f6001
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
13 changes: 7 additions & 6 deletions lib/PayPal/Auth/OAuthTokenCredential.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class OAuthTokenCredential extends PayPalResourceModel
*
* @var string $clientId
*/
private $clientId;
private $clientId;

/**
* Client secret as obtained from the developer portal
Expand Down Expand Up @@ -76,7 +76,7 @@ class OAuthTokenCredential extends PayPalResourceModel
private $cipher;

/**
* The encryted account number of the merchant on whose behalf the transaction is being done
* The encrypted account number of the merchant on whose behalf the transaction is being done
*
* @var Subject
*/
Expand Down Expand Up @@ -140,8 +140,9 @@ public function getAccessToken($config)
if ($this->accessToken && (time() - $this->tokenCreateTime) < ($this->tokenExpiresIn - self::$expiryBufferTime)) {
return $this->accessToken;
}

// Check for persisted data first
$token = AuthorizationCache::pull($config, $this->clientId);
$token = AuthorizationCache::pull($config, $this->clientId, $this->subject);
if ($token) {
// We found it
// This code block is for backward compatibility only.
Expand All @@ -154,7 +155,7 @@ public function getAccessToken($config)

// Case where we have an old unencrypted cache file
if (!array_key_exists('accessTokenEncrypted', $token)) {
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn, $this->subject);
} else {
$this->accessToken = $this->decrypt($token['accessTokenEncrypted']);
}
Expand All @@ -177,7 +178,7 @@ public function getAccessToken($config)
if ($this->accessToken == null) {
// Get a new one by making calls to API
$this->updateAccessToken($config);
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn, $this->subject);
}

return $this->accessToken;
Expand Down Expand Up @@ -287,7 +288,7 @@ private function generateAccessToken($config, $refreshToken = null)
$params['refresh_token'] = $refreshToken;
}

if ($this->subject != null && $refreshToken != null) {
if ($this->subject != null && $refreshToken == null) {
$params['target_subject'] = $this->subject;
}

Expand Down
12 changes: 7 additions & 5 deletions lib/PayPal/Cache/AuthorizationCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ abstract class AuthorizationCache
* @param string $clientId
* @return mixed|null
*/
public static function pull($config = null, $clientId = null)
public static function pull($config = null, $clientId = null, $subject = null)
{
// Return if not enabled
if (!self::isEnabled($config)) {
Expand All @@ -26,14 +26,15 @@ public static function pull($config = null, $clientId = null)

$tokens = null;
$cachePath = self::cachePath($config);
$cacheKey = $subject == null ? $clientId : $clientId . "." . $subject;
if (file_exists($cachePath)) {
// Read from the file
$cachedToken = file_get_contents($cachePath);
if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
$tokens = json_decode($cachedToken, true);
if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
if ($cacheKey && is_array($tokens) && array_key_exists($cacheKey, $tokens)) {
// If client Id is found, just send in that data only
return $tokens[$clientId];
return $tokens[$cacheKey];
} elseif ($clientId) {
// If client Id is provided, but no key in persisted data found matching it.
return null;
Expand All @@ -53,7 +54,7 @@ public static function pull($config = null, $clientId = null)
* @param $tokenExpiresIn
* @throws \Exception
*/
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn, $subject=null)
{
// Return if not enabled
if (!self::isEnabled($config)) {
Expand All @@ -70,8 +71,9 @@ public static function push($config = null, $clientId, $accessToken, $tokenCreat
// Reads all the existing persisted data
$tokens = self::pull();
$tokens = $tokens ? $tokens : array();
$cacheKey = $subject == null ? $clientId : $clientId . "." . $subject;
if (is_array($tokens)) {
$tokens[$clientId] = array(
$tokens[$cacheKey] = array(
'clientId' => $clientId,
'accessTokenEncrypted' => $accessToken,
'tokenCreateTime' => $tokenCreateTime,
Expand Down
16 changes: 13 additions & 3 deletions tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,25 @@ public function testGetAccessTokenWithSubjectUnit()
$cred = new OAuthTokenCredential('clientId', 'clientSecret', 'subject');

//{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}}
AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessTokenWithSubject'), 1421204091, 288000000);
AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessTokenWithSubject'), 1421204091, 288000000, 'subject');
AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessToken1'), 1421204091, 288000000);

$apiContext = new ApiContext($cred);
$apiContext->setConfig($config);
$this->assertEquals('clientId', $cred->getClientId());
$this->assertEquals('clientSecret', $cred->getClientSecret());
$this->assertEquals('subject', $cred->getSubject());
$result = $cred->getAccessToken($config);
$this->assertNotNull($result);
$result = $cred->getAccessToken($config);
$this->assertEquals('accessTokenWithSubject', $result);

$cred = new OAuthTokenCredential('clientId', 'clientSecret');
$apiContext = new ApiContext($cred);
$apiContext->setConfig($config);
$this->assertEquals('clientId', $cred->getClientId());
$this->assertEquals('clientSecret', $cred->getClientSecret());
$this->assertNull($cred->getSubject());
$result = $cred->getAccessToken($config);
$this->assertEquals('accessToken1', $result);
}

public function testGetAccessTokenUnitMock()
Expand Down

0 comments on commit 31f6001

Please sign in to comment.