Skip to content

Commit f76bd59

Browse files
Merge pull request #312 from all3nT/master
Controller Wildcard Authentication Rules
2 parents 0cf142a + e510522 commit f76bd59

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
@@ -97,6 +97,8 @@
9797
| If 'rest_auth' is 'session' then set 'auth_source' to the name of the session variable to check for.
9898
|
9999
*/
100+
101+
//change this to '' for wildcard unit test
100102
$config['auth_source'] = 'ldap';
101103

102104
/*
@@ -130,15 +132,20 @@
130132
| $config['auth_override_class_method']['deals']['view'] = 'none';
131133
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
132134
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
135+
| $config['auth_override_class_method']['dashboard']['*'] = 'none|digest|basic';
133136
|
134-
| 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)
137+
| 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)
135138
| Acceptable values are; 'none', 'digest' and 'basic'.
136139
|
137140
*/
138141
// $config['auth_override_class_method']['deals']['view'] = 'none';
139142
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
140143
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
144+
// $config['auth_override_class_method']['dashboard']['*'] = 'basic';
145+
141146

147+
//---Uncomment list line for the wildard unit test
148+
//$config['auth_override_class_method']['wildcard_test_cases']['*'] = 'basic';
142149
/*
143150
|--------------------------------------------------------------------------
144151
| 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
@@ -852,6 +852,36 @@ protected function _auth_override_check()
852852
return false;
853853
}
854854

855+
// check for wildcard flag for rules for classes
856+
if(!empty($this->overrides_array[$this->router->class]['*'])){//check for class overides
857+
// None auth override found, prepare nothing but send back a true override flag
858+
if ($this->overrides_array[$this->router->class]['*'] == 'none')
859+
{
860+
return true;
861+
}
862+
863+
// Basic auth override found, prepare basic
864+
if ($this->overrides_array[$this->router->class]['*'] == 'basic')
865+
{
866+
$this->_prepare_basic_auth();
867+
return true;
868+
}
869+
870+
// Digest auth override found, prepare digest
871+
if ($this->overrides_array[$this->router->class]['*'] == 'digest')
872+
{
873+
$this->_prepare_digest_auth();
874+
return true;
875+
}
876+
877+
// Whitelist auth override found, check client's ip against config whitelist
878+
if ($this->overrides_array[$this->router->class]['*'] == 'whitelist')
879+
{
880+
$this->_check_whitelist_auth();
881+
return true;
882+
}
883+
}
884+
855885
// Check to see if there's an override value set for the current class/method being called
856886
if (empty($this->overrides_array[$this->router->class][$this->router->method])) {
857887
return false;

0 commit comments

Comments
 (0)