Commit eec736c6 authored by Bas de Nooijer's avatar Bas de Nooijer

Merge branch 'develop' into feature/nextgen

Changes related to HTTP authentication handling. Also added a few small improvements to the original implementation in the develop branch.

Conflicts:
	library/Solarium/Core/Client/Adapter/PeclHttp.php
	library/Solarium/Core/Client/Request.php
	tests/Solarium/Tests/Core/Client/Adapter/HttpTest.php
	tests/Solarium/Tests/Core/Client/RequestTest.php
	tests/Solarium/Tests/QueryType/Select/Result/Spellcheck/SpellcheckTest.php
parents 9e9885fb 99016c93
...@@ -152,6 +152,12 @@ class Curl extends Configurable implements AdapterInterface ...@@ -152,6 +152,12 @@ class Curl extends Configurable implements AdapterInterface
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; $options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
} }
$authData = $request->getAuthentication();
if ( !empty($authData['username']) && !empty($authData['password'])) {
curl_setopt($handler, CURLOPT_USERPWD, $authData['username']. ':' . $authData['password'] );
curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if (count($options['headers'])) { if (count($options['headers'])) {
$headers = array(); $headers = array();
foreach ($options['headers'] as $key => $value) { foreach ($options['headers'] as $key => $value) {
......
...@@ -118,6 +118,11 @@ class Http extends Configurable implements AdapterInterface ...@@ -118,6 +118,11 @@ class Http extends Configurable implements AdapterInterface
} }
} }
$authData = $request->getAuthentication();
if ( !empty($authData['username']) && !empty($authData['password'])) {
$request->addHeader('Authorization: Basic ' . base64_encode($authData['username']. ':' . $authData['password'] ));
}
$headers = $request->getHeaders(); $headers = $request->getHeaders();
if (count($headers) > 0) { if (count($headers) > 0) {
stream_context_set_option( stream_context_set_option(
......
...@@ -150,6 +150,11 @@ class PeclHttp extends Configurable implements AdapterInterface ...@@ -150,6 +150,11 @@ class PeclHttp extends Configurable implements AdapterInterface
} }
} }
$authData = $request->getAuthentication();
if ( !empty($authData['username']) && !empty($authData['password'])) {
$headers['Authorization'] = 'Basic ' . base64_encode($authData['username']. ':' . $authData['password'] );
}
switch ($request->getMethod()) { switch ($request->getMethod()) {
case Request::METHOD_GET: case Request::METHOD_GET:
$method = HTTP_METH_GET; $method = HTTP_METH_GET;
......
...@@ -107,6 +107,10 @@ class Request extends Configurable ...@@ -107,6 +107,10 @@ class Request extends Configurable
case 'header': case 'header':
$this->setHeaders($value); $this->setHeaders($value);
break; break;
case 'authentication':
if (isset($value['username']) && isset($value['password'])) {
$this->setAuthentication($value['username'], $value['password']);
}
} }
} }
} }
...@@ -410,6 +414,7 @@ class Request extends Configurable ...@@ -410,6 +414,7 @@ class Request extends Configurable
$output = __CLASS__ . '::__toString' . "\n" $output = __CLASS__ . '::__toString' . "\n"
. 'method: ' . $this->getMethod() . "\n" . 'method: ' . $this->getMethod() . "\n"
. 'header: ' . print_r($this->getHeaders(), 1) //don't add newline when using print_r . 'header: ' . print_r($this->getHeaders(), 1) //don't add newline when using print_r
. 'authentication: ' . print_r($this->getAuthentication(), 1)
. 'resource: ' . $this->getUri() . "\n" . 'resource: ' . $this->getUri() . "\n"
. 'resource urldecoded: ' . urldecode($this->getUri()) . "\n" . 'resource urldecoded: ' . urldecode($this->getUri()) . "\n"
. 'raw data: ' . $this->getRawData() . "\n"; . 'raw data: ' . $this->getRawData() . "\n";
...@@ -417,4 +422,33 @@ class Request extends Configurable ...@@ -417,4 +422,33 @@ class Request extends Configurable
return $output; return $output;
} }
/**
* Set HTTP basic auth settings
*
* If one or both values are NULL authentication will be disabled
*
* @param string $username
* @param string $password
* @return self Provides fluent interface
*/
public function setAuthentication($username, $password) {
$this->setOption('username', $username);
$this->setOption('password', $password);
return $this;
}
/**
* Get HTTP basis auth settings
*
* @return array
*/
public function getAuthentication()
{
return array(
'username' => $this->getOption('username'),
'password' => $this->getOption('password'),
);
}
} }
...@@ -162,4 +162,24 @@ class HttpTest extends \PHPUnit_Framework_TestCase ...@@ -162,4 +162,24 @@ class HttpTest extends \PHPUnit_Framework_TestCase
); );
} }
public function testCreateContextWithAuthorization()
{
$timeout = 13;
$method = Request::METHOD_HEAD;
$request = new Request();
$request->setMethod($method);
$request->setAuthentication('someone', 'S0M3p455');
$endpoint = new Endpoint();
$endpoint->setTimeout($timeout);
$context = $this->adapter->createContext($request, $endpoint);
$this->assertEquals(
array('http' => array('method' => $method, 'timeout' => $timeout, 'header' => 'Authorization: Basic c29tZW9uZTpTME0zcDQ1NQ==')),
stream_context_get_options($context)
);
}
} }
\ No newline at end of file
...@@ -59,6 +59,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase ...@@ -59,6 +59,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
'myHeader1' => 'X-myHeader1: value1', 'myHeader1' => 'X-myHeader1: value1',
'myHeader2' => 'X-myHeader2: value2', 'myHeader2' => 'X-myHeader2: value2',
), ),
'authentication' => array(
'username' => 'testuser',
'password' => 'testpass',
),
); );
$this->request->setOptions($options); $this->request->setOptions($options);
...@@ -89,6 +93,14 @@ class RequestTest extends \PHPUnit_Framework_TestCase ...@@ -89,6 +93,14 @@ class RequestTest extends \PHPUnit_Framework_TestCase
), ),
$this->request->getHeaders() $this->request->getHeaders()
); );
$this->assertEquals(
array(
'username' => $options['authentication']['username'],
'password' => $options['authentication']['password']
),
$this->request->getAuthentication()
);
} }
public function testGetDefaultMethod() public function testGetDefaultMethod()
...@@ -453,6 +465,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase ...@@ -453,6 +465,10 @@ class RequestTest extends \PHPUnit_Framework_TestCase
'myHeader1' => 'X-myHeader1: value1', 'myHeader1' => 'X-myHeader1: value1',
'myHeader2' => 'X-myHeader2: value2', 'myHeader2' => 'X-myHeader2: value2',
), ),
'authentication' => array(
'username' => 'testuser',
'password' => 'testpass',
),
); );
$this->request->setOptions($options); $this->request->setOptions($options);
...@@ -464,6 +480,11 @@ header: Array ...@@ -464,6 +480,11 @@ header: Array
[0] => X-myHeader1: value1 [0] => X-myHeader1: value1
[1] => X-myHeader2: value2 [1] => X-myHeader2: value2
) )
authentication: Array
(
[username] => testuser
[password] => testpass
)
resource: /myHandler?param1=1&param2=test+content resource: /myHandler?param1=1&param2=test+content
resource urldecoded: /myHandler?param1=1&param2=test content resource urldecoded: /myHandler?param1=1&param2=test content
raw data: post data raw data: post data
...@@ -472,4 +493,20 @@ raw data: post data ...@@ -472,4 +493,20 @@ raw data: post data
); );
} }
public function testGetAndSetAuthentication()
{
$user = 'someone';
$pass = 'S0M3p455';
$this->request->setAuthentication($user, $pass);
$this->assertEquals(
array(
'username' => $user,
'password' => $pass,
),
$this->request->getAuthentication()
);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment