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

Added authentication support to endpoint

parent 82e5539c
......@@ -156,7 +156,10 @@ class Curl extends Configurable implements AdapterInterface
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
}
$authData = $request->getAuthentication();
// Try endpoint authentication first, fallback to request for backwards compatibility
$authData = $endpoint->getAuthentication();
if(empty($authData['username'])) $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);
......
......@@ -228,4 +228,34 @@ class Endpoint extends Configurable
return $uri;
}
/**
* 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'),
);
}
}
......@@ -47,11 +47,13 @@ class EndpointTest extends \PHPUnit_Framework_TestCase
public function testConfigMode()
{
$options = array(
'host' => '192.168.0.1',
'port' => 123,
'path' => '/mysolr/',
'core' => 'mycore',
'timeout' => 3,
'host' => '192.168.0.1',
'port' => 123,
'path' => '/mysolr/',
'core' => 'mycore',
'timeout' => 3,
'username' => 'x',
'password' => 'y'
);
$this->endpoint->setOptions($options);
......@@ -110,4 +112,19 @@ class EndpointTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('http://myserver:123/mypath/mycore/', $this->endpoint->getBaseUri());
}
public function testGetAndSetAuthentication()
{
$user = 'someone';
$pass = 'S0M3p455';
$this->endpoint->setAuthentication($user, $pass);
$this->assertEquals(
array(
'username' => $user,
'password' => $pass,
),
$this->endpoint->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