/** * Get an authenticated connection handle to the Swift proxy * * @throws CloudFilesException * @throws CloudFilesException|Exception * @return CF_Connection|bool False on failure */ protected function getConnection() { if ($this->connException instanceof CloudFilesException) { if (time() - $this->connErrorTime < 60) { throw $this->connException; // failed last attempt; don't bother } else { // actually retry this time $this->connException = null; $this->connErrorTime = 0; } } // Session keys expire after a while, so we renew them periodically $reAuth = time() - $this->sessionStarted > $this->authTTL; // Authenticate with proxy and get a session key... if (!$this->conn || $reAuth) { $this->sessionStarted = 0; $this->connContainerCache->clear(); $cacheKey = $this->getCredsCacheKey($this->auth->username); $creds = $this->srvCache->get($cacheKey); // credentials if (is_array($creds)) { // cache hit $this->auth->load_cached_credentials($creds['auth_token'], $creds['storage_url'], $creds['cdnm_url']); $this->sessionStarted = time() - ceil($this->authTTL / 2); // skew for worst case } else { // cache miss try { $this->auth->authenticate(); $creds = $this->auth->export_credentials(); $this->srvCache->add($cacheKey, $creds, ceil($this->authTTL / 2)); // cache $this->sessionStarted = time(); } catch (CloudFilesException $e) { $this->connException = $e; // don't keep re-trying $this->connErrorTime = time(); throw $e; // throw it back } } if ($this->conn) { // re-authorizing? $this->conn->close(); // close active cURL handles in CF_Http object } $this->conn = new CF_Connection($this->auth); } return $this->conn; }
public function testTokenErrors() { $auth = new CF_Authentication(USER, API_KEY); $auth->authenticate(); $arr = $auth->export_credentials(); $this->assertNotNull($arr['storage_url']); $this->assertNotNull($arr['cdnm_url']); $this->assertNotNull($arr['auth_token']); $this->auth = new CF_Authentication(); $this->setExpectedException('SyntaxException'); $auth->load_cached_credentials(NULL, $arr['storage_url'], $arr['cdnm_url']); $this->setExpectedException('SyntaxException'); $auth->load_cached_credentials($arr['auth_token'], NULL, $arr['cdnm_url']); $this->setExpectedException('SyntaxException'); $auth->load_cached_credentials($arr['auth_token'], $arr['storage_url'], NULL); }
/** * Get a connection to the Swift proxy * * @return CF_Connection|false * @throws InvalidResponseException */ protected function getConnection() { if ($this->conn === false) { throw new InvalidResponseException(); // failed last attempt } // Session keys expire after a while, so we renew them periodically if ($this->conn && time() - $this->connStarted > $this->authTTL) { $this->closeConnection(); } // Authenticate with proxy and get a session key... if ($this->conn === null) { $cacheKey = $this->getCredsCacheKey($this->auth->username); $creds = $this->srvCache->get($cacheKey); // credentials if (is_array($creds)) { // cache hit $this->auth->load_cached_credentials($creds['auth_token'], $creds['storage_url'], $creds['cdnm_url']); $this->connStarted = time() - ceil($this->authTTL / 2); // skew for worst case } else { // cache miss try { $this->auth->authenticate(); $creds = $this->auth->export_credentials(); $this->srvCache->set($cacheKey, $creds, ceil($this->authTTL / 2)); // cache $this->connStarted = time(); } catch (AuthenticationException $e) { $this->conn = false; // don't keep re-trying $this->logException($e, __METHOD__, $creds); } catch (InvalidResponseException $e) { $this->conn = false; // don't keep re-trying $this->logException($e, __METHOD__, $creds); } } $this->conn = new CF_Connection($this->auth); } if (!$this->conn) { throw new InvalidResponseException(); // auth/connection problem } return $this->conn; }
private static function getAuth() { global $wgMemc; $cacheKey = wfMemcKey('rscloudauth'); $auth = new CF_Authentication(WH_RSCLOUD_USERNAME, WH_RSCLOUD_API_KEY); $creds = $wgMemc->get($cacheKey); if (!$creds) { # $auth->ssl_use_cabundle(); # bypass cURL's old CA bundle $auth->authenticate(); // makes a call to a remote web server $creds = $auth->export_credentials(); $wgMemc->set($cacheKey, $creds); } else { $auth->load_cached_credentials($creds['auth_token'], $creds['storage_url'], $creds['cdnm_url']); } return $auth; }
public function authenticate() { /** @var Host $host */ foreach ($this->hosts as $host) { $config = $host->getAuthConfig(); $auth = new \CF_Authentication($config['swiftUser'], $config['swiftKey'], null, $config['swiftAuthUrl']); $auth->authenticate(); $credentials = $auth->export_credentials(); $host->setCredentials($credentials['auth_token'], $credentials['storage_url']); } }