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

Skip to content

Commit 548f4fd

Browse files
[HttpClient] Add support for NTLM authentication
1 parent 45526a1 commit 548f4fd

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,6 +1414,9 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
14141414
->scalarNode('auth_bearer')
14151415
->info('A token enabling HTTP Bearer authorization.')
14161416
->end()
1417+
->scalarNode('auth_ntlm')
1418+
->info('A "username:password" pair to use Microsoft NTLM authentication (requires the cURL extension).')
1419+
->end()
14171420
->arrayNode('query')
14181421
->info('Associative array of query string values merged with the base URI.')
14191422
->useAttributeAsKey('key')

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* made `Psr18Client` implement relevant PSR-17 factories
88
* added `HttplugClient`
9+
* added support for NTLM authentication
910

1011
4.3.0
1112
-----

src/Symfony/Component/HttpClient/CurlHttpClient.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface
3939
use HttpClientTrait;
4040
use LoggerAwareTrait;
4141

42-
private $defaultOptions = self::OPTIONS_DEFAULTS;
42+
private $defaultOptions = self::OPTIONS_DEFAULTS + [
43+
'auth_ntlm' => null, // array|string - an array containing the username as first value, and optionally the
44+
// password as the second one; or string like username:password - enabling NTLM auth
45+
];
4346

4447
/**
4548
* An internal object to share state between the client and its responses.
@@ -152,6 +155,25 @@ public function request(string $method, string $url, array $options = []): Respo
152155
CURLOPT_CERTINFO => $options['capture_peer_cert_chain'],
153156
];
154157

158+
if (isset($options['auth_ntlm'])) {
159+
$curlopts[CURLOPT_HTTPAUTH] = CURLAUTH_NTLM;
160+
161+
if (\is_array($options['auth_ntlm'])) {
162+
$count = \count($options['auth_ntlm']);
163+
if ($count <= 0 || $count > 2) {
164+
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must contain 1 or 2 elements, %s given.', $count));
165+
}
166+
167+
$options['auth_ntlm'] = implode(':', $options['auth_ntlm']);
168+
}
169+
170+
if (!\is_string($options['auth_ntlm'])) {
171+
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must be string or an array, %s given.', \gettype($options['auth_ntlm'])));
172+
}
173+
174+
$curlopts[CURLOPT_USERPWD] = $options['auth_ntlm'];
175+
}
176+
155177
if (!ZEND_THREAD_SAFE) {
156178
$curlopts[CURLOPT_DNS_USE_GLOBAL_CACHE] = false;
157179
}

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
181181
}
182182
}
183183

184+
if ('auth_ntlm' === $name) {
185+
throw new InvalidArgumentException(sprintf('Option "%s" is not supported by %s, try using CurlHttpClient instead.', __CLASS__));
186+
}
187+
184188
throw new InvalidArgumentException(sprintf('Unsupported option "%s" passed to %s, did you mean "%s"?', $name, __CLASS__, implode('", "', $alternatives ?: array_keys($defaultOptions))));
185189
}
186190

0 commit comments

Comments
 (0)