_clearErrors(); try { $consumer = new \Zend_Oauth_Consumer($this->getCustomerConfig()); $token = $consumer->getRequestToken(); $this->_saveRequestToken($token); $consumer->redirect(); exit; } catch (\Exception $e) { $this->_addError('Can not make twitter authentication request'); \Qs_Debug::log($e->__toString(), E_USER_ERROR); } return false; } /** * Second step of Twitter OAuth authentication * @param array $requestData * @return bool * @throws \Exception */ public function completeAuthRequest($requestData) { $this->_clearErrors(); if (empty($requestData)) { $this->_addError('Empty authorization request'); return false; } try { $requestToken = $this->_readRequestToken(); $consumer = new \Zend_Oauth_Consumer($this->getCustomerConfig()); $accessToken = $consumer->getAccessToken($requestData, $requestToken); $this->_saveAccessToken($accessToken); $this->_saveRequestToken(null); } catch (\Exception $e) { $this->_addError('Wrong twitter authorization request'); \Qs_Debug::log($e->__toString(), E_USER_ERROR); $this->clearAccessToken(); $this->_saveRequestToken(null); return false; } return true; } protected function _getAuthSession() { if (null === $this->_authSession) { $this->_authSession = new \Zend_Session_Namespace(); } return $this->_authSession; } /** * Stores twitter request token in session * @param \Zend_Oauth_Token_Request|null $token * @return $this */ protected function _saveRequestToken(\Zend_Oauth_Token_Request $token = null) { $session = $this->_getAuthSession(); if (null == $token) { unset($session->twitterRequestToken); } else { $session->twitterRequestToken = serialize($token); } return $this; } /** * Reads twitter request token from session * @throws \Exception * @return \Zend_Oauth_Token_Request */ protected function _readRequestToken() { $session = $this->_getAuthSession(); if (empty($session->twitterRequestToken) || false == ($token = unserialize($session->twitterRequestToken)) instanceof \Zend_Oauth_Token_Request ) { throw new \Exception('Can not read request token'); } return $token; } /** * @param $token \Zend_Oauth_Token_Access * @return bool */ public function checkAuthToken($token) { if (empty($token) || !$token instanceof \Zend_Oauth_Token_Access) { return false; } $client = $this->_getAccessTokenClient($token); $response = $client->request(); return (200 == $response->getStatus()); } public function deleteCache() { $this->_getTable()->delete([]); return $this; } }