Browse Source

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.

Stephan Hochdörfer 12 years ago
parent
commit
f2afbbac2f

+ 13 - 0
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;
                 }

+ 12 - 0
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.
      *