-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetStackPathOAuth1.php
More file actions
70 lines (58 loc) · 2.09 KB
/
getStackPathOAuth1.php
File metadata and controls
70 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
if (empty($argv) || empty($argv[1]) || empty($argv[2]) || empty($argv[3])) {
echo "Please supply the comsumer key, consumer secret, and the company alias when invoking this script.\n";
echo "This would appear something like:\n\n";
echo " > php getStackPathOAuth1.php somelongletternumbermixofcharacters anotherlongletternumbermix athirdshortermix\n\n";
die;
}
$consumerKey = $argv[1];
$consumerSecret = $argv[2];
$companyAlias = $argv[3];
function generateNonce(int $length, $cs = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ') : string
{
for ($result = ''; strlen($result) <= $length;) {
$result .= $cs[rand(0, strlen($cs) - 1)];
}
return $result;
}
function getSignatureBasePairs($pairs)
{
ksort($pairs);
return urlencode(http_build_query($pairs));
}
$url = 'https://api.stackpath.com/v1/'.$companyAlias.'/logs';
$nonce = generateNonce(32);
$timestamp = time();
$parameterPairs = [
'limit' => 2,
'status' => '200',
];
$oauthPairs = [
'oauth_consumer_key' => $consumerKey,
'oauth_nonce' => $nonce,
'oauth_signature_method'=> 'HMAC-SHA1',
'oauth_timestamp' => $timestamp,
'oauth_version' => '1.0'
];
$signatureBase = 'GET'.'&'.
urlencode(strtolower($url)).'&'.
getSignatureBasePairs(array_merge($parameterPairs, $oauthPairs));
$signature = urlencode(base64_encode(hash_hmac('sha1', $signatureBase, urlencode($consumerSecret).'&', true)));
$oauthPairs['oauth_signature'] = $signature;
ksort($oauthPairs);
$authHeader = 'Authorization: OAuth ';
$authHeader.= implode('', array_map(function ($key, $value) {
return $key.'="'.$value.'"';
}, array_keys($oauthPairs), $oauthPairs));
$header = [$authHeader];
$ch = curl_init($url.'?'.http_build_query($parameterPairs));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
$result = curl_exec($ch);
if (curl_error($ch)) {
echo 'Error: '.curl_error($ch)."\n";
} else {
var_dump(json_decode($result));
}
curl_close($ch);