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

Skip to content
Merged
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
[HttpClient] relax auth bearer format requirements
  • Loading branch information
xabbuh authored and nicolas-grekas committed Oct 24, 2020
commit ac2059426795739365fc39e906bf1e4f0e1c353c
9 changes: 7 additions & 2 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,13 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
throw new InvalidArgumentException(sprintf('Option "auth_basic" must be string or an array, "%s" given.', \gettype($options['auth_basic'])));
}

if (isset($options['auth_bearer']) && (!\is_string($options['auth_bearer']) || !preg_match('{^[-._=:~+/0-9a-zA-Z]++$}', $options['auth_bearer']))) {
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, '.(\is_string($options['auth_bearer']) ? 'invalid string given.' : '"%s" given.'), \gettype($options['auth_bearer'])));
if (isset($options['auth_bearer'])) {
if (!\is_string($options['auth_bearer'])) {
throw new InvalidArgumentException(sprintf('Option "auth_bearer" must be a string, "%s" given.', \gettype($options['auth_bearer'])));
}
if (preg_match('{[^\x21-\x7E]}', $options['auth_bearer'])) {
throw new InvalidArgumentException('Invalid character found in option "auth_bearer": '.json_encode($options['auth_bearer']).'.');
}
}

if (isset($options['auth_basic'], $options['auth_bearer'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ public function testAuthBearerOption()
public function testInvalidAuthBearerOption()
{
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, "object" given.');
$this->expectExceptionMessage('Option "auth_bearer" must be a string, "object" given.');
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => new \stdClass()], HttpClientInterface::OPTIONS_DEFAULTS);
}

public function testInvalidAuthBearerValue()
{
$this->expectException('Symfony\Component\HttpClient\Exception\InvalidArgumentException');
$this->expectExceptionMessage('Option "auth_bearer" must be a string containing only characters from the base 64 alphabet, invalid string given.');
$this->expectExceptionMessage('Invalid character found in option "auth_bearer": "a\nb".');
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => "a\nb"], HttpClientInterface::OPTIONS_DEFAULTS);
}

Expand Down