From 9059d70ba07ecad40011a056c985cc96354537f3 Mon Sep 17 00:00:00 2001 From: Paul Wenke Date: Sun, 20 Mar 2016 15:56:58 -0400 Subject: [PATCH] Corrected username / access token parameters for $this->io->setAuthentication when read from git config. Grant type is now only set when requesting an access token. Removed bitbucket-domains and bitbucket-protocols from config. Fixed bitbucket typo in JsonConfigSource. Removed unecessary comments. Changed visibility of Composer/Util/Bitbucket properties to private. Added https to bitbucket url. Removed unused $note variable. --- src/Composer/Config.php | 2 -- src/Composer/Config/JsonConfigSource.php | 2 +- src/Composer/Util/Bitbucket.php | 31 +++++++++++----------- src/Composer/Util/Git.php | 9 ++----- src/Composer/Util/RemoteFilesystem.php | 5 ---- tests/Composer/Test/Util/BitbucketTest.php | 1 - 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 982f70e62..8bf2e7a5a 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -27,7 +27,6 @@ class Config 'preferred-install' => 'auto', 'notify-on-install' => true, 'github-protocols' => array('https', 'ssh', 'git'), - 'bitbucket-protocols' => array('https'), 'vendor-dir' => 'vendor', 'bin-dir' => '{$vendor-dir}/bin', 'cache-dir' => '{$home}/cache', @@ -46,7 +45,6 @@ class Config 'classmap-authoritative' => false, 'prepend-autoloader' => true, 'github-domains' => array('github.com'), - 'bitbucket-domains' => array('bitbucket.org'), 'bitbucket-expose-hostname' => true, 'disable-tls' => false, 'secure-http' => true, diff --git a/src/Composer/Config/JsonConfigSource.php b/src/Composer/Config/JsonConfigSource.php index 9da47d0c3..ec777100b 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)\.}', $key)) { + if (preg_match('{^(github-oauth|gitlab-oauth|http-basic|platform|bitbucket-oauth)\.}', $key)) { list($key, $host) = explode('.', $key, 2); if ($authConfig) { $config[$key][$host] = $val; diff --git a/src/Composer/Util/Bitbucket.php b/src/Composer/Util/Bitbucket.php index 24ba98f98..8246b3cb1 100644 --- a/src/Composer/Util/Bitbucket.php +++ b/src/Composer/Util/Bitbucket.php @@ -18,16 +18,15 @@ use Composer\Config; use Composer\Downloader\TransportException; /** - * I used GitHub.php as a template. * @author Paul Wenke */ class Bitbucket { - protected $io; - protected $config; - protected $process; - protected $remoteFilesystem; - protected $token = array(); + private $io; + private $config; + private $process; + private $remoteFilesystem; + private $token = array(); /** * Constructor. @@ -61,13 +60,13 @@ class Bitbucket */ public function authorizeOAuth($originUrl) { - if (!in_array($originUrl, $this->config->get('bitbucket-domains'))) { + if ($originUrl !== 'bitbucket.org') { return false; } // if available use token from git config if (0 === $this->process->execute('git config bitbucket.accesstoken', $output)) { - $this->io->setAuthentication($originUrl, trim($output), 'x-oauth-basic'); + $this->io->setAuthentication($originUrl, 'x-token-auth', trim($output)); return true; } @@ -82,10 +81,16 @@ class Bitbucket private function requestAccessToken($originUrl) { try { - $apiUrl = 'bitbucket.org/site/oauth2/access_token'; + $apiUrl = 'https://bitbucket.org/site/oauth2/access_token'; - $json = $this->remoteFilesystem->getContents($originUrl, 'https://'.$apiUrl, false, array( + $json = $this->remoteFilesystem->getContents($originUrl, $apiUrl, false, array( 'retry-auth-failure' => false, + 'http' => array( + 'method' => 'POST', + 'content' => array( + 'grant_type' => 'client_credentials' + ) + ) )); $this->token = json_decode($json, true); @@ -116,12 +121,6 @@ class Bitbucket $this->io->writeError($message); } - $note = 'Composer'; - if ($this->config->get('bitbucket-expose-hostname') === true && 0 === $this->process->execute('hostname', $output)) { - $note .= ' on ' . trim($output); - } - $note .= ' ' . date('Y-m-d Hi'); - $url = 'https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html'; $this->io->writeError(sprintf('Follow the instructions on %s', $url)); $this->io->writeError(sprintf('to create a consumer. It will be stored in "%s" for future use by Composer.', $this->config->getAuthConfigSource()->getName())); diff --git a/src/Composer/Util/Git.php b/src/Composer/Util/Git.php index b839bdc56..818a01aa5 100644 --- a/src/Composer/Util/Git.php +++ b/src/Composer/Util/Git.php @@ -114,11 +114,11 @@ class Git return; } } - } elseif (preg_match('{^(?:https?|git)://'.self::getBitbucketDomainsRegex($this->config).'/(.*)}', $url, $match)) { //bitbucket oauth + } elseif (preg_match('{^https://(bitbucket.org)/(.*)}', $url, $match)) { //bitbucket oauth $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process); if (!$this->io->hasAuthentication($match[1])) { - $message = 'Cloning failed using an ssh key for authentication, enter your Bitbucket credentials to access private repos'; + $message = 'Enter your Bitbucket credentials to access private repos'; if (!$bitbucketUtil->authorizeOAuth($match[1]) && $this->io->isInteractive()) { $bitbucketUtil->authorizeOAuthInteractively($match[1], $message); @@ -244,11 +244,6 @@ class Git return '('.implode('|', array_map('preg_quote', $config->get('github-domains'))).')'; } - public static function getBitbucketDomainsRegex(Config $config) - { - return '('.implode('|', array_map('preg_quote', $config->get('bitbucket-domains'))).')'; - } - public static function sanitizeUrl($message) { return preg_replace('{://([^@]+?):.+?@}', '://$1:***@', $message); diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index cfd25aaf5..7d37b5df3 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/src/Composer/Util/RemoteFilesystem.php @@ -672,11 +672,6 @@ class RemoteFilesystem $authStr = base64_encode($auth['username'] . ':' . $auth['password']); $headers[] = 'Authorization: Basic '.$authStr; } - - if ('bitbucket.org' === $originUrl) { - $options['http']['method'] = 'POST'; - $options['http']['content']['grant_type'] = 'client_credentials'; - } } if (isset($options['http']['header']) && !is_array($options['http']['header'])) { diff --git a/tests/Composer/Test/Util/BitbucketTest.php b/tests/Composer/Test/Util/BitbucketTest.php index 6decb5f74..5be6da7b0 100644 --- a/tests/Composer/Test/Util/BitbucketTest.php +++ b/tests/Composer/Test/Util/BitbucketTest.php @@ -15,7 +15,6 @@ namespace Composer\Test\Util; use Composer\Util\Bitbucket; /** - * I used GitHubTest.php as a template. * @author Paul Wenke */ class BitbucketTest extends \PHPUnit_Framework_TestCase