Skip to content

Commit

Permalink
Merge pull request #1569 from miguelbalboa/master
Browse files Browse the repository at this point in the history
Added curl ntlm auth support
  • Loading branch information
sagikazarmark committed Mar 22, 2017
2 parents e52a94c + 6e2c1d8 commit a1c4a74
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/request-options.rst
Expand Up @@ -162,6 +162,20 @@ digest
creating a replacement that can be used with any HTTP handler is
planned.

ntlm
Use `Microsoft NTLM authentication <https://msdn.microsoft.com/en-us/library/windows/desktop/aa378749(v=vs.85).aspx>`_
(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
----
Expand Down
4 changes: 4 additions & 0 deletions src/Client.php
Expand Up @@ -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;
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/Handler/StreamHandler.php
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions tests/ClientTest.php
Expand Up @@ -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()]);
Expand Down

0 comments on commit a1c4a74

Please sign in to comment.