Skip to content

Commit 2fdb704

Browse files
author
Phil Sturgeon
committed
2 parents f5eca71 + f63915d commit 2fdb704

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

application/config/rest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@
4949
*/
5050
$config['rest_auth'] = '';
5151

52+
/*
53+
|--------------------------------------------------------------------------
54+
| Override auth types for specific class/method
55+
|--------------------------------------------------------------------------
56+
|
57+
| Set specific authentication types for methods within a class (controller)
58+
|
59+
| Set as many config entries as needed. Any methods not set will use the default 'rest_auth' config value.
60+
|
61+
| example:
62+
|
63+
| $config['auth_override_class_method']['deals']['view'] = 'none';
64+
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
65+
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
66+
|
67+
| 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)
68+
| Acceptable values are; 'none', 'digest' and 'basic'.
69+
|
70+
*/
71+
// $config['auth_override_class_method']['deals']['view'] = 'none';
72+
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
73+
// $config['auth_override_class_method']['accounts']['user'] = 'basic';
74+
5275
/*
5376
|--------------------------------------------------------------------------
5477
| REST Login usernames
@@ -119,7 +142,7 @@
119142
| Max: 40
120143
|
121144
*/
122-
$config['rest_key_length'] = 32;
145+
$config['rest_key_length'] = 40;
123146

124147
/*
125148
|--------------------------------------------------------------------------

application/controllers/api/key.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Key extends REST_Controller
2525
);
2626

2727
/**
28-
* Key Delete
28+
* Key Create
2929
*
30-
* Remove a key from the database to stop it working.
30+
* Insert a key into the database.
3131
*
3232
* @access public
3333
* @return void

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->request->method = $this->_detect_method();
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
@@ -455,6 +463,55 @@ private function _check_limit($controller_method)
455463

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

459516
// INPUT FUNCTION --------------------------------------------------------------
460517

0 commit comments

Comments
 (0)