Skip to content

Commit b6df2b5

Browse files
author
Phil Sturgeon
committed
New version of CodeIgniter REST Server with API Key management, permissions and method limiting. All turned off by default, turn them on and find the schema for each feature in application/config/rest.php.
1 parent 9decacd commit b6df2b5

File tree

5 files changed

+265
-124
lines changed

5 files changed

+265
-124
lines changed

application/config/rest.php

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
22

3+
/*
4+
|--------------------------------------------------------------------------
5+
| REST Format
6+
|--------------------------------------------------------------------------
7+
|
8+
| What format should the data be returned in by default?
9+
|
10+
| Default: xml
11+
|
12+
*/
13+
$config['rest_default_format'] = 'xml';
14+
315
/*
416
|--------------------------------------------------------------------------
517
| REST Realm
@@ -58,9 +70,18 @@
5870
| If no key is provided, the request will return an error.
5971
|
6072
| FALSE
73+
74+
CREATE TABLE `keys` (
75+
`id` int(11) NOT NULL AUTO_INCREMENT,
76+
`key` varchar(40) NOT NULL,
77+
`level` int(2) NOT NULL,
78+
`ignore_limits` tinyint(1) NOT NULL DEFAULT '0',
79+
`date_created` int(11) NOT NULL,
80+
PRIMARY KEY (`id`)
81+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
6182
|
6283
*/
63-
$config['rest_enable_keys'] = TRUE;
84+
$config['rest_enable_keys'] = FALSE;
6485

6586
/*
6687
|--------------------------------------------------------------------------
@@ -94,12 +115,48 @@
94115
|--------------------------------------------------------------------------
95116
|
96117
| When set to true REST_Controller will log actions based on key, date,
97-
| time and IP address.
118+
| time and IP address. This is a general rule that can be overridden in the
119+
| $this->method array in each controller.
120+
|
121+
| FALSE
122+
|
123+
CREATE TABLE `logs` (
124+
`id` int(11) NOT NULL AUTO_INCREMENT,
125+
`uri` varchar(255) NOT NULL,
126+
`method` varchar(6) NOT NULL,
127+
`params` text NOT NULL,
128+
`api_key` varchar(40) NOT NULL,
129+
`ip_address` varchar(15) NOT NULL,
130+
`time` int(11) NOT NULL,
131+
`authorized` tinyint(1) NOT NULL,
132+
PRIMARY KEY (`id`)
133+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
134+
|
135+
*/
136+
$config['rest_enable_logging'] = FALSE;
137+
138+
/*
139+
|--------------------------------------------------------------------------
140+
| REST Enable Limits
141+
|--------------------------------------------------------------------------
142+
|
143+
| When set to true REST_Controller will count the number of uses of each method
144+
| by an API key each hour. This is a general rule that can be overridden in the
145+
| $this->method array in each controller.
98146
|
99147
| FALSE
148+
|
149+
CREATE TABLE `limits` (
150+
`id` int(11) NOT NULL AUTO_INCREMENT,
151+
`uri` varchar(255) NOT NULL,
152+
`count` int(10) NOT NULL,
153+
`hour_started` int(11) NOT NULL,
154+
`api_key` varchar(40) NOT NULL,
155+
PRIMARY KEY (`id`)
156+
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
100157
|
101158
*/
102-
$config['rest_enable_logging'] = TRUE;
159+
$config['rest_enable_limits'] = FALSE;
103160

104161
/*
105162
|--------------------------------------------------------------------------

application/controllers/api/example.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,4 @@ function users_get()
8080
$this->response(array('error' => 'Couldn\'t find any users!'), 404);
8181
}
8282
}
83-
84-
}
85-
86-
?>
83+
}

application/controllers/api/keys.php renamed to application/controllers/api/key.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// This can be removed if you use __autoload() in config.php
1616
require(APPPATH.'/libraries/REST_Controller.php');
1717

18-
class Keys extends REST_Controller
18+
class Key extends REST_Controller
1919
{
2020
protected $methods = array(
21-
'index_put' => array('level' => 10),
21+
'index_put' => array('level' => 10, 'limit' => 10),
2222
'index_delete' => array('level' => 10),
2323
'level_post' => array('level' => 10),
2424
'regenerate_post' => array('level' => 10),
@@ -39,9 +39,10 @@ public function index_put()
3939

4040
// If no key level provided, give them a rubbish one
4141
$level = $this->put('level') ? $this->put('level') : 1;
42+
$ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
4243

4344
// Insert the new key
44-
if (self::_insert_key($key, $level))
45+
if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
4546
{
4647
$this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
4748
}
@@ -105,7 +106,7 @@ public function level_post()
105106
// Update the key level
106107
if (self::_update_key($key, array('level' => $new_level)))
107108
{
108-
$this->response(array('status' => 1, 'success' => 'Key was updated.'), 200); // 200 = OK
109+
$this->response(array('status' => 1, 'success' => 'API Key was updated.'), 200); // 200 = OK
109110
}
110111

111112
else
@@ -173,7 +174,7 @@ public function regenerate_post()
173174
$new_key = self::_generate_key();
174175

175176
// Insert the new key
176-
if (self::_insert_key($new_key, $key_details->level))
177+
if (self::_insert_key($new_key, array('level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits)))
177178
{
178179
// Suspend old key
179180
self::_update_key($old_key, array('level' => 0));
@@ -225,13 +226,14 @@ private function _key_exists($key)
225226

226227
// --------------------------------------------------------------------
227228

228-
private function _insert_key($key, $level)
229+
private function _insert_key($key, $data)
229230
{
230-
return $this->rest->db->set(array(
231-
'key' => $key,
232-
'level' => $level,
233-
'date_created' => function_exists('now') ? now() : time()
234-
))->insert('keys');
231+
var_dump($data);
232+
233+
$data['key'] = $key;
234+
$data['date_created'] = function_exists('now') ? now() : time();
235+
236+
return $this->rest->db->set($data)->insert('keys');
235237
}
236238

237239
// --------------------------------------------------------------------

0 commit comments

Comments
 (0)