コード例 #1
0
ファイル: OpenApi.php プロジェクト: bjtenao/tudu-web
 /**
  * (non-PHPdoc)
  * @see Zend_Controller_Action::init()
  */
 public function init()
 {
     parent::init();
     $this->_bootstrap = $this->getInvokeArg('bootstrap');
     $accessToken = $this->_request->getParam('access_token', $this->_request->getHeader('OAuth-AccessToken'));
     $memcache = $this->_bootstrap->getResource('memcache');
     Tudu_User::setMemcache($memcache);
     $this->_user = Tudu_User::getInstance();
     // 提供访问令牌
     if (!empty($accessToken)) {
         $storage = new TuduX_OAuth_Storage_Session();
         $storage->setMemcache($memcache);
         $oauth = new OpenApi_OAuth_OAuth(array(OpenApi_OAuth_OAuth::STORAGE => $storage));
         $scope = $this->_request->getParam('client_id', $this->_request->getHeader('OAuth-Scope'));
         try {
             $token = $oauth->verifyAccessToken($accessToken, $scope);
             $this->_user->init($token['auth']);
             // 用户被禁用或已被退出登录
             if (!$this->_user->isLogined()) {
                 $oauth->destroyAccessToken($accessToken);
                 throw new OpenApi_OAuth_Exception("Invalid access token provided", OpenApi_OAuth_OAuth::ERROR_INVALID_ACCESSTOKEN);
             }
             // ts服务器
             $tsServer = 'ts' . $this->_user->tsId;
             Tudu_Dao_Manager::setDbs(array(Tudu_Dao_Manager::DB_TS => $this->_bootstrap->multidb->getDb($tsServer)));
             $this->_clientId = $token[OpenApi_OAuth_OAuth::PARAM_CLIENT_ID];
             $this->_accessToken = $accessToken;
             $this->_token = $token;
             // 验证失败
         } catch (OpenApi_OAuth_Exception $e) {
             throw $e;
         }
     }
 }
コード例 #2
0
ファイル: AuthController.php プロジェクト: bjtenao/tudu-web
 /**
  * 验证操作
  */
 public function authorizeAction()
 {
     $grantType = $this->_request->getParam('grant_type');
     $memcache = $this->_bootstrap->getResource('memcache');
     try {
         $storage = new TuduX_OAuth_Storage_Session();
         $storage->setMemcache($memcache);
         $oauth = new OpenApi_OAuth_OAuth(array(OpenApi_OAuth_OAuth::STORAGE => $storage));
         $oauth->setGrantClass(OpenApi_OAuth_OAuth::GRANT_TYPE_USER_CREDENTIALS, 'TuduX_OAuth_Grant_User');
         $params = array(OpenApi_OAuth_OAuth::PARAM_GRANT_TYPE => $grantType, OpenApi_OAuth_OAuth::PARAM_CLIENT_ID => $this->_request->getParam(OpenApi_OAuth_OAuth::PARAM_CLIENT_ID), OpenApi_OAuth_OAuth::PARAM_CLIENT_SECRET => $this->_request->getParam(OpenApi_OAuth_OAuth::PARAM_CLIENT_SECRET), OpenApi_OAuth_OAuth::PARAM_SCOPE => $this->_request->getParam(OpenApi_OAuth_OAuth::PARAM_SCOPE));
         switch ($grantType) {
             case OpenApi_OAuth_OAuth::GRANT_TYPE_USER_CREDENTIALS:
                 $params[OpenApi_OAuth_OAuth::PARAM_USERNAME] = $this->_request->getParam('username');
                 $params[OpenApi_OAuth_OAuth::PARAM_PASSWORD] = $this->_request->getParam('password');
                 break;
             case OpenApi_OAuth_OAuth::GRANT_TYPE_REFRESH_TOKEN:
                 $params[OpenApi_OAuth_OAuth::PARAM_REFRESH_TOKEN] = $this->_request->getParam('refresh_token');
                 break;
         }
         $assign = $oauth->grantAccessToken($params);
         $token = $oauth->getStorage()->getAccessToken($assign['access_token']);
         // 获取用户设置
         /* @var $daoUser Dao_Md_User_User */
         $daoOption = Tudu_Dao_Manager::getDao('Dao_Md_User_Option', Tudu_Dao_Manager::DB_MD);
         $data = $daoOption->getOption(array('orgid' => $token['auth']['orgid'], 'userid' => $token['auth']['userid']));
         if (!empty($data->settings['ios'])) {
             $assign['setting'] = $data->settings['ios'];
         } else {
             $assign['setting'] = array('push' => array('task' => 1, 'discuss' => 1, 'notice' => 1, 'meeting' => 1));
         }
         $this->view->assign($assign);
     } catch (OpenApi_OAuth_Exception $e) {
         //$this->getResponse()->setHttpResponseCode(401);
         $this->view->code = TuduX_OpenApi_ResponseCode::AUTHORIZE_FAILED;
         $this->view->error = $e->getError();
         $this->view->error_description = $e->getDescription();
     }
 }