2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util;
|
|
|
|
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
use Composer\Util\AuthHelper;
|
|
|
|
use Composer\Util\Bitbucket;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Michael Chekin <mchekin@gmail.com>
|
|
|
|
*/
|
|
|
|
class AuthHelperTest extends TestCase
|
|
|
|
{
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\IO\IOInterface&\PHPUnit\Framework\MockObject\MockObject */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $io;
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config&\PHPUnit\Framework\MockObject\MockObject */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $config;
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var AuthHelper */
|
2020-05-04 07:48:23 +00:00
|
|
|
private $authHelper;
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->io = $this
|
|
|
|
->getMockBuilder('Composer\IO\IOInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config = $this->getMockBuilder('Composer\Config')->getMock();
|
|
|
|
|
|
|
|
$this->authHelper = new AuthHelper($this->io, $this->config);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithoutAuthCredentials(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'http://example.org';
|
|
|
|
$url = 'file://' . __FILE__;
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$headers,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBearerPassword(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'http://example.org';
|
|
|
|
$url = 'file://' . __FILE__;
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'bearer',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedHeaders = array_merge($headers, ['Authorization: Bearer ' . $auth['username']]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGithubToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'github.com';
|
|
|
|
$url = 'https://api.github.com/';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'x-oauth-basic',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitHub token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedHeaders = array_merge($headers, ['Authorization: token ' . $auth['username']]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGitlabOathToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'gitlab.com';
|
|
|
|
$url = 'https://api.gitlab.com/';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'oauth2',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([$origin]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitLab OAuth token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedHeaders = array_merge($headers, ['Authorization: Bearer ' . $auth['username']]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function gitlabPrivateTokenProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['private-token'],
|
|
|
|
['gitlab-ci-token'],
|
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider gitlabPrivateTokenProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithGitlabPrivateToken(string $password): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'gitlab.com';
|
|
|
|
$url = 'https://api.gitlab.com/';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => $password,
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([$origin]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using GitLab private token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedHeaders = array_merge($headers, ['PRIVATE-TOKEN: ' . $auth['username']]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBitbucketOathToken(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'bitbucket.org';
|
|
|
|
$url = 'https://bitbucket.org/site/oauth2/authorize';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with('Using Bitbucket OAuth token authentication', true, IOInterface::DEBUG);
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$expectedHeaders = array_merge($headers, ['Authorization: Bearer ' . $auth['password']]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function bitbucketPublicUrlProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['https://bitbucket.org/user/repo/downloads/whatever'],
|
|
|
|
['https://bbuseruploads.s3.amazonaws.com/9421ee72-638e-43a9-82ea-39cfaae2bfaa/downloads/b87c59d9-54f3-4922-b711-d89059ec3bcf'],
|
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider bitbucketPublicUrlProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBitbucketPublicUrl(string $url): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'bitbucket.org';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$headers,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function basicHttpAuthenticationProvider(): array
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[
|
2020-05-04 07:48:23 +00:00
|
|
|
Bitbucket::OAUTH2_ACCESS_TOKEN_URL,
|
|
|
|
'bitbucket.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'x-token-auth',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2020-05-04 07:48:23 +00:00
|
|
|
'https://some-api.url.com',
|
|
|
|
'some-api.url.com',
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
[
|
2020-08-27 04:45:49 +00:00
|
|
|
'https://gitlab.com',
|
|
|
|
'gitlab.com',
|
2022-08-17 12:20:07 +00:00
|
|
|
[
|
2020-08-27 04:45:49 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider basicHttpAuthenticationProvider
|
|
|
|
*
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param array<string, string|null> $auth
|
|
|
|
*
|
|
|
|
* @phpstan-param array{username: string|null, password: string|null} $auth
|
2020-05-04 07:48:23 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testAddAuthenticationHeaderWithBasicHttpAuthentication(string $url, string $origin, array $auth): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$headers = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'Accept-Encoding: gzip',
|
2020-11-22 13:48:56 +00:00
|
|
|
'Connection: close',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->expectsAuthentication($origin, $auth);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('gitlab-domains')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([$origin]);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('writeError')
|
|
|
|
->with(
|
|
|
|
'Using HTTP basic authentication with username "' . $auth['username'] . '"',
|
|
|
|
true,
|
|
|
|
IOInterface::DEBUG
|
|
|
|
);
|
|
|
|
|
|
|
|
$expectedHeaders = array_merge(
|
|
|
|
$headers,
|
2022-08-17 12:20:07 +00:00
|
|
|
['Authorization: Basic ' . base64_encode($auth['username'] . ':' . $auth['password'])]
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
$expectedHeaders,
|
|
|
|
$this->authHelper->addAuthenticationHeader($headers, $origin, $url)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider bitbucketPublicUrlProvider
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testIsPublicBitBucketDownloadWithBitbucketPublicUrl(string $url): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->authHelper->isPublicBitBucketDownload($url));
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testIsPublicBitBucketDownloadWithNonBitbucketPublicUrl(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2020-11-22 13:48:56 +00:00
|
|
|
$this->assertFalse(
|
|
|
|
$this->authHelper->isPublicBitBucketDownload(
|
|
|
|
'https://bitbucket.org/site/oauth2/authorize'
|
|
|
|
)
|
2020-05-04 07:48:23 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthAutomatically(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = true;
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('addConfigSetting')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with('http-basic.'.$origin, $auth);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptYesAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
2022-08-17 12:20:07 +00:00
|
|
|
$auth = [
|
2020-05-04 07:48:23 +00:00
|
|
|
'username' => 'my_username',
|
2020-11-22 13:48:56 +00:00
|
|
|
'password' => 'my_password',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-05-04 07:48:23 +00:00
|
|
|
$answer = 'y';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('addConfigSetting')
|
2022-02-22 21:10:52 +00:00
|
|
|
->with('http-basic.'.$origin, $auth);
|
2020-05-04 07:48:23 +00:00
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptNoAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
|
|
|
$answer = 'n';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testStoreAuthWithPromptInvalidAnswer(): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('RuntimeException');
|
2020-09-10 15:21:11 +00:00
|
|
|
|
2020-05-04 07:48:23 +00:00
|
|
|
$origin = 'github.com';
|
|
|
|
$storeAuth = 'prompt';
|
|
|
|
$answer = 'invalid';
|
|
|
|
$configSourceName = 'https://api.gitlab.com/source';
|
|
|
|
|
2021-10-27 14:18:46 +00:00
|
|
|
/** @var \Composer\Config\ConfigSourceInterface&\PHPUnit\Framework\MockObject\MockObject $configSource */
|
2020-05-04 07:48:23 +00:00
|
|
|
$configSource = $this
|
|
|
|
->getMockBuilder('Composer\Config\ConfigSourceInterface')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getAuthConfigSource')
|
|
|
|
->willReturn($configSource);
|
|
|
|
|
|
|
|
$configSource->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($configSourceName);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('askAndValidate')
|
|
|
|
->with(
|
|
|
|
'Do you want to store credentials for '.$origin.' in '.$configSourceName.' ? [Yn] ',
|
|
|
|
$this->anything(),
|
|
|
|
null,
|
|
|
|
'y'
|
|
|
|
)
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnCallback(static function ($question, $validator, $attempts, $default) use ($answer): string {
|
2020-05-04 07:48:23 +00:00
|
|
|
$validator($answer);
|
|
|
|
|
|
|
|
return $answer;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->authHelper->storeAuth($origin, $storeAuth);
|
|
|
|
}
|
|
|
|
|
2022-04-29 10:15:20 +00:00
|
|
|
public function testPromptAuthIfNeededGitLabNoAuthChange(): void
|
2022-04-21 08:37:55 +00:00
|
|
|
{
|
2022-04-29 10:15:20 +00:00
|
|
|
self::expectException('Composer\Downloader\TransportException');
|
2022-04-21 08:37:55 +00:00
|
|
|
|
|
|
|
$origin = 'gitlab.com';
|
|
|
|
|
|
|
|
$this->io
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->io
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturn([
|
2022-04-21 08:37:55 +00:00
|
|
|
'username' => 'gitlab-user',
|
|
|
|
'password' => 'gitlab-password',
|
2022-08-17 12:20:07 +00:00
|
|
|
]);
|
2022-04-21 08:37:55 +00:00
|
|
|
|
|
|
|
$this->io
|
|
|
|
->expects($this->once())
|
|
|
|
->method('setAuthentication')
|
|
|
|
->with('gitlab.com', 'gitlab-user', 'gitlab-password');
|
|
|
|
|
|
|
|
$this->config
|
|
|
|
->method('get')
|
2022-08-17 12:20:07 +00:00
|
|
|
->willReturnMap([
|
|
|
|
['github-domains', 0, []],
|
|
|
|
['gitlab-domains', 0, ['gitlab.com']],
|
|
|
|
['gitlab-token', 0, ['gitlab.com' => ['username' => 'gitlab-user', 'token' => 'gitlab-password']]],
|
|
|
|
]);
|
2022-04-21 08:37:55 +00:00
|
|
|
|
|
|
|
$this->authHelper->promptAuthIfNeeded('https://gitlab.com/acme/archive.zip', $origin, 404, 'GitLab requires authentication and it was not provided');
|
|
|
|
}
|
|
|
|
|
2020-05-04 07:48:23 +00:00
|
|
|
/**
|
2021-10-27 14:18:46 +00:00
|
|
|
* @param array<string, string|null> $auth
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @phpstan-param array{username: string|null, password: string|null} $auth
|
2020-05-04 07:48:23 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
private function expectsAuthentication(string $origin, array $auth): void
|
2020-05-04 07:48:23 +00:00
|
|
|
{
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('hasAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->io->expects($this->once())
|
|
|
|
->method('getAuthentication')
|
|
|
|
->with($origin)
|
|
|
|
->willReturn($auth);
|
|
|
|
}
|
|
|
|
}
|