PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/api/Key.php

http://github.com/philsturgeon/codeigniter-restserver
PHP | 272 lines | 173 code | 35 blank | 64 comment | 10 complexity | 9bc8c59e46670ec693add247dd555e0d MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3. // This can be removed if you use __autoload() in config.php OR use Modular Extensions
  4. require APPPATH . '/libraries/REST_Controller.php';
  5. /**
  6. * Keys Controller
  7. * This is a basic Key Management REST controller to make and delete keys
  8. *
  9. * @package CodeIgniter
  10. * @subpackage Rest Server
  11. * @category Controller
  12. * @author Phil Sturgeon, Chris Kacerguis
  13. * @license MIT
  14. * @link https://github.com/chriskacerguis/codeigniter-restserver
  15. */
  16. class Key extends REST_Controller {
  17. protected $methods = [
  18. 'index_put' => ['level' => 10, 'limit' => 10],
  19. 'index_delete' => ['level' => 10],
  20. 'level_post' => ['level' => 10],
  21. 'regenerate_post' => ['level' => 10],
  22. ];
  23. /**
  24. * Insert a key into the database
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. public function index_put()
  30. {
  31. // Build a new key
  32. $key = $this->_generate_key();
  33. // If no key level provided, provide a generic key
  34. $level = $this->put('level') ? $this->put('level') : 1;
  35. $ignore_limits = ctype_digit($this->put('ignore_limits')) ? (int) $this->put('ignore_limits') : 1;
  36. // Insert the new key
  37. if ($this->_insert_key($key, ['level' => $level, 'ignore_limits' => $ignore_limits]))
  38. {
  39. $this->response([
  40. 'status' => TRUE,
  41. 'key' => $key
  42. ], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
  43. }
  44. else
  45. {
  46. $this->response([
  47. 'status' => FALSE,
  48. 'message' => 'Could not save the key'
  49. ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
  50. }
  51. }
  52. /**
  53. * Remove a key from the database to stop it working
  54. *
  55. * @access public
  56. * @return void
  57. */
  58. public function index_delete()
  59. {
  60. $key = $this->delete('key');
  61. // Does this key exist?
  62. if (!$this->_key_exists($key))
  63. {
  64. // It doesn't appear the key exists
  65. $this->response([
  66. 'status' => FALSE,
  67. 'message' => 'Invalid API key'
  68. ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
  69. }
  70. // Destroy it
  71. $this->_delete_key($key);
  72. // Respond that the key was destroyed
  73. $this->response([
  74. 'status' => TRUE,
  75. 'message' => 'API key was deleted'
  76. ], REST_Controller::HTTP_NO_CONTENT); // NO_CONTENT (204) being the HTTP response code
  77. }
  78. /**
  79. * Change the level
  80. *
  81. * @access public
  82. * @return void
  83. */
  84. public function level_post()
  85. {
  86. $key = $this->post('key');
  87. $new_level = $this->post('level');
  88. // Does this key exist?
  89. if (!$this->_key_exists($key))
  90. {
  91. // It doesn't appear the key exists
  92. $this->response([
  93. 'status' => FALSE,
  94. 'message' => 'Invalid API key'
  95. ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
  96. }
  97. // Update the key level
  98. if ($this->_update_key($key, ['level' => $new_level]))
  99. {
  100. $this->response([
  101. 'status' => TRUE,
  102. 'message' => 'API key was updated'
  103. ], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
  104. }
  105. else
  106. {
  107. $this->response([
  108. 'status' => FALSE,
  109. 'message' => 'Could not update the key level'
  110. ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
  111. }
  112. }
  113. /**
  114. * Suspend a key
  115. *
  116. * @access public
  117. * @return void
  118. */
  119. public function suspend_post()
  120. {
  121. $key = $this->post('key');
  122. // Does this key exist?
  123. if (!$this->_key_exists($key))
  124. {
  125. // It doesn't appear the key exists
  126. $this->response([
  127. 'status' => FALSE,
  128. 'message' => 'Invalid API key'
  129. ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
  130. }
  131. // Update the key level
  132. if ($this->_update_key($key, ['level' => 0]))
  133. {
  134. $this->response([
  135. 'status' => TRUE,
  136. 'message' => 'Key was suspended'
  137. ], REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
  138. }
  139. else
  140. {
  141. $this->response([
  142. 'status' => FALSE,
  143. 'message' => 'Could not suspend the user'
  144. ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
  145. }
  146. }
  147. /**
  148. * Regenerate a key
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. public function regenerate_post()
  154. {
  155. $old_key = $this->post('key');
  156. $key_details = $this->_get_key($old_key);
  157. // Does this key exist?
  158. if (!$key_details)
  159. {
  160. // It doesn't appear the key exists
  161. $this->response([
  162. 'status' => FALSE,
  163. 'message' => 'Invalid API key'
  164. ], REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
  165. }
  166. // Build a new key
  167. $new_key = $this->_generate_key();
  168. // Insert the new key
  169. if ($this->_insert_key($new_key, ['level' => $key_details->level, 'ignore_limits' => $key_details->ignore_limits]))
  170. {
  171. // Suspend old key
  172. $this->_update_key($old_key, ['level' => 0]);
  173. $this->response([
  174. 'status' => TRUE,
  175. 'key' => $new_key
  176. ], REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
  177. }
  178. else
  179. {
  180. $this->response([
  181. 'status' => FALSE,
  182. 'message' => 'Could not save the key'
  183. ], REST_Controller::HTTP_INTERNAL_SERVER_ERROR); // INTERNAL_SERVER_ERROR (500) being the HTTP response code
  184. }
  185. }
  186. /* Helper Methods */
  187. private function _generate_key()
  188. {
  189. do
  190. {
  191. // Generate a random salt
  192. $salt = base_convert(bin2hex($this->security->get_random_bytes(64)), 16, 36);
  193. // If an error occurred, then fall back to the previous method
  194. if ($salt === FALSE)
  195. {
  196. $salt = hash('sha256', time() . mt_rand());
  197. }
  198. $new_key = substr($salt, 0, config_item('rest_key_length'));
  199. }
  200. while ($this->_key_exists($new_key));
  201. return $new_key;
  202. }
  203. /* Private Data Methods */
  204. private function _get_key($key)
  205. {
  206. return $this->db
  207. ->where(config_item('rest_key_column'), $key)
  208. ->get(config_item('rest_keys_table'))
  209. ->row();
  210. }
  211. private function _key_exists($key)
  212. {
  213. return $this->db
  214. ->where(config_item('rest_key_column'), $key)
  215. ->count_all_results(config_item('rest_keys_table')) > 0;
  216. }
  217. private function _insert_key($key, $data)
  218. {
  219. $data[config_item('rest_key_column')] = $key;
  220. $data['date_created'] = function_exists('now') ? now() : time();
  221. return $this->db
  222. ->set($data)
  223. ->insert(config_item('rest_keys_table'));
  224. }
  225. private function _update_key($key, $data)
  226. {
  227. return $this->db
  228. ->where(config_item('rest_key_column'), $key)
  229. ->update(config_item('rest_keys_table'), $data);
  230. }
  231. private function _delete_key($key)
  232. {
  233. return $this->db
  234. ->where(config_item('rest_key_column'), $key)
  235. ->delete(config_item('rest_keys_table'));
  236. }
  237. }