Skip to content

Commit cd24aca

Browse files
committed
Removing auth key from request and putting this as attribute.
1 parent e1e8026 commit cd24aca

File tree

4 files changed

+46
-75
lines changed

4 files changed

+46
-75
lines changed

cake/libs/http/basic_authentication.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class BasicAuthentication {
3030
* Authentication
3131
*
3232
* @param HttpSocket $http
33+
* @param array $authInfo
3334
* @return void
3435
* @see http://www.ietf.org/rfc/rfc2617.txt
3536
*/
36-
public static function authentication(HttpSocket $http) {
37-
if (isset($http->request['auth']['user'], $http->request['auth']['pass'])) {
38-
$http->request['header']['Authorization'] = self::_generateHeader($http->request['auth']['user'], $http->request['auth']['pass']);
37+
public static function authentication(HttpSocket $http, &$authInfo) {
38+
if (isset($authInfo['user'], $authInfo['pass'])) {
39+
$http->request['header']['Authorization'] = self::_generateHeader($authInfo['user'], $authInfo['pass']);
3940
}
4041
}
4142

cake/libs/http_socket.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ class HttpSocket extends CakeSocket {
5959
'query' => null,
6060
'fragment' => null
6161
),
62-
'auth' => array(
63-
'method' => 'Basic',
64-
'user' => null,
65-
'pass' => null
66-
),
6762
'proxy' => array(
6863
'method' => 'Basic',
6964
'host' => null,
@@ -123,11 +118,6 @@ class HttpSocket extends CakeSocket {
123118
'host' => 'localhost',
124119
'port' => 80
125120
),
126-
'auth' => array(
127-
'method' => 'Basic',
128-
'user' => null,
129-
'pass' => null
130-
),
131121
'proxy' => array(
132122
'method' => 'Basic',
133123
'host' => null,
@@ -147,6 +137,14 @@ class HttpSocket extends CakeSocket {
147137
*/
148138
public $lineBreak = "\r\n";
149139

140+
/**
141+
* Authentication settings
142+
*
143+
* @var array
144+
* @access protected
145+
*/
146+
protected $_auth = array();
147+
150148
/**
151149
* Build an HTTP Socket using the specified configuration.
152150
*
@@ -181,6 +179,26 @@ public function __construct($config = array()) {
181179
parent::__construct($this->config);
182180
}
183181

182+
/**
183+
* Set authentication settings
184+
*
185+
* @param string $method Authentication method (ex. Basic, Digest). If empty, disable authentication
186+
* @param mixed $user Username for authentication. Can be an array with settings to authentication class
187+
* @param string $pass Password for authentication
188+
* @return void
189+
*/
190+
public function setAuthConfig($method, $user, $pass = null) {
191+
if (empty($method)) {
192+
$this->_auth = array();
193+
return;
194+
}
195+
if (is_array($user)) {
196+
$this->_auth = array($method => $user);
197+
return;
198+
}
199+
$this->_auth = array($method => compact('user', 'pass'));
200+
}
201+
184202
/**
185203
* Issue the specified request. HttpSocket::get() and HttpSocket::post() wrap this
186204
* method and provide a more granular interface.
@@ -201,10 +219,6 @@ public function request($request = array()) {
201219
$request['uri'] = null;
202220
}
203221
$uri = $this->_parseUri($request['uri']);
204-
$hadAuth = false;
205-
if (is_array($uri) && array_key_exists('user', $uri)) {
206-
$hadAuth = true;
207-
}
208222
if (!isset($uri['host'])) {
209223
$host = $this->config['host'];
210224
}
@@ -216,10 +230,6 @@ public function request($request = array()) {
216230
$request['uri'] = $this->_parseUri($request['uri'], true);
217231
$this->request = Set::merge($this->request, $this->config['request'], $request);
218232

219-
if (!$hadAuth && !empty($this->config['request']['auth']['user'])) {
220-
$this->request['uri']['user'] = $this->config['request']['auth']['user'];
221-
$this->request['uri']['pass'] = $this->config['request']['auth']['pass'];
222-
}
223233
$this->_configUri($this->request['uri']);
224234

225235
if (isset($host)) {
@@ -251,6 +261,9 @@ public function request($request = array()) {
251261
$this->request['header'] = array_merge(compact('Host'), $this->request['header']);
252262
}
253263

264+
if (isset($this->request['uri']['user'], $this->request['uri']['pass'])) {
265+
$this->setAuthConfig('Basic', $this->request['uri']['user'], $this->request['uri']['pass']);
266+
}
254267
$this->_setAuth();
255268
$this->_setProxyConfig();
256269

@@ -457,28 +470,18 @@ public function url($url = null, $uriTemplate = null) {
457470
* @throws Exception
458471
*/
459472
protected function _setAuth() {
460-
if ($this->request['auth']['method'] === false) {
473+
if (empty($this->_auth)) {
461474
return;
462475
}
463-
if (empty($this->request['auth']['method'])) {
464-
if (isset($this->request['uri']['user'], $this->request['uri']['pass']) && !isset($this->request['auth']['user'])) {
465-
$this->request['auth'] = array(
466-
'method' => 'Basic',
467-
'user' => $this->request['uri']['user'],
468-
'pass' => $this->request['uri']['pass']
469-
);
470-
} else {
471-
return;
472-
}
473-
}
474-
$authClass = Inflector::camelize($this->request['auth']['method']) . 'Authentication';
476+
$method = key($this->_auth);
477+
$authClass = Inflector::camelize($method) . 'Authentication';
475478
if (!App::import('Lib', 'http/' . $authClass)) {
476479
throw new Exception(__('Unknown authentication method.'));
477480
}
478481
if (!method_exists($authClass, 'authentication')) {
479482
throw new Exception(sprintf(__('The %s do not support authentication.'), $authClass));
480483
}
481-
call_user_func("$authClass::authentication", $this);
484+
call_user_func("$authClass::authentication", $this, &$this->_auth[$method]);
482485
}
483486

484487
/**
@@ -677,8 +680,7 @@ protected function _configUri($uri = null) {
677680
}
678681
$config = array(
679682
'request' => array(
680-
'uri' => array_intersect_key($uri, $this->config['request']['uri']),
681-
'auth' => array_intersect_key($uri, $this->config['request']['auth'])
683+
'uri' => array_intersect_key($uri, $this->config['request']['uri'])
682684
)
683685
);
684686
$this->config = Set::merge($this->config, $config);

cake/tests/cases/libs/http/basic_authentication.test.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ class BasicMethodTest extends CakeTestCase {
3636
*/
3737
public function testAuthentication() {
3838
$http = new HttpSocket();
39-
$http->request['auth'] = array(
39+
$auth = array(
4040
'method' => 'Basic',
4141
'user' => 'mark',
4242
'pass' => 'secret'
4343
);
4444

45-
BasicAuthentication::authentication($http);
45+
BasicAuthentication::authentication($http, $auth);
4646
$this->assertEqual($http->request['header']['Authorization'], 'Basic bWFyazpzZWNyZXQ=');
4747
}
4848

cake/tests/cases/libs/http_socket.test.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ class TestAuthentication {
3131
* authentication method
3232
*
3333
* @param HttpSocket $http
34+
* @param array $authInfo
3435
* @return void
3536
*/
36-
public static function authentication(HttpSocket $http) {
37-
$http->request['header']['Authorization'] = 'Test ' . $http->request['auth']['user'] . '.' . $http->request['auth']['pass'];
37+
public static function authentication(HttpSocket $http, &$authInfo) {
38+
$http->request['header']['Authorization'] = 'Test ' . $authInfo['user'] . '.' . $authInfo['pass'];
3839
}
3940

4041
/**
@@ -286,11 +287,6 @@ function testConfigUri() {
286287
, 'host' => 'www.cakephp.org'
287288
, 'port' => 23
288289
),
289-
'auth' => array(
290-
'method' => 'Basic'
291-
, 'user' => 'bob'
292-
, 'pass' => 'secret'
293-
),
294290
'proxy' => array(
295291
'method' => 'Basic',
296292
'host' => null,
@@ -322,11 +318,6 @@ function testConfigUri() {
322318
, 'host' => 'www.foo.com'
323319
, 'port' => 80
324320
),
325-
'auth' => array(
326-
'method' => 'Basic'
327-
, 'user' => null
328-
, 'pass' => null
329-
),
330321
'proxy' => array(
331322
'method' => 'Basic',
332323
'host' => null,
@@ -374,11 +365,6 @@ function testRequest() {
374365
'scheme' => 'http'
375366
, 'host' => 'www.cakephp.org'
376367
, 'port' => 80,
377-
)
378-
, 'auth' => array(
379-
'method' => 'Basic'
380-
,'user' => null
381-
,'pass' => null
382368
),
383369
'proxy' => array(
384370
'method' => 'Basic',
@@ -401,11 +387,6 @@ function testRequest() {
401387
, 'path' => '/'
402388
, 'query' => array('foo' => 'bar')
403389
, 'fragment' => null
404-
)
405-
, 'auth' => array(
406-
'method' => 'Basic'
407-
, 'user' => null
408-
, 'pass' => null
409390
),
410391
'proxy' => array(
411392
'method' => 'Basic',
@@ -677,11 +658,7 @@ function testProxy() {
677658
$this->assertEqual($this->Socket->config['host'], 'proxy.server');
678659
$this->assertEqual($this->Socket->config['port'], 123);
679660

680-
$request['auth'] = array(
681-
'method' => 'Test',
682-
'user' => 'login',
683-
'pass' => 'passwd'
684-
);
661+
$this->Socket->setAuthConfig('Test', 'login', 'passwd');
685662
$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";
686663
$this->Socket->request($request);
687664
$this->assertEqual($this->Socket->request['raw'], $expected);
@@ -785,24 +762,15 @@ function testGet() {
785762
*/
786763
function testConsecutiveGetResetsAuthCredentials() {
787764
$socket = new MockHttpSocket();
788-
$socket->config['request']['auth'] = array(
789-
'method' => 'Basic',
790-
'user' => 'mark',
791-
'pass' => 'secret'
792-
);
793765
$socket->get('http://mark:secret@example.com/test');
794766
$this->assertEqual($socket->request['uri']['user'], 'mark');
795767
$this->assertEqual($socket->request['uri']['pass'], 'secret');
796768
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
797769

798770
$socket->get('/test2');
799-
$this->assertEqual($socket->request['auth']['user'], 'mark');
800-
$this->assertEqual($socket->request['auth']['pass'], 'secret');
801771
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
802772

803773
$socket->get('/test3');
804-
$this->assertEqual($socket->request['auth']['user'], 'mark');
805-
$this->assertEqual($socket->request['auth']['pass'], 'secret');
806774
$this->assertTrue(strpos($socket->request['header'], 'Authorization: Basic bWFyazpzZWNyZXQ=') !== false);
807775
}
808776

0 commit comments

Comments
 (0)