Skip to content

Commit

Permalink
Removing auth key from request and putting this as attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 3, 2010
1 parent e1e8026 commit cd24aca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 75 deletions.
7 changes: 4 additions & 3 deletions cake/libs/http/basic_authentication.php
Expand Up @@ -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']);
}
}

Expand Down
70 changes: 36 additions & 34 deletions cake/libs/http_socket.php
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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.
*
Expand Down Expand Up @@ -181,6 +179,26 @@ public function __construct($config = array()) {
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.
Expand All @@ -201,10 +219,6 @@ public function request($request = array()) {
$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'];
}
Expand All @@ -216,10 +230,6 @@ public function request($request = array()) {
$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)) {
Expand Down Expand Up @@ -251,6 +261,9 @@ public function request($request = array()) {
$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();

Expand Down Expand Up @@ -457,28 +470,18 @@ public function url($url = null, $uriTemplate = null) {
* @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]);
}

/**
Expand Down Expand Up @@ -677,8 +680,7 @@ protected function _configUri($uri = null) {
}
$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);
Expand Down
4 changes: 2 additions & 2 deletions cake/tests/cases/libs/http/basic_authentication.test.php
Expand Up @@ -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=');
}

Expand Down
40 changes: 4 additions & 36 deletions cake/tests/cases/libs/http_socket.test.php
Expand Up @@ -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'];
}

/**
Expand Down Expand Up @@ -286,11 +287,6 @@ function testConfigUri() {
, 'host' => 'www.cakephp.org'
, 'port' => 23
),
'auth' => array(
'method' => 'Basic'
, 'user' => 'bob'
, 'pass' => 'secret'
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
Expand Down Expand Up @@ -322,11 +318,6 @@ function testConfigUri() {
, 'host' => 'www.foo.com'
, 'port' => 80
),
'auth' => array(
'method' => 'Basic'
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
'host' => null,
Expand Down Expand Up @@ -374,11 +365,6 @@ function testRequest() {
'scheme' => 'http'
, 'host' => 'www.cakephp.org'
, 'port' => 80,
)
, 'auth' => array(
'method' => 'Basic'
,'user' => null
,'pass' => null
),
'proxy' => array(
'method' => 'Basic',
Expand All @@ -401,11 +387,6 @@ function testRequest() {
, 'path' => '/'
, 'query' => array('foo' => 'bar')
, 'fragment' => null
)
, 'auth' => array(
'method' => 'Basic'
, 'user' => null
, 'pass' => null
),
'proxy' => array(
'method' => 'Basic',
Expand Down Expand Up @@ -677,11 +658,7 @@ function testProxy() {
$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);
Expand Down Expand Up @@ -785,24 +762,15 @@ function testGet() {
*/
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);
}

Expand Down

0 comments on commit cd24aca

Please sign in to comment.