예제 #1
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/sinawb/saetv2.ex.class.php";
     $config = OAuth::getConf('sinawb');
     $sinawb = new SaeTOAuthV2($config['wb_akey'], $config['wb_skey']);
     $code_url = $sinawb->getAuthorizeURL($config['callback']);
     $this->controller->redirect($code_url);
 }
예제 #2
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/renren/RennClient.php";
     $config = OAuth::getConf('renren');
     $rennClient = new RennClient($config['app_key'], $config['app_secret']);
     // 生成state并存入SESSION,以供CALLBACK时验证使用
     $state = uniqid('renren_', true);
     Yii::app()->session['renren_state'] = $state;
     // 取得认证授权的url
     $code_url = $rennClient->getAuthorizeURL($config['callback'], 'code', $state);
     $this->controller->redirect($code_url);
 }
예제 #3
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/weixin/weixin.class.php";
     Yii::import('application.extensions.qrcode.QrCode');
     $config = OAuth::getConf('weixin');
     $random_uuid = $this->_createUUid();
     $wx = new WeiXin($config['app_id'], $config['app_secret'], $config['callback'], $random_uuid);
     $qr = new QrCode();
     $qrcode_url = $qr->createCode($wx->auth_url, 8);
     $this->controller->layout = false;
     $this->controller->_seoTitle = '微信扫一扫登录' . ' - ' . $this->controller->_setting['site_name'];
     $this->controller->render('wx_qrcode', array('qrcode_url' => $qrcode_url));
 }
예제 #4
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/renren/rennclient/RennClient.php";
     $config = OAuth::getConf('renren');
     $rennClient = new RennClient($config['app_key'], $config['app_secret']);
     // 处理code -- 根据code来获得token
     if (isset($_REQUEST['code'])) {
         // 验证state,防止伪造请求跨站攻击
         $state = $_REQUEST['state'];
         if (empty($state) || $state !== Yii::app()->session['renren_state']) {
             throw new CHttpException(500, 'Error: Illegal Request');
         }
         unset(Yii::app()->session['renren_state']);
         // 获得code
         $code = $_REQUEST['code'];
         $redirect_uri = $config['callback'];
         try {
             // 根据code来获得token
             $token = $rennClient->authWithAuthorizationCode($code, $redirect_uri);
         } catch (RennException $e) {
             throw new CHttpException(500, 'Error:' . $e->getMessage());
         }
     } else {
         throw new CHttpException(500, 'Auth Failed');
     }
     if ($token) {
         // 获得保存的token
         $rennClient->authWithStoredToken();
         // 获得用户接口
         $user_service = $rennClient->getUserService();
         // 获得用户信息
         $user = $user_service->getUser(null);
         if (!$user) {
             throw new CHttpException('500', Yii::t('common', 'Login Failed') . '(get userinfo failed)');
         }
         // 获取accessToken
         $access_token = $token->accessToken;
         $openid = $user['id'];
         //查看是否已绑定
         $bind = OAuthRenren::model()->findByPk($openid);
         //数据
         $data = array('type' => 'renren', 'access_token' => $access_token, 'openid' => $openid, 'uid' => $bind ? $bind->uid : 0, 'username' => $user['name'], 'avatar' => $user['avatar'][1]['url']);
         //绑定注册
         $this->controller->bind_register($bind, $data);
     } else {
         $this->controller->message('error', Yii::t('common', 'Login Failed') . '(renren_x_0000)', $this->createUrl('user/login'));
     }
 }
예제 #5
0
 public function run()
 {
     require_once Yii::getPathOfAlias('ext') . "/OAuth/sinawb/saetv2.ex.class.php";
     $config = OAuth::getConf('sinawb');
     $sinawb = new SaeTOAuthV2($config['wb_akey'], $config['wb_skey']);
     if (isset($_REQUEST['code'])) {
         $keys = array();
         $keys['code'] = $_REQUEST['code'];
         $keys['redirect_uri'] = $config['callback'];
         try {
             $token = $sinawb->getAccessToken('code', $keys);
         } catch (OAuthException $e) {
             throw new CHttpException(500, 'Error:' . $e->getMessage());
         }
     }
     if ($token) {
         $access_token = Yii::app()->session['access_token'] = $token['access_token'];
         $openid = $token['uid'];
         //设置cookie
         $cookie_name = 'weibojs_' . $openid;
         $cookie = new CHttpCookie($cookie_name, http_build_query($token));
         $cookie->expire = time() + 60 * 60 * 24 * 30;
         //有限期30天
         Yii::app()->request->cookies[$cookie_name] = $cookie;
         //获取用户信息
         $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $access_token);
         $user_info = $c->show_user_by_id($openid);
         //根据ID获取用户等基本信息
         if ($user_info['error']) {
             throw new CHttpException('500', Yii::t('common', 'Login Failed') . '(' . $user_info['error_code'] . ')');
         }
         //查看是否已绑定
         $bind = OAuthSinawb::model()->findByPk($openid);
         //数据
         $data = array('type' => 'sinawb', 'access_token' => $access_token, 'openid' => $openid, 'uid' => $bind ? $bind->uid : 0, 'username' => $user_info['screen_name'], 'avatar' => $user_info['avatar_large']);
         //绑定注册
         $this->controller->bind_register($bind, $data);
     } else {
         $this->controller->message('error', Yii::t('common', 'Login Failed') . '(sinawb_x_0000)', $this->createUrl('user/login'));
     }
 }