Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d5096a

Browse files
committed
[HttpClient] Allow to pass user/pw as an array
1 parent 535c482 commit 8d5096a

File tree

5 files changed

+78
-10
lines changed

5 files changed

+78
-10
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ 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_array($options['auth_basic'] ?? null)) {
75+
$count = \count($options['auth_basic']);
76+
if ($count <= 0 || $count > 2) {
77+
throw new InvalidArgumentException(sprintf('Option "auth_basic" must contain 1 or 2 elements, %s given.', $count));
78+
}
79+
80+
$options['auth_basic'] = implode(':', $options['auth_basic']);
81+
}
82+
7483
if (!\is_string($options['auth_basic'] ?? '')) {
75-
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string, %s given.', \gettype($options['auth_basic'])));
84+
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, %s given.', \gettype($options['auth_basic'])));
7685
}
7786

7887
if (!\is_string($options['auth_bearer'] ?? '')) {

src/Symfony/Component/HttpClient/HttpOptions.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ public function toArray(): array
3434
/**
3535
* @return $this
3636
*/
37-
public function setAuthBasic(string $userinfo)
37+
public function setAuthBasic(string $user, string $password = '')
3838
{
39-
$this->options['auth'] = $userinfo;
39+
$this->options['auth_basic'] = $user;
40+
if ('' !== $password) {
41+
$this->options['auth_basic'] .= ':'.$password;
42+
}
4043

4144
return $this;
4245
}

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,25 @@ public function testInvalidAuthBearerOption()
185185
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
186186
* @expectedExceptionMessage Define either the "auth_basic" or the "auth_bearer" option, setting both is not supported.
187187
*/
188-
public function testSetBasicAndBearerOption()
188+
public function testSetAuthBasicAndBearerOptions()
189189
{
190190
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foo', 'auth_basic' => 'foo:bar'], HttpClientInterface::OPTIONS_DEFAULTS);
191191
}
192+
193+
public function providePrepareAuthBasic()
194+
{
195+
yield ['foo:bar', 'Zm9vOmJhcg=='];
196+
yield [['foo', 'bar'], 'Zm9vOmJhcg=='];
197+
yield ['foo', 'Zm9v'];
198+
yield [['foo'], 'Zm9v'];
199+
}
200+
201+
/**
202+
* @dataProvider providePrepareAuthBasic
203+
*/
204+
public function testPrepareAuthBasic($arg, $result)
205+
{
206+
[, $options] = $this->prepareRequest('POST', 'http://example.com', ['auth_basic' => $arg], HttpClientInterface::OPTIONS_DEFAULTS);
207+
$this->assertSame('Basic '.$result, $options['headers']['authorization'][0]);
208+
}
192209
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpClient\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpClient\HttpOptions;
16+
17+
/**
18+
* @author Kévin Dunglas <[email protected]>
19+
*/
20+
class HttpOptionsTest extends TestCase
21+
{
22+
public function provideSetAuth()
23+
{
24+
yield ['user:password', 'user', 'password'];
25+
yield ['user:password', 'user:password'];
26+
yield ['user', 'user'];
27+
yield ['user:0', 'user', '0'];
28+
}
29+
30+
/**
31+
* @dataProvider provideSetAuth
32+
*/
33+
public function testSetAuth(string $expected, string $user, string $password = '')
34+
{
35+
$this->assertSame($expected, (new HttpOptions())->setAuthBasic($user, $password)->toArray()['auth_basic']);
36+
}
37+
}

src/Symfony/Contracts/HttpClient/HttpClientInterface.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
interface HttpClientInterface
2727
{
2828
public const OPTIONS_DEFAULTS = [
29-
'auth_basic' => null, // string - a username:password enabling HTTP Basic authentication (RFC 7617)
29+
'auth_basic' => null, // array|string - an array containing the username as first value, and optionally the
30+
// password as the second one; or string like username:password - enabling HTTP Basic
31+
// authentication (RFC 7617)
3032
'auth_bearer' => null, // string - a token enabling HTTP Bearer authorization (RFC 6750)
3133
'query' => [], // string[] - associative array of query string values to merge with the request's URL
3234
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
@@ -37,16 +39,16 @@ interface HttpClientInterface
3739
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible
3840
// value it is they are not defined - typically "application/json"
3941
'user_data' => null, // mixed - any extra data to attach to the request (scalar, callable, object...) that
40-
// MUST be available via $response->getInfo('data') - not used internally
42+
// MUST be available via $response->getInfo('data') - not used internally
4143
'max_redirects' => 20, // int - the maximum number of redirects to follow; a value lower or equal to 0 means
42-
// redirects should not be followed; "Authorization" and "Cookie" headers MUST
43-
// NOT follow except for the initial host name
44+
// redirects should not be followed; "Authorization" and "Cookie" headers MUST
45+
// NOT follow except for the initial host name
4446
'http_version' => null, // string - defaults to the best supported version, typically 1.1 or 2.0
4547
'base_uri' => null, // string - the URI to resolve relative URLs, following rules in RFC 3986, section 2
4648
'buffer' => true, // bool - whether the content of the response should be buffered or not
4749
'on_progress' => null, // callable(int $dlNow, int $dlSize, array $info) - throwing any exceptions MUST abort
48-
// the request; it MUST be called on DNS resolution, on arrival of headers and on
49-
// completion; it SHOULD be called on upload/download of data and at least 1/s
50+
// the request; it MUST be called on DNS resolution, on arrival of headers and on
51+
// completion; it SHOULD be called on upload/download of data and at least 1/s
5052
'resolve' => [], // string[] - a map of host to IP address that SHOULD replace DNS resolution
5153
'proxy' => null, // string - by default, the proxy-related env vars handled by curl SHOULD be honored
5254
'no_proxy' => null, // string - a comma separated list of hosts that do not require a proxy to be reached

0 commit comments

Comments
 (0)