Ejemplo n.º 1
0
 /**
  * Load Facebook data.
  *
  * @return  View_Alert|array
  */
 public function load_facebook()
 {
     if (!$this->consumer) {
         return '';
     }
     try {
         if ($response = $this->consumer->api_call('/' . $this->external->external_user_id . '?fields=id,name,link')) {
             // Received a response from 3rd party
             if ($error = Arr::get($response, 'error')) {
                 Kohana::$log->add(Log::NOTICE, 'OAuth2: Failed to load Facebook profile: :error', array(':error' => $error->message));
                 // .. but it was an error
                 return new View_Alert(__('They said ":error"', array(':error' => HTML::chars($error->message))), __('Failed to load your profile :('), View_Alert::ERROR);
             } else {
                 return $response;
             }
         } else {
             // No data received, this should be handled by exceptions
             return new View_Alert(__('No data received'), __('Failed to load your profile :('), View_Alert::ERROR);
         }
     } catch (Kohana_Exception $e) {
         Kohana::$log->add(Log::NOTICE, 'OAuth2: Exception: :error', array(':error' => $e->getMessage()));
         return new View_Alert(HTML::chars($e->getMessage()), __('Failed to load your profile :('), View_Alert::ERROR);
     }
 }
Ejemplo n.º 2
0
Archivo: oauth.php Proyecto: anqh/anqh
 /**
  * Action: Redirected from 3rd party.
  */
 public function action_redirect()
 {
     $provider = $this->consumer->get_provider();
     if ($provider != 'facebook') {
         // Unsupported provider
         $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('We are not entirely sure what 3rd party service redirected you here'), __('Failed to load your profile :('), View_Alert::ERROR));
         Kohana::$log->add(Log::NOTICE, 'OAuth2: Unsupported provider: :provider', array(':provider' => $provider));
         return;
     }
     if ($response = Arr::get($_REQUEST, OAuth2::RESPONSE_TYPE_CODE)) {
         // Code received, change it to access token
         try {
             $token = $this->consumer->request_token(array(OAuth2::RESPONSE_TYPE_CODE => $response));
             if (Visitor::$user) {
                 // Already logged in
                 $external = Model_User_External::factory()->find_by_user_id(Visitor::$user->id, $provider);
                 if ($this->_update_token($external, $token)) {
                     // Already paired with local user
                     $this->request->redirect(URL::user(Visitor::$user, 'settings'));
                     //Request::back();
                 } else {
                     // Not paired with local user, do so
                     if ($response = $this->consumer->api_call('/me', array('fields' => 'id,email'))) {
                         // Received a response from 3rd party
                         if ($error = Arr::get($response, 'error')) {
                             // .. but it was an error
                             $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('They said ":error"', array(':error' => HTML::chars($error->message))), __('Failed to load your profile :('), View_Alert::ERROR));
                             Kohana::$log->add(Log::NOTICE, 'OAuth2: Failed to load Facebook profile: :error', array(':error' => $error->message));
                         } else {
                             // Received required information
                             $external = new Model_User_External();
                             $external->set_fields(array('token' => $token['access_token'], 'user_id' => Visitor::$user->id, 'external_user_id' => Arr::get($response, 'id'), 'created' => time(), 'expires' => time() + (int) $token['expires'], 'provider' => $provider));
                             $external->save();
                             $this->request->redirect(URL::user(Visitor::$user, 'settings'));
                             //Request::back();
                         }
                     } else {
                         // No data received, this should be handled by exceptions
                     }
                 }
             } else {
                 // No signed in user available
                 if ($response = $this->consumer->api_call('/me')) {
                     // Received a response from 3rd party
                     if ($error = Arr::get($response, 'error')) {
                         // .. but it was an error
                         $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('They said ":error"', array(':error' => HTML::chars($error->message))), __('Failed to load your profile :('), View_Alert::ERROR));
                         Kohana::$log->add(Log::NOTICE, 'OAuth2: Failed to load Facebook profile: :error', array(':error' => $error->message));
                     } else {
                         // Received required information
                         $external_user_id = Arr::get($response, 'id');
                         $external = Model_User_External::factory()->find_by_external_user_id($external_user_id, $provider);
                         if ($this->_update_token($external, $token)) {
                             // Already paired with local user, login
                             Kohana::$log->add(Log::DEBUG, 'OAuth2: Attempting to login :external_user_id => :user_id', array(':external_user_id' => $external->external_user_id, ':user_id' => $external->user_id));
                             if ($this->_login($external)) {
                                 Request::back();
                             }
                             Kohana::$log->add(Log::WARNING, 'OAuth2: Login failed');
                         } else {
                             // Not paired with a local user, check if we have unpaired user available
                             $email = Arr::get($response, 'email');
                             // Store external user id in session data, token should be stored in OAuth2
                             Session::instance()->set('oauth2.' . $provider . '.id', $external_user_id);
                             if ($user = Model_User::find_user($email)) {
                                 // User with same email found, ask to sign in
                                 Kohana::$log->add(Log::DEBUG, 'OAuth2: Existing user with same email found');
                                 $this->view->add(View_Page::COLUMN_CENTER, $this->section_signin($user, $response));
                             } else {
                                 // No user with same email found, start registering
                                 Kohana::$log->add(Log::DEBUG, 'OAuth2: Starting new user registration');
                                 Session::instance()->set('oauth2.' . $provider . '.response', $response);
                                 $this->request->redirect(Route::url('sign', array('action' => 'up')) . '?provider=' . $provider);
                             }
                         }
                     }
                 } else {
                     // No data received, this should be handled by exceptions
                 }
             }
         } catch (OAuth2_Exception_InvalidGrant $e) {
             $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(HTML::chars($e->getMessage()), __('Failed to load your profile :('), View_Alert::ERROR));
             Kohana::$log->add(Log::NOTICE, 'OAuth2: Invalid grant: :error', array(':error' => $e->getMessage()));
         } catch (Kohana_Exception $e) {
             $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(HTML::chars($e->getMessage()), __('Failed to load your profile :('), View_Alert::ERROR));
             Kohana::$log->add(Log::NOTICE, 'OAuth2: Exception: :error', array(':error' => $e->getMessage()));
         }
     } else {
         $this->view->add(View_Page::COLUMN_CENTER, new View_Alert(__('Did not receive required code from 3rd party'), __('Failed to load your profile :('), View_Alert::ERROR));
         Kohana::$log->add(Log::NOTICE, 'OAuth2: No code received');
     }
 }