Skip to content

Commit 0cf142a

Browse files
Merge pull request chriskacerguis#319 from c4xp/master
Fix for issue chriskacerguis#283
2 parents 34d78ae + 6f39f26 commit 0cf142a

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!
@@ -1368,20 +1379,24 @@ protected function _prepare_digest_auth()
13681379
preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)=[\'"]?([^\'",]+)@', $digest_string, $matches);
13691380
$digest = (empty($matches[1]) || empty($matches[2])) ? array() : array_combine($matches[1], $matches[2]);
13701381

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

1375-
$valid_logins = $this->config->item('rest_valid_logins');
1376-
$valid_pass = $valid_logins[$digest['username']];
1387+
// If the auth_source is not selected use the rest.php configuration valid logins
1388+
if ( ! $this->config->item('auth_source') ) {
1389+
$valid_logins = $this->config->item('rest_valid_logins');
1390+
$valid_pass = $valid_logins[$digest['username']];
13771391

1378-
// This is the valid response expected
1379-
$A1 = md5($digest['username'].':'.$this->config->item('rest_realm').':'.$valid_pass);
1392+
// This is the valid response expected
1393+
$A1 = md5($digest['username'].':'.$this->config->item('rest_realm').':'.$valid_pass);
1394+
}
13801395
$A2 = md5(strtoupper($this->request->method).':'.$digest['uri']);
13811396
$valid_response = md5($A1.':'.$digest['nonce'].':'.$digest['nc'].':'.$digest['cnonce'].':'.$digest['qop'].':'.$A2);
13821397

13831398
if ($digest['response'] != $valid_response) {
1384-
set_status_header(401);
1399+
$this->response(array(config_item('rest_status_field_name') => 0, config_item('rest_message_field_name') => 'Invalid credentials'), 401);
13851400
exit;
13861401
}
13871402
}

0 commit comments

Comments
 (0)