From f2afbbac2f9ce9e3b29a8eb2eb00cae53c987c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Mon, 4 Mar 2013 12:49:14 -0500 Subject: [PATCH] Extended the fetchFile() method of the ComposerRepository class to be able to deal with http basic auth. In case the remote resource responds with a 401 ask the user for access credentials and retry quering the resource again. --- src/Composer/Repository/ComposerRepository.php | 13 +++++++++++++ src/Composer/Util/RemoteFilesystem.php | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 323848061..cc01f2b77 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -17,6 +17,7 @@ use Composer\Package\PackageInterface; use Composer\Package\AliasPackage; use Composer\Package\Version\VersionParser; use Composer\DependencyResolver\Pool; +use Composer\Downloader\TransportException; use Composer\Json\JsonFile; use Composer\Cache; use Composer\Config; @@ -498,6 +499,18 @@ class ComposerRepository extends ArrayRepository implements StreamableRepository continue; } + // in case the remote filesystem responds with an 401 error ask for credentials + if($e instanceof TransportException && ($e->getCode() == 401)) + { + $this->io->write('Enter the access credentials needed to access the repository'); + $username = $this->io->ask('Username: '); + $password = $this->io->askAndHideAnswer('Password: '); + $this->rfs->setAuthentication($filename, $username, $password); + + // try fetching the file again + return $this->fetchFile($filename, $cacheKey, $sha256); + } + if ($e instanceof RepositorySecurityException) { throw $e; } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 79efd3298..7411f6787 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -44,6 +44,18 @@ class RemoteFilesystem $this->options = $options; } + /** + * Set the authentication information for the repository. + * + * @param string $originUrl The origin URL + * @param string $username The username + * @param string $password The password + */ + public function setAuthentication($originUrl, $username, $password = null) + { + return $this->io->setAuthentication($originUrl, $username, $password); + } + /** * Copy the remote file in local. *