Browse Source

Add support for gitlab deploy token (#8867)

* feat: Added ability to work with GitLab deploy tokens: https://docs.gitlab.com/ee/user/project/deploy_tokens/

Deploy tokens can be specified two ways:
1) GIT CONFIG:
git config --add gitlab.deploytoken.user USERNAME && git config --add gitlab.deploytoken.token TOKEN
2) Auth.json:
"gitlab-token": {
    "gitlab.com": {"username": "USERNAME", "token": "TOKEN"}
}
Matěj Kmínek 4 years ago
parent
commit
a074819a51
3 changed files with 24 additions and 7 deletions
  1. 6 2
      doc/06-config.md
  2. 3 1
      src/Composer/IO/BaseIO.php
  3. 15 4
      src/Composer/Util/GitLab.php

+ 6 - 2
doc/06-config.md

@@ -85,9 +85,13 @@ gitlab.com the domain names must be also specified with the
 
 ## gitlab-token
 
-A list of domain names and private tokens. For example using `{"gitlab.com":
+A list of domain names and private tokens. Private token can be either simple 
+string, or array with username and token. 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
+private repositories on gitlab. Using `{"gitlab.com": {"username": "gitlabuser",
+ "token": "privatetoken"}}` will use both username and token for gitlab deploy 
+token functionality (https://docs.gitlab.com/ee/user/project/deploy_tokens/)
+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.
 

+ 3 - 1
src/Composer/IO/BaseIO.php

@@ -135,7 +135,9 @@ abstract class BaseIO implements IOInterface
         }
 
         foreach ($gitlabToken as $domain => $token) {
-            $this->checkAndSetAuthentication($domain, $token, 'private-token');
+            $username = is_array($token) && array_key_exists("username", $token) ? $token["username"] : $token;
+            $password = is_array($token) && array_key_exists("token", $token) ? $token["token"] : 'private-token';
+            $this->checkAndSetAuthentication($domain, $username, $password);
         }
 
         // reload http basic credentials from config if available

+ 15 - 4
src/Composer/Util/GitLab.php

@@ -71,17 +71,28 @@ class GitLab
             return true;
         }
 
+        // if available use deploy token from git config
+        if (0 === $this->process->execute('git config gitlab.deploytoken.user', $tokenUser) && 0 === $this->process->execute('git config gitlab.deploytoken.token', $tokenPassword)) {
+            $this->io->setAuthentication($originUrl, trim($tokenUser), trim($tokenPassword));
+
+            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;
+            $token = $authTokens[$originUrl];
         }
 
         if (isset($authTokens[$bcOriginUrl])) {
-            $this->io->setAuthentication($originUrl, $authTokens[$bcOriginUrl], 'private-token');
+            $token = $authTokens[$bcOriginUrl];
+        }
+        
+        if(isset($token)){
+            $username = is_array($token) && array_key_exists("username", $token) ? $token["username"] : $token;
+            $password = is_array($token) && array_key_exists("token", $token) ? $token["token"] : 'private-token';
+            $this->io->setAuthentication($originUrl, $username, $password);
 
             return true;
         }