Ejemplo n.º 1
0
 /**
  * OAuth callback.
  * 
  * @param  string    $provider
  * @param  string    $referer
  * @access public
  * @return void
  */
 public function oauthCallback($provider)
 {
     /* First check the state and provider fields. */
     if ($this->get->state != $this->session->oauthState) {
         die('state wrong!');
     }
     if ($provider != $this->session->oauthProvider) {
         die('provider wrong.');
     }
     $referer = $this->session->referer;
     /* Init the OAuth client. */
     $this->app->loadClass('oauth', $static = true);
     $this->config->oauth->{$provider} = json_decode($this->config->oauth->{$provider});
     $client = oauth::factory($provider, $this->config->oauth->{$provider}, $this->user->createOAuthCallbackURL($provider));
     /* Begin OAuth authing. */
     $token = $client->getToken($this->get->code);
     // Step1: get token by the code.
     $openID = $client->getOpenID($token);
     // Step2: get open id by the token.
     $openUser = $client->getUserInfo($token, $openID);
     // Get open user info.
     $this->session->set('openUser', $openUser);
     $this->session->set('openID', $openID);
     // Save the openID to session.
     /* Step3: Try to get user by the open id, if got, login him. */
     $user = $this->user->getUserByOpenID($provider, $openID);
     $this->session->set('random', md5(time() . mt_rand()));
     if ($user) {
         if ($this->user->login($user->account, md5($user->password . $this->session->random))) {
             if ($referer) {
                 $this->locate(helper::safe64Decode($referer));
             }
             /* No referer, go to the user control panel. */
             $default = $this->config->user->default;
             $this->locate($this->createLink($default->module, $default->method));
         }
         exit;
     }
     /* Step4.1: if bind, display the register or bind page. */
     if ($this->get->referer != false) {
         $this->setReferer($referer);
     }
     // Set the referer.
     $this->config->oauth->{$provider} = json_encode($this->config->oauth->{$provider});
     $this->view->title = $this->lang->user->login->common;
     $this->view->referer = $referer;
     $this->view->mobileURL = helper::createLink('user', 'oauthCallback', "provider={$provider}", '', 'mhtml');
     $this->view->desktopURL = helper::createLink('user', 'oauthCallback', "provider={$provider}", '', 'html');
     die($this->display());
 }
Ejemplo n.º 2
0
 /**
  * OAuth callback.
  * 
  * @param  string    $provider
  * @param  string    $referer
  * @access public
  * @return void
  */
 public function oauthCallback($provider, $referer = '')
 {
     /* First check the state and provider fields. */
     if ($this->get->state != $this->session->oauthState) {
         die('state wrong!');
     }
     if ($provider != $this->session->oauthProvider) {
         die('provider wrong.');
     }
     /* Init the OAuth client. */
     $this->app->loadClass('oauth', $static = true);
     $this->config->oauth->{$provider} = json_decode($this->config->oauth->{$provider});
     $client = oauth::factory($provider, $this->config->oauth->{$provider}, $this->user->createOAuthCallbackURL($provider, $referer));
     /* Begin OAuth authing. */
     $token = $client->getToken($this->get->code);
     // Step1: get token by the code.
     $openID = $client->getOpenID($token);
     // Step2: get open id by the token.
     /* Step3: Try to get user by the open id, if got, login him. */
     $user = $this->user->getUserByOpenID($provider, $openID);
     $this->session->set('random', md5(time() . mt_rand()));
     if ($user) {
         if ($this->user->login($user->account, md5($user->password . $this->session->random))) {
             if ($referer) {
                 $this->locate(helper::safe64Decode($referer));
             }
             /* No referer, go to the user control panel. */
             $default = $this->config->user->default;
             $this->locate($this->createLink($default->module, $default->method));
         }
         exit;
     }
     /* Step4.1: if the provider is sina, display the register or bind page. */
     if ($provider == 'sina') {
         $this->session->set('oauthOpenID', $openID);
         // Save the openID to session.
         if ($this->get->referer != false) {
             $this->setReferer($referer);
         }
         // Set the referer.
         $this->view->title = $this->lang->user->login->common;
         $this->view->referer = $referer;
         die($this->display());
     }
     /* Step4.2: if the provider is qq, register a user with random user. Shit! */
     if ($provider == 'qq') {
         $openUser = $client->getUserInfo($token, $openID);
         // Get open user info.
         $this->post->set('account', uniqid('qq_'));
         // Create a uniq account.
         $this->post->set('realname', htmlspecialchars($openUser->nickname));
         // Set the realname.
         $this->user->registerOauthAccount($provider, $openID);
         $user = $this->user->getUserByOpenID($provider, $openID);
         $this->session->set('random', md5(time() . mt_rand()));
         if ($user and $this->user->login($user->account, md5($user->password . $this->session->random))) {
             if ($referer) {
                 $this->locate(helper::safe64Decode($referer));
             }
             /* No referer, go to the user control panel. */
             $default = $this->config->user->default;
             $this->locate($this->createLink($default->module, $default->method));
         } else {
             die('some error occers.');
         }
     }
 }