Skip to content

Commit

Permalink
Merge pull request #44 from hirak/feature/improve-scrutinizer-score-2
Browse files Browse the repository at this point in the history
Improve scrutinizer score
  • Loading branch information
hirak committed Feb 7, 2016
2 parents cf526ee + 2ab098f commit c4e4767
Show file tree
Hide file tree
Showing 17 changed files with 468 additions and 363 deletions.
65 changes: 24 additions & 41 deletions src/Aspects/AspectAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,46 @@ class AspectAuth implements SplObserver
{
public function update(SplSubject $ev)
{
switch ((string)$ev) {
case 'pre-download':
$this->before($ev->refRequest());
break;
case 'post-download':
$this->after($ev->refResponse());
break;
$name = (string)$ev;
if ('pre-download' === $name) {
return $this->before($ev->refRequest());
}
if ('post-download' === $name) {
$this->after($ev->refResponse());
}
}

public function before(HttpGetRequest $req)
private function before(HttpGetRequest $req)
{
if (!$req->username || !$req->password) {
$req->username = null;
$req->password = null;
$req->username = $req->password = null;
return;
}

if ($req instanceof GitHubRequest && $req->password === 'x-oauth-basic') {
$req->query['access_token'] = $req->username;
// forbid basic-auth
$req->username = $req->password = null;
return;
}

switch ($req->special) {
case 'github':
if ($req->password === 'x-oauth-basic') {
$req->query['access_token'] = $req->username;
// forbid basic-auth
$req->username = null;
$req->password = null;
return;
}
break;
case 'gitlab':
if ($req->password === 'oauth2') {
$req->headers[] = 'Authorization: Bearer ' . $req->username;
// forbid basic-auth
$req->username = null;
$req->password = null;
return;
}
break;
if ($req instanceof GitLabRequest && $req->password === 'oauth2') {
$req->headers[] = 'Authorization: Bearer ' . $req->username;
// forbid basic-auth
$req->username = $req->password = null;
return;
}
}

// どうしようもない失敗なのか、リトライする余地があるのかを判別する
public function after(HttpGetResponse $res)
private function after(HttpGetResponse $res)
{
if (CURLE_OK !== $res->errno) {
throw new Downloader\TransportException("$res->error:$res->errno");
}

switch ($res->info['http_code']) {
case 200: //OK
return;
case 401: //Unauthorized
case 403: //Forbidden
case 404: //Not Found
$res->setNeedAuth();
break;
case 407: //Proxy Authentication Required
break;
if (in_array($res->info['http_code'], array(401, 403, 404))) {
$res->setNeedAuth();
return;
}
}
}
27 changes: 0 additions & 27 deletions src/Aspects/AspectDegradedMode.php

This file was deleted.

14 changes: 6 additions & 8 deletions src/Aspects/AspectProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ class AspectProxy implements SplObserver
{
public function update(SplSubject $ev)
{
switch ((string)$ev) {
case 'pre-download':
$this->before($ev->refRequest());
return;
if ('pre-download' === (string)$ev) {
$this->before($ev->refRequest());
}
}

public static function before(HttpGetRequest $req)
private static function before(HttpGetRequest $req)
{
// no_proxy skip
if (isset($_SERVER['no_proxy'])) {
$pattern = new NoProxyPattern($_SERVER['no_proxy']);
if ($pattern->test($req->getURL())) {
$req->curlOpts[CURLOPT_PROXY] = null;
unset($req->curlOpts[CURLOPT_PROXY]);
return;
}
}
Expand All @@ -47,8 +45,8 @@ public static function before(HttpGetRequest $req)
return;
}

$req->curlOpts[CURLOPT_PROXY] = null;
$req->curlOpts[CURLOPT_PROXYUSERPWD] = null;
unset($req->curlOpts[CURLOPT_PROXY]);
unset($req->curlOpts[CURLOPT_PROXYUSERPWD]);
}

private static function issetOr(array $arr, $key1, $key2)
Expand Down
8 changes: 3 additions & 5 deletions src/Aspects/AspectRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ class AspectRedirect implements SplObserver
{
public function update(SplSubject $ev)
{
switch ((string)$ev) {
case 'pre-download':
$this->before($ev->refRequest());
break;
if ('pre-download' === (string)$ev) {
$this->before($ev->refRequest());
}
}

public function before(HttpGetRequest $req)
private function before(HttpGetRequest $req)
{
if ('api.github.com' !== $req->host || !$req->maybePublic) {
return;
Expand Down
56 changes: 56 additions & 0 deletions src/Aspects/GitHubRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo\Aspects;

use Composer\IO;
use Composer\Config as CConfig;
use Composer\Util;
use Composer\Downloader;

/**
* Simple Container for http-get request
* GitHub edition
*/
class GitHubRequest extends HttpGetRequest
{
public function processRFSOption(array $options)
{
if (isset($options['github-token'])) {
$this->query['access_token'] = $options['github-token'];
}
}

public function getCurlOpts()
{
$curlOpts = parent::getCurlOpts();
return $curlOpts;
}

public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
{
$httpCode = $res->info['http_code'];
$message = "\nCould not fetch {$this->getURL()}, please create a GitHub OAuth token ";
if (404 === $httpCode) {
$message .= 'to access private repos';
} else {
$message .= 'to go over the API rate limit';
}
$github = new Util\GitHub($io, $config, null);
if ($github->authorizeOAuth($this->origin)) {
return true;
}
if ($io->isInteractive() &&
$github->authorizeOAuthInteractively($this->origin, $message)) {
return true;
}

throw new Downloader\TransportException(
"Could not authenticate against $this->origin",
401
);
}
}
56 changes: 56 additions & 0 deletions src/Aspects/GitLabRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/*
* hirak/prestissimo
* @author Hiraku NAKANO
* @license MIT https://github.com/hirak/prestissimo
*/
namespace Hirak\Prestissimo\Aspects;

use Composer\IO;
use Composer\Config as CConfig;
use Composer\Composer;
use Composer\Downloader;

/**
* Simple Container for http-get request
* GitLab edition
*/
class GitLabRequest extends HttpGetRequest
{
public function processRFSOption(array $options)
{
if (isset($options['gitlab-token'])) {
$this->query['access_token'] = $options['gitlab-token'];
}
}

public function getCurlOpts()
{
$curlOpts = parent::getCurlOpts();
return $curlOpts;
}

public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
{
$httpCode = $res->info['http_code'];
$message = "\nCould not fetch {$this->getURL()}, enter your $this->origin credentials ";
if (401 === $httpCode) {
$message .= 'to access private repos';
} else {
$message .= 'to go over the API rate limit';
}
$gitlab = new Util\GitLab($io, $config, null);
if ($gitlab->authorizeOAuth($this->origin)) {
return true;
}
if ($io->isInteractive() &&
$gitlab->authorizeOAuthInteractively($this->origin, $message)) {
return true;
}

throw new Downloader\TransportException(
"Could not authenticate against $this->origin",
401
);
}
}
59 changes: 40 additions & 19 deletions src/Aspects/HttpGetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use Composer\IO;
use Composer\Composer;
use Composer\Config as CConfig;
use Composer\Downloader;

/**
* Simple Container for http-get request
Expand All @@ -20,8 +22,6 @@ class HttpGetRequest
public $port = 80;
public $path = '/';

public $special = null;

public $query = array();
public $headers = array();

Expand All @@ -37,17 +37,11 @@ class HttpGetRequest
* normalize url and authentication info
* @param string $origin domain text
* @param string $url
* @param IO/IOInterface $io
* @param IO\IOInterface $io
*/
public function __construct($origin, $url, IO\IOInterface $io)
{
// normalize github origin
if (substr($origin, -10) === 'github.com') {
$origin = 'github.com';
$this->special = 'github';
}
$this->origin = $origin;

$this->importURL($url);

if ($this->username && $this->password) {
Expand Down Expand Up @@ -83,7 +77,6 @@ public function importURL($url)
}
}

// utility for __construct

/**
* @param string $key
Expand All @@ -98,6 +91,15 @@ private static function setOr(array $struct, $key, $default=null)
return $default;
}

/**
* process option for RemortFileSystem
* @return void
*/
public function processRFSOption(array $option)
{
// template method
}

public function getCurlOpts()
{
$curlOpts = $this->curlOpts + array(
Expand Down Expand Up @@ -144,18 +146,37 @@ public function getURL()
return $url;
}

/**
* special domain special flag
* @param array $map
*/
public function setSpecial(array $map)
public function promptAuth(HttpGetResponse $res, CConfig $config, IO\IOInterface $io)
{
foreach ($map as $key => $domains) {
if (in_array($this->origin, $domains)) {
$this->special = $key;
return;
$httpCode = $res->info['http_code'];
// 404s are only handled for github
if (404 === $httpCode) {
return false;
}

// fail if the console is not interactive
if (!$io->isInteractive()) {
switch ($httpCode) {
case 401:
$message = "The '{$this->getURL()}' URL required authentication.\nYou must be using the interactive console to authenticate";
break;
case 403:
$message = "The '{$this->getURL()}' URL could not be accessed.";
break;
}
throw new Downloader\TransportException($message, $httpCode);
}

// fail if we already have auth
if ($io->hasAuthentication($this->origin)) {
throw new Downloader\TransportException("Invalid credentials for '{$this->getURL()}', aborting.", $httpCode);
}

$io->overwrite(" Authentication required (<info>$this->host</info>):");
$username = $io->ask(' Username: ');
$password = $io->askAndHideAnswer(' Password: ');
$io->setAuthentication($this->origin, $username, $password);
return true;
}

public static function genUA()
Expand Down
Loading

0 comments on commit c4e4767

Please sign in to comment.