Skip to content

Commit f79ef21

Browse files
dunglasfabpot
authored andcommitted
[HttpClient] Add new bearer option
1 parent 29f81b0 commit f79ef21

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,30 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
7171
throw new InvalidArgumentException(sprintf('Option "on_progress" must be callable, %s given.', \is_object($onProgress) ? \get_class($onProgress) : \gettype($onProgress)));
7272
}
7373

74-
if (!\is_string($options['auth'] ?? '')) {
75-
throw new InvalidArgumentException(sprintf('Option "auth" must be string, %s given.', \gettype($options['auth'])));
74+
if (!\is_string($options['auth_basic'] ?? '')) {
75+
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string, %s given.', \gettype($options['auth_basic'])));
76+
}
77+
78+
if (!\is_string($options['auth_bearer'] ?? '')) {
79+
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be string, %s given.', \gettype($options['auth_bearer'])));
80+
}
81+
82+
if (isset($options['auth_basic'], $options['auth_bearer'])) {
83+
throw new InvalidArgumentException('Define either the "auth_basic" or the "auth_bearer" option, setting both is not supported.');
7684
}
7785

7886
if (null !== $url) {
7987
// Merge auth with headers
80-
if (($options['auth'] ?? false) && !($headers['authorization'] ?? false)) {
81-
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth']);
88+
if (($options['auth_basic'] ?? false) && !($headers['authorization'] ?? false)) {
89+
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Basic '.base64_encode($options['auth_basic']);
90+
}
91+
// Merge bearer with headers
92+
if (($options['auth_bearer'] ?? false) && !($headers['authorization'] ?? false)) {
93+
$rawHeaders[] = 'authorization: '.$headers['authorization'][] = 'Bearer '.$options['auth_bearer'];
8294
}
8395

8496
$options['raw_headers'] = $rawHeaders;
85-
unset($options['auth']);
97+
unset($options['auth_basic'], $options['auth_bearer']);
8698

8799
// Parse base URI
88100
if (\is_string($options['base_uri'])) {

src/Symfony/Component/HttpClient/HttpOptions.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,23 @@ public function toArray(): array
3434
/**
3535
* @return $this
3636
*/
37-
public function setAuth(string $userinfo)
37+
public function setAuthBasic(string $userinfo)
3838
{
3939
$this->options['auth'] = $userinfo;
4040

4141
return $this;
4242
}
4343

44+
/**
45+
* @return $this
46+
*/
47+
public function setAuthBearer(string $token)
48+
{
49+
$this->options['bearer'] = $token;
50+
51+
return $this;
52+
}
53+
4454
/**
4555
* @return $this
4656
*/

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpClient\HttpClientTrait;
16+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1617

1718
class HttpClientTraitTest extends TestCase
1819
{
@@ -163,4 +164,29 @@ public function provideRemoveDotSegments()
163164
yield ['/a/', '/a/b/..'];
164165
yield ['/a///b', '/a///b'];
165166
}
167+
168+
public function testAuthBearerOption()
169+
{
170+
[, $options] = self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foobar'], HttpClientInterface::OPTIONS_DEFAULTS);
171+
$this->assertSame('Bearer foobar', $options['headers']['authorization'][0]);
172+
$this->assertSame('authorization: Bearer foobar', $options['raw_headers'][0]);
173+
}
174+
175+
/**
176+
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
177+
* @expectedExceptionMessage Option "auth_bearer" must be string, object given.
178+
*/
179+
public function testInvalidAuthBearerOption()
180+
{
181+
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => new \stdClass()], HttpClientInterface::OPTIONS_DEFAULTS);
182+
}
183+
184+
/**
185+
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
186+
* @expectedExceptionMessage Define either the "auth_basic" or the "auth_bearer" option, setting both is not supported.
187+
*/
188+
public function testSetBasicAndBearerOption()
189+
{
190+
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foo', 'auth_basic' => 'foo:bar'], HttpClientInterface::OPTIONS_DEFAULTS);
191+
}
166192
}

src/Symfony/Contracts/HttpClient/HttpClientInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
interface HttpClientInterface
2727
{
2828
public const OPTIONS_DEFAULTS = [
29-
'auth' => null, // string - a username:password enabling HTTP Basic authentication
29+
'auth_basic' => null, // string - a username:password enabling HTTP Basic authentication (RFC 7617)
30+
'auth_bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
3031
'query' => [], // string[] - associative array of query string values to merge with the request's URL
3132
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
3233
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public function testRedirects()
214214
{
215215
$client = $this->getHttpClient();
216216
$response = $client->request('POST', 'http://localhost:8057/301', [
217-
'auth' => 'foo:bar',
217+
'auth_basic' => 'foo:bar',
218218
'body' => function () {
219219
yield 'foo=bar';
220220
},
@@ -291,7 +291,7 @@ public function testMaxRedirects()
291291
$client = $this->getHttpClient();
292292
$response = $client->request('GET', 'http://localhost:8057/301', [
293293
'max_redirects' => 1,
294-
'auth' => 'foo:bar',
294+
'auth_basic' => 'foo:bar',
295295
]);
296296

297297
try {

0 commit comments

Comments
 (0)