Skip to content

Commit 6f39f26

Browse files
Fix for issue chriskacerguis#283
1 parent 95dd903 commit 6f39f26

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

application/config/rest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
| The function should accept two parameters: class->function($username, $password)
110110
| In other cases override the function _perform_library_auth in your controller
111111
|
112+
| For digest authentication the library function should return already stored md5(username:restrealm:password) for that username
113+
| E.g: md5('admin:REST API:1234') = '1e957ebc35631ab22d5bd6526bd14ea2'
114+
|
112115
*/
113116
$config['auth_library_class'] = '';
114117
$config['auth_library_function'] = '';

application/libraries/REST_Controller.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,17 @@ protected function _check_limit($controller_method)
805805
));
806806
}
807807

808+
// Been an hour since they called
809+
else if ($result->hour_started < time() - (60 * 60)) {
810+
// Reset the started period
811+
$this->rest->db
812+
->where('uri', $this->uri->uri_string())
813+
->where('api_key_id', $this->rest->key_id)
814+
->set('hour_started', time())
815+
->set('count', 1)
816+
->update(config_item('rest_limits_table'));
817+
}
818+
808819
// They have called within the hour, so lets update
809820
else {
810821
// Your luck is out, you've called too many times!
@@ -1361,20 +1372,24 @@ protected function _prepare_digest_auth()
13611372
preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)=[\'"]?([^\'",]+)@', $digest_string, $matches);
13621373
$digest = (empty($matches[1]) || empty($matches[2])) ? array() : array_combine($matches[1], $matches[2]);
13631374

1364-
if ( ! array_key_exists('username', $digest) or !$this->_check_login($digest['username'])) {
1375+
// For digest authentication the library function should return already stored md5(username:restrealm:password) for that username @see rest.php::auth_library_function config
1376+
if ( ! array_key_exists('username', $digest) or ! ($A1 = $this->_check_login($digest['username'])) ) {
13651377
$this->_force_login($uniqid);
13661378
}
13671379

1368-
$valid_logins = $this->config->item('rest_valid_logins');
1369-
$valid_pass = $valid_logins[$digest['username']];
1380+
// If the auth_source is not selected use the rest.php configuration valid logins
1381+
if ( ! $this->config->item('auth_source') ) {
1382+
$valid_logins = $this->config->item('rest_valid_logins');
1383+
$valid_pass = $valid_logins[$digest['username']];
13701384

1371-
// This is the valid response expected
1372-
$A1 = md5($digest['username'].':'.$this->config->item('rest_realm').':'.$valid_pass);
1385+
// This is the valid response expected
1386+
$A1 = md5($digest['username'].':'.$this->config->item('rest_realm').':'.$valid_pass);
1387+
}
13731388
$A2 = md5(strtoupper($this->request->method).':'.$digest['uri']);
13741389
$valid_response = md5($A1.':'.$digest['nonce'].':'.$digest['nc'].':'.$digest['cnonce'].':'.$digest['qop'].':'.$A2);
13751390

13761391
if ($digest['response'] != $valid_response) {
1377-
set_status_header(401);
1392+
$this->response(array(config_item('rest_status_field_name') => 0, config_item('rest_message_field_name') => 'Invalid credentials'), 401);
13781393
exit;
13791394
}
13801395
}

0 commit comments

Comments
 (0)