diff --git a/cake/libs/http/basic_method.php b/cake/libs/http/basic_method.php new file mode 100644 index 000000000..da7951c6c --- /dev/null +++ b/cake/libs/http/basic_method.php @@ -0,0 +1,42 @@ +request['auth']['user'], $http->request['auth']['pass'])) { + $http->request['header']['Authorization'] = 'Basic ' . base64_encode($http->request['auth']['user'] . ':' . $http->request['auth']['pass']); + } + } + +} diff --git a/cake/libs/http_socket.php b/cake/libs/http_socket.php index b25020530..3def34cea 100644 --- a/cake/libs/http_socket.php +++ b/cake/libs/http_socket.php @@ -236,12 +236,7 @@ class HttpSocket extends CakeSocket { $this->request['header'] = array_merge(compact('Host'), $this->request['header']); } - if (isset($this->request['auth']['user']) && isset($this->request['auth']['pass'])) { - $this->request['header']['Authorization'] = $this->request['auth']['method'] . " " . base64_encode($this->request['auth']['user'] . ":" . $this->request['auth']['pass']); - } - if (isset($this->request['uri']['user']) && isset($this->request['uri']['pass'])) { - $this->request['header']['Authorization'] = $this->request['auth']['method'] . " " . base64_encode($this->request['uri']['user'] . ":" . $this->request['uri']['pass']); - } + $this->_setAuth(); if (is_array($this->request['body'])) { $this->request['body'] = $this->_httpSerialize($this->request['body']); @@ -439,6 +434,31 @@ class HttpSocket extends CakeSocket { return $this->_buildUri($url); } +/** + * Set authentication in request + * + * @return void + * @throws Exception + */ + protected function _setAuth() { + if (empty($this->request['auth']['method'])) { + if (isset($this->request['uri']['user'], $this->request['uri']['pass'])) { + $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']) . 'Method'; + if (!App::import('Lib', 'http/' . $authClass)) { + throw new Exception(__('Authentication method unknown.')); + } + $authClass::authentication($this); + } + /** * Parses the given message and breaks it down in parts. * diff --git a/cake/tests/cases/libs/http/basic_method.test.php b/cake/tests/cases/libs/http/basic_method.test.php new file mode 100644 index 000000000..c9d487f70 --- /dev/null +++ b/cake/tests/cases/libs/http/basic_method.test.php @@ -0,0 +1,49 @@ + + * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice + * + * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs.http + * @since CakePHP(tm) v 2.0.0 + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) + */ + +App::import('Core', 'HttpSocket'); +App::import('Lib', 'http/BasicMethod'); + +/** + * BasicMethodTest class + * + * @package cake + * @subpackage cake.tests.cases.libs.http + */ +class BasicMethodTest extends CakeTestCase { + +/** + * testAuthentication method + * + * @return void + */ + public function testAuthentication() { + $http = new HttpSocket(); + $http->request['auth'] = array( + 'method' => 'Basic', + 'user' => 'mark', + 'pass' => 'secret' + ); + + BasicMethod::authentication($http); + $this->assertEqual($http->request['header']['Authorization'], 'Basic bWFyazpzZWNyZXQ='); + } + +} \ No newline at end of file diff --git a/cake/tests/cases/libs/http_socket.test.php b/cake/tests/cases/libs/http_socket.test.php index 5b3f79e0c..beb0fc0c5 100644 --- a/cake/tests/cases/libs/http_socket.test.php +++ b/cake/tests/cases/libs/http_socket.test.php @@ -694,14 +694,17 @@ class HttpSocketTest extends CakeTestCase { $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); } /**