Skip to content

Commit f9520b2

Browse files
author
Phil Sturgeon
committed
Merge pull request chriskacerguis#146 from davidstanley01/master
Added config elements to force ssl and to allow user to specify column name containing api key
2 parents 5955247 + da1fc96 commit f9520b2

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

application/config/rest.php

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

3+
/*
4+
|--------------------------------------------------------------------------
5+
| HTTP protocol
6+
|--------------------------------------------------------------------------
7+
|
8+
| Should the service accept only HTTPS requests or not?
9+
|
10+
| Default: FALSE
11+
|
12+
*/
13+
$config['force_https'] = FALSE;
14+
315
/*
416
|--------------------------------------------------------------------------
517
| REST Format
@@ -163,6 +175,17 @@
163175
*/
164176
$config['rest_enable_keys'] = FALSE;
165177

178+
/*
179+
|--------------------------------------------------------------------------
180+
| REST Table Key Column Name
181+
|--------------------------------------------------------------------------
182+
|
183+
| If you are not using the default table schema as shown above, what is the
184+
| name of the db column that holds the api key value?
185+
|
186+
*/
187+
$config['rest_key_column'] = 'key';
188+
166189
/*
167190
|--------------------------------------------------------------------------
168191
| REST Key Length
@@ -219,7 +242,7 @@
219242
`api_key` varchar(40) NOT NULL,
220243
`ip_address` varchar(45) NOT NULL,
221244
`time` int(11) NOT NULL,
222-
`authorized` tinyint(1) NOT NULL,
245+
`authorized` tinyint(1) NOT NULL
223246
PRIMARY KEY (`id`)
224247
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
225248
|

application/libraries/REST_Controller.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ abstract class REST_Controller extends CI_Controller
6060
*/
6161
protected $rest = NULL;
6262

63+
/**
64+
* Object to store data about the client sending the request
65+
*
66+
* @var object
67+
*/
68+
protected $client = NULL;
69+
6370
/**
6471
* The arguments for the GET request method
6572
*
@@ -145,8 +152,13 @@ public function __construct()
145152
// Lets grab the config and get ready to party
146153
$this->load->config('rest');
147154

148-
// How is this request being made? POST, DELETE, GET, PUT?
155+
// let's learn about the request
149156
$this->request = new stdClass();
157+
158+
// Is it over SSL?
159+
$this->request->ssl = $this->_detect_ssl();
160+
161+
// How is this request being made? POST, DELETE, GET, PUT?
150162
$this->request->method = $this->_detect_method();
151163

152164
// Create argument container, if nonexistent
@@ -250,6 +262,12 @@ public function __construct()
250262
*/
251263
public function _remap($object_called, $arguments)
252264
{
265+
// Should we answer if not over SSL?
266+
if (config_item('force_https') AND !$this->_detect_ssl())
267+
{
268+
$this->response(array('status' => false, 'error' => 'Unsupported protocol'), 403);
269+
}
270+
253271
$pattern = '/^(.*)\.('.implode('|', array_keys($this->_supported_formats)).')$/';
254272
if (preg_match($pattern, $object_called, $matches))
255273
{
@@ -407,6 +425,17 @@ public function response($data = array(), $http_code = null)
407425
exit($output);
408426
}
409427

428+
/*
429+
* Detect SSL use
430+
*
431+
* Detect whether SSL is being used or not
432+
*/
433+
protected function _detect_ssl()
434+
{
435+
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on"));
436+
}
437+
438+
410439
/*
411440
* Detect input format
412441
*
@@ -569,18 +598,20 @@ protected function _detect_api_key()
569598
// Find the key from server or arguments
570599
if (($key = isset($this->_args[$api_key_variable]) ? $this->_args[$api_key_variable] : $this->input->server($key_name)))
571600
{
572-
if ( ! ($row = $this->rest->db->where('key', $key)->get(config_item('rest_keys_table'))->row()))
601+
if ( ! ($this->client = $this->rest->db->where(config_item('rest_key_column'), $key)->get(config_item('rest_keys_table'))->row()))
573602
{
574603
return FALSE;
575604
}
576605

577-
$this->rest->key = $row->key;
606+
$this->rest->key = $this->client->{config_item('rest_key_column')};
578607

608+
/*
579609
isset($row->user_id) AND $this->rest->user_id = $row->user_id;
580610
isset($row->level) AND $this->rest->level = $row->level;
581611
isset($row->ignore_limits) AND $this->rest->ignore_limits = $row->ignore_limits;
612+
*/
582613

583-
return TRUE;
614+
return $this->client;
584615
}
585616

586617
// No key has been sent

0 commit comments

Comments
 (0)