Skip to content

Commit b0a502e

Browse files
committed
Added two new fields to the keys table: 1) is_private_key and 2) ip_addresses. These two fields work together. If the key is private a list of IP addresses can be associated with the API key, If the API key is private and no valid address if found, the request is rejected. IP addresses need to be separated by a line break n. This is an alternative to the ip-whitelist, but it is tight to the API key.
1 parent 0b871cf commit b0a502e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

application/config/rest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@
156156
`key` varchar(40) NOT NULL,
157157
`level` int(2) NOT NULL,
158158
`ignore_limits` tinyint(1) NOT NULL DEFAULT '0',
159+
`is_private_key` tinyint(1) NOT NULL DEFAULT '0',
160+
`ip_addresses` TEXT NULL DEFAULT NULL,
159161
`date_created` int(11) NOT NULL,
160162
PRIMARY KEY (`id`)
161163
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

application/libraries/REST_Controller.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,33 @@ protected function _detect_api_key()
579579
isset($row->user_id) AND $this->rest->user_id = $row->user_id;
580580
isset($row->level) AND $this->rest->level = $row->level;
581581
isset($row->ignore_limits) AND $this->rest->ignore_limits = $row->ignore_limits;
582+
583+
if(!empty($row->is_private_key))
584+
{
585+
586+
// Check for a list of valid ip addresses
587+
if(isset($row->ip_addresses))
588+
{
589+
$list_ip_addresses = explode("\n", $row->ip_addresses);
590+
$found_address = FALSE;
591+
592+
foreach($list_ip_addresses as $ip_address)
593+
{
594+
if($this->input->ip_address() == $ip_address)
595+
{
596+
$found_address = TRUE;
597+
break;
598+
}
599+
}
600+
601+
return $found_address;
602+
}
603+
else
604+
{
605+
// There should be at least one IP address for this private key.
606+
return FALSE;
607+
}
608+
}
582609

583610
return TRUE;
584611
}

0 commit comments

Comments
 (0)