From 1d15910fa6350f3d9509e7e9d72672880c90657a Mon Sep 17 00:00:00 2001 From: Stephan Hochdoerfer Date: Mon, 26 May 2014 13:02:34 +0200 Subject: [PATCH] Will read configured http basic auth credentials from users auth.json file and pass the credentials to the configured IOInterface. --- src/Composer/Factory.php | 63 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index b1bdd9599..3ab683780 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -36,14 +36,11 @@ use Composer\Package\Version\VersionParser; class Factory { /** - * @throws \RuntimeException - * @return Config + * @return string */ - public static function createConfig() + protected static function getHomeDir() { - // determine home and cache dirs $home = getenv('COMPOSER_HOME'); - $cacheDir = getenv('COMPOSER_CACHE_DIR'); if (!$home) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if (!getenv('APPDATA')) { @@ -57,6 +54,16 @@ class Factory $home = rtrim(getenv('HOME'), '/') . '/.composer'; } } + + return $home; + } + + /** + * @return string + */ + protected static function getCacheDir($home) + { + $cacheDir = getenv('COMPOSER_CACHE_DIR'); if (!$cacheDir) { if (defined('PHP_WINDOWS_VERSION_MAJOR')) { if ($cacheDir = getenv('LOCALAPPDATA')) { @@ -70,6 +77,18 @@ class Factory } } + return $cacheDir; + } + + /** + * @return Config + */ + public static function createConfig() + { + // determine home and cache dirs + $home = self::getHomeDir(); + $cacheDir = self::getCacheDir($home); + // Protect directory against web access. Since HOME could be // the www-data's user home and be web-accessible it is a // potential security risk @@ -128,6 +147,26 @@ class Factory return $config; } + /** + * @return Config + */ + protected static function createAuthConfig() + { + $home = self::getHomeDir(); + + $config = new Config(); + // add dirs to the config + $config->merge(array('config' => array('home' => $home))); + + $file = new JsonFile($home.'/auth.json'); + if ($file->exists()) { + $config->merge($file->read()); + } + $config->setConfigSource(new JsonConfigSource($file)); + + return $config; + } + public static function getComposerFile() { return trim(getenv('COMPOSER')) ?: './composer.json'; @@ -214,6 +253,20 @@ class Factory $config->merge($localConfig); $io->loadConfiguration($config); + // load separate auth config + $authConfig = static::createAuthConfig(); + if ($basicauth = $authConfig->get('basic-auth')) { + foreach ($basicauth as $domain => $credentials) { + if(!isset($credentials['username'])) { + continue; + } + if(!isset($credentials['password'])) { + $credentials['password'] = null; + } + $io->setAuthentication($domain, $credentials['username'], $credentials['password']); + } + } + $vendorDir = $config->get('vendor-dir'); $binDir = $config->get('bin-dir');