diff --git a/src/Composer/Util/AuthHelper.php b/src/Composer/Util/AuthHelper.php new file mode 100644 index 000000000..4accba716 --- /dev/null +++ b/src/Composer/Util/AuthHelper.php @@ -0,0 +1,63 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Util; + +use Composer\Config; +use Composer\IO\IOInterface; + +/** + * @author Jordi Boggiano + */ +class AuthHelper +{ + protected $io; + protected $config; + + public function __construct(IOInterface $io, Config $config) + { + $this->io = $io; + $this->config = $config; + } + + public function storeAuth($originUrl, $storeAuth) + { + $store = false; + $configSource = $this->config->getAuthConfigSource(); + if ($storeAuth === true) { + $store = $configSource; + } elseif ($storeAuth === 'prompt') { + $answer = $this->io->askAndValidate( + 'Do you want to store credentials for '.$originUrl.' in '.$configSource->getName().' ? [Yn] ', + function ($value) { + $input = strtolower(substr(trim($value), 0, 1)); + if (in_array($input, array('y','n'))) { + return $input; + } + throw new \RuntimeException('Please answer (y)es or (n)o'); + }, + false, + 'y' + ); + + if ($answer === 'y') { + $store = $configSource; + } + } + if ($store) { + $store->addConfigSetting( + 'http-basic.'.$originUrl, + $this->io->getAuthentication($originUrl) + ); + } + } +} diff --git a/src/Composer/Util/Git.php b/src/Composer/Util/Git.php index 296aa548f..3c60157e9 100644 --- a/src/Composer/Util/Git.php +++ b/src/Composer/Util/Git.php @@ -102,28 +102,32 @@ class Git } } } elseif ( // private non-github repo that failed to authenticate - $this->io->isInteractive() && preg_match('{(https?://)([^/]+)(.*)$}i', $url, $match) && strpos($this->process->getErrorOutput(), 'fatal: Authentication failed') !== false ) { - // TODO this should use an auth manager class that prompts and stores in the config + $storeAuth = false; if ($this->io->hasAuthentication($match[2])) { $auth = $this->io->getAuthentication($match[2]); - } else { - $this->io->write($url.' requires Authentication'); + } elseif ($this->io->isInteractive()) { + $this->io->write(' Authentication required ('.parse_url($url, PHP_URL_HOST).'):'); $auth = array( - 'username' => $this->io->ask('Username: '), - 'password' => $this->io->askAndHideAnswer('Password: '), + 'username' => $this->io->ask(' Username: '), + 'password' => $this->io->askAndHideAnswer(' Password: '), ); + $storeAuth = $this->config->get('store-auths'); } - $url = $match[1].rawurlencode($auth['username']).':'.rawurlencode($auth['password']).'@'.$match[2].$match[3]; + if ($auth) { + $url = $match[1].rawurlencode($auth['username']).':'.rawurlencode($auth['password']).'@'.$match[2].$match[3]; - $command = call_user_func($commandCallable, $url); - if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) { - $this->io->setAuthentication($match[2], $auth['username'], $auth['password']); + $command = call_user_func($commandCallable, $url); + if (0 === $this->process->execute($command, $ignoredOutput, $cwd)) { + $this->io->setAuthentication($match[2], $auth['username'], $auth['password']); + $authHelper = new AuthHelper($this->io, $this->config); + $authHelper->storeAuth($match[2], $storeAuth); - return; + return; + } } } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index ee15f9d50..780f437c6 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -253,35 +253,8 @@ class RemoteFilesystem $result = $this->get($this->originUrl, $this->fileUrl, $additionalOptions, $this->fileName, $this->progress); - $store = false; - $configSource = $this->config->getAuthConfigSource(); - if ($this->storeAuth === true) { - $store = $configSource; - } elseif ($this->storeAuth === 'prompt') { - $answer = $this->io->askAndValidate( - 'Do you want to store credentials for '.$this->originUrl.' in '.$configSource->getName().' ? [Yn] ', - function ($value) { - $input = strtolower(substr(trim($value), 0, 1)); - if (in_array($input, array('y','n'))) { - return $input; - } - throw new \RuntimeException('Please answer (y)es or (n)o'); - }, - false, - 'y' - ); - - if ($answer === 'y') { - $store = $configSource; - } - } - if ($store) { - $store->addConfigSetting( - 'http-basic.'.$this->originUrl, - $this->io->getAuthentication($this->originUrl) - ); - } - + $authHelper = new AuthHelper($this->io, $this->config); + $authHelper->storeAuth($this->originUrl, $this->storeAuth); $this->storeAuth = false; return $result;