From f964b8301837a49ee1a4cf77dbfb38d0c2c2a340 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 10 Mar 2020 13:39:22 +0100 Subject: [PATCH] Add bearer support in config command and add to docs/schema, refs #8671 --- doc/06-config.md | 18 ++++++++++++------ res/composer-schema.json | 5 +++++ src/Composer/Command/ConfigCommand.php | 6 +++--- src/Composer/Config.php | 3 ++- src/Composer/Config/JsonConfigSource.php | 4 ++-- 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/doc/06-config.md b/doc/06-config.md index 0f110a428..603de014a 100644 --- a/doc/06-config.md +++ b/doc/06-config.md @@ -79,16 +79,16 @@ an OAuth token for GitHub. A list of domain names and oauth keys. For example using `{"gitlab.com": "oauthtoken"}` as the value of this option will use `oauthtoken` to access -private repositories on gitlab. Please note: If the package is not hosted at -gitlab.com the domain names must be also specified with the +private repositories on gitlab. Please note: If the package is not hosted at +gitlab.com the domain names must be also specified with the [`gitlab-domains`](06-config.md#gitlab-domains) option. ## gitlab-token A list of domain names and private tokens. For example using `{"gitlab.com": "privatetoken"}` as the value of this option will use `privatetoken` to access -private repositories on gitlab. Please note: If the package is not hosted at -gitlab.com the domain names must be also specified with the +private repositories on gitlab. Please note: If the package is not hosted at +gitlab.com the domain names must be also specified with the [`gitlab-domains`](06-config.md#gitlab-domains) option. ## disable-tls @@ -129,11 +129,17 @@ A list of domain names and username/passwords to authenticate against them. For example using `{"example.org": {"username": "alice", "password": "foo"}}` as the value of this option will let Composer authenticate against example.org. -> **Note:** Authentication-related config options like `http-basic` and +> **Note:** Authentication-related config options like `http-basic`, `bearer` and > `github-oauth` can also be specified inside a `auth.json` file that goes > besides your `composer.json`. That way you can gitignore it and every > developer can place their own credentials in there. +## bearer + +A list of domain names and tokens to authenticate against them. For example using +`{"example.org": "foo"}` as the value of this option will let Composer authenticate +against example.org using an `Authorization: Bearer foo` header. + ## platform Lets you fake platform packages (PHP and extensions) so that you can emulate a @@ -298,7 +304,7 @@ in the composer home, cache, and data directories. ## lock -Defaults to `true`. If set to `false`, Composer will not create a `composer.lock` +Defaults to `true`. If set to `false`, Composer will not create a `composer.lock` file. ← [Repositories](05-repositories.md) | [Community](07-community.md) → diff --git a/res/composer-schema.json b/res/composer-schema.json index a74819baa..d238c265e 100644 --- a/res/composer-schema.json +++ b/res/composer-schema.json @@ -142,6 +142,11 @@ "description": "A hash of domain name => gitlab private tokens, typically {\"gitlab.com\":\"\"}.", "additionalProperties": true }, + "bearer": { + "type": "object", + "description": "A hash of domain name => bearer authentication token, for example {\"example.com\":\"\"}.", + "additionalProperties": true + }, "disable-tls": { "type": "boolean", "description": "Defaults to `false`. If set to true all HTTPS URLs will be tried with HTTP instead and no network level encryption is performed. Enabling this is a security risk and is NOT recommended. The better way is to enable the php_openssl extension in php.ini." diff --git a/src/Composer/Command/ConfigCommand.php b/src/Composer/Command/ConfigCommand.php index 7f79f3251..be1743ba0 100644 --- a/src/Composer/Command/ConfigCommand.php +++ b/src/Composer/Command/ConfigCommand.php @@ -187,7 +187,7 @@ EOT } if ($input->getOption('global') && !$this->authConfigFile->exists()) { touch($this->authConfigFile->getPath()); - $this->authConfigFile->write(array('bitbucket-oauth' => new \ArrayObject, 'github-oauth' => new \ArrayObject, 'gitlab-oauth' => new \ArrayObject, 'gitlab-token' => new \ArrayObject, 'http-basic' => new \ArrayObject)); + $this->authConfigFile->write(array('bitbucket-oauth' => new \ArrayObject, 'github-oauth' => new \ArrayObject, 'gitlab-oauth' => new \ArrayObject, 'gitlab-token' => new \ArrayObject, 'http-basic' => new \ArrayObject, 'bearer' => new \ArrayObject)); Silencer::call('chmod', $this->authConfigFile->getPath(), 0600); } @@ -667,7 +667,7 @@ EOT } // handle auth - if (preg_match('/^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic)\.(.+)/', $settingKey, $matches)) { + if (preg_match('/^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic|bearer)\.(.+)/', $settingKey, $matches)) { if ($input->getOption('unset')) { $this->authConfigSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); @@ -681,7 +681,7 @@ EOT } $this->configSource->removeConfigSetting($matches[1].'.'.$matches[2]); $this->authConfigSource->addConfigSetting($matches[1].'.'.$matches[2], array('consumer-key' => $values[0], 'consumer-secret' => $values[1])); - } elseif (in_array($matches[1], array('github-oauth', 'gitlab-oauth', 'gitlab-token'), true)) { + } elseif (in_array($matches[1], array('github-oauth', 'gitlab-oauth', 'gitlab-token', 'bearer'), true)) { if (1 !== count($values)) { throw new \RuntimeException('Too many arguments, expected only one token'); } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 1050096b1..e57d21cab 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -70,6 +70,7 @@ class Config // gitlab-oauth // gitlab-token // http-basic + // bearer ); public static $defaultRepositories = array( @@ -133,7 +134,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('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic')) && isset($this->config[$key])) { + if (in_array($key, array('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic', 'bearer')) && 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 f455845a4..6084d66e8 100644 --- a/src/Composer/Config/JsonConfigSource.php +++ b/src/Composer/Config/JsonConfigSource.php @@ -96,7 +96,7 @@ class JsonConfigSource implements ConfigSourceInterface { $authConfig = $this->authConfig; $this->manipulateJson('addConfigSetting', $name, $value, function (&$config, $key, $val) use ($authConfig) { - if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic|platform)\.}', $key)) { + if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|bearer|http-basic|platform)\.}', $key)) { list($key, $host) = explode('.', $key, 2); if ($authConfig) { $config[$key][$host] = $val; @@ -116,7 +116,7 @@ class JsonConfigSource implements ConfigSourceInterface { $authConfig = $this->authConfig; $this->manipulateJson('removeConfigSetting', $name, function (&$config, $key) use ($authConfig) { - if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|http-basic|platform)\.}', $key)) { + if (preg_match('{^(bitbucket-oauth|github-oauth|gitlab-oauth|gitlab-token|bearer|http-basic|platform)\.}', $key)) { list($key, $host) = explode('.', $key, 2); if ($authConfig) { unset($config[$key][$host]);