1
0
Fork 0

Merge remote-tracking branch 'benja-M-1/feature/svn-auth-from-auth-json'

pull/2995/merge
Jordi Boggiano 2014-06-05 11:08:19 +02:00
commit 89ec322929
5 changed files with 91 additions and 25 deletions

View File

@ -84,7 +84,7 @@ class SvnDownloader extends VcsDownloader
*/ */
protected function execute($baseUrl, $command, $url, $cwd = null, $path = null) protected function execute($baseUrl, $command, $url, $cwd = null, $path = null)
{ {
$util = new SvnUtil($baseUrl, $this->io); $util = new SvnUtil($baseUrl, $this->io, $this->config);
try { try {
return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose()); return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {

View File

@ -306,7 +306,7 @@ class SvnDriver extends VcsDriver
protected function execute($command, $url) protected function execute($command, $url)
{ {
if (null === $this->util) { if (null === $this->util) {
$this->util = new SvnUtil($this->baseUrl, $this->io, $this->process); $this->util = new SvnUtil($this->baseUrl, $this->io, $this->config, $this->process);
} }
try { try {

View File

@ -12,6 +12,7 @@
namespace Composer\Util; namespace Composer\Util;
use Composer\Config;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
/** /**
@ -57,15 +58,21 @@ class Svn
*/ */
protected $qtyAuthTries = 0; protected $qtyAuthTries = 0;
/**
* @var \Composer\Config
*/
protected $config;
/** /**
* @param string $url * @param string $url
* @param \Composer\IO\IOInterface $io * @param \Composer\IO\IOInterface $io
* @param ProcessExecutor $process * @param ProcessExecutor $process
*/ */
public function __construct($url, IOInterface $io, ProcessExecutor $process = null) public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null)
{ {
$this->url = $url; $this->url = $url;
$this->io = $io; $this->io = $io;
$this->config = $config;
$this->process = $process ?: new ProcessExecutor; $this->process = $process ?: new ProcessExecutor;
} }
@ -123,16 +130,12 @@ class Svn
throw new \RuntimeException($output); throw new \RuntimeException($output);
} }
// no auth supported for non interactive calls if (!$this->hasAuth()) {
if (!$this->io->isInteractive()) { $this->doAuthDance();
throw new \RuntimeException(
'can not ask for authentication in non interactive mode ('.$output.')'
);
} }
// try to authenticate if maximum quantity of tries not reached // try to authenticate if maximum quantity of tries not reached
if ($this->qtyAuthTries++ < self::MAX_QTY_AUTH_TRIES || !$this->hasAuth()) { if ($this->qtyAuthTries++ < self::MAX_QTY_AUTH_TRIES) {
$this->doAuthDance();
// restart the process // restart the process
return $this->execute($command, $url, $cwd, $path, $verbose); return $this->execute($command, $url, $cwd, $path, $verbose);
@ -147,9 +150,17 @@ class Svn
* Repositories requests credentials, let's put them in. * Repositories requests credentials, let's put them in.
* *
* @return \Composer\Util\Svn * @return \Composer\Util\Svn
* @throws \RuntimeException
*/ */
protected function doAuthDance() protected function doAuthDance()
{ {
// cannot ask for credentials in non interactive mode
if (!$this->io->isInteractive()) {
throw new \RuntimeException(
'can not ask for authentication in non interactive mode'
);
}
$this->io->write("The Subversion server ({$this->url}) requested credentials:"); $this->io->write("The Subversion server ({$this->url}) requested credentials:");
$this->hasAuth = true; $this->hasAuth = true;
@ -248,6 +259,53 @@ class Svn
return $this->hasAuth; return $this->hasAuth;
} }
if (false === $this->createAuthFromConfig()) {
$this->createAuthFromUrl();
}
return $this->hasAuth;
}
/**
* Return the no-auth-cache switch.
*
* @return string
*/
protected function getAuthCache()
{
return $this->cacheCredentials ? '' : '--no-auth-cache ';
}
/**
* Create the auth params from the configuration file.
*
* @return bool
*/
private function createAuthFromConfig()
{
if (!$this->config->has('http-basic')) {
return $this->hasAuth = false;
}
$authConfig = $this->config->get('http-basic');
if (array_key_exists($this->url, $authConfig)) {
$this->credentials['username'] = $authConfig[$this->url]['username'];
$this->credentials['password'] = $authConfig[$this->url]['password'];
return $this->hasAuth = true;
}
return $this->hasAuth = false;
}
/**
* Create the auth params from the url
*
* @return bool
*/
private function createAuthFromUrl()
{
$uri = parse_url($this->url); $uri = parse_url($this->url);
if (empty($uri['user'])) { if (empty($uri['user'])) {
return $this->hasAuth = false; return $this->hasAuth = false;
@ -260,14 +318,4 @@ class Svn
return $this->hasAuth = true; return $this->hasAuth = true;
} }
/**
* Return the no-auth-cache switch.
*
* @return string
*/
protected function getAuthCache()
{
return $this->cacheCredentials ? '' : '--no-auth-cache ';
}
} }

View File

@ -23,9 +23,6 @@ class SvnDriverTest extends \PHPUnit_Framework_TestCase
public function testWrongCredentialsInUrl() public function testWrongCredentialsInUrl()
{ {
$console = $this->getMock('Composer\IO\IOInterface'); $console = $this->getMock('Composer\IO\IOInterface');
$console->expects($this->exactly(6))
->method('isInteractive')
->will($this->returnValue(true));
$output = "svn: OPTIONS of 'http://corp.svn.local/repo':"; $output = "svn: OPTIONS of 'http://corp.svn.local/repo':";
$output .= " authorization failed: Could not authenticate to server:"; $output .= " authorization failed: Could not authenticate to server:";

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Composer\Test\Util; namespace Composer\Test\Util;
use Composer\Config;
use Composer\IO\NullIO; use Composer\IO\NullIO;
use Composer\Util\Svn; use Composer\Util\Svn;
@ -16,7 +17,7 @@ class SvnTest extends \PHPUnit_Framework_TestCase
*/ */
public function testCredentials($url, $expect) public function testCredentials($url, $expect)
{ {
$svn = new Svn($url, new NullIO); $svn = new Svn($url, new NullIO, new Config());
$reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString'); $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
$reflMethod->setAccessible(true); $reflMethod->setAccessible(true);
@ -41,7 +42,7 @@ class SvnTest extends \PHPUnit_Framework_TestCase
{ {
$url = 'http://svn.example.org'; $url = 'http://svn.example.org';
$svn = new Svn($url, new NullIO()); $svn = new Svn($url, new NullIO(), new Config());
$reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCommand'); $reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCommand');
$reflMethod->setAccessible(true); $reflMethod->setAccessible(true);
@ -51,6 +52,26 @@ class SvnTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testCredentialsFromConfig()
{
$url = 'http://svn.apache.org';
$config = new Config();
$config->merge(array(
'config' => array(
'http-basic' => array(
$url => array('username' => 'foo', 'password' => 'bar')
)
)
));
$svn = new Svn($url, new NullIO, $config);
$reflMethod = new \ReflectionMethod('Composer\\Util\\Svn', 'getCredentialString');
$reflMethod->setAccessible(true);
$this->assertEquals($this->getCmd(" --username 'foo' --password 'bar' "), $reflMethod->invoke($svn));
}
private function getCmd($cmd) private function getCmd($cmd)
{ {
if (defined('PHP_WINDOWS_VERSION_BUILD')) { if (defined('PHP_WINDOWS_VERSION_BUILD')) {