diff --git a/Tests/Transport/StreamCredentialsTest.php b/Tests/Transport/StreamCredentialsTest.php new file mode 100644 index 00000000..8873e4c1 --- /dev/null +++ b/Tests/Transport/StreamCredentialsTest.php @@ -0,0 +1,70 @@ +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); + } +} diff --git a/docs/classes/TransportInterface.md b/docs/classes/TransportInterface.md index ffb364a5..7d49537e 100644 --- a/docs/classes/TransportInterface.md +++ b/docs/classes/TransportInterface.md @@ -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 | diff --git a/src/Transport/Stream.php b/src/Transport/Stream.php index ef468ba0..55c9522c 100644 --- a/src/Transport/Stream.php +++ b/src/Transport/Stream.php @@ -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; @@ -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