mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
separeted auth from HttpSocket. Refs #880
This commit is contained in:
parent
ef2a7d4608
commit
777afb6d3e
4 changed files with 120 additions and 6 deletions
42
cake/libs/http/basic_method.php
Normal file
42
cake/libs/http/basic_method.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* Basic authentication
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
||||
* 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://cakephp.org CakePHP(tm) Project
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
* @since CakePHP(tm) v 2.0.0
|
||||
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Basic authentication
|
||||
*
|
||||
* @package cake
|
||||
* @subpackage cake.cake.libs.http
|
||||
*/
|
||||
class BasicMethod {
|
||||
|
||||
/**
|
||||
* Authentication
|
||||
*
|
||||
* @param HttpSocket $http
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function authentication(&$http) {
|
||||
if (isset($http->request['auth']['user'], $http->request['auth']['pass'])) {
|
||||
$http->request['header']['Authorization'] = 'Basic ' . base64_encode($http->request['auth']['user'] . ':' . $http->request['auth']['pass']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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.
|
||||
*
|
||||
|
|
49
cake/tests/cases/libs/http/basic_method.test.php
Normal file
49
cake/tests/cases/libs/http/basic_method.test.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
/**
|
||||
* BasicMethodTest file
|
||||
*
|
||||
* PHP 5
|
||||
*
|
||||
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
||||
* 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=');
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue