Add auth helper and reuse it in git downloader
parent
0ce0cf42e8
commit
4ebc5c9a08
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Composer.
|
||||
*
|
||||
* (c) Nils Adermann <naderman@naderman.de>
|
||||
* Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* 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 <j.boggiano@seld.be>
|
||||
*/
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 (<info>'.parse_url($url, PHP_URL_HOST).'</info>):');
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue