Exemple #1
0
 public function accessAction()
 {
     $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
     $this->view->setVar('error', null);
     $this->view->setVar('token', null);
     $this->view->setVar('user', null);
     $this->view->setVar('exception', null);
     $service = $this->dispatcher->getParam('service');
     $oauthStr = $this->dispatcher->getParam('auth');
     $oauthStr = $oauthStr === 'oauth1' ? 'oauth1' : 'oauth2';
     $config = $this->getDI()->getConfig();
     $url = $this->getDI()->getUrl();
     $callback = $url->get("/auth/access/{$service}/{$oauthStr}");
     $oauth = new OAuthService();
     $oauth->setOptions(array('callbackUrl' => $callback, 'consumerKey' => $config->oauth->{$oauthStr}->{$service}->consumer_key, 'consumerSecret' => $config->oauth->{$oauthStr}->{$service}->consumer_secret));
     $oauth->initAdapter($service, $oauthStr);
     OAuthService::setHttpClientOptions(array('timeout' => 2));
     $requestToken = OAuthManager::getRequestToken();
     if (!$requestToken) {
         return $this->view->setVar('error', 'ERR_OAUTH_REQUEST_TOKEN_FAILED');
     }
     try {
         $accessToken = $oauth->getAdapter()->getAccessToken($_GET, $requestToken);
         $accessTokenArray = $oauth->getAdapter()->accessTokenToArray($accessToken);
         OAuthManager::saveAccessToken($accessTokenArray);
     } catch (\Exception $e) {
         //TODO: log exception here
         $this->view->setVar('exception', $e->__toString());
         return $this->view->setVar('error', 'ERR_OAUTH_AUTHORIZATION_FAILED');
     }
     OAuthManager::removeRequestToken();
     $loginUser = UserModels\Login::getCurrentUser();
     //已登录,直接绑定
     if ($loginUser && $loginUser['id'] > 0) {
         $oauthManager = new OAuthManager();
         try {
             $oauthManager->bindUserOAuth($loginUser['id'], $accessTokenArray);
             $this->view->setVar('user', $loginUser);
             OAuthManager::removeAccessToken();
         } catch (\Exception $e) {
             $this->view->setVar('exception', $e->__toString());
             $this->view->setVar('error', 'ERR_OAUTH_LOGIN_FAILED');
         }
     } else {
         $accessTokenArray['suggestUsername'] = $this->getSuggestUsername($accessTokenArray);
         $accessTokenArray['suggestEmail'] = isset($accessTokenArray['remoteEmail']) ? $accessTokenArray['remoteEmail'] : '';
         $this->view->setVar('token', $accessTokenArray);
         $user = new OAuthModels\Login();
         try {
             if ($user->loginWithAccessToken($accessTokenArray)) {
                 $this->view->setVar('user', UserModels\Login::getCurrentUser());
                 OAuthManager::removeAccessToken();
             }
         } catch (\Exception $e) {
             $this->view->setVar('exception', $e->__toString());
             $this->view->setVar('error', 'ERR_OAUTH_LOGIN_FAILED');
         }
     }
 }
Exemple #2
0
 public function accessAction()
 {
     $service = $this->dispatcher->getParam('service');
     $oauthStr = $this->dispatcher->getParam('auth');
     $oauthStr = $oauthStr === 'oauth1' ? 'oauth1' : 'oauth2';
     $config = $this->getDI()->getConfig();
     $url = $this->getDI()->get('url');
     $callback = $url->get("/auth/access/{$service}/{$oauthStr}");
     $oauth = new OAuthService();
     $oauth->setOptions(array('callbackUrl' => $callback, 'consumerKey' => $config->oauth->{$oauthStr}->{$service}->consumer_key, 'consumerSecret' => $config->oauth->{$oauthStr}->{$service}->consumer_secret));
     $oauth->initAdapter($service, $oauthStr);
     OAuthService::setHttpClientOptions(array('timeout' => 2));
     $session = $this->getDI()->get('session');
     $requestToken = $session->get('request-token');
     if (!$requestToken) {
         return $this->response->redirect($this->getDI()->getConfig()->oauth->authFailedRedirectUri);
     }
     try {
         $accessToken = $oauth->getAdapter()->getAccessToken($_GET, $requestToken);
         $accessTokenArray = $oauth->getAdapter()->accessTokenToArray($accessToken);
         $session->set('access-token', $accessTokenArray);
         $session->remove('request-token');
     } catch (\Exception $e) {
         $this->flashSession->error('ERR_OAUTH_AUTHORIZATION_FAILED');
         $this->ignoreException($e);
         return $this->response->redirect($this->getDI()->getConfig()->oauth->authFailedRedirectUri);
     }
     $user = new Models\Login();
     try {
         if ($user->loginWithAccessToken($accessTokenArray)) {
             return $this->response->redirect($this->getDI()->getConfig()->oauth->loginSuccessRedirectUri);
         } else {
             return $this->response->redirect('/auth/register');
         }
     } catch (\Exception $e) {
         $this->showException($e, $user->getMessages());
         return $this->response->redirect($this->getDI()->getConfig()->oauth->registerFailedRedirectUri);
     }
 }