From a074819a5193e6cee802947eb9ad873a2b5fe63e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?= Date: Fri, 8 May 2020 17:43:07 +0200 Subject: [PATCH] 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"} } --- doc/06-config.md | 8 ++++++-- src/Composer/IO/BaseIO.php | 4 +++- src/Composer/Util/GitLab.php | 19 +++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/doc/06-config.md b/doc/06-config.md index 1b0a8e648..30f31b98e 100644 --- a/doc/06-config.md +++ b/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. diff --git a/src/Composer/IO/BaseIO.php b/src/Composer/IO/BaseIO.php index e2d916a15..e3a263301 100644 --- a/src/Composer/IO/BaseIO.php +++ b/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 diff --git a/src/Composer/Util/GitLab.php b/src/Composer/Util/GitLab.php index fb2489b01..ea0c72477 100644 --- a/src/Composer/Util/GitLab.php +++ b/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; }