checkOAuthValid() public static method

验证授权是否有效
public static checkOAuthValid ( )
Beispiel #1
0
 public function shouquan($code, $openid, $openkey, $redirect_uri)
 {
     OAuth::init($this->client_id, $this->client_secret);
     Tencent::$debug = $this->debug;
     $callback = $redirect_uri;
     //回调url
     if ($code) {
         //已获得code
         //获取授权token
         $url = OAuth::getAccessToken($code, $callback);
         $r = Http::request($url);
         parse_str($r, $out);
         //存储授权数据
         if (@$out['access_token']) {
             $_SESSION['t_access_token'] = $out['access_token'];
             $_SESSION['t_refresh_token'] = $out['refresh_token'];
             $_SESSION['t_expire_in'] = $out['expires_in'];
             $_SESSION['t_code'] = $code;
             $_SESSION['t_openid'] = $openid;
             $_SESSION['t_openkey'] = $openkey;
             //验证授权
             $r = OAuth::checkOAuthValid();
             if ($r) {
                 //echo('<h3>授权成功!!!</h3><br>');
                 //print_r($r);exit;
                 //header('Location: ' . $callback);//刷新页面
                 return $r;
             } else {
                 exit('<h3>授权失败,请重试</h3>');
             }
         } else {
             exit($r);
         }
     }
 }
Beispiel #2
0
 public function checkUser($do)
 {
     // dump($_REQUEST);
     // dump($do);
     // exit;
     OAuth::init(QQ_KEY, QQ_SECRET);
     $callback = $this->getCallback('qq', $do);
     if ($_REQUEST['code']) {
         $code = $_REQUEST['code'];
         $openid = $_REQUEST['openid'];
         $openkey = $_REQUEST['openkey'];
         //获取授权token
         $url = OAuth::getAccessToken($code, $callback);
         $r = Http::request($url);
         parse_str($r, $out);
         //存储授权数据
         if ($out['access_token']) {
             $_SESSION['t_access_token'] = $out['access_token'];
             $_SESSION['t_refresh_token'] = $out['refresh_token'];
             $_SESSION['t_expire_in'] = $out['expires_in'];
             $_SESSION['t_code'] = $code;
             $_SESSION['t_openid'] = $openid;
             $_SESSION['t_openkey'] = $openkey;
             $_SESSION['qq']['access_token'] = $out['access_token'];
             $_SESSION['qq']['refresh_token'] = $out['refresh_token'];
             $_SESSION['open_platform_type'] = 'qq';
             //验证授权
             $r = OAuth::checkOAuthValid();
             if ($r) {
                 // header('Location: ' . $callback);//刷新页面
                 return true;
             } else {
                 // exit('<h3>授权失败,请重试</h3>');
                 return false;
             }
         } else {
             exit($r);
         }
     } else {
         //获取授权code
         if ($_GET['openid'] && $_GET['openkey']) {
             //应用频道
             $_SESSION['t_openid'] = $_GET['openid'];
             $_SESSION['t_openkey'] = $_GET['openkey'];
             //验证授权
             $r = OAuth::checkOAuthValid();
             if ($r) {
                 // header('Location: ' . $callback);//刷新页面
                 return true;
             } else {
                 // exit('<h3>授权失败,请重试</h3>');
                 return false;
             }
         } else {
             $url = OAuth::getAuthorizeURL($callback);
             header('Location: ' . $url);
         }
     }
 }
Beispiel #3
0
 function actionReturn()
 {
     if ($_GET['code']) {
         //已获得code
         $code = $_GET['code'];
         $openid = $_GET['openid'];
         $openkey = $_GET['openkey'];
         //获取授权token
         $url = \OAuth::getAccessToken($code, $this->url);
         $access_token = $_SESSION['t_access_token'];
         $r = \Http::request($url);
         parse_str($r, $out);
         //存储授权数据
         if ($out['access_token']) {
             $_SESSION['t_access_token'] = $out['access_token'];
             $_SESSION['t_expire_in'] = $out['expire_in'];
             $_SESSION['t_code'] = $code;
             $_SESSION['t_openid'] = $openid;
             $_SESSION['t_openkey'] = $openkey;
             //验证授权
             $ret = \OAuth::checkOAuthValid();
             $ret = \Tencent::api('user/info');
             $uid_get = json_decode($ret, true);
             try {
                 $uid = $uid_get['data']['openid'];
                 $me['id'] = $uid;
                 $me['name'] = $uid_get['data']['name'];
                 $me['email'] = $uid_get['data']['email'];
                 $me['nickname'] = $uid_get['data']['nick'];
                 $r = $this->member_get_third_set_user($me, $this->oauth_id, $access_token);
                 flash('success', __('login success'));
                 $this->redirect(return_url());
             } catch (OAuthException $e) {
                 flash('error', __('login error'));
                 $this->redirect(return_url());
             }
         }
     }
     exit;
 }
Beispiel #4
0
    $url = OAuth::getAccessToken($code, $callback);
    $r = Http::request($url);
    parse_str($r, $out);
    //存储授权数据
    if ($out['access_token']) {
        $_SESSION['qqweibo']['t_access_token'] = $out['access_token'];
        $_SESSION['qqweibo']['t_expire_in'] = $out['expires_in'];
        $_SESSION['qqweibo']['t_refresh_token'] = $out['refresh_token'];
        $_SESSION['qqweibo']['t_uname'] = $out['name'];
        $_SESSION['qqweibo']['t_code'] = $code;
        $_SESSION['qqweibo']['t_openid'] = $openid;
        //OpenID可以唯一标识一个用户。在同一个应用下,同一个QQ号码的OpenID是相同的;但在不同应用下,同一个QQ号码可能有不同的OpenID
        $_SESSION['qqweibo']['t_openkey'] = $openkey;
        //OpenKey是与OpenID对应的用户key(用户在第三方应用的腾讯登录态),是验证OpenID身份的验证密钥,大多数API的访问,都需要同时具备OpenID和OpenKey的信息,其有效期为2小时
        //验证授权
        $r = OAuth::checkOAuthValid();
        if ($r) {
            //成功
            //header('Location: ' . $callback);//刷新页面
        } else {
            //exit('<h3>授权失败,请重试</h3>');
            echo "<script>alert('授权失败,请重试');</script>";
            echo "<script>window.close();</script>";
            exit;
        }
    } else {
        exit($r);
    }
} else {
    //获取授权code
    echo "<script>alert('授权失败,请重试');</script>";
Beispiel #5
0
 public function callback()
 {
     OAuth::init($this->api['config']['app_key'], $this->api['config']['app_secret']);
     Tencent::$debug = $debug;
     $callback = SITE_DOMAIN . APP_ROOT . "/api_callback.php?c=Tencent";
     if (es_session::is_set('t_access_token') || es_session::is_set('t_openid') && es_session::is_set('t_openkey')) {
         //用户已授权
         //echo '<pre><h3>已授权</h3>用户信息:<br>';
         //获取用户信息
         $r = Tencent::api('user/info');
         $json_data = json_decode($r, true);
         //print_r($json_data);
         // echo '</pre>';
     } else {
         if ($_GET['code']) {
             //已获得code
             $code = $_GET['code'];
             $openid = $_GET['openid'];
             $openkey = $_GET['openkey'];
             //获取授权token
             $url = OAuth::getAccessToken($code, $callback);
             $r = Http::request($url);
             parse_str($r, $out);
             //存储授权数据
             if ($out['access_token']) {
                 es_session::set('t_access_token', $out['access_token']);
                 es_session::set('refresh_token', $out['refresh_token']);
                 es_session::set('expires_in', $out['expires_in']);
                 es_session::set('t_code', $code);
                 es_session::set('t_openid', $openid);
                 es_session::set('t_openkey', $openkey);
                 //验证授权
                 $r = OAuth::checkOAuthValid();
                 if ($r) {
                     app_redirect($callback);
                     //刷新页面
                 } else {
                     exit('<h3>授权失败,请重试</h3>');
                 }
             } else {
                 exit($r);
             }
         } else {
             //获取授权code
             if ($_GET['openid'] && $_GET['openkey']) {
                 //应用频道
                 s_session::set('t_openid', $_GET['openid']);
                 es_session::set('t_openkey', $_GET['openkey']);
                 //验证授权
                 $r = OAuth::checkOAuthValid();
                 if ($r) {
                     app_redirect($callback);
                     //刷新页面
                 } else {
                     exit('<h3>授权失败,请重试</h3>');
                 }
             } else {
                 $url = OAuth::getAuthorizeURL($callback);
                 app_redirect($url);
             }
         }
     }
     if ($json_data['msg'] != "ok") {
         echo '<pre><h3>出错了</h3><pre>';
         die;
     }
     $is_bind = intval($_REQUEST['is_bind']);
     $tencent_id = $json_data['data']['openid'];
     $msg['field'] = 'tencent_id';
     $msg['id'] = $tencent_id;
     $msg['name'] = $json_data['data']['name'];
     es_session::set("api_user_info", $msg);
     $user_data = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where tencent_id = '" . $tencent_id . "' and tencent_id <> ''");
     if ($user_data) {
         $user_current_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where id = " . intval($user_data['group_id']));
         $user_group = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user_group where score <=" . intval($user_data['score']) . " order by score desc");
         if ($user_current_group['score'] < $user_group['score']) {
             $user_data['group_id'] = intval($user_group['id']);
         }
         //$GLOBALS['db']->query("update ".DB_PREFIX."user set tencent_app_key ='".$last_key['oauth_token']."',tencent_app_secret = '".$last_key['oauth_token_secret']."', login_ip = '".get_client_ip()."',login_time= ".TIME_UTC.",group_id=".intval($user_data['group_id'])." where id =".$user_data['id']);
         //$GLOBALS['db']->query("update ".DB_PREFIX."deal_cart set user_id = ".intval($user_data['id'])." where session_id = '".es_session::id()."'");
         es_session::delete("api_user_info");
         if ($is_bind) {
             if (intval($user_data['id']) != intval($GLOBALS['user_info']['id'])) {
                 showErr("该帐号已经被别的会员绑定过,请直接用帐号登录", 0, url("shop", "uc_center#setweibo"));
             } else {
                 es_session::set("user_info", $user_data);
                 app_redirect(url("shop", "uc_center#setweibo"));
             }
         } else {
             es_session::set("user_info", $user_data);
             app_recirect_preview();
         }
     } elseif ($is_bind == 1 && $GLOBALS['user_info']) {
         //当有用户身份且要求绑定时
         $GLOBALS['db']->query("update " . DB_PREFIX . "user set tencent_id= '" . $tencent_id . "' where id =" . $GLOBALS['user_info']['id']);
         app_redirect(url("index", "uc_center#setweibo"));
     } else {
         $this->create_user();
         //app_redirect(url("index","user#api_login"));
         app_recirect_preview();
     }
 }