Commit 8c94e08c authored by Bas de Nooijer's avatar Bas de Nooijer

Moved auth settings to request object and updated adapter implementations

parent 69f330de
...@@ -138,11 +138,12 @@ class Solarium_Client_Adapter_Curl extends Solarium_Client_Adapter ...@@ -138,11 +138,12 @@ class Solarium_Client_Adapter_Curl extends Solarium_Client_Adapter
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8'; $options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
} }
if ( isset( $this->_options['username']) && isset( $this->_options['password'])) { $authData = $request->getAuthentication();
curl_setopt($handler, CURLOPT_USERPWD, $this->_options['username']. ':' . $this->_options['password'] ); if ( !empty($authData['username']) && !empty($authData['password'])) {
curl_setopt($handler, CURLOPT_USERPWD, $authData['username']. ':' . $authData['password'] );
curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 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) {
......
...@@ -111,8 +111,9 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter ...@@ -111,8 +111,9 @@ class Solarium_Client_Adapter_Http extends Solarium_Client_Adapter
} }
} }
if ( isset( $this->_options['username']) && isset( $this->_options['password'])) { $authData = $request->getAuthentication();
$request->addHeader('Authorization: Basic ' . base64_encode($this->_options['username']. ':' . $this->_options['password'] )); if ( !empty($authData['username']) && !empty($authData['password'])) {
$request->addHeader('Authorization: Basic ' . base64_encode($authData['username']. ':' . $authData['password'] ));
} }
$headers = $request->getHeaders(); $headers = $request->getHeaders();
......
...@@ -137,6 +137,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -137,6 +137,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
} }
} }
$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 Solarium_Client_Request::METHOD_GET: case Solarium_Client_Request::METHOD_GET:
$method = HTTP_METH_GET; $method = HTTP_METH_GET;
......
...@@ -406,4 +406,32 @@ class Solarium_Client_Request extends Solarium_Configurable ...@@ -406,4 +406,32 @@ class Solarium_Client_Request extends Solarium_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'),
);
}
} }
\ No newline at end of file
...@@ -157,11 +157,8 @@ class Solarium_Client_Adapter_HttpTest extends PHPUnit_Framework_TestCase ...@@ -157,11 +157,8 @@ class Solarium_Client_Adapter_HttpTest extends PHPUnit_Framework_TestCase
$request = new Solarium_Client_Request(); $request = new Solarium_Client_Request();
$request->setMethod($method); $request->setMethod($method);
$request->setAuthentication('someone', 'S0M3p455');
$this->_adapter->setOptions(array(
'username' => 'someone',
'password' => 'S0M3p455'
));
$this->_adapter->setTimeout($timeout); $this->_adapter->setTimeout($timeout);
$context = $this->_adapter->createContext($request); $context = $this->_adapter->createContext($request);
......
...@@ -472,4 +472,20 @@ raw data: post data ...@@ -472,4 +472,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()
);
}
} }
\ No newline at end of file
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