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 1/4] 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. * From dcdcf57f3f6205ffd1895affcffe6af9acfd9252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Mon, 4 Mar 2013 22:14:00 +0100 Subject: [PATCH 2/4] Moved 401 handling from ComposerRepository to RemoteFilesystem and displaying the url when asking for the credentials. --- .../Repository/ComposerRepository.php | 13 ---------- src/Composer/Util/RemoteFilesystem.php | 24 +++++++++---------- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index cc01f2b77..323848061 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -17,7 +17,6 @@ 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; @@ -499,18 +498,6 @@ 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 7411f6787..db41bdcfa 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -44,18 +44,6 @@ 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. * @@ -137,6 +125,18 @@ class RemoteFilesystem if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } + + // 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 resource at '.$originUrl); + $username = $this->io->ask('Username: '); + $password = $this->io->askAndHideAnswer('Password: '); + $this->io->setAuthentication($originUrl, $username, $password); + + // try getting the file again + return $this->get($originUrl, $fileUrl, $additionalOptions, $fileName, $progress); + } } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; From 906563451ea14212584842e72c3bf1f02f13fa82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Tue, 5 Mar 2013 13:34:48 +0100 Subject: [PATCH 3/4] Reverted the last changes. Changed logic in callbackGet() method to respect the 401 handling also if STREAM_NOTIFY_FAILURE fires as on my local machine the handling of STREAM_NOTIFY_AUTH_REQUIRED never got executed. --- src/Composer/Util/RemoteFilesystem.php | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index db41bdcfa..d1161a3db 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -125,18 +125,6 @@ class RemoteFilesystem if ($e instanceof TransportException && !empty($http_response_header[0])) { $e->setHeaders($http_response_header); } - - // 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 resource at '.$originUrl); - $username = $this->io->ask('Username: '); - $password = $this->io->askAndHideAnswer('Password: '); - $this->io->setAuthentication($originUrl, $username, $password); - - // try getting the file again - return $this->get($originUrl, $fileUrl, $additionalOptions, $fileName, $progress); - } } if ($errorMessage && !ini_get('allow_url_fopen')) { $errorMessage = 'allow_url_fopen must be enabled in php.ini ('.$errorMessage.')'; @@ -223,9 +211,6 @@ class RemoteFilesystem { switch ($notificationCode) { case STREAM_NOTIFY_FAILURE: - throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode); - break; - case STREAM_NOTIFY_AUTH_REQUIRED: if (401 === $messageCode) { if (!$this->io->isInteractive()) { @@ -240,7 +225,10 @@ class RemoteFilesystem $this->io->setAuthentication($this->originUrl, $username, $password); $this->get($this->originUrl, $this->fileUrl, $this->fileName, $this->progress); + break; } + + throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode); break; case STREAM_NOTIFY_AUTH_RESULT: From df897b42c2e30cafa6f92001fa586e7d440b5df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20Hochd=C3=B6rfer?= Date: Wed, 20 Mar 2013 08:44:36 +0100 Subject: [PATCH 4/4] Removed dead statement. --- src/Composer/Util/RemoteFilesystem.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index d1161a3db..f1c48060e 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -229,7 +229,6 @@ class RemoteFilesystem } throw new TransportException('The "'.$this->fileUrl.'" file could not be downloaded ('.trim($message).')', $messageCode); - break; case STREAM_NOTIFY_AUTH_RESULT: if (403 === $messageCode) {