diff --git a/docs/request-options.rst b/docs/request-options.rst index a076cccae..bb99f82ae 100644 --- a/docs/request-options.rst +++ b/docs/request-options.rst @@ -162,6 +162,20 @@ digest creating a replacement that can be used with any HTTP handler is planned. +ntlm + Use `Microsoft NTLM authentication `_ + (must be supported by the HTTP handler). + +.. code-block:: php + + $client->request('GET', '/get', [ + 'auth' => ['username', 'password', 'ntlm'] + ]); + +.. note:: + + This is currently only supported when using the cURL handler. + body ---- diff --git a/src/Client.php b/src/Client.php index dd8a0dcfa..de4df8a5c 100644 --- a/src/Client.php +++ b/src/Client.php @@ -352,6 +352,10 @@ private function applyOptions(RequestInterface $request, array &$options) $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST; $options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]"; break; + case 'ntlm': + $options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_NTLM; + $options['curl'][CURLOPT_USERPWD] = "$value[0]:$value[1]"; + break; } } diff --git a/src/Handler/StreamHandler.php b/src/Handler/StreamHandler.php index be2ab9a1d..d36787ac5 100644 --- a/src/Handler/StreamHandler.php +++ b/src/Handler/StreamHandler.php @@ -301,6 +301,16 @@ private function createStream(RequestInterface $request, array $options) ); } + // Microsoft NTLM authentication only supported with curl handler + if (isset($options['auth']) + && is_array($options['auth']) + && isset($options['auth'][2]) + && 'ntlm' == $options['auth'][2] + ) { + + throw new \InvalidArgumentException('Microsoft NTLM authentication only supported with curl handler'); + } + $context = $this->createResource( function () use ($context, $params) { return stream_context_create($context, $params); diff --git a/tests/ClientTest.php b/tests/ClientTest.php index c25a76f59..771c43ecc 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -389,6 +389,18 @@ public function testAuthCanBeArrayForDigestAuth() ], $last['curl']); } + public function testAuthCanBeArrayForNtlmAuth() + { + $mock = new MockHandler([new Response()]); + $client = new Client(['handler' => $mock]); + $client->get('http://foo.com', ['auth' => ['a', 'b', 'ntlm']]); + $last = $mock->getLastOptions(); + $this->assertEquals([ + CURLOPT_HTTPAUTH => 8, + CURLOPT_USERPWD => 'a:b' + ], $last['curl']); + } + public function testAuthCanBeCustomType() { $mock = new MockHandler([new Response()]);