Removing auth key from request and putting this as attribute.

This commit is contained in:
Juan Basso 2010-12-03 00:46:11 -02:00
parent e1e8026392
commit cd24aca39d
4 changed files with 46 additions and 75 deletions

View file

@ -30,12 +30,13 @@ class BasicAuthentication {
* Authentication
*
* @param HttpSocket $http
* @param array $authInfo
* @return void
* @see http://www.ietf.org/rfc/rfc2617.txt
*/
public static function authentication(HttpSocket $http) {
if (isset($http->request['auth']['user'], $http->request['auth']['pass'])) {
$http->request['header']['Authorization'] = self::_generateHeader($http->request['auth']['user'], $http->request['auth']['pass']);
public static function authentication(HttpSocket $http, &$authInfo) {
if (isset($authInfo['user'], $authInfo['pass'])) {
$http->request['header']['Authorization'] = self::_generateHeader($authInfo['user'], $authInfo['pass']);
}
}

View file

@ -59,11 +59,6 @@ class HttpSocket extends CakeSocket {
'query' => null,
'fragment' => null
),
'auth' => array(
'method' => 'Basic',
'user' => null,
'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
@ -123,11 +118,6 @@ class HttpSocket extends CakeSocket {
'host' => 'localhost',
'port' => 80
),
'auth' => array(
'method' => 'Basic',
'user' => null,
'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
@ -147,6 +137,14 @@ class HttpSocket extends CakeSocket {
*/
public $lineBreak = "\r\n";
/**
* Authentication settings
*
* @var array
* @access protected
*/
protected $_auth = array();
/**
* Build an HTTP Socket using the specified configuration.
*
@ -181,6 +179,26 @@ class HttpSocket extends CakeSocket {
parent::__construct($this->config);
}
/**
* Set authentication settings
*
* @param string $method Authentication method (ex. Basic, Digest). If empty, disable authentication
* @param mixed $user Username for authentication. Can be an array with settings to authentication class
* @param string $pass Password for authentication
* @return void
*/
public function setAuthConfig($method, $user, $pass = null) {
if (empty($method)) {
$this->_auth = array();
return;
}
if (is_array($user)) {
$this->_auth = array($method => $user);
return;
}
$this->_auth = array($method => compact('user', 'pass'));
}
/**
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
* method and provide a more granular interface.
@ -201,10 +219,6 @@ class HttpSocket extends CakeSocket {
$request['uri'] = null;
}
$uri = $this->_parseUri($request['uri']);
$hadAuth = false;
if (is_array($uri) && array_key_exists('user', $uri)) {
$hadAuth = true;
}
if (!isset($uri['host'])) {
$host = $this->config['host'];
}
@ -216,10 +230,6 @@ class HttpSocket extends CakeSocket {
$request['uri'] = $this->_parseUri($request['uri'], true);
$this->request = Set::merge($this->request, $this->config['request'], $request);
if (!$hadAuth && !empty($this->config['request']['auth']['user'])) {
$this->request['uri']['user'] = $this->config['request']['auth']['user'];
$this->request['uri']['pass'] = $this->config['request']['auth']['pass'];
}
$this->_configUri($this->request['uri']);
if (isset($host)) {
@ -251,6 +261,9 @@ class HttpSocket extends CakeSocket {
$this->request['header'] = array_merge(compact('Host'), $this->request['header']);
}
if (isset($this->request['uri']['user'], $this->request['uri']['pass'])) {
$this->setAuthConfig('Basic', $this->request['uri']['user'], $this->request['uri']['pass']);
}
$this->_setAuth();
$this->_setProxyConfig();
@ -457,28 +470,18 @@ class HttpSocket extends CakeSocket {
* @throws Exception
*/
protected function _setAuth() {
if ($this->request['auth']['method'] === false) {
if (empty($this->_auth)) {
return;
}
if (empty($this->request['auth']['method'])) {
if (isset($this->request['uri']['user'], $this->request['uri']['pass']) && !isset($this->request['auth']['user'])) {
$this->request['auth'] = array(
'method' => 'Basic',
'user' => $this->request['uri']['user'],
'pass' => $this->request['uri']['pass']
);
} else {
return;
}
}
$authClass = Inflector::camelize($this->request['auth']['method']) . 'Authentication';
$method = key($this->_auth);
$authClass = Inflector::camelize($method) . 'Authentication';
if (!App::import('Lib', 'http/' . $authClass)) {
throw new Exception(__('Unknown authentication method.'));
}
if (!method_exists($authClass, 'authentication')) {
throw new Exception(sprintf(__('The %s do not support authentication.'), $authClass));
}
call_user_func("$authClass::authentication", $this);
call_user_func("$authClass::authentication", $this, &$this->_auth[$method]);
}
/**
@ -677,8 +680,7 @@ class HttpSocket extends CakeSocket {
}
$config = array(
'request' => array(
'uri' => array_intersect_key($uri, $this->config['request']['uri']),
'auth' => array_intersect_key($uri, $this->config['request']['auth'])
'uri' => array_intersect_key($uri, $this->config['request']['uri'])
)
);
$this->config = Set::merge($this->config, $config);

View file

@ -36,13 +36,13 @@ class BasicMethodTest extends CakeTestCase {
*/
public function testAuthentication() {
$http = new HttpSocket();
$http->request['auth'] = array(
$auth = array(
'method' => 'Basic',
'user' => 'mark',
'pass' => 'secret'
);
BasicAuthentication::authentication($http);
BasicAuthentication::authentication($http, $auth);
$this->assertEqual($http->request['header']['Authorization'], 'Basic bWFyazpzZWNyZXQ=');
}

View file

@ -31,10 +31,11 @@ class TestAuthentication {
* authentication method
*
* @param HttpSocket $http
* @param array $authInfo
* @return void
*/
public static function authentication(HttpSocket $http) {
$http->request['header']['Authorization'] = 'Test ' . $http->request['auth']['user'] . '.' . $http->request['auth']['pass'];
public static function authentication(HttpSocket $http, &$authInfo) {
$http->request['header']['Authorization'] = 'Test ' . $authInfo['user'] . '.' . $authInfo['pass'];
}
/**
@ -286,11 +287,6 @@ class HttpSocketTest extends CakeTestCase {
, 'host' => 'www.cakephp.org'
, 'port' => 23
),
'auth' => array(
'method' => 'Basic'
, 'user' => 'bob'
, 'pass' => 'secret'
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
@ -322,11 +318,6 @@ class HttpSocketTest extends CakeTestCase {
, 'host' => 'www.foo.com'
, 'port' => 80
),
'auth' => array(
'method' => 'Basic'
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
@ -374,11 +365,6 @@ class HttpSocketTest extends CakeTestCase {
'scheme' => 'http'
, 'host' => 'www.cakephp.org'
, 'port' => 80,
)
, 'auth' => array(
'method' => 'Basic'
,'user' => null
,'pass' => null
),
'proxy' => array(
'method' => 'Basic',
@ -401,11 +387,6 @@ class HttpSocketTest extends CakeTestCase {
, 'path' => '/'
, 'query' => array('foo' => 'bar')
, 'fragment' => null
)
, 'auth' => array(
'method' => 'Basic'
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
@ -677,11 +658,7 @@ class HttpSocketTest extends CakeTestCase {
$this->assertEqual($this->Socket->config['host'], 'proxy.server');
$this->assertEqual($this->Socket->config['port'], 123);
$request['auth'] = array(
'method' => 'Test',
'user' => 'login',
'pass' => 'passwd'
);
$this->Socket->setAuthConfig('Test', 'login', 'passwd');
$expected = "GET http://www.cakephp.org/ HTTP/1.1\r\nHost: www.cakephp.org\r\nConnection: close\r\nUser-Agent: CakePHP\r\nAuthorization: Test login.passwd\r\nProxy-Authorization: Test mark.secret\r\n\r\n";
$this->Socket->request($request);
$this->assertEqual($this->Socket->request['raw'], $expected);
@ -785,24 +762,15 @@ class HttpSocketTest extends CakeTestCase {
*/
function testConsecutiveGetResetsAuthCredentials() {
$socket = new MockHttpSocket();
$socket->config['request']['auth'] = array(
'method' => 'Basic',
'user' => 'mark',
'pass' => 'secret'
);
$socket->get('http://mark:secret@example.com/test');
$this->assertEqual($socket->request['uri']['user'], 'mark');
$this->assertEqual($socket->request['uri']['pass'], 'secret');
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
$socket->get('/test2');
$this->assertEqual($socket->request['auth']['user'], 'mark');
$this->assertEqual($socket->request['auth']['pass'], 'secret');
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
$socket->get('/test3');
$this->assertEqual($socket->request['auth']['user'], 'mark');
$this->assertEqual($socket->request['auth']['pass'], 'secret');
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
}