PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/api/key.php

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