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

Merge branch 'feature/endpoint-auth' into develop

parents 82e5539c 97ec474d
......@@ -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);
......
......@@ -127,7 +127,10 @@ class Http extends Configurable implements AdapterInterface
}
}
$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'])) {
$request->addHeader('Authorization: Basic ' . base64_encode($authData['username']. ':' . $authData['password'] ));
}
......
......@@ -150,7 +150,10 @@ class PeclHttp extends Configurable implements AdapterInterface
}
}
$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'])) {
$headers['Authorization'] = 'Basic ' . base64_encode($authData['username']. ':' . $authData['password'] );
}
......
......@@ -228,4 +228,56 @@ 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 basic auth settings
*
* @return array
*/
public function getAuthentication()
{
return array(
'username' => $this->getOption('username'),
'password' => $this->getOption('password'),
);
}
/**
* Magic method enables a object to be transformed to a string
*
* Get a summary showing significant variables in the object
* note: uri resource is decoded for readability
*
* @return string
*/
public function __toString()
{
$output = __CLASS__ . '::__toString' . "\n"
. 'base uri: ' . $this->getBaseUri() . "\n"
. 'host: ' . $this->getHost() . "\n"
. 'port: ' . $this->getPort() ."\n"
. 'path: ' . $this->getPath() ."\n"
. 'core: ' . $this->getCore() . "\n"
. 'timeout: ' . $this->getTimeout() . "\n"
. 'authentication: ' . print_r($this->getAuthentication(), 1);
return $output;
}
}
......@@ -473,7 +473,7 @@ class Request extends Configurable
}
/**
* Get HTTP basis auth settings
* Get HTTP basic auth settings
*
* @return array
*/
......
......@@ -52,6 +52,8 @@ class EndpointTest extends \PHPUnit_Framework_TestCase
'path' => '/mysolr/',
'core' => 'mycore',
'timeout' => 3,
'username' => 'x',
'password' => 'y'
);
$this->endpoint->setOptions($options);
......@@ -110,4 +112,50 @@ 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()
);
}
public function testToString()
{
$options = array(
'host' => '192.168.0.1',
'port' => 123,
'path' => '/mysolr/',
'core' => 'mycore',
'timeout' => 3,
'username' => 'x',
'password' => 'y'
);
$this->endpoint->setOptions($options);
$this->assertEquals(
'Solarium\Core\Client\Endpoint::__toString
base uri: http://192.168.0.1:123/mysolr/mycore/
host: 192.168.0.1
port: 123
path: /mysolr
core: mycore
timeout: 3
authentication: Array
(
[username] => x
[password] => y
)
',
(string) $this->endpoint
);
}
}
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