public function getConsumer()
 {
     if ($this->consumer) {
         return $this->consumer;
     }
     $consumer = new Consumer($this->getOptions());
     //to void the error :  make sure the "sslcapath" option points to a valid SSL certificate directory
     $consumer->getHttpClient()->setOptions(array('sslverifypeer' => false));
     return $this->consumer = $consumer;
 }
Exemple #2
0
 public function twitterAction()
 {
     $config = array('callbackUrl' => 'http://cursozf2.local', 'consumerKey' => '6m5PRdgc8L34OHt0jl7XQ', 'consumerSecret' => 'MbrgvIa7AQRCkNaHToKGdyIfuBPlhUVKKN2fo7k', 'siteUrl' => 'https://api.twitter.com/oauth', 'authorizeUrl' => 'https://api.twitter.com/oauth/authenticate', 'requestTokenUrl' => 'https://api.twitter.com/oauth/request_token', 'accessTokenUrl' => 'https://api.twitter.com/oauth/access_token');
     $httpConfig = array('adapter' => 'Zend\\Http\\Client\\Adapter\\Socket', 'sslverifypeer' => false);
     $httpClient = new HTTPClient(null, $httpConfig);
     OAuth::setHttpClient($httpClient);
     $consumer = new Consumer($config);
     $token = null;
     if (!$token) {
         $token = $consumer->getRequestToken();
         $_SESSION['TWITTER_REQUEST_TOKEN'] = serialize($token);
         $consumer->redirect();
     }
 }
Exemple #3
0
 /**
  * @return \Zend\Http\Response
  */
 public function twitterCallbackAction()
 {
     $config = $this->getServiceLocator()->get('config')['twitter'];
     $config['callbackUrl'] = $this->url()->fromRoute('user/default', ['controller' => 'auth', 'action' => 'twitter-callback'], ['force_canonical' => true]);
     OAuth::setHttpClient(new Client(null, $config['httpClientOptions']));
     $consumer = new Consumer($config);
     $container = new Container('twitter');
     if ($this->getRequest()->isGet() && $this->params()->fromQuery() && isset($container->requestToken)) {
         $token = $consumer->getAccessToken($this->params()->fromQuery(), unserialize($container->requestToken));
         /**
          * @var \Doctrine\ORM\EntityManager $objectManager
          */
         $objectManager = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
         /**
          * @var \User\Entity\Auth $auth
          */
         $auth = $objectManager->getRepository('User\\Entity\\Auth')->getAuthRow(Auth::PROVIDER_TWITTER, $token->user_id);
         if ($auth) {
             $user = $auth->getUser();
             if (!$user->isActive()) {
                 $this->flashMessenger()->addSuccessMessage("User is not active");
                 return $this->redirect()->toRoute('home');
             }
             $auth->setToken($token->oauth_token);
             $auth->setTokenSecret($token->oauth_token_secret);
             $auth->setTokenType(Auth::TYPE_ACCESS);
             $message = "You have successfully logged in via twitter";
         } else {
             //if there is no user with provided twitter id and user is not logged in
             if (!$this->identity()) {
                 //create new user
                 /**
                  * @var \User\Entity\User $user
                  */
                 $user = $user = new \User\Entity\User();
                 //todo: need to be checked for unique
                 $user->setDisplayName($token->screen_name);
                 $user->setRole($user::ROLE_USER);
                 $user->activate();
                 $objectManager->persist($user);
                 $objectManager->flush();
             } else {
                 //get current authorized user
                 $user = $this->identity()->getUser();
             }
             $auth = new \User\Entity\Auth();
             $auth->setToken($token->oauth_token);
             $auth->setTokenSecret($token->oauth_token_secret);
             $auth->setForeignKey($token->user_id);
             $auth->setProvider(Auth::PROVIDER_TWITTER);
             $auth->setTokenType(Auth::TYPE_ACCESS);
             $auth->setUserId($user->getId());
             $user->getAuths()->add($auth);
             $auth->setUser($user);
             $message = "You've successfully registered via twitter";
         }
         $objectManager->persist($user);
         $objectManager->persist($auth);
         $objectManager->flush();
         $auth->login($this->getServiceLocator());
         // Now that we have an Access Token, we can discard the Request Token
         $container->requestToken = null;
         $this->flashMessenger()->addSuccessMessage($message);
         $session = new Container('location');
         $location = $session->location;
         if ($location) {
             $session->getManager()->getStorage()->clear('location');
             return $this->redirect()->toUrl($location);
         }
         return $this->redirect()->toRoute('home');
     } else {
         $this->flashMessenger()->addErrorMessage("Invalid callback request. Oops. Sorry.");
         return $this->redirect()->toRoute('home');
     }
 }
 public function __construct(array $options = array())
 {
     $this->_requestToken = new Token\Request();
     $this->_accessToken = new Token\Access();
     parent::__construct($options);
 }
 /**
  * @return bool|NormalizedData
  */
 public function getUserData()
 {
     if ($this->data) {
         return $this->data;
     }
     $config = array('consumerKey' => $this->consumerKey, 'consumerSecret' => $this->consumerSecret, 'siteUrl' => 'https://api.twitter.com/oauth');
     $httpClientOptions = array('adapter' => 'Zend\\Http\\Client\\Adapter\\Curl', 'curloptions' => array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false));
     $consumer = new Consumer($config);
     $consumer->setHttpClient($consumer->getHttpClient()->setOptions($httpClientOptions));
     $tw_session = new Container('twitter');
     if (!empty($this->getParams) && $tw_session->offsetExists('request_token')) {
         try {
             $tw_request_token = $tw_session->offsetGet('request_token');
             $token = $consumer->getAccessToken($this->getParams, unserialize($tw_request_token));
             $tw_session->offsetUnset('request_token');
             $client = $token->getHttpClient($config, null, array('adapter' => new Client\Adapter\Curl()));
             $client->setUri('https://api.twitter.com/1.1/account/verify_credentials.json');
             $client->setMethod(Request::METHOD_GET);
             $adapter = new Client\Adapter\Curl();
             $adapter->setCurlOption(CURLOPT_SSL_VERIFYHOST, false);
             $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER, false);
             $client->setAdapter($adapter);
             $response = $client->send();
             $data = Json::decode($response->getBody(), Json::TYPE_ARRAY);
             $data['token'] = serialize($token);
             $this->isValid = true;
             return new NormalizedData(NormalizedData::PROVIDER_TYPE_TWITTER, $data);
         } catch (\Exception $e) {
             return false;
         }
     }
     return false;
 }