Browse Source

Support gitlab private-token (or personal-access-token) for easier access via gitlab API

Config example:

$HOME/.composer/auth.json

{
  "gitlab-token": {
    "gitlab.com": "YOUR-TOKEN-HERE"
  }
}

Gitlab API authentication doc: http://doc.gitlab.com/ce/api/README.html
moyo 8 years ago
parent
commit
997a062ebd
3 changed files with 14 additions and 1 deletions
  1. 2 1
      src/Composer/Config.php
  2. 9 0
      src/Composer/Util/GitLab.php
  3. 3 0
      src/Composer/Util/RemoteFilesystem.php

+ 2 - 1
src/Composer/Config.php

@@ -63,6 +63,7 @@ class Config
         // bitbucket-oauth
         // github-oauth
         // gitlab-oauth
+        // gitlab-token
         // http-basic
     );
 
@@ -125,7 +126,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', 'http-basic')) && isset($this->config[$key])) {
+                if (in_array($key, array('bitbucket-oauth', 'github-oauth', 'gitlab-oauth', 'gitlab-token', 'http-basic')) && 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])) {

+ 9 - 0
src/Composer/Util/GitLab.php

@@ -64,6 +64,15 @@ class GitLab
             return true;
         }
 
+        // if available use token from composer config
+        $authTokens = $this->config->get('gitlab-token');
+
+        if (isset($authTokens[$originUrl])) {
+            $this->io->setAuthentication($originUrl, $authTokens[$originUrl], 'private-token');
+
+            return true;
+        }
+
         return false;
     }
 

+ 3 - 0
src/Composer/Util/RemoteFilesystem.php

@@ -714,6 +714,9 @@ class RemoteFilesystem
                 if ($auth['password'] === 'oauth2') {
                     $headers[] = 'Authorization: Bearer '.$auth['username'];
                 }
+                else if ($auth['password'] === 'private-token') {
+                    $headers[] = 'PRIVATE-TOKEN: '.$auth['username'];
+                }
             } elseif ('bitbucket.org' === $originUrl
                 && $this->fileUrl !== Bitbucket::OAUTH2_ACCESS_TOKEN_URL && 'x-token-auth' === $auth['username']
             ) {