Browse Source

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

Jordi Boggiano 9 years ago
parent
commit
593b88e414
2 changed files with 28 additions and 11 deletions
  1. 7 0
      doc/03-cli.md
  2. 21 11
      src/Composer/IO/BaseIO.php

+ 7 - 0
doc/03-cli.md

@@ -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
 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
 
 This env var controls the [`discard-changes`](06-config.md#discard-changes) config option.

+ 21 - 11
src/Composer/IO/BaseIO.php

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