Exemplo n.º 1
0
 /**
  * Tests the getAvailableDriver method.
  *
  * @return  void
  *
  * @covers  Joomla\Http\HttpFactory::getAvailableDriver
  * @since   1.0
  */
 public function testGetAvailableDriver()
 {
     $this->assertInstanceOf('Joomla\\Http\\TransportInterface', HttpFactory::getAvailableDriver(array(), null));
     $this->assertFalse(HttpFactory::getAvailableDriver(array(), array()), 'Passing an empty array should return false due to there being no adapters to test');
     $this->assertFalse(HttpFactory::getAvailableDriver(array(), array('fopen')), 'A false should be returned if a class is not present or supported');
     include_once __DIR__ . '/stubs/DummyTransport.php';
     $this->assertFalse(HttpFactory::getAvailableDriver(array(), array('DummyTransport')), 'Passing an empty array should return false due to there being no adapters to test');
 }
Exemplo n.º 2
0
 /**
  * Constructor.
  *
  * @param   array               $options    Client options array. If the registry contains any headers.* elements,
  *                                          these will be added to the request headers.
  * @param   TransportInterface  $transport  The HTTP transport object.
  *
  * @since   1.0
  * @throws  \InvalidArgumentException
  */
 public function __construct($options = array(), TransportInterface $transport = null)
 {
     $this->options = $options;
     if (!isset($transport)) {
         $transport = HttpFactory::getAvailableDriver($this->options);
     }
     // Ensure the transport is a TransportInterface instance or bail out
     if (!$transport instanceof TransportInterface) {
         throw new \InvalidArgumentException('A valid TransportInterface object was not set.');
     }
     $this->transport = $transport;
 }
Exemplo n.º 3
0
 /**
  * Retrieves an instance of the GitHub object
  *
  * @param   \Joomla\Application\AbstractApplication  $app          Application object
  * @param   boolean                                  $useBot       Flag to use a bot account.
  * @param   string                                   $botUser      The bot account user name.
  * @param   string                                   $botPassword  The bot account password.
  *
  * @return  GitHub
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public static function getInstance($app, $useBot = false, $botUser = '', $botPassword = '')
 {
     $options = new Registry();
     // Check if we're in the web application and a token exists
     if ($app instanceof \JTracker\Application) {
         $session = $app->getSession();
         $token = $session->get('gh_oauth_access_token');
     } else {
         $token = false;
     }
     // If a token is active in the session (web app), and we haven't been instructed to use a bot account, use that for authentication
     if ($token && !$useBot) {
         $options->set('gh.token', $token);
     } else {
         // Check if credentials are supplied
         if ($botUser && $botPassword) {
             $user = $botUser;
             $password = $botPassword;
         } else {
             // Check for support for multiple accounts
             $accounts = $app->get('github.accounts');
             if ($accounts) {
                 $user = isset($accounts[0]->username) ? $accounts[0]->username : null;
                 $password = isset($accounts[0]->password) ? $accounts[0]->password : null;
                 // Store the other accounts
                 $options->set('api.accounts', $accounts);
             } else {
                 // Support for a single account
                 $user = $app->get('github.username');
                 $password = $app->get('github.password');
             }
         }
         // Add the username and password to the options object if both are set
         if ($user && $password) {
             // Set the options from the first account
             $options->set('api.username', $user);
             $options->set('api.password', $password);
         }
     }
     // The cURL extension is required to properly work.
     $transport = HttpFactory::getAvailableDriver($options, array('curl'));
     // Check if we *really* got a cURL transport...
     if (!$transport instanceof Curl) {
         throw new \RuntimeException('Please enable cURL.');
     }
     $http = new Http($options, $transport);
     // Instantiate the object
     return new Github($options, $http);
 }
Exemplo n.º 4
0
 /**
  * Constructor.
  *
  * @param   array|\ArrayAccess  $options    Client options array. If the registry contains any headers.* elements,
  *                                          these will be added to the request headers.
  * @param   TransportInterface  $transport  The HTTP transport object.
  *
  * @since   1.0
  * @throws  \InvalidArgumentException
  */
 public function __construct($options = array(), TransportInterface $transport = null)
 {
     if (!is_array($options) && !$options instanceof \ArrayAccess) {
         throw new \InvalidArgumentException('The options param must be an array or implement the ArrayAccess interface.');
     }
     $this->options = $options;
     if (!isset($transport)) {
         $transport = HttpFactory::getAvailableDriver($this->options);
     }
     // Ensure the transport is a TransportInterface instance or bail out
     if (!$transport instanceof TransportInterface) {
         throw new \InvalidArgumentException('A valid TransportInterface object was not set.');
     }
     $this->transport = $transport;
 }
Exemplo n.º 5
0
 /**
  * Execute the controller.
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  * @throws  \Exception
  */
 public function execute()
 {
     /* @type \JTracker\Application $app */
     $app = $this->getContainer()->get('app');
     $user = $app->getUser();
     if ($user->id) {
         // The user is already logged in.
         $app->redirect(' ');
     }
     $error = $app->input->get('error');
     if ($error) {
         // GitHub reported an error.
         throw new \Exception($error);
     }
     $code = $app->input->get('code');
     if (!$code) {
         // No auth code supplied.
         throw new \Exception('Missing login code');
     }
     // Do login
     /*
      * @todo J\oAuth scrambles our redirects - investigate..
      *
     
     $options = new Registry(
     	array(
     		'tokenurl' => 'https://github.com/login/oauth/access_token',
     		'redirect_uri' => $app->get('uri.request'),
     		'clientid' => $app->get('github.client_id'),
     		'clientsecret' => $app->get('github.client_secret')
     	)
     );
     
     $oAuth = new oAuthClient($options);
     
     $token = $oAuth->authenticate();
     
     $accessToken = $token['access_token'];
     */
     $loginHelper = new GitHubLoginHelper($this->getContainer());
     $accessToken = $loginHelper->requestToken($code);
     // Store the token into the session
     $app->getSession()->set('gh_oauth_access_token', $accessToken);
     // Get the current logged in GitHub user
     $options = new Registry();
     $options->set('gh.token', $accessToken);
     // GitHub API works best with cURL
     $transport = HttpFactory::getAvailableDriver($options, array('curl'));
     $http = new Http($options, $transport);
     // Instantiate Github
     $gitHub = new Github($options, $http);
     $gitHubUser = $gitHub->users->getAuthenticatedUser();
     $user = new GithubUser($app->getProject(), $this->getContainer()->get('db'));
     $user->loadGitHubData($gitHubUser)->loadByUserName($user->username);
     // Save the avatar
     $loginHelper->saveAvatar($user->username);
     // Set the last visit time
     $loginHelper->setLastVisitTime($user->id);
     // User login
     $app->setUser($user);
     $redirect = $app->input->getBase64('usr_redirect');
     $redirect = $redirect ? base64_decode($redirect) : '';
     // Set a "remember me" cookie.
     $app->setRememberMe(true);
     $app->redirect($redirect);
 }
Exemplo n.º 6
0
 /**
  * Request an oAuth token from GitHub.
  *
  * @param   string  $code  The code obtained form GitHub on the previous step.
  *
  * @return  string  The OAuth token
  *
  * @since   1.0
  * @throws  \RuntimeException
  * @throws  \DomainException
  */
 public function requestToken($code)
 {
     // GitHub API works best with cURL
     $options = new Registry();
     $transport = HttpFactory::getAvailableDriver($options, array('curl'));
     if (false == $transport) {
         throw new \DomainException('No transports available (please install php-curl)');
     }
     $http = new Http($options, $transport);
     $data = array('client_id' => $this->clientId, 'client_secret' => $this->clientSecret, 'code' => $code);
     $response = $http->post('https://github.com/login/oauth/access_token', $data, array('Accept' => 'application/json'));
     if (200 != $response->code) {
         if (JDEBUG) {
             var_dump($response);
         }
         throw new \DomainException('Invalid response from GitHub (2) :(');
     }
     $body = json_decode($response->body);
     if (isset($body->error)) {
         switch ($body->error) {
             case 'bad_verification_code':
                 throw new \DomainException('bad verification code');
                 break;
             default:
                 throw new \DomainException('Unknown (2) ' . $body->error);
                 break;
         }
     }
     if (!isset($body->access_token)) {
         throw new \DomainException('Can not retrieve the access token');
     }
     return $body->access_token;
 }
Exemplo n.º 7
0
 /**
  * Constructor.
  *
  * @param   array               $options    Client options array. If the registry contains any headers.* elements,
  *                                          these will be added to the request headers.
  * @param   TransportInterface  $transport  The HTTP transport object.
  *
  * @since   1.0
  */
 public function __construct($options = array(), TransportInterface $transport = null)
 {
     $this->options = $options;
     $this->transport = isset($transport) ? $transport : HttpFactory::getAvailableDriver($this->options);
 }
Exemplo n.º 8
0
 /**
  * Tests the getAvailableDriver method.
  *
  * @return  void
  *
  * @since   1.0
  */
 public function testGetAvailableDriver()
 {
     $this->assertThat(HttpFactory::getAvailableDriver(array(), array()), $this->isFalse(), 'Passing an empty array should return false due to there being no adapters to test');
     $this->assertThat(HttpFactory::getAvailableDriver(array(), array('fopen')), $this->isFalse(), 'A false should be returned if a class is not present or supported');
 }