1
0
Fork 0

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.

pull/5055/head
Paul Wenke 2016-03-20 15:56:58 -04:00
parent b4d9d0fd0d
commit 9059d70ba0
6 changed files with 18 additions and 32 deletions

View File

@ -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,

View File

@ -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;

View File

@ -18,16 +18,15 @@ use Composer\Config;
use Composer\Downloader\TransportException;
/**
* I used GitHub.php as a template.
* @author Paul Wenke <wenke.paul@gmail.com>
*/
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()));

View File

@ -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);

View File

@ -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'])) {

View File

@ -15,7 +15,6 @@ namespace Composer\Test\Util;
use Composer\Util\Bitbucket;
/**
* I used GitHubTest.php as a template.
* @author Paul Wenke <wenke.paul@gmail.com>
*/
class BitbucketTest extends \PHPUnit_Framework_TestCase