Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Tests/Transport/StreamCredentialsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* @copyright Copyright (C) 2005 - 2026 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Http\Tests\Transport;

use Joomla\Http\Transport\Stream;
use Joomla\Uri\Uri;
use Joomla\Uri\UriImmutable;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

/**
* Test class for the userauth/passwordauth options of Joomla\Http\Transport\Stream.
*/
#[CoversClass(Stream::class)]
class StreamCredentialsTest extends TestCase
{
/**
* The URL of the HTTP stub.
*
* @var string
*/
private $stubUrl;

/**
* Sets up the fixture.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

if (!Stream::isSupported()) {
$this->markTestSkipped('The stream transport is not supported in this environment.');
}

if (!\defined('JTEST_HTTP_STUB') && getenv('JTEST_HTTP_STUB') == '') {
$this->markTestSkipped('The Transport test stub has not been configured');
}

$this->stubUrl = \defined('JTEST_HTTP_STUB') ? JTEST_HTTP_STUB : getenv('JTEST_HTTP_STUB');
}

public function testCredentialsAreSentWithoutChangingTheUri(): void
{
$uri = new Uri($this->stubUrl);
$transport = new Stream(['userauth' => 'username', 'passwordauth' => 'secret']);

$body = json_decode((string) $transport->request('GET', $uri)->getBody());

$this->assertSame('username', $body->username);
$this->assertSame('secret', $body->password);
$this->assertSame($this->stubUrl, (string) $uri, 'The credentials must not be written into the URI object.');
}

public function testCredentialsAreSentForAnImmutableUri(): void
{
$transport = new Stream(['userauth' => 'username', 'passwordauth' => 'secret']);

$body = json_decode((string) $transport->request('GET', new UriImmutable($this->stubUrl))->getBody());

$this->assertSame('username', $body->username);
$this->assertSame('secret', $body->password);
}
}
2 changes: 1 addition & 1 deletion docs/classes/TransportInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ with `getOption($key, $default)` and are transport-specific; the commonly used o

| Option | Used by | Meaning |
|---|---|---|
| `userauth` / `passwordauth` | Curl | HTTP Basic credentials |
| `userauth` / `passwordauth` | Curl, Stream, Socket | HTTP Basic credentials; the stream transport sends them as an `Authorization` header and leaves the URI object unchanged |
| `follow_location` | Curl | Follow redirects, default `true` |
| `transport.curl` | Curl | Raw `CURLOPT_*` overrides, applied last |
| `stream.certpath` | Stream | CA bundle file or directory |
Expand Down
12 changes: 7 additions & 5 deletions src/Transport/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Joomla\Http\AbstractTransport;
use Joomla\Http\Exception\InvalidResponseCodeException;
use Joomla\Http\Response;
use Joomla\Uri\Uri;
use Joomla\Uri\UriInterface;
use Laminas\Diactoros\Stream as StreamResponse;

Expand Down Expand Up @@ -128,10 +127,13 @@ public function request(
$options['header'] = trim($headerString, "\r\n");
}

// Authentication, if needed
if ($uri instanceof Uri && $this->getOption('userauth') && $this->getOption('passwordauth')) {
$uri->setUser($this->getOption('userauth'));
$uri->setPass($this->getOption('passwordauth'));
// Authentication, if needed. Sent as a header, so the credentials do not end up in the caller's URI object.
if (
$this->getOption('userauth') && $this->getOption('passwordauth')
&& !isset(array_change_key_case($headers)['authorization'])
) {
$authorization = 'Authorization: Basic ' . base64_encode($this->getOption('userauth') . ':' . $this->getOption('passwordauth'));
$options['header'] = isset($options['header']) ? $options['header'] . "\r\n" . $authorization : $authorization;
}

// Set any custom transport options
Expand Down
Loading