Skip to content

Commit c0bfb00

Browse files
author
Mike Kozicki
authored
Merge pull request #3046 from catchpoint/api_key_header_only
get api key from http header only
2 parents a7c585a + 683f576 commit c0bfb00

File tree

7 files changed

+60
-53
lines changed

7 files changed

+60
-53
lines changed

batchtool/wpt_batch.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python2.6
1+
#!/usr/bin/python3
22
#
33
# Copyright 2010 Google Inc. All Rights Reserved.
44

@@ -95,17 +95,15 @@ def RunBatch(options):
9595
test_params['tcpdump'] = options.tcpdump
9696
if options.script:
9797
test_params['script'] = open(options.script, 'rb').read()
98-
if options.key:
99-
test_params['k'] = options.key
10098

10199
requested_urls = wpt_batch_lib.ImportUrls(options.urlfile)
102-
id_url_dict = wpt_batch_lib.SubmitBatch(requested_urls, test_params,
100+
id_url_dict = wpt_batch_lib.SubmitBatch(requested_urls, test_params, options.key,
103101
options.server)
104102

105103
submitted_urls = set(id_url_dict.values())
106104
for url in requested_urls:
107105
if url not in submitted_urls:
108-
logging.warn('URL submission failed: %s', url)
106+
logging.warning('URL submission failed: %s', url)
109107

110108
pending_test_ids = id_url_dict.keys()
111109
if not os.path.isdir(options.outputdir):
@@ -129,13 +127,13 @@ def RunBatch(options):
129127
if test_status == '200':
130128
completed_test_ids.append(test_id)
131129
else:
132-
logging.warn('Tests failed with status $s: %s', test_status, test_id)
130+
logging.warning('Tests failed with status $s: %s', test_status, test_id)
133131
test_results = wpt_batch_lib.GetXMLResult(completed_test_ids,
134132
server_url=options.server)
135133
result_test_ids = set(test_results.keys())
136134
for test_id in completed_test_ids:
137135
if test_id not in result_test_ids:
138-
logging.warn('The XML failed to retrieve: %s', test_id)
136+
logging.warning('The XML failed to retrieve: %s', test_id)
139137

140138
for test_id, dom in test_results.iteritems():
141139
SaveTestResult(options.outputdir, id_url_dict[test_id], test_id,

batchtool/wpt_batch_lib.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python2.6
1+
#!/usr/bin/python3
22
#
33
# Copyright 2011 Google Inc. All Rights Reserved.
44

@@ -13,12 +13,12 @@
1313

1414
__author__ = '[email protected] (Qi Zhao)'
1515

16-
import re
17-
import urllib
16+
import urllib.request
17+
import urllib.parse
1818
from xml.dom import minidom
1919

2020

21-
def __LoadEntity(url, urlopen=urllib.urlopen):
21+
def __LoadEntity(url, apiKey, urlopen=urllib.request.urlopen):
2222
"""A helper function to load an entity such as an URL.
2323
2424
Args:
@@ -28,7 +28,15 @@ def __LoadEntity(url, urlopen=urllib.urlopen):
2828
Returns:
2929
The response message
3030
"""
31-
response = urlopen(url)
31+
headers = {}
32+
33+
if apiKey:
34+
headers = {
35+
"X-WPT-API-KEY" : apiKey
36+
}
37+
38+
request = urllib.request.Request(url, headers=headers)
39+
response = urlopen(request)
3240
return response
3341

3442

@@ -44,14 +52,14 @@ def ImportUrls(url_filename):
4452
url_list = []
4553
for line in open(url_filename, 'rb'):
4654
# Remove newline and trailing whitespaces
47-
url = line.rstrip(' \r\n')
55+
url = line.rstrip(b' \r\n')
4856
if url:
4957
url_list.append(url)
5058
return url_list
5159

5260

53-
def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
54-
urlopen=urllib.urlopen):
61+
def SubmitBatch(url_list, test_params, apiKey, server_url='http://www.webpagetest.org/',
62+
urlopen=urllib.request.urlopen,):
5563
"""Submit the tests to WebPageTest server.
5664
5765
Args:
@@ -67,8 +75,8 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
6775
id_url_dict = {}
6876
for url in url_list:
6977
test_params['url'] = url
70-
request = server_url + 'runtest.php?%s' % urllib.urlencode(test_params)
71-
response = __LoadEntity(request, urlopen)
78+
request = server_url + 'runtest.php?%s' % urllib.parse.urlencode(test_params)
79+
response = __LoadEntity(request, apiKey, urlopen)
7280
return_code = response.getcode()
7381
if return_code == 200:
7482
dom = minidom.parseString(response.read())
@@ -81,7 +89,7 @@ def SubmitBatch(url_list, test_params, server_url='http://www.webpagetest.org/',
8189

8290

8391
def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
84-
urlopen=urllib.urlopen):
92+
urlopen=urllib.request.urlopen):
8593
"""Check the status of tests.
8694
8795
Args:
@@ -105,7 +113,7 @@ def CheckBatchStatus(test_ids, server_url='http://www.webpagetest.org/',
105113

106114

107115
def GetXMLResult(test_ids, server_url='http://www.webpagetest.org/',
108-
urlopen=urllib.urlopen):
116+
urlopen=urllib.request.urlopen):
109117
"""Obtain the test result in XML format.
110118
111119
Args:

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 (!empty($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: 3 additions & 2 deletions
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 (!empty($user_api_key)) {
205206
foreach ($loc as $name => $location) {
206207
if (isset($location['browser']) && isset($location['noapi'])) {
207208
unset($loc[$name]);
@@ -214,7 +215,7 @@ function LoadLocations($isPaid = false)
214215
}
215216
}
216217
}
217-
$isPaid = !is_null($request_context->getUser()) && $request_context->getUser()->isPaid();
218+
$isPaid = !is_null($request_context->getUser()) && $request_context->getUser()->isPaid();
218219
$includePaid = $isPaid || $admin;
219220

220221
FilterLocations($loc, $includePaid);

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: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class RequestContext
2525
private ?BannerMessageManager $banner_message_manager;
2626
// Should use an enum, TODO
2727
private string $environment;
28-
private string $api_key_in_use;
28+
private ?string $api_key_in_use;
2929

3030
private string $user_api_key_header = 'X-WPT-API-KEY';
3131

@@ -49,7 +49,7 @@ public function __construct(array $global_request, array $server = [], array $op
4949
$this->host = $options['host'] ?? Util::getSetting('host', "");
5050

5151
$this->environment = Environment::$Production;
52-
$this->api_key_in_use = "";
52+
$this->api_key_in_use = null;
5353
}
5454

5555
public function getRaw(): array
@@ -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;
@@ -160,22 +160,20 @@ public function getEnvironment(): string
160160
* */
161161
public function getApiKeyInUse(): string
162162
{
163-
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-
}
174-
}
175-
176-
$this->api_key_in_use = $user_api_key;
163+
if ($this->api_key_in_use == null) {
164+
$this->api_key_in_use = $this->readApiKey();
177165
}
178-
179166
return $this->api_key_in_use;
180167
}
168+
169+
private function readApiKey()
170+
{
171+
$request_headers = getallheaders();
172+
foreach ($request_headers as $k => $value) {
173+
if (strtolower($k) == strtolower($this->user_api_key_header)) {
174+
return trim($value);
175+
}
176+
}
177+
return '';
178+
}
181179
}

www/usage.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
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 (!empty($user_api_key)) {
3131
$keys_file = SETTINGS_PATH . '/keys.ini';
3232
if (file_exists(SETTINGS_PATH . '/common/keys.ini')) {
3333
$keys_file = SETTINGS_PATH . '/common/keys.ini';
@@ -37,7 +37,7 @@
3737
}
3838
$keys = parse_ini_file($keys_file, true);
3939

40-
if ($admin && $key == 'all') {
40+
if ($admin && $user_api_key == 'all') {
4141
if (!isset($_REQUEST['days'])) {
4242
$days = 1;
4343
}
@@ -65,7 +65,7 @@
6565
}
6666
$used = array();
6767
foreach ($keys as $key => &$keyUser) {
68-
$u = isset($usage[$key]) ? (int)$usage[$key] : 0;
68+
$u = isset($usage[$key]) ? (int) $usage[$key] : 0;
6969
if ($u) {
7070
$used[] = array('used' => $u, 'description' => $keyUser['description'], 'contact' => $keyUser['contact'], 'limit' => $keyUser['limit']);
7171
}
@@ -100,9 +100,9 @@
100100
}
101101
}
102102
} else {
103-
if (isset($keys[$key])) {
103+
if (isset($keys[$user_api_key])) {
104104
$out = array();
105-
$limit = (int)@$keys[$key]['limit'];
105+
$limit = (int) @$keys[$user_api_key]['limit'];
106106
if (!$json) {
107107
echo "<table class=\"table\"><tr><th>Date</th><th>Used</th><th>Limit</th></tr>";
108108
}
@@ -113,7 +113,7 @@
113113
$used = 0;
114114
if (is_file($keyfile)) {
115115
$usage = json_decode(file_get_contents($keyfile), true);
116-
$used = (int)@$usage[$key];
116+
$used = (int) @$usage[$user_api_key];
117117
}
118118
$date = $targetDate->format("Y/m/d");
119119
if ($json) {
@@ -127,9 +127,9 @@
127127
echo '</table>';
128128
}
129129

130-
$limit = (int)$keys[$key]['limit'];
131-
if (isset($usage[$key])) {
132-
$used = (int)$usage[$key];
130+
$limit = (int) $keys[$user_api_key]['limit'];
131+
if (isset($usage[$user_api_key])) {
132+
$used = (int) $usage[$user_api_key];
133133
} else {
134134
$used = 0;
135135
}

0 commit comments

Comments
 (0)