1
0
Fork 0

Let users configure *any auth* via COMPOSER_AUTH and add it to the docs, refs #4546

pull/4840/head
Jordi Boggiano 2016-01-26 19:09:44 +00:00
parent e513f27674
commit 593b88e414
2 changed files with 29 additions and 12 deletions

View File

@ -717,6 +717,13 @@ commands) to finish executing. The default value is 300 seconds (5 minutes).
By setting this environmental value, you can set a path to a certificate bundle By setting this environmental value, you can set a path to a certificate bundle
file to be used during SSL/TLS peer verification. file to be used during SSL/TLS peer verification.
### COMPOSER_AUTH
The `COMPOSER_AUTH` var allows you to set up authentication as an environment variable.
The contents of the variable should be a JSON formatted object containing http-basic,
github-oauth, ... objects as needed, and following the
[spec from the config](06-config.md#gitlab-oauth).
### COMPOSER_DISCARD_CHANGES ### COMPOSER_DISCARD_CHANGES
This env var controls the [`discard-changes`](06-config.md#discard-changes) config option. This env var controls the [`discard-changes`](06-config.md#discard-changes) config option.

View File

@ -60,22 +60,32 @@ abstract class BaseIO implements IOInterface
*/ */
public function loadConfiguration(Config $config) public function loadConfiguration(Config $config)
{ {
// Use COMPOSER_AUTH environment variable if set $githubOauth = $config->get('github-oauth');
if ($envvar_data = getenv('COMPOSER_AUTH')) { $gitlabOauth = $config->get('gitlab-oauth');
$auth_data = json_decode($envvar_data); $httpBasic = $config->get('http-basic');
if (is_null($auth_data)) { // Use COMPOSER_AUTH environment variable if set
if ($composerAuthEnv = getenv('COMPOSER_AUTH')) {
$authData = json_decode($composerAuthEnv, true);
if (is_null($authData)) {
throw new \UnexpectedValueException('COMPOSER_AUTH environment variable is malformed'); throw new \UnexpectedValueException('COMPOSER_AUTH environment variable is malformed');
} }
foreach ($auth_data as $domain => $credentials) { if (isset($authData['github-oauth'])) {
$this->setAuthentication($domain, $credentials->username, $credentials->password); $githubOauth = array_merge($githubOauth, $authData['github-oauth']);
}
if (isset($authData['gitlab-oauth'])) {
$gitlabOauth = array_merge($gitlabOauth, $authData['gitlab-oauth']);
}
if (isset($authData['http-basic'])) {
$httpBasic = array_merge($httpBasic, $authData['http-basic']);
} }
} }
// reload oauth token from config if available // reload oauth token from config if available
if ($tokens = $config->get('github-oauth')) { if ($githubOauth) {
foreach ($tokens as $domain => $token) { foreach ($githubOauth as $domain => $token) {
if (!preg_match('{^[a-z0-9]+$}', $token)) { if (!preg_match('{^[a-z0-9]+$}', $token)) {
throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"'); throw new \UnexpectedValueException('Your github oauth token for '.$domain.' contains invalid characters: "'.$token.'"');
} }
@ -83,15 +93,15 @@ abstract class BaseIO implements IOInterface
} }
} }
if ($tokens = $config->get('gitlab-oauth')) { if ($gitlabOauth) {
foreach ($tokens as $domain => $token) { foreach ($gitlabOauth as $domain => $token) {
$this->setAuthentication($domain, $token, 'oauth2'); $this->setAuthentication($domain, $token, 'oauth2');
} }
} }
// reload http basic credentials from config if available // reload http basic credentials from config if available
if ($creds = $config->get('http-basic')) { if ($httpBasic) {
foreach ($creds as $domain => $cred) { foreach ($httpBasic as $domain => $cred) {
$this->setAuthentication($domain, $cred['username'], $cred['password']); $this->setAuthentication($domain, $cred['username'], $cred['password']);
} }
} }