From 997a062ebdc2c5396ed8aa86bfc450a32e03b4b0 Mon Sep 17 00:00:00 2001 From: moyo Date: Tue, 5 Jul 2016 18:02:39 +0800 Subject: [PATCH] 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 --- src/Composer/Config.php | 3 ++- src/Composer/Util/GitLab.php | 9 +++++++++ src/Composer/Util/RemoteFilesystem.php | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 59306f9d2..2bb8d054b 100644 --- a/src/Composer/Config.php +++ b/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])) { diff --git a/src/Composer/Util/GitLab.php b/src/Composer/Util/GitLab.php index 1cfe6b15d..6f1dc8683 100644 --- a/src/Composer/Util/GitLab.php +++ b/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; } diff --git a/src/Composer/Util/RemoteFilesystem.php b/src/Composer/Util/RemoteFilesystem.php index 9a16b0d0d..e9d5715fa 100644 --- a/src/Composer/Util/RemoteFilesystem.php +++ b/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'] ) {