From e9cac53f907aa80396e8dca6c25e0bd6b494bc4e Mon Sep 17 00:00:00 2001 From: Oliver Vartiainen Date: Tue, 27 Oct 2015 19:47:30 +0200 Subject: [PATCH 1/3] Allow fetching auth credentials from an envvar When an environmental variable named "COMPOSER_AUTH" is set as $USERNAME:$PASSWORD, it is automatically used for authentication e.g. when fetching packages from Satis. The envvar credentials are of lower priority than URL credentials. Fixes #4285 --- src/Composer/Util/RemoteFilesystem.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 4754e304b..7c0e3e76c 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -207,6 +207,16 @@ class RemoteFilesystem $this->retryAuthFailure = true; $this->lastHeaders = array(); + // Use COMPOSER_AUTH environment variable if set + if (getenv('COMPOSER_AUTH')) { + $credentials = []; + preg_match('/(.+):(.+)/', getenv('COMPOSER_AUTH'), $credentials); + + if (count($credentials) === 2) { + $this->io->setAuthentication($originUrl, $credentials[0], $credentials[1]); + } + } + // capture username/password from URL if there is one if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2])); From aaee6dc0b08bbe800fab0be2b91fc5113c4a995d Mon Sep 17 00:00:00 2001 From: Oliver Vartiainen Date: Tue, 27 Oct 2015 20:44:10 +0200 Subject: [PATCH 2/3] Simplify envvar credential parsing --- src/Composer/Util/RemoteFilesystem.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 7c0e3e76c..738bd36c4 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -209,8 +209,7 @@ class RemoteFilesystem // Use COMPOSER_AUTH environment variable if set if (getenv('COMPOSER_AUTH')) { - $credentials = []; - preg_match('/(.+):(.+)/', getenv('COMPOSER_AUTH'), $credentials); + $credentials = explode(':', getenv('COMPOSER_AUTH'), 2); if (count($credentials) === 2) { $this->io->setAuthentication($originUrl, $credentials[0], $credentials[1]); From b39b113fc3b4376d5c6519a18143f45d8c367086 Mon Sep 17 00:00:00 2001 From: Oliver Vartiainen Date: Tue, 19 Jan 2016 20:34:04 +0200 Subject: [PATCH 3/3] Handle envvar auth credentials as a JSON blob As well as move the handling to a proper place --- src/Composer/IO/BaseIO.php | 13 +++++++++++++ src/Composer/Util/RemoteFilesystem.php | 9 --------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Composer/IO/BaseIO.php b/src/Composer/IO/BaseIO.php index 139e58723..43248c92c 100644 --- a/src/Composer/IO/BaseIO.php +++ b/src/Composer/IO/BaseIO.php @@ -60,6 +60,19 @@ abstract class BaseIO implements IOInterface */ public function loadConfiguration(Config $config) { + // Use COMPOSER_AUTH environment variable if set + if ($envvar_data = getenv('COMPOSER_AUTH')) { + $auth_data = json_decode($envvar_data); + + if (is_null($auth_data)) { + throw new \UnexpectedValueException('COMPOSER_AUTH environment variable is malformed'); + } + + foreach ($auth_data as $domain => $credentials) { + $this->setAuthentication($domain, $credentials->username, $credentials->password); + } + } + // reload oauth token from config if available if ($tokens = $config->get('github-oauth')) { foreach ($tokens as $domain => $token) { diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 738bd36c4..4754e304b 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -207,15 +207,6 @@ class RemoteFilesystem $this->retryAuthFailure = true; $this->lastHeaders = array(); - // Use COMPOSER_AUTH environment variable if set - if (getenv('COMPOSER_AUTH')) { - $credentials = explode(':', getenv('COMPOSER_AUTH'), 2); - - if (count($credentials) === 2) { - $this->io->setAuthentication($originUrl, $credentials[0], $credentials[1]); - } - } - // capture username/password from URL if there is one if (preg_match('{^https?://(.+):(.+)@([^/]+)}i', $fileUrl, $match)) { $this->io->setAuthentication($originUrl, urldecode($match[1]), urldecode($match[2]));