Skip to content

Commit 70204f1

Browse files
author
Christian Cordiviola
committed
get api key from http header only
1 parent a7c585a commit 70204f1

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

www/addresses.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// found in the LICENSE.md file.
66
require_once __DIR__ . '/common.inc';
77

8-
if (isset($_REQUEST['k'])) {
8+
$user_api_key = $request_context->getApiKeyInUse();
9+
if (strlen($user_api_key)) {
910
$keys_file = SETTINGS_PATH . '/keys.ini';
1011
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
1112
$keys_file = SETTINGS_PATH . '/common/keys.ini';
@@ -14,7 +15,7 @@
1415
$keys_file = SETTINGS_PATH . '/server/keys.ini';
1516
}
1617
$keys = parse_ini_file($keys_file, true);
17-
if (isset($keys['server']['key']) && $_REQUEST['k'] == $keys['server']['key']) {
18+
if (isset($keys['server']['key']) && $user_api_key == $keys['server']['key']) {
1819
$admin = true;
1920
}
2021
}

www/getLocations.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ function LoadLocations($isPaid = false)
201201
$isPaid = false;
202202
$locations = array();
203203
$loc = LoadLocationsIni();
204-
if (isset($_REQUEST['k'])) {
204+
$user_api_key = $request_context->getApiKeyInUse();
205+
if (strlen($user_api_key)) {
205206
foreach ($loc as $name => $location) {
206207
if (isset($location['browser']) && isset($location['noapi'])) {
207208
unset($loc[$name]);

www/getTesters.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
//header("HTTP/1.1 403 Unauthorized");
99
//exit;
1010
}
11-
if (isset($_REQUEST['k'])) {
11+
$user_api_key = $request_context->getApiKeyInUse();
12+
if (strlen($user_api_key)) {
1213
$keys_file = SETTINGS_PATH . '/keys.ini';
1314
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
1415
$keys_file = SETTINGS_PATH . '/common/keys.ini';
@@ -17,7 +18,7 @@
1718
$keys_file = SETTINGS_PATH . '/server/keys.ini';
1819
}
1920
$keys = parse_ini_file($keys_file, true);
20-
if (isset($keys['server']['key']) && $_REQUEST['k'] == $keys['server']['key']) {
21+
if (isset($keys['server']['key']) && $user_api_key == $keys['server']['key']) {
2122
$admin = true;
2223
}
2324
}

www/src/RequestContext.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function getHost(): string
131131

132132
public function setEnvironment(?string $env = ''): void
133133
{
134-
// This should really be a match, but we're on 7.4
134+
// This should really be a match, but we're on 7.4
135135
switch ($env) {
136136
case 'development':
137137
$this->environment = Environment::$Development;
@@ -161,21 +161,17 @@ public function getEnvironment(): string
161161
public function getApiKeyInUse(): string
162162
{
163163
if (empty($this->api_key_in_use)) {
164-
$user_api_key = $this->getRaw()['k'] ?? "";
165-
if (empty($user_api_key)) {
166-
$user_api_key_header = $this->user_api_key_header;
167-
$request_headers = getallheaders();
168-
$matching_headers = array_filter($request_headers, function ($k) use ($user_api_key_header) {
169-
return strtolower($k) == strtolower($user_api_key_header);
170-
}, ARRAY_FILTER_USE_KEY);
171-
if (!empty($matching_headers)) {
172-
$user_api_key = array_values($matching_headers)[0];
173-
}
164+
$user_api_key_header = $this->user_api_key_header;
165+
$request_headers = getallheaders();
166+
$matching_headers = array_filter($request_headers, function ($k) use ($user_api_key_header) {
167+
return strtolower($k) == strtolower($user_api_key_header);
168+
}, ARRAY_FILTER_USE_KEY);
169+
if (!empty($matching_headers)) {
170+
$user_api_key = array_values($matching_headers)[0];
174171
}
175-
176-
$this->api_key_in_use = $user_api_key;
172+
$this->api_key_in_use = $user_api_key ?? "";
177173
}
178174

179-
return $this->api_key_in_use;
175+
return $this->api_key_in_use ?? "";
180176
}
181177
}

www/usage.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
?>
2727

2828
<?php
29-
if (array_key_exists('k', $_REQUEST) && strlen($_REQUEST['k'])) {
30-
$key = trim($_REQUEST['k']);
29+
$user_api_key = $request_context->getApiKeyInUse();
30+
if (strlen($user_api_key)) {
31+
$key = trim($user_api_key);
3132
$keys_file = SETTINGS_PATH . '/keys.ini';
3233
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
3334
$keys_file = SETTINGS_PATH . '/common/keys.ini';

0 commit comments

Comments
 (0)