Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Sturgeon committed Mar 2, 2011
2 parents f5eca71 + f63915d commit 2fdb704
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 10 deletions.
25 changes: 24 additions & 1 deletion application/config/rest.php
Expand Up @@ -49,6 +49,29 @@
*/
$config['rest_auth'] = '';

/*
|--------------------------------------------------------------------------
| Override auth types for specific class/method
|--------------------------------------------------------------------------
|
| Set specific authentication types for methods within a class (controller)
|
| Set as many config entries as needed. Any methods not set will use the default 'rest_auth' config value.
|
| example:
|
| $config['auth_override_class_method']['deals']['view'] = 'none';
| $config['auth_override_class_method']['deals']['insert'] = 'digest';
| $config['auth_override_class_method']['accounts']['user'] = 'basic';
|
| 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)
| Acceptable values are; 'none', 'digest' and 'basic'.
|
*/
// $config['auth_override_class_method']['deals']['view'] = 'none';
// $config['auth_override_class_method']['deals']['insert'] = 'digest';
// $config['auth_override_class_method']['accounts']['user'] = 'basic';

/*
|--------------------------------------------------------------------------
| REST Login usernames
Expand Down Expand Up @@ -119,7 +142,7 @@
| Max: 40
|
*/
$config['rest_key_length'] = 32;
$config['rest_key_length'] = 40;

/*
|--------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions application/controllers/api/key.php
Expand Up @@ -25,9 +25,9 @@ class Key extends REST_Controller
);

/**
* Key Delete
* Key Create
*
* Remove a key from the database to stop it working.
* Insert a key into the database.
*
* @access public
* @return void
Expand Down
71 changes: 64 additions & 7 deletions application/libraries/REST_Controller.php
Expand Up @@ -37,13 +37,21 @@ public function __construct()
$this->request->method = $this->_detect_method();

$this->load->library('security');
if ($this->config->item('rest_auth') == 'basic')
{
$this->_prepare_basic_auth();
}
elseif ($this->config->item('rest_auth') == 'digest')
{
$this->_prepare_digest_auth();

// Check if there is a specific auth type for the current class/method
$this->auth_override = $this->_auth_override_check();

// When there is no specific override for the current class/method, use the default auth value set in the config
if ( $this->auth_override !== TRUE )
{
if ($this->config->item('rest_auth') == 'basic')
{
$this->_prepare_basic_auth();
}
elseif ($this->config->item('rest_auth') == 'digest')
{
$this->_prepare_digest_auth();
}
}

// Some Methods cant have a body
Expand Down Expand Up @@ -455,6 +463,55 @@ private function _check_limit($controller_method)

return TRUE;
}
/*
* Auth override check
*
* Check if there is a specific auth type set for the current class/method being called
*/

private function _auth_override_check()
{

// Assign the class/method auth type override array from the config
$this->overrides_array = $this->config->item('auth_override_class_method');

// Check to see if the override array is even populated, otherwise return false
if ( empty($this->overrides_array) )
{
return false;
}

// Check to see if there's an override value set for the current class/method being called
if ( empty($this->overrides_array[$this->router->class][$this->router->method]) )
{
return false;
}

// None auth override found, prepare nothing but send back a true override flag
if ($this->overrides_array[$this->router->class][$this->router->method] == 'none')
{
return true;
}

// Basic auth override found, prepare basic
if ($this->overrides_array[$this->router->class][$this->router->method] == 'basic')
{
$this->_prepare_basic_auth();
return true;
}

// Digest auth override found, prepare digest
if ($this->overrides_array[$this->router->class][$this->router->method] == 'digest')
{
$this->_prepare_digest_auth();
return true;
}

// Return false when there is an override value set but it doesn't match 'basic', 'digest', or 'none'. (the value was misspelled)
return false;

}


// INPUT FUNCTION --------------------------------------------------------------

Expand Down

0 comments on commit 2fdb704

Please sign in to comment.