Skip to content

Commit e510522

Browse files
committed
Rebased and added wildcard feature. This allows authentication rules to be defined at the controller level as well as the method level.
1 parent 976ca45 commit e510522

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

application/config/rest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
| If 'rest_auth' is 'session' then set 'auth_source' to the name of the session variable to check for.
7474
|
7575
*/
76+
77+
//change this to '' for wildcard unit test
7678
$config['auth_source'] = 'ldap';
7779

7880
/*
@@ -103,15 +105,20 @@
103105
| $config['auth_override_class_method']['deals']['view'] = 'none';
104106
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
105107
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
108+
| $config['auth_override_class_method']['dashboard']['*'] = 'none|digest|basic';
106109
|
107-
| Here 'deals' and 'accounts' are controller names, 'view', 'insert' and 'user' are methods within. (NOTE: leave off the '_get' or '_post' from the end of the method name)
110+
| Here 'deals', 'accounts' and 'dashboard' are controller names, 'view', 'insert' and 'user' are methods within. An asterisk may also be used to specify an authentication method for an entire classes methods. Ex: $config['auth_override_class_method']['dashboard']['*'] = 'basic'; (NOTE: leave off the '_get' or '_post' from the end of the method name)
108111
| Acceptable values are; 'none', 'digest' and 'basic'.
109112
|
110113
*/
111114
// $config['auth_override_class_method']['deals']['view'] = 'none';
112115
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
113116
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
117+
// $config['auth_override_class_method']['dashboard']['*'] = 'basic';
118+
114119

120+
//---Uncomment list line for the wildard unit test
121+
//$config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic';
115122
/*
116123
|--------------------------------------------------------------------------
117124
| REST Login usernames
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2+
3+
/**
4+
* Example
5+
*
6+
* This is a test for the wildcard .
7+
*
8+
* @package CodeIgniter
9+
* @subpackage Rest Server
10+
* @category Controller
11+
* @author Allen Taylor
12+
* @link http://philsturgeon.co.uk/code/
13+
*/
14+
15+
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
16+
17+
/*
18+
In order for this test to work you will need to change the auth_source option in the rest.php config file to '' and uncomment this line $config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic'; in the file as well. Once these are uncommented the tests will work.
19+
*/
20+
require APPPATH.'/libraries/REST_Controller.php';
21+
class Wildcard_test_cases extends REST_Controller{
22+
function __construct(){
23+
parent::__construct();
24+
//set config for test
25+
$this->config->load('rest');
26+
$this->config->set_item('rest_auth', 'none');//turn on rest auth
27+
$this->config->set_item('auth_source', '');//use config array for authentication
28+
$this->config->set_item('auth_override_class_method', array('wildcard_test_cases' => array('*' => 'basic')));
29+
$this->load->helper('url');
30+
}
31+
32+
33+
function digest_get(){
34+
$this->response("welcome", 200);
35+
}
36+
}
37+
?>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php defined('BASEPATH') OR exit('No direct script access allowed');
2+
3+
/**
4+
* Example
5+
*
6+
* This is a test for the wildcard. Wildcard allows you to specify an authentication type rule for an entire controller. Example would be $config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic'; This sets the authentication method for the Wildcard_test_harness controller to basic.
7+
*
8+
* @package CodeIgniter
9+
* @subpackage Rest Server
10+
* @category Controller
11+
* @author Allen Taylor
12+
* @link http://philsturgeon.co.uk/code/
13+
*/
14+
15+
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
16+
17+
/*
18+
In order for this test to work you will need to change the auth_source option in the rest.php config file to '' and uncomment this line $config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic'; in the file as well. Once these are uncommented the tests will work.
19+
*/
20+
class Wildcard_test_harness extends CI_Controller
21+
{
22+
function __construct(){
23+
parent::__construct();
24+
$this->load->library('unit_test');
25+
$this->load->helper('url');
26+
}
27+
28+
//curl interface functions
29+
private function makeRequest($url, $cred = '', $curlopts = array()){
30+
$ch = curl_init($url);
31+
$items = array(
32+
CURLOPT_URL => $url,
33+
CURLOPT_USERPWD => $cred
34+
);
35+
foreach($curlopts as $opt => $value)
36+
$items[$opt] = $value;
37+
curl_setopt_array($ch, $items);
38+
ob_start();
39+
$response = curl_exec($ch);
40+
$contents = ob_get_contents();
41+
ob_end_clean();
42+
$info = curl_getinfo($ch);
43+
44+
$errno = curl_errno($ch);
45+
$error = curl_error($ch);
46+
curl_close($ch);
47+
return array('response' => $response, 'contents' => $contents, 'errno' => $errno, 'error' => $error, 'info' => $info);//return
48+
}
49+
50+
/*
51+
These two test cases will test if the authentication is working for the wildcard method. The curl requests may not work if you do not have an .htaccess file with mod rewrite in the same directory as your index.php file. If you don't have that file you can add it or change the url below to the one that includes index.php.
52+
*/
53+
function index(){
54+
55+
//not authorized
56+
//no htaccess: $test = $this->makeRequest(base_url() . 'index.php/unit_tests/wildcard_test_cases/digest', '');
57+
$test = $this->makeRequest(base_url() . 'unit_tests/wildcard_test_cases/digest', '');
58+
// print_r($test);
59+
$this->unit->run($test['info']['http_code'], '401', 'Not Authorized test (No credentials provided)');
60+
//no htaccess: $test = $this->makeRequest(base_url() . 'index.php/unit_tests/wildcard_test_cases/digest', 'admin:1234');
61+
$test = $this->makeRequest(base_url() . 'unit_tests/wildcard_test_cases/digest', 'admin:1234');
62+
//print_r($test);
63+
$this->unit->run($test['info']['http_code'], '200', 'Authorized, credentials given');
64+
echo $this->unit->report();
65+
}
66+
}
67+
?>

application/libraries/REST_Controller.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,36 @@ protected function _auth_override_check()
840840
return false;
841841
}
842842

843+
// check for wildcard flag for rules for classes
844+
if(!empty($this->overrides_array[$this->router->class]['*'])){//check for class overides
845+
// None auth override found, prepare nothing but send back a true override flag
846+
if ($this->overrides_array[$this->router->class]['*'] == 'none')
847+
{
848+
return true;
849+
}
850+
851+
// Basic auth override found, prepare basic
852+
if ($this->overrides_array[$this->router->class]['*'] == 'basic')
853+
{
854+
$this->_prepare_basic_auth();
855+
return true;
856+
}
857+
858+
// Digest auth override found, prepare digest
859+
if ($this->overrides_array[$this->router->class]['*'] == 'digest')
860+
{
861+
$this->_prepare_digest_auth();
862+
return true;
863+
}
864+
865+
// Whitelist auth override found, check client's ip against config whitelist
866+
if ($this->overrides_array[$this->router->class]['*'] == 'whitelist')
867+
{
868+
$this->_check_whitelist_auth();
869+
return true;
870+
}
871+
}
872+
843873
// Check to see if there's an override value set for the current class/method being called
844874
if (empty($this->overrides_array[$this->router->class][$this->router->method])) {
845875
return false;

0 commit comments

Comments
 (0)