Overview

Packages

  • CodeIgniter
    • Libraries
  • None

Classes

  • Format

Functions

  • _perform_ldap_auth
  • Overview
  • Package
  • Function
  • Tree
  • Todo
  1: <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2: 
  3: /**
  4:  * Keys Controller
  5:  *
  6:  * This is a basic Key Management REST controller to make and delete keys.
  7:  *
  8:  * @package     CodeIgniter
  9:  * @subpackage  Rest Server
 10:  * @category    Controller
 11:  * @author      Phil Sturgeon
 12:  * @link        http://philsturgeon.co.uk/code/
 13: */
 14: 
 15: // This can be removed if you use __autoload() in config.php
 16: require(APPPATH.'/libraries/REST_Controller.php');
 17: 
 18: class Key extends REST_Controller
 19: {
 20:     protected $methods = array(
 21:         'index_put' => array('level' => 10, 'limit' => 10),
 22:         'index_delete' => array('level' => 10),
 23:         'level_post' => array('level' => 10),
 24:         'regenerate_post' => array('level' => 10),
 25:     );
 26: 
 27:     /**
 28:      * Key Create
 29:      *
 30:      * Insert a key into the database.
 31:      *
 32:      * @access  public
 33:      * @return  void
 34:      */
 35:     public function index_put()
 36:     {
 37:         // Build a new key
 38:         $key = self::_generate_key();
 39: 
 40:         // If no key level provided, give them a rubbish one
 41:         $level = $this->put('level') ? $this->put('level') : 1;
 42:         $ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;
 43: 
 44:         // Insert the new key
 45:         if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits)))
 46:         {
 47:             $this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
 48:         }
 49: 
 50:         else
 51:         {
 52:             $this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
 53:         }
 54:     }
 55: 
 56:     // --------------------------------------------------------------------
 57: 
 58:     /**
 59:      * Key Delete
 60:      *
 61:      * Remove a key from the database to stop it working.
 62:      *
 63:      * @access  public
 64:      * @return  void
 65:      */
 66:     public function index_delete()
 67:     {
 68:         $key = $this->delete('key');
 69: 
 70:         // Does this key even exist?
 71:         if ( ! self::_key_exists($key))
 72:         {
 73:             // NOOOOOOOOO!
 74:             $this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
 75:         }
 76: 
 77:         // Kill it
 78:         self::_delete_key($key);
 79: 
 80:         // Tell em we killed it
 81:         $this->response(array('status' => 1, 'success' => 'API Key was deleted.'), 200);
 82:     }
 83: 
 84:     // --------------------------------------------------------------------
 85: 
 86:     /**
 87:      * Update Key
 88:      *
 89:      * Change the level
 90:      *
 91:      * @access  public
 92:      * @return  void
 93:      */
 94:     public function level_post()
 95:     {
 96:         $key = $this->post('key');
 97:         $new_level = $this->post('level');
 98: 
 99:         // Does this key even exist?
100:         if ( ! self::_key_exists($key))
101:         {
102:             // NOOOOOOOOO!
103:             $this->response(array('error' => 'Invalid API Key.'), 400);
104:         }
105: 
106:         // Update the key level
107:         if (self::_update_key($key, array('level' => $new_level)))
108:         {
109:             $this->response(array('status' => 1, 'success' => 'API Key was updated.'), 200); // 200 = OK
110:         }
111: 
112:         else
113:         {
114:             $this->response(array('status' => 0, 'error' => 'Could not update the key level.'), 500); // 500 = Internal Server Error
115:         }
116:     }
117: 
118:     // --------------------------------------------------------------------
119: 
120:     /**
121:      * Update Key
122:      *
123:      * Change the level
124:      *
125:      * @access  public
126:      * @return  void
127:      */
128:     public function suspend_post()
129:     {
130:         $key = $this->post('key');
131: 
132:         // Does this key even exist?
133:         if ( ! self::_key_exists($key))
134:         {
135:             // NOOOOOOOOO!
136:             $this->response(array('error' => 'Invalid API Key.'), 400);
137:         }
138: 
139:         // Update the key level
140:         if (self::_update_key($key, array('level' => 0)))
141:         {
142:             $this->response(array('status' => 1, 'success' => 'Key was suspended.'), 200); // 200 = OK
143:         }
144: 
145:         else
146:         {
147:             $this->response(array('status' => 0, 'error' => 'Could not suspend the user.'), 500); // 500 = Internal Server Error
148:         }
149:     }
150: 
151:     // --------------------------------------------------------------------
152: 
153:     /**
154:      * Regenerate Key
155:      *
156:      * Remove a key from the database to stop it working.
157:      *
158:      * @access  public
159:      * @return  void
160:      */
161:     public function regenerate_post()
162:     {
163:         $old_key = $this->post('key');
164:         $key_details = self::_get_key($old_key);
165: 
166:         // The key wasnt found
167:         if ( ! $key_details)
168:         {
169:             // NOOOOOOOOO!
170:             $this->response(array('status' => 0, 'error' => 'Invalid API Key.'), 400);
171:         }
172: 
173:         // Build a new key
174:         $new_key = self::_generate_key();
175: 
176:         // Insert the new key
177:         if (self::_insert_key($new_key, array('level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits)))
178:         {
179:             // Suspend old key
180:             self::_update_key($old_key, array('level' => 0));
181: 
182:             $this->response(array('status' => 1, 'key' => $new_key), 201); // 201 = Created
183:         }
184: 
185:         else
186:         {
187:             $this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
188:         }
189:     }
190: 
191:     // --------------------------------------------------------------------
192: 
193:     /* Helper Methods */
194:     
195:     private function _generate_key()
196:     {
197:         //$this->load->helper('security');
198:         
199:         do
200:         {
201:             $salt = do_hash(time().mt_rand());
202:             $new_key = substr($salt, 0, config_item('rest_key_length'));
203:         }
204: 
205:         // Already in the DB? Fail. Try again
206:         while (self::_key_exists($new_key));
207: 
208:         return $new_key;
209:     }
210: 
211:     // --------------------------------------------------------------------
212: 
213:     /* Private Data Methods */
214: 
215:     private function _get_key($key)
216:     {
217:         return $this->db->where(config_item('rest_key_column'), $key)->get(config_item('rest_keys_table'))->row();
218:     }
219: 
220:     // --------------------------------------------------------------------
221: 
222:     private function _key_exists($key)
223:     {
224:         return $this->db->where(config_item('rest_key_column'), $key)->count_all_results(config_item('rest_keys_table')) > 0;
225:     }
226: 
227:     // --------------------------------------------------------------------
228: 
229:     private function _insert_key($key, $data)
230:     {
231:         
232:         $data[config_item('rest_key_column')] = $key;
233:         $data['date_created'] = function_exists('now') ? now() : time();
234: 
235:         return $this->db->set($data)->insert(config_item('rest_keys_table'));
236:     }
237: 
238:     // --------------------------------------------------------------------
239: 
240:     private function _update_key($key, $data)
241:     {
242:         return $this->db->where(config_item('rest_key_column'), $key)->update(config_item('rest_keys_table'), $data);
243:     }
244: 
245:     // --------------------------------------------------------------------
246: 
247:     private function _delete_key($key)
248:     {
249:         return $this->db->where(config_item('rest_key_column'), $key)->delete(config_item('rest_keys_table'));
250:     }
251: }
252: 
API documentation generated by ApiGen