Skip to content

Commit 8c53948

Browse files
Merge pull request #335 from c4xp/master
Digest fix
2 parents f76bd59 + 9469bf8 commit 8c53948

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

application/libraries/REST_Controller.php

100644100755
Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function __construct()
286286
$this->auth_override = $this->_auth_override_check();
287287

288288
// Checking for keys? GET TO WorK!
289-
// Skip keys test for $config['auth_override_class_method']['class'['method'] = 'none'
289+
// Skip keys test for $config['auth_override_class_method']['class'['method'] = 'none'
290290
if (config_item('rest_enable_keys') and $this->auth_override !== true) {
291291
$this->_allow = $this->_detect_api_key();
292292
}
@@ -444,8 +444,6 @@ protected function _fire_method($method, $args)
444444
*/
445445
public function response($data = null, $http_code = null, $continue = false)
446446
{
447-
global $CFG;
448-
449447
// If data is null and not code provide, error and bail
450448
if ($data === null && $http_code === null) {
451449
$http_code = 404;
@@ -462,7 +460,7 @@ public function response($data = null, $http_code = null, $continue = false)
462460
// Otherwise (if no data but 200 provided) or some data, carry on camping!
463461
else {
464462
// Is compression requested?
465-
if ($CFG->item('compress_output') === true && $this->_zlib_oc == false) {
463+
if ($this->config->item('compress_output') === true && $this->_zlib_oc == false) {
466464
if (extension_loaded('zlib')) {
467465
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) and strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false) {
468466
ob_start('ob_gzhandler');
@@ -501,7 +499,7 @@ public function response($data = null, $http_code = null, $continue = false)
501499
// but it will not modify the content-length header to compensate for
502500
// the reduction, causing the browser to hang waiting for more data.
503501
// We'll just skip content-length in those cases.
504-
if ( ! $this->_zlib_oc && ! $CFG->item('compress_output')) {
502+
if ( ! $this->_zlib_oc && ! $this->config->item('compress_output')) {
505503
header('Content-Length: ' . strlen($output));
506504
}
507505

@@ -794,8 +792,8 @@ protected function _check_limit($controller_method)
794792
->get(config_item('rest_limits_table'))
795793
->row();
796794

797-
// No calls yet, or been an hour since they called
798-
if ( ! $result or $result->hour_started < time() - (60 * 60)) {
795+
// No calls yet for this key
796+
if ( ! $result ) {
799797
// Right, set one up from scratch
800798
$this->rest->db->insert(config_item('rest_limits_table'), array(
801799
'uri' => $this->uri->uri_string(),
@@ -810,7 +808,7 @@ protected function _check_limit($controller_method)
810808
// Reset the started period
811809
$this->rest->db
812810
->where('uri', $this->uri->uri_string())
813-
->where('api_key_id', $this->rest->key_id)
811+
->where('api_key', isset($this->rest->key) ? $this->rest->key : '')
814812
->set('hour_started', time())
815813
->set('count', 1)
816814
->update(config_item('rest_limits_table'));
@@ -1303,13 +1301,19 @@ protected function _check_login($username = '', $password = false)
13031301
if (empty($username)) {
13041302
return false;
13051303
}
1304+
1305+
$auth_source = strtolower($this->config->item('auth_source'));
1306+
$rest_auth = strtolower($this->config->item('rest_auth'));
1307+
$valid_logins = $this->config->item('rest_valid_logins');
1308+
1309+
if (!$this->config->item('auth_source') && $rest_auth == 'digest') { // for digest we do not have a password passed as argument
1310+
return md5($username.':'.$this->config->item('rest_realm').':'.(isset($valid_logins[$username])?$valid_logins[$username]:''));
1311+
}
13061312

13071313
if ($password === false) {
13081314
return false;
13091315
}
13101316

1311-
$auth_source = strtolower($this->config->item('auth_source'));
1312-
13131317
if ($auth_source == 'ldap') {
13141318
log_message('debug', 'performing LDAP authentication for $username');
13151319

@@ -1322,8 +1326,6 @@ protected function _check_login($username = '', $password = false)
13221326
return $this->_perform_library_auth($username, $password);
13231327
}
13241328

1325-
$valid_logins = $this->config->item('rest_valid_logins');
1326-
13271329
if (!array_key_exists($username, $valid_logins)) {
13281330
return false;
13291331
}
@@ -1410,18 +1412,11 @@ protected function _prepare_digest_auth()
14101412
$digest = (empty($matches[1]) || empty($matches[2])) ? array() : array_combine($matches[1], $matches[2]);
14111413

14121414
// For digest authentication the library function should return already stored md5(username:restrealm:password) for that username @see rest.php::auth_library_function config
1413-
if ( ! array_key_exists('username', $digest) or ! ($A1 = $this->_check_login($digest['username'])) ) {
1415+
$A1 = $this->_check_login($digest['username'], true);
1416+
if ( ! array_key_exists('username', $digest) or ! $A1 ) {
14141417
$this->_force_login($uniqid);
14151418
}
14161419

1417-
// If the auth_source is not selected use the rest.php configuration valid logins
1418-
if ( ! $this->config->item('auth_source') ) {
1419-
$valid_logins = $this->config->item('rest_valid_logins');
1420-
$valid_pass = $valid_logins[$digest['username']];
1421-
1422-
// This is the valid response expected
1423-
$A1 = md5($digest['username'].':'.$this->config->item('rest_realm').':'.$valid_pass);
1424-
}
14251420
$A2 = md5(strtoupper($this->request->method).':'.$digest['uri']);
14261421
$valid_response = md5($A1.':'.$digest['nonce'].':'.$digest['nc'].':'.$digest['cnonce'].':'.$digest['qop'].':'.$A2);
14271422

0 commit comments

Comments
 (0)