Пример #1
0
 public function testGetAccessTokenReturnsInstanceOfOauthTokenAccess()
 {
     $config = array('consumerKey' => '12345', 'consumerSecret' => '54321');
     $consumer = new Consumer($config);
     $rtoken = new Token\Request();
     $rtoken->setToken('token');
     $token = $consumer->getAccessToken(array('oauth_token' => 'token'), $rtoken, null, new AccessToken48231());
     $this->assertInstanceOf('ZendOAuth\\Token\\Access', $token);
 }
Пример #2
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');
     }
 }
 /**
  * @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;
 }