/** * @depends testCachePush */ public function testCachePull() { $result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId'); $this->assertNotNull($result); $this->assertTrue(is_array($result)); $this->assertEquals('clientId', $result['clientId']); $this->assertEquals('accessToken', $result['accessTokenEncrypted']); $this->assertEquals('tokenCreateTime', $result['tokenCreateTime']); $this->assertEquals('tokenExpiresIn', $result['tokenExpiresIn']); unlink(AuthorizationCacheTest::CACHE_FILE); }
public function testGetAccessTokenUnit() { $config = array('mode' => 'sandbox', 'cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE); $cred = new OAuthTokenCredential('clientId', 'clientSecret'); //{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}} AuthorizationCache::push($config, 'clientId', $cred->encrypt('accessToken'), 1421204091, 288000000); $apiContext = new ApiContext($cred); $apiContext->setConfig($config); $this->assertEquals('clientId', $cred->getClientId()); $this->assertEquals('clientSecret', $cred->getClientSecret()); $result = $cred->getAccessToken($config); $this->assertNotNull($result); }
/** * Get AccessToken * * @param $config * * @return null|string */ public function getAccessToken($config) { // Check if we already have accessToken in Cache if ($this->accessToken && time() - $this->tokenCreateTime < $this->tokenExpiresIn - self::$expiryBufferTime) { return $this->accessToken; } // Check for persisted data first $token = AuthorizationCache::pull($config, $this->clientId); if ($token) { // We found it // This code block is for backward compatibility only. if (array_key_exists('accessToken', $token)) { $this->accessToken = $token['accessToken']; } $this->tokenCreateTime = $token['tokenCreateTime']; $this->tokenExpiresIn = $token['tokenExpiresIn']; // 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); } else { $this->accessToken = $this->decrypt($token['accessTokenEncrypted']); } } // 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 // for API call delays and any delay between the time the token is // retrieved and subsequently used if ($this->accessToken != null && time() - $this->tokenCreateTime > $this->tokenExpiresIn - self::$expiryBufferTime) { $this->accessToken = null; } // 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->encrypt($this->accessToken), $this->tokenCreateTime, $this->tokenExpiresIn); } return $this->accessToken; }
/** * @dataProvider CachePathProvider */ public function testCachePath($config, $expected) { $result = AuthorizationCache::cachePath($config); $this->assertContains($expected, $result); }
/** * Get AccessToken * * @param $config * * @return null|string */ 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 // for API call delays and any delay between the time the token is // retrieved and subsequently used if ($this->accessToken != null && time() - $this->tokenCreateTime > $this->tokenExpiresIn - self::$expiryBufferTime) { $this->accessToken = null; } // 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; }