From cf43244f858d27f9f7854c5113be0d575c44a7bd Mon Sep 17 00:00:00 2001 From: David Zuelke Date: Wed, 6 Apr 2016 11:46:29 +0200 Subject: [PATCH] fix config add/remove/unset/merge for bitbucket --- src/Composer/Command/ConfigCommand.php | 25 ++++++++++-------------- src/Composer/Config.php | 4 ++-- src/Composer/Config/JsonConfigSource.php | 4 ++-- src/Composer/IO/BaseIO.php | 13 ++++++------ 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index b83f599de..363708adf 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -167,7 +167,7 @@ EOT } if ($input->getOption('global') && !$this->authConfigFile->exists()) { touch($this->authConfigFile->getPath()); - $this->authConfigFile->write(array('http-basic' => new \ArrayObject, 'github-oauth' => new \ArrayObject, 'gitlab-oauth' => new \ArrayObject)); + $this->authConfigFile->write(array('bitbucket-oauth' => new \ArrayObject, 'github-oauth' => new \ArrayObject, 'gitlab-oauth' => new \ArrayObject, 'http-basic' => new \ArrayObject)); Silencer::call('chmod', $this->authConfigFile->getPath(), 0600); } @@ -465,8 +465,8 @@ EOT return $this->configSource->addConfigSetting($settingKey, $values[0]); } - // handle github-oauth - if (preg_match('/^(github-oauth|gitlab-oauth|http-basic)\.(.+)/', $settingKey, $matches)) { + // handle auth + if (preg_match('/^(bitbucket-oauth|github-oauth|gitlab-oauth|http-basic)\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); @@ -474,7 +474,13 @@ EOT return; } - if ($matches[1] === 'github-oauth' || $matches[1] === 'gitlab-oauth') { + if ($matches[1] === 'bitbucket-oauth') { + if (2 !== count($values)) { + throw new \RuntimeException('Expected two arguments (consumer-key, consumer-secret), got '.count($values)); + } + $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); + $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('consumer-key' => $values[0], 'consumer-secret' => $values[1])); + } elseif ($matches[1] === 'github-oauth' || $matches[1] === 'gitlab-oauth') { if (1 !== count($values)) { throw new \RuntimeException('Too many arguments, expected only one token'); } @@ -491,17 +497,6 @@ EOT return; } - // handle bitbucket-oauth - if (preg_match('/^(bitbucket-oauth)\.(.+)/', $settingKey, $matches)) { - if (2 !== count($values)) { - throw new \RuntimeException('Expected two arguments (consumer-key, consumer-secret), got '.count($values)); - } - $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); - $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('consumer-key' => $values[0], 'consumer-secret' => $values[1])); - - return; - } - throw new \InvalidArgumentException('Setting '.$settingKey.' does not exist or is not supported by this command'); } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 4c16913cf..6fd535fe2 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -58,10 +58,10 @@ class Config 'archive-format' => 'tar', 'archive-dir' => '.', // valid keys without defaults (auth config stuff): + // bitbucket-oauth // github-oauth // gitlab-oauth // http-basic - // bitbucket-oauth ); public static $defaultRepositories = array( @@ -121,7 +121,7 @@ class Config // override defaults with given config if (!empty($config['config']) && is_array($config['config'])) { foreach ($config['config'] as $key => $val) { - if (in_array($key, array('github-oauth', 'gitlab-oauth', 'http-basic')) && isset($this->config[$key])) { + if (in_array($key, array('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'http-basic')) && isset($this->config[$key])) { $this->config[$key] = array_merge($this->config[$key], $val); } elseif ('preferred-install' === $key && isset($this->config[$key])) { if (is_array($val) || is_array($this->config[$key])) { diff --git a/src/Composer/Config/JsonConfigSource.php b/src/Composer/Config/JsonConfigSource.php index ec777100b..2d9d6e792 100644 --- a/src/Composer/Config/JsonConfigSource.php +++ b/src/Composer/Config/JsonConfigSource.php @@ -81,7 +81,7 @@ class JsonConfigSource implements ConfigSourceInterface { $authConfig = $this->authConfig; $this->manipulateJson('addConfigSetting', $name, $value, function (&$config, $key, $val) use ($authConfig) { - if (preg_match('{^(github-oauth|gitlab-oauth|http-basic|platform|bitbucket-oauth)\.}', $key)) { + if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|http-basic|platform)\.}', $key)) { list($key, $host) = explode('.', $key, 2); if ($authConfig) { $config[$key][$host] = $val; @@ -101,7 +101,7 @@ class JsonConfigSource implements ConfigSourceInterface { $authConfig = $this->authConfig; $this->manipulateJson('removeConfigSetting', $name, function (&$config, $key) use ($authConfig) { - if (preg_match('{^(github-oauth|gitlab-oauth|http-basic|platform)\.}', $key)) { + if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|http-basic|platform)\.}', $key)) { list($key, $host) = explode('.', $key, 2); if ($authConfig) { unset($config[$key][$host]); diff --git a/src/Composer/IO/BaseIO.php b/src/Composer/IO/BaseIO.php index 35cf10c42..9c3f84f71 100644 --- a/src/Composer/IO/BaseIO.php +++ b/src/Composer/IO/BaseIO.php @@ -85,12 +85,17 @@ abstract class BaseIO implements IOInterface */ public function loadConfiguration(Config $config) { + $bitbucketOauth = $config->get('bitbucket-oauth') ?: array(); $githubOauth = $config->get('github-oauth') ?: array(); $gitlabOauth = $config->get('gitlab-oauth') ?: array(); $httpBasic = $config->get('http-basic') ?: array(); - $bitbucketOauth = $config->get('bitbucket-oauth') ?: array(); - // reload oauth token from config if available + // reload oauth tokens from config if available + + foreach ($bitbucketOauth as $domain => $cred) { + $this->checkAndSetAuthentication($domain, $cred['consumer-key'], $cred['consumer-secret']); + } + foreach ($githubOauth as $domain => $token) { if (!preg_match('{^[a-z0-9]+$}', $token)) { throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"'); @@ -107,10 +112,6 @@ abstract class BaseIO implements IOInterface $this->checkAndSetAuthentication($domain, $cred['username'], $cred['password']); } - foreach ($bitbucketOauth as $domain => $cred) { - $this->checkAndSetAuthentication($domain, $cred['consumer-key'], $cred['consumer-secret']); - } - // setup process timeout ProcessExecutor::setTimeout((int) $config->get('process-timeout')); }