Example #1
0
 /**
  * Test to make sure we throw an exception for a bad oauth response
  *
  * @expectedException  Exception
  * @return  void
  **/
 public function testAuthenticateThrowsExceptionForFailedRequest()
 {
     // Overload some curl methods to simple return self
     $http = m::mock('Orcid\\Http\\Curl');
     $http->shouldReceive('setPostFields')->andReturn(m::self())->getMock()->shouldReceive('setUrl')->andReturn(m::self())->getMock()->shouldReceive('setHeader')->andReturn(m::self());
     $response = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'Fixtures' . DIRECTORY_SEPARATOR . 'response-failure.json');
     $http->shouldReceive('execute')->andReturn($response);
     $oauth = new Oauth($http);
     $oauth->setClientId('1234')->setClientSecret('12345')->setRedirectUri('here')->authenticate('123456');
 }
Example #2
0
 /**
  * Similar to onAuthenticate, except we already have a logged in user, we're just linking accounts
  *
  * @param   array  $options
  * @return  void
  */
 public function link($options = array())
 {
     // Set up the config for the ORCID api instance
     $oauth = new Oauth();
     $oauth->setClientId($this->params->get('client_id'))->setClientSecret($this->params->get('client_secret'))->setRedirectUri(self::getRedirectUri('orcid'));
     // If we have a code coming back, the user has authorized our app, and we can authenticate
     if (!Request::getVar('code', NULL)) {
         // User didn't authorize our app, or, clicked cancel...
         App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_ORCID_MUST_AUTHORIZE_TO_LINK', Config::get('sitename')), 'error');
     }
     // Authenticate the user
     $oauth->authenticate(Request::getVar('code'));
     // Check for successful authentication
     if ($oauth->isAuthenticated()) {
         $orcid = new Profile($oauth);
         // Set username to ORCID iD
         $username = $orcid->id();
         $hzad = \Hubzero\Auth\Domain::getInstance('authentication', 'orcid', '');
         // Create the link
         if (\Hubzero\Auth\Link::getInstance($hzad->id, $username)) {
             // This orcid account is already linked to another hub account
             App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_ORCID_ACCOUNT_ALREADY_LINKED'), 'error');
         } else {
             // Create the hubzero auth link
             $hzal = \Hubzero\Auth\Link::find_or_create('authentication', 'orcid', null, $username);
             $hzal->user_id = User::get('id');
             $hzal->email = $orcid->email();
             $hzal->update();
         }
     } else {
         // User didn't authorize our app, or, clicked cancel...
         App::redirect(Route::url('index.php?option=com_members&id=' . User::get('id') . '&active=account'), Lang::txt('PLG_AUTHENTICATION_ORCID_MUST_AUTHORIZE_TO_LINK', Config::get('sitename')), 'error');
     }
 }