From 931a1ff1f8d7e567bb27514c196d5ecdf47be726 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 27 Aug 2020 11:45:49 +0700 Subject: [PATCH] AuthHelper: Allow fall-through GitLab-specific HTTP headers for auth Previously, `AuthHelper` consumed the authentication credentials for GitLab domains and added access tokens as GitLab-specific headers. [Composer repositories now supported in GitLab](https://php.watch/articles/composer-gitlab-repositories) require standard Authorization headers with a personal access to function, which failed to work due to out GitLab-specific headers. With this commit, AuthHelper checks if the password is an access token, and falls through to HTTP basic authentication even if the domain name is a GitLab domain name. --- src/Composer/Util/AuthHelper.php | 5 ++++- tests/Composer/Test/Util/AuthHelperTest.php | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Composer/Util/AuthHelper.php b/src/Composer/Util/AuthHelper.php index a318931e3..5a3f950c7 100644 --- a/src/Composer/Util/AuthHelper.php +++ b/src/Composer/Util/AuthHelper.php @@ -205,7 +205,10 @@ class AuthHelper $headers[] = 'Authorization: token '.$auth['username']; $authenticationDisplayMessage = 'Using GitHub token authentication'; } - } elseif (in_array($origin, $this->config->get('gitlab-domains'), true)) { + } elseif ( + in_array($origin, $this->config->get('gitlab-domains'), true) + && in_array($auth['password'], array('oauth2', 'private-token', 'gitlab-ci-token'), true) + ) { if ($auth['password'] === 'oauth2') { $headers[] = 'Authorization: Bearer '.$auth['username']; $authenticationDisplayMessage = 'Using GitLab OAuth token authentication'; diff --git a/tests/Composer/Test/Util/AuthHelperTest.php b/tests/Composer/Test/Util/AuthHelperTest.php index 567299345..11e546ed1 100644 --- a/tests/Composer/Test/Util/AuthHelperTest.php +++ b/tests/Composer/Test/Util/AuthHelperTest.php @@ -280,6 +280,14 @@ class AuthHelperTest extends TestCase 'password' => 'my_password' ) ), + array( + 'https://gitlab.com', + 'gitlab.com', + array( + 'username' => 'my_username', + 'password' => 'my_password' + ) + ), ); } @@ -302,7 +310,7 @@ class AuthHelperTest extends TestCase $this->config->expects($this->once()) ->method('get') ->with('gitlab-domains') - ->willReturn(array()); + ->willReturn(array($origin)); $this->io->expects($this->once()) ->method('writeError')