Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 31f6001

Browse files
author
Anubhav Chaturvedi
committed
fixed caching issue
1 parent 7d2bec6 commit 31f6001

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

lib/PayPal/Auth/OAuthTokenCredential.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class OAuthTokenCredential extends PayPalResourceModel
3838
*
3939
* @var string $clientId
4040
*/
41-
private $clientId;
41+
private $clientId;
4242

4343
/**
4444
* Client secret as obtained from the developer portal
@@ -76,7 +76,7 @@ class OAuthTokenCredential extends PayPalResourceModel
7676
private $cipher;
7777

7878
/**
79-
* The encryted account number of the merchant on whose behalf the transaction is being done
79+
* The encrypted account number of the merchant on whose behalf the transaction is being done
8080
*
8181
* @var Subject
8282
*/
@@ -140,8 +140,9 @@ public function getAccessToken($config)
140140
if ($this->accessToken && (time() - $this->tokenCreateTime) < ($this->tokenExpiresIn - self::$expiryBufferTime)) {
141141
return $this->accessToken;
142142
}
143+
143144
// Check for persisted data first
144-
$token = AuthorizationCache::pull($config, $this->clientId);
145+
$token = AuthorizationCache::pull($config, $this->clientId, $this->subject);
145146
if ($token) {
146147
// We found it
147148
// This code block is for backward compatibility only.
@@ -154,7 +155,7 @@ public function getAccessToken($config)
154155

155156
// Case where we have an old unencrypted cache file
156157
if (!array_key_exists('accessTokenEncrypted', $token)) {
157-
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
158+
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn, $this->subject);
158159
} else {
159160
$this->accessToken = $this->decrypt($token['accessTokenEncrypted']);
160161
}
@@ -177,7 +178,7 @@ public function getAccessToken($config)
177178
if ($this->accessToken == null) {
178179
// Get a new one by making calls to API
179180
$this->updateAccessToken($config);
180-
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn);
181+
AuthorizationCache::push($config, $this->clientId, $this->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn, $this->subject);
181182
}
182183

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

290-
if ($this->subject != null && $refreshToken != null) {
291+
if ($this->subject != null && $refreshToken == null) {
291292
$params['target_subject'] = $this->subject;
292293
}
293294

lib/PayPal/Cache/AuthorizationCache.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class AuthorizationCache
1717
* @param string $clientId
1818
* @return mixed|null
1919
*/
20-
public static function pull($config = null, $clientId = null)
20+
public static function pull($config = null, $clientId = null, $subject = null)
2121
{
2222
// Return if not enabled
2323
if (!self::isEnabled($config)) {
@@ -26,14 +26,15 @@ public static function pull($config = null, $clientId = null)
2626

2727
$tokens = null;
2828
$cachePath = self::cachePath($config);
29+
$cacheKey = $subject == null ? $clientId : $clientId . "." . $subject;
2930
if (file_exists($cachePath)) {
3031
// Read from the file
3132
$cachedToken = file_get_contents($cachePath);
3233
if ($cachedToken && JsonValidator::validate($cachedToken, true)) {
3334
$tokens = json_decode($cachedToken, true);
34-
if ($clientId && is_array($tokens) && array_key_exists($clientId, $tokens)) {
35+
if ($cacheKey && is_array($tokens) && array_key_exists($cacheKey, $tokens)) {
3536
// If client Id is found, just send in that data only
36-
return $tokens[$clientId];
37+
return $tokens[$cacheKey];
3738
} elseif ($clientId) {
3839
// If client Id is provided, but no key in persisted data found matching it.
3940
return null;
@@ -53,7 +54,7 @@ public static function pull($config = null, $clientId = null)
5354
* @param $tokenExpiresIn
5455
* @throws \Exception
5556
*/
56-
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn)
57+
public static function push($config = null, $clientId, $accessToken, $tokenCreateTime, $tokenExpiresIn, $subject=null)
5758
{
5859
// Return if not enabled
5960
if (!self::isEnabled($config)) {
@@ -70,8 +71,9 @@ public static function push($config = null, $clientId, $accessToken, $tokenCreat
7071
// Reads all the existing persisted data
7172
$tokens = self::pull();
7273
$tokens = $tokens ? $tokens : array();
74+
$cacheKey = $subject == null ? $clientId : $clientId . "." . $subject;
7375
if (is_array($tokens)) {
74-
$tokens[$clientId] = array(
76+
$tokens[$cacheKey] = array(
7577
'clientId' => $clientId,
7678
'accessTokenEncrypted' => $accessToken,
7779
'tokenCreateTime' => $tokenCreateTime,

tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,25 @@ public function testGetAccessTokenWithSubjectUnit()
7070
$cred = new OAuthTokenCredential('clientId', 'clientSecret', 'subject');
7171

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

7576
$apiContext = new ApiContext($cred);
7677
$apiContext->setConfig($config);
7778
$this->assertEquals('clientId', $cred->getClientId());
7879
$this->assertEquals('clientSecret', $cred->getClientSecret());
7980
$this->assertEquals('subject', $cred->getSubject());
80-
$result = $cred->getAccessToken($config);
81-
$this->assertNotNull($result);
81+
$result = $cred->getAccessToken($config);
82+
$this->assertEquals('accessTokenWithSubject', $result);
83+
84+
$cred = new OAuthTokenCredential('clientId', 'clientSecret');
85+
$apiContext = new ApiContext($cred);
86+
$apiContext->setConfig($config);
87+
$this->assertEquals('clientId', $cred->getClientId());
88+
$this->assertEquals('clientSecret', $cred->getClientSecret());
89+
$this->assertNull($cred->getSubject());
90+
$result = $cred->getAccessToken($config);
91+
$this->assertEquals('accessToken1', $result);
8292
}
8393

8494
public function testGetAccessTokenUnitMock()

0 commit comments

Comments
 (0)