Skip to content

Commit f63915d

Browse files
committed
Added the ability to choose an overriding auth type per class/method by setting the variable 'auth_override_class_method' in the rest config.
1 parent f2bfbd7 commit f63915d

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

application/config/rest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@
3636
*/
3737
$config['rest_auth'] = '';
3838

39+
/*
40+
|--------------------------------------------------------------------------
41+
| Override auth types for specific class/method
42+
|--------------------------------------------------------------------------
43+
|
44+
| Set specific authentication types for methods within a class (controller)
45+
|
46+
| Set as many config entries as needed. Any methods not set will use the default 'rest_auth' config value.
47+
|
48+
| example:
49+
|
50+
| $config['auth_override_class_method']['deals']['view'] = 'none';
51+
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
52+
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
53+
|
54+
| 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)
55+
| Acceptable values are; 'none', 'digest' and 'basic'.
56+
|
57+
*/
58+
// $config['auth_override_class_method']['deals']['view'] = 'none';
59+
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
60+
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
61+
3962
/*
4063
|--------------------------------------------------------------------------
4164
| REST Login usernames

application/libraries/REST_Controller.php

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,21 @@ public function __construct()
3737
$this->load->config('rest');
3838

3939
$this->load->library('security');
40-
if ($this->config->item('rest_auth') == 'basic')
41-
{
42-
$this->_prepare_basic_auth();
43-
}
44-
elseif ($this->config->item('rest_auth') == 'digest')
45-
{
46-
$this->_prepare_digest_auth();
40+
41+
// Check if there is a specific auth type for the current class/method
42+
$this->auth_override = $this->_auth_override_check();
43+
44+
// When there is no specific override for the current class/method, use the default auth value set in the config
45+
if ( $this->auth_override !== TRUE )
46+
{
47+
if ($this->config->item('rest_auth') == 'basic')
48+
{
49+
$this->_prepare_basic_auth();
50+
}
51+
elseif ($this->config->item('rest_auth') == 'digest')
52+
{
53+
$this->_prepare_digest_auth();
54+
}
4755
}
4856

4957
// Some Methods cant have a body
@@ -445,6 +453,55 @@ private function _check_limit($controller_method)
445453

446454
return TRUE;
447455
}
456+
/*
457+
* Auth override check
458+
*
459+
* Check if there is a specific auth type set for the current class/method being called
460+
*/
461+
462+
private function _auth_override_check()
463+
{
464+
465+
// Assign the class/method auth type override array from the config
466+
$this->overrides_array = $this->config->item('auth_override_class_method');
467+
468+
// Check to see if the override array is even populated, otherwise return false
469+
if ( empty($this->overrides_array) )
470+
{
471+
return false;
472+
}
473+
474+
// Check to see if there's an override value set for the current class/method being called
475+
if ( empty($this->overrides_array[$this->router->class][$this->router->method]) )
476+
{
477+
return false;
478+
}
479+
480+
// None auth override found, prepare nothing but send back a true override flag
481+
if ($this->overrides_array[$this->router->class][$this->router->method] == 'none')
482+
{
483+
return true;
484+
}
485+
486+
// Basic auth override found, prepare basic
487+
if ($this->overrides_array[$this->router->class][$this->router->method] == 'basic')
488+
{
489+
$this->_prepare_basic_auth();
490+
return true;
491+
}
492+
493+
// Digest auth override found, prepare digest
494+
if ($this->overrides_array[$this->router->class][$this->router->method] == 'digest')
495+
{
496+
$this->_prepare_digest_auth();
497+
return true;
498+
}
499+
500+
// Return false when there is an override value set but it doesn't match 'basic', 'digest', or 'none'. (the value was misspelled)
501+
return false;
502+
503+
}
504+
448505

449506
// INPUT FUNCTION --------------------------------------------------------------
450507

0 commit comments

Comments
 (0)